Tofu/scripts/import-recipes/odt/readOdtParagraphs.js
2026-09-18 01:41:02 +02:00

18 lines
916 B
JavaScript

import { readFileSync } from 'node:fs'
import { extractOdtText } from '#import-recipes/odt/extractOdtText.js'
import { readOdtEntry } from '#import-recipes/odt/readOdtEntry.js'
// Reads one .odt from disk and returns the lines it displays. Never throws: a
// missing file, a file that is not a zip, or a document without a body all come
// back as { ok: false, reason } so the run carries on with the next file.
export function readOdtParagraphs(filePath) {
try {
const contentXml = readOdtEntry(readFileSync(filePath), 'content.xml')
if (contentXml === null) return { ok: false, reason: 'no content.xml in this archive' }
const paragraphs = extractOdtText(contentXml)
if (paragraphs.length === 0) return { ok: false, reason: 'no readable text in this document' }
return { ok: true, paragraphs }
} catch (error) {
return { ok: false, reason: `unreadable file (${error.message})` }
}
}