Last active
April 11, 2020 15:50
-
-
Save bradhowes/68750e3a2e543267de8e4fc6d67a956b to your computer and use it in GitHub Desktop.
Enable async render operations on Remarkable
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
// Snippets from my build.js script for my static site generator. Shows how I handle async render operations | |
// during the Remarkable render operation. | |
// | |
const md = new Remarkable("full", markdownOptions).use(katexPlugin).use(require("./consoleFence.js")); | |
// My own custom codeFence processing for static highlighting using Prism. | |
md.renderer.rules.fence = require("./codeFence.js"); | |
md.renderer.rules.fence_custom.graph = require("./graphFence.js"); | |
md.renderer.promises = {}; | |
md.renderer.addPromise = (key, promise) => { | |
const placeholder = '@+@' + key + '@+@'; | |
md.renderer.promises[placeholder] = promise; | |
return placeholder; | |
} | |
const processMarkdown = (files, metalsmith, done) => { | |
Object.keys(files).forEach(file => { | |
const data = files[file]; | |
const dirName = path.dirname(file); | |
const htmlName = path.basename(file, path.extname(file)) + '.html'; | |
const htmlPath = dirName !== '.' ? path.join(dirName, htmlName) : htmlName; | |
let contents = md.render(data.contents.toString()); | |
for (let [placeholder, promise] of Object.entries(md.renderer.promises)) { | |
promise.then(value => { | |
contents = contents.replace(placeholder, value); | |
return value; | |
}); | |
} | |
let allPromise = Promise.all(Object.values(md.renderer.promises)); | |
allPromise.then(value => { | |
data.contents = Buffer.from(contents); | |
delete files[file]; | |
files[htmlPath] = data; | |
done(); | |
}); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment