From 2db6069460e2982be5f8c39cdb0157a664a04bb2 Mon Sep 17 00:00:00 2001 From: Osman Gormus Date: Wed, 24 Jan 2018 07:54:30 -0800 Subject: [PATCH] Update markdown.js The code blocks in the markdown are rendered with the syntax highlighting with matching markup already available in `code.js` helper. --- helpers/markdown.js | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/helpers/markdown.js b/helpers/markdown.js index 6b03f23..39a18a2 100644 --- a/helpers/markdown.js +++ b/helpers/markdown.js @@ -1,12 +1,25 @@ +var hljs = require('highlight.js'); var marked = require('marked'); /** * Handlebars block helper that converts Markdown to HTML. + * The code blocks in the markdown are rendered with the syntax highlighting. * @param {object} options - Handlebars object. * @example * {{#markdown}}Welcome to [zombo.com](http://zombo.com){{/markdown}} * @returns The Markdown inside the helper, converted to HTML. */ -module.exports = function(options) { - return marked(options.fn(this)); -} + module.exports = function(options) { + var renderer = new marked.Renderer(); + + renderer.code = function(code, language) { + if (typeof language === 'undefined') language = 'html'; + + var renderedCode = hljs.highlight(language, code).value; + var output = `
${renderedCode}
`; + + return output; + }; + + return marked(options.fn(this), { renderer }); + }