import "@johnlindquist/kit"
import Tesseract from 'tesseract.js'
const buffer = await screenshot()
const tmpImagePath = tmpPath('screenshot-ocr.png')
await writeFile(tmpImagePath, buffer)
const extractedText = await div({
html: md(`
## Extracting text from screenshot...
<img src="file://${tmpImagePath}" class="max-w-full max-h-96 object-contain" />
`),
onInit: async () => {
try {
const { data } = await Tesseract.recognize(buffer, 'eng', {
logger: (m) => console.log(m),
})
submit(data.text)
} catch (error) {
console.error('OCR Error:', error)
submit('Error extracting text from screenshot')
}
},
})
const editedText = await editor({
value: extractedText,
language: 'markdown',
placeholder: 'Edit the extracted text...',
hint: 'Make any corrections to the OCR text, then save',
})
const notesDir = kenvPath('notes')
await ensureDir(notesDir)
const timestamp = formatDate(new Date(), 'yyyy-MM-dd-HH-mm-ss')
const filename = `screenshot-note-${timestamp}.md`
const notePath = path.join(notesDir, filename)
await writeFile(notePath, editedText)
await div(md(`
# Note Saved! 📝
**File:** ${filename}
**Location:** ${notePath}
The extracted and edited text has been saved to your notes.
`))
await revealFile(notePath)