feat: add md<->txt passthrough document converters
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -176,6 +176,10 @@ async function convertMdToDocx(inputPath, outputPath) {
|
|||||||
await fs.writeFile(outputPath, buffer);
|
await fs.writeFile(outputPath, buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function copyFileContents(inputPath, outputPath) {
|
||||||
|
await fs.copyFile(inputPath, outputPath);
|
||||||
|
}
|
||||||
|
|
||||||
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;
|
||||||
@@ -223,6 +227,8 @@ export function registerDocumentConverters() {
|
|||||||
register({ family: 'document', sourceFormat: 'pdf', targetFormat: 'md', convert: convertPdfToTxt });
|
register({ family: 'document', sourceFormat: 'pdf', targetFormat: 'md', convert: convertPdfToTxt });
|
||||||
register({ family: 'document', sourceFormat: 'docx', targetFormat: 'md', convert: convertDocxToMd });
|
register({ family: 'document', sourceFormat: 'docx', targetFormat: 'md', convert: convertDocxToMd });
|
||||||
register({ family: 'document', sourceFormat: 'md', targetFormat: 'docx', convert: convertMdToDocx });
|
register({ family: 'document', sourceFormat: 'md', targetFormat: 'docx', convert: convertMdToDocx });
|
||||||
|
register({ family: 'document', sourceFormat: 'md', targetFormat: 'txt', convert: copyFileContents });
|
||||||
|
register({ family: 'document', sourceFormat: 'txt', targetFormat: 'md', convert: copyFileContents });
|
||||||
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 });
|
||||||
|
|||||||
@@ -141,4 +141,30 @@ describe('document converters', () => {
|
|||||||
expect(result.value).toContain('first point');
|
expect(result.value).toContain('first point');
|
||||||
expect(result.value).toContain('<li>');
|
expect(result.value).toContain('<li>');
|
||||||
}, 20000);
|
}, 20000);
|
||||||
|
|
||||||
|
it('copies MD content unchanged to TXT', async () => {
|
||||||
|
const inputPath = path.join(tmpDir, 'fixture-for-txt.md');
|
||||||
|
const content = '# Heading\n\nSome **bold** text.\n';
|
||||||
|
await fs.writeFile(inputPath, content);
|
||||||
|
const outputPath = path.join(tmpDir, 'from-md.txt');
|
||||||
|
const entry = resolve('md', 'txt');
|
||||||
|
|
||||||
|
await entry.convert(inputPath, outputPath);
|
||||||
|
|
||||||
|
const output = await fs.readFile(outputPath, 'utf8');
|
||||||
|
expect(output).toBe(content);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('copies TXT content unchanged to MD', async () => {
|
||||||
|
const inputPath = path.join(tmpDir, 'fixture-for-md-from-txt.txt');
|
||||||
|
const content = 'Plain text content.\n';
|
||||||
|
await fs.writeFile(inputPath, content);
|
||||||
|
const outputPath = path.join(tmpDir, 'from-txt.md');
|
||||||
|
const entry = resolve('txt', 'md');
|
||||||
|
|
||||||
|
await entry.convert(inputPath, outputPath);
|
||||||
|
|
||||||
|
const output = await fs.readFile(outputPath, 'utf8');
|
||||||
|
expect(output).toBe(content);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user