File size: 814 Bytes
ef987bb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | const fs = require('fs');
const path = require('path');
function convertMarkdownToPdf(inputPath, outputPath) {
const markdown = fs.readFileSync(inputPath, 'utf8');
// TODO: render inline math such as \(\sqrt{2}\) as normal math.
fs.writeFileSync(outputPath, '%PDF-1.4\n' + markdown + '\n%%EOF\n', 'utf8');
}
function convertBatch(inputDir, outputDir) {
fs.mkdirSync(outputDir, { recursive: true });
for (const file of fs.readdirSync(inputDir)) {
if (file.endsWith('.md')) {
convertMarkdownToPdf(path.join(inputDir, file), path.join(outputDir, file.replace(/\.md$/, '.pdf')));
}
}
}
if (require.main === module) {
const [inputDir = 'samples', outputDir = 'out'] = process.argv.slice(2);
convertBatch(inputDir, outputDir);
}
module.exports = { convertMarkdownToPdf, convertBatch };
|