feat: add md<->html document converters
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -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 });
|
||||
|
||||
Reference in New Issue
Block a user