Skip to content

Commit

Permalink
feat: allow to extend renderer
Browse files Browse the repository at this point in the history
Closes #46.
  • Loading branch information
simonhaenisch committed Aug 30, 2019
1 parent d77ab20 commit 4611688
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions lib/get-marked-with-highlighter.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
const marked = require('marked');
const { getLanguage, highlight } = require('highlight.js');

const renderer = new marked.Renderer();

renderer.code = (code, language) => {
// if the given language is not available in highlight.js, fall back to plaintext
language = (getLanguage(language) && language) || 'plaintext';

return `<pre><code class="hljs ${language}">${highlight(language, code).value}</code></pre>`;
};

/**
* Get a marked renderer with an attached highlighter.
*
* @param {Object} options Marked cnfiguration object
* @param {Object} options Marked configuration object
*
* @returns a marked renderer with highlight.js parser attached
*/
module.exports = options => {
const renderer = options.renderer || new marked.Renderer();

if (!renderer.hasOwnProperty('code')) {
renderer.code = (code, language) => {
// if the given language is not available in highlight.js, fall back to plaintext
language = (getLanguage(language) && language) || 'plaintext';

return `<pre><code class="hljs ${language}">${highlight(language, code).value}</code></pre>`;
};
}

marked.setOptions({ ...options, renderer });

return marked;
Expand Down

0 comments on commit 4611688

Please sign in to comment.