markdown-it has the highlight
option.
Apply syntax highlighting to fenced code blocks.
In README.md, use highlight.js as highlighting engine, but can also use Prism.
const MarkdownIt = require("markdown-it");
const Prism = require("prismjs");
require("prismjs/components/prism-javascript");
const md = new MarkdownIt({
highlight(str, lang) {
let hl;
try {
hl = Prism.highlight(str, Prism.languages[lang]);
} catch (error) {
console.error(error);
hl = md.utils.escapeHtml(str);
}
return `<pre class="language-${lang}"><code class="language-${lang}">${hl}</code></pre>`;
},
});
This code works on nodejs.