Skip to content

Instantly share code, notes, and snippets.

@bradhowes
Last active April 11, 2020 15:50
Show Gist options
  • Save bradhowes/68750e3a2e543267de8e4fc6d67a956b to your computer and use it in GitHub Desktop.
Save bradhowes/68750e3a2e543267de8e4fc6d67a956b to your computer and use it in GitHub Desktop.
Enable async render operations on Remarkable
// 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