Skip to content

Instantly share code, notes, and snippets.

@simonhaenisch
Created January 14, 2020 06:23
Show Gist options
  • Save simonhaenisch/b5c935e76052a40b1e32bfe28673b188 to your computer and use it in GitHub Desktop.
Save simonhaenisch/b5c935e76052a40b1e32bfe28673b188 to your computer and use it in GitHub Desktop.
Custom marked renderer for including other markdown files in md-to-pdf.
const { readFileSync } = require('fs');
const { Renderer } = require('marked');
const getMarked = require('md-to-pdf/lib/get-marked-with-highlighter');
const renderer = new Renderer();
const originalLinkRenderer = renderer.link.bind(renderer);
renderer.link = (href, title, text) => {
if (text !== 'include') {
return originalLinkRenderer(href, title, text);
}
// 1. read file
const md = readFileSync(href, 'utf-8');
// 2. get marked instance that's equivalent to the one from md-to-pdf
const marked = getMarked(marked_options);
// 3. convert to html string
return marked(md);
};
const marked_options = { renderer };
module.exports = { marked_options };
const mdToPdf = require('md-to-pdf');
const config = require('./config');
async function main() {
await mdToPdf('test.md', config);
}
main().catch(error => console.error(error) || process.exit(1));
{
"name": "md-to-pdf-includes",
"version": "1.0.0",
"description": "Custom marked renderer for including other markdown files in md-to-pdf.",
"main": "index.js",
"scripts": {
"start": "node .",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "Simon Haenisch (https://github.com/simonhaenisch)",
"license": "MIT",
"dependencies": {
"marked": "0.8.0",
"md-to-pdf": "2.8.2"
}
}

And I'm included in the include.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment