UNPKG

@mieweb/wikigdrive

Version:

Google Drive to MarkDown synchronization

42 lines (41 loc) 2.21 kB
import process from 'node:process'; import fs from 'node:fs'; import path from 'node:path'; import { OdtToMarkdown } from './OdtToMarkdown.js'; import { UnMarshaller } from './UnMarshaller.js'; import { LIBREOFFICE_CLASSES } from './LibreOffice.js'; import { generateDocumentFrontMatter } from '../containers/transform/frontmatters/generateDocumentFrontMatter.js'; import { OdtProcessor } from './OdtProcessor.js'; export async function executeOdtToMarkdown(workerData) { const processor = new OdtProcessor(true); await processor.load(workerData.odtPath); await processor.unzipAssets(workerData.destinationPath, workerData.realFileName); const content = processor.getContentXml(); const stylesXml = processor.getStylesXml(); const fileNameMap = processor.getFileNameMap(); const xmlMap = processor.getXmlMap(); const parser = new UnMarshaller(LIBREOFFICE_CLASSES, 'DocumentContent'); const document = parser.unmarshal(content); const parserStyles = new UnMarshaller(LIBREOFFICE_CLASSES, 'DocumentStyles'); const styles = parserStyles.unmarshal(stylesXml); if (!styles) { throw Error('No styles unmarshalled'); } const converter = new OdtToMarkdown(document, styles, fileNameMap, xmlMap); converter.setRewriteRules(workerData.rewriteRules); converter.setPicturesDir('./' + workerData.realFileName.replace(/.md$/, '.assets/'), workerData.picturesDirAbsolute); const markdown = await converter.convert(); const links = Array.from(converter.links); const frontMatterOverload = {}; if (markdown.match(/^ *A. {2}/igm)) { frontMatterOverload['markup'] = 'pandoc'; } const frontMatter = generateDocumentFrontMatter(workerData.localFile, links, workerData.fm_without_version, frontMatterOverload); const errors = converter.getErrors(); if (process.env.VERSION === 'dev') { fs.writeFileSync(path.join(workerData.destinationPath, workerData.realFileName.replace(/.md$/, '.debug.xml')), content); } const headersMap = converter.getHeadersMap(); const invisibleBookmarks = converter.getInvisibleBookmarks(); return { links, frontMatter, markdown, errors, headersMap, invisibleBookmarks }; }