Yay, I'm included.
Created
January 14, 2020 06:23
-
-
Save simonhaenisch/b5c935e76052a40b1e32bfe28673b188 to your computer and use it in GitHub Desktop.
Custom marked renderer for including other markdown files in md-to-pdf.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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.
Foo
Bar
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment