From 46116886cf51bce4d6eeff25211ce5154d90f4d2 Mon Sep 17 00:00:00 2001 From: simonhaenisch Date: Fri, 30 Aug 2019 17:25:47 +0100 Subject: [PATCH] feat: allow to extend renderer Closes #46. --- lib/get-marked-with-highlighter.js | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/lib/get-marked-with-highlighter.js b/lib/get-marked-with-highlighter.js index 96bba98..65bf798 100644 --- a/lib/get-marked-with-highlighter.js +++ b/lib/get-marked-with-highlighter.js @@ -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 `
${highlight(language, code).value}
`; -}; - /** * 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 `
${highlight(language, code).value}
`; + }; + } + marked.setOptions({ ...options, renderer }); return marked;