feat: add md<->html document converters

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-31 08:31:00 +02:00
co-authored by Claude Sonnet 5
parent 28b6edac82
commit 1595c5e0af
2 changed files with 43 additions and 1 deletions
+17 -1
View File
@@ -2,7 +2,9 @@ import fs from 'node:fs/promises';
import mammoth from 'mammoth'; import mammoth from 'mammoth';
import puppeteer from 'puppeteer'; import puppeteer from 'puppeteer';
import * as pdfjsLib from 'pdfjs-dist/legacy/build/pdf.mjs'; 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'; import { register } from './registry.js';
async function renderHtmlToPdf(html, outputPath) { async function renderHtmlToPdf(html, outputPath) {
@@ -44,6 +46,18 @@ async function convertDocxToPdf(inputPath, outputPath) {
await renderHtmlToPdf(result.value, 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) { async function extractPdfPageTexts(inputPath) {
const data = new Uint8Array(await fs.readFile(inputPath)); const data = new Uint8Array(await fs.readFile(inputPath));
const doc = await pdfjsLib.getDocument({ data }).promise; 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: 'docx', targetFormat: 'html', convert: convertDocxToHtml });
register({ family: 'document', sourceFormat: 'txt', targetFormat: 'pdf', convert: convertTxtToPdf }); register({ family: 'document', sourceFormat: 'txt', targetFormat: 'pdf', convert: convertTxtToPdf });
register({ family: 'document', sourceFormat: 'html', targetFormat: 'pdf', convert: convertHtmlToPdf }); 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: 'docx', targetFormat: 'pdf', convert: convertDocxToPdf });
register({ family: 'document', sourceFormat: 'pdf', targetFormat: 'txt', convert: convertPdfToTxt }); register({ family: 'document', sourceFormat: 'pdf', targetFormat: 'txt', convert: convertPdfToTxt });
register({ family: 'document', sourceFormat: 'pdf', targetFormat: 'html', convert: convertPdfToHtml }); register({ family: 'document', sourceFormat: 'pdf', targetFormat: 'html', convert: convertPdfToHtml });
+26
View File
@@ -74,4 +74,30 @@ describe('document converters', () => {
const detected = await detectInputMime(outputPath); const detected = await detectInputMime(outputPath);
expect(detected.mime).toBe('application/pdf'); expect(detected.mime).toBe('application/pdf');
}, 20000); }, 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('<h1>Heading</h1>');
expect(html).toContain('<strong>bold</strong>');
});
it('converts HTML to MD', async () => {
const inputPath = path.join(tmpDir, 'fixture-for-md.html');
await fs.writeFile(inputPath, '<h1>Heading</h1><p>Some <strong>bold</strong> text.</p>');
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**');
});
}); });