Skip to content

Instantly share code, notes, and snippets.

@offbynull
Last active April 16, 2019 23:49
Show Gist options
  • Select an option

  • Save offbynull/bfb49f036def1c4f7e11520108e95b2f to your computer and use it in GitHub Desktop.

Select an option

Save offbynull/bfb49f036def1c4f7e11520108e95b2f to your computer and use it in GitHub Desktop.
Override markdown-it inline code and fence
module.exports = function(md, options) {
const blockIdMap = options.blockIdMap;
const inlineIdMap = options.inlineIdMap;
const oldFenceRule = md.renderer.rules.fence;
const oldInlineCodeRule = md.renderer.rules.code_inline;
md.renderer.rules.fence = function(tokens, idx, options, env, self) {
const token = tokens[idx];
const infoMatch = token.info.match(/^\s*\{([A-Za-z0-9]*)\}\s*/);
if (infoMatch !== null && infoMatch.length === 2) { //infoMatch[0] is the whole thing, infoMatch[1] is the group
const info = infoMatch[1];
// if empty identifier, remove identifer and fallback to normal
if (info.length === 0) {
const skipLen = infoMatch[0].length;
token.info = token.info.slice(skipLen);
return oldFenceRule(tokens, idx, options, env, self);
}
// apply the func for this identifier
const handler = blockIdMap[info];
if (typeof handler === 'function') {
return handler(token.content, tokens, idx, options, env, self);
}
}
return oldFenceRule(tokens, idx, options, env, self);
}
md.renderer.rules.code_inline = function(tokens, idx, options, env, self) {
const token = tokens[idx];
const infoMatch = token.content.match(/^\s*\{([A-Za-z0-9]*)\}\s*/);
if (infoMatch !== null && infoMatch.length === 2) { //infoMatch[0] is the whole thing, infoMatch[1] is the group
const skipLen = infoMatch[0].length;
const info = infoMatch[1];
// if empty identifier, remove identifer and fallback to normal
if (info.length === 0) {
token.content = token.content.slice(skipLen);
return oldInlineCodeRule(tokens, idx, options, env, self);
}
// apply the func for this identifier
const handler = inlineIdMap[info];
if (typeof handler === 'function') {
const content = token.content.slice(skipLen);
return handler(content, tokens, idx, options, env, self);
}
}
return oldInlineCodeRule(tokens, idx, options, env, self);
}
}
const MarkdownIt = require('markdown-it');
const CustomBlocksPlugin = require('./custom_blocks');
const md = new MarkdownIt('commonmark');
let customBlocksOptions = {
blockIdMap: {},
inlineIdMap: {}
}
customBlocksOptions.blockIdMap.print = function(content) { return "IN BLOCK PRINT!"; };
customBlocksOptions.inlineIdMap.print = function(content) { return "IN INLINE PRINT!"; };
md.use(CustomBlocksPlugin, customBlocksOptions);
var result;
result = md.render('# hello!\npooper ```hello1```\n\n```\nhello2\ntest\n\n\ntesttest\n```\n\n');
console.log(result);
result = md.render('# hello!\npooper ```{} hello1```\n\n```{} \nhello2\ntest\n\n\ntesttest\n```\n\n');
console.log(result);
result = md.render('# hello!\npooper ```{print} hello1```\n\n```{print} \nhello2\ntest\n\n\ntesttest\n```\n\n');
console.log(result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment