diff --git a/src/converters/document.js b/src/converters/document.js index 87497a9..e5389eb 100644 --- a/src/converters/document.js +++ b/src/converters/document.js @@ -2,7 +2,9 @@ import fs from 'node:fs/promises'; import mammoth from 'mammoth'; import puppeteer from 'puppeteer'; import * as pdfjsLib from 'pdfjs-dist/legacy/build/pdf.mjs'; -import { Document, Paragraph, TextRun, Packer } from 'docx'; +import { Document, Paragraph, TextRun, HeadingLevel, Packer } from 'docx'; +import MarkdownIt from 'markdown-it'; +import TurndownService from 'turndown'; import { register } from './registry.js'; async function renderHtmlToPdf(html, outputPath) { @@ -44,6 +46,18 @@ async function convertDocxToPdf(inputPath, outputPath) { await renderHtmlToPdf(result.value, outputPath); } +async function convertMdToHtml(inputPath, outputPath) { + const markdown = await fs.readFile(inputPath, 'utf8'); + const html = new MarkdownIt().render(markdown); + await fs.writeFile(outputPath, html); +} + +async function convertHtmlToMd(inputPath, outputPath) { + const html = await fs.readFile(inputPath, 'utf8'); + const markdown = new TurndownService({ headingStyle: 'atx' }).turndown(html); + await fs.writeFile(outputPath, markdown); +} + async function extractPdfPageTexts(inputPath) { const data = new Uint8Array(await fs.readFile(inputPath)); const doc = await pdfjsLib.getDocument({ data }).promise; @@ -85,6 +99,8 @@ export function registerDocumentConverters() { register({ family: 'document', sourceFormat: 'docx', targetFormat: 'html', convert: convertDocxToHtml }); register({ family: 'document', sourceFormat: 'txt', targetFormat: 'pdf', convert: convertTxtToPdf }); register({ family: 'document', sourceFormat: 'html', targetFormat: 'pdf', convert: convertHtmlToPdf }); + register({ family: 'document', sourceFormat: 'md', targetFormat: 'html', convert: convertMdToHtml }); + register({ family: 'document', sourceFormat: 'html', targetFormat: 'md', convert: convertHtmlToMd }); register({ family: 'document', sourceFormat: 'docx', targetFormat: 'pdf', convert: convertDocxToPdf }); register({ family: 'document', sourceFormat: 'pdf', targetFormat: 'txt', convert: convertPdfToTxt }); register({ family: 'document', sourceFormat: 'pdf', targetFormat: 'html', convert: convertPdfToHtml }); diff --git a/test/converters/document.test.js b/test/converters/document.test.js index 6df1ad7..c01a0ab 100644 --- a/test/converters/document.test.js +++ b/test/converters/document.test.js @@ -74,4 +74,30 @@ describe('document converters', () => { const detected = await detectInputMime(outputPath); expect(detected.mime).toBe('application/pdf'); }, 20000); + + it('converts MD to HTML', async () => { + const inputPath = path.join(tmpDir, 'fixture.md'); + await fs.writeFile(inputPath, '# Heading\n\nSome **bold** text.'); + const outputPath = path.join(tmpDir, 'from-md.html'); + const entry = resolve('md', 'html'); + + await entry.convert(inputPath, outputPath); + + const html = await fs.readFile(outputPath, 'utf8'); + expect(html).toContain('
Some bold text.
'); + const outputPath = path.join(tmpDir, 'from-html.md'); + const entry = resolve('html', 'md'); + + await entry.convert(inputPath, outputPath); + + const markdown = await fs.readFile(outputPath, 'utf8'); + expect(markdown).toContain('# Heading'); + expect(markdown).toContain('**bold**'); + }); });