From 58f2b8c37effe7ce76ad742ce038faaebac979ee Mon Sep 17 00:00:00 2001 From: Vitaly Puzrin Date: Sat, 4 Oct 2014 06:27:24 +0400 Subject: [PATCH] Added syntax highlight support for fenced blocks --- README.md | 8 +++++--- demo/assets/index.js | 16 ++++++++++++++++ lib/defaults.js | 3 ++- lib/lexer_inline.js | 2 -- lib/renderer.js | 14 +++++++++----- 5 files changed, 32 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index dc7ef37..a0d1e72 100644 --- a/README.md +++ b/README.md @@ -33,12 +33,13 @@ Usage ----- ```javascript -var Remarkable = require('remarkable')(); +var Remarkable = require('remarkable'); var md = new Remarkable({ html: false, xhtml: false, breaks: true, - langprefix: 'language-' + langprefix: 'language-', + highlight: function (str, lang) { return str; } }); console.log(md.parse('# Remarkable rulezz!')); @@ -55,7 +56,8 @@ md.set({ html: false, xhtml: false, breaks: true, - langprefix: 'language-' + langprefix: 'language-', + highlight: function (str, lang) { return str; } }); ``` diff --git a/demo/assets/index.js b/demo/assets/index.js index ad7d260..22c4bd5 100644 --- a/demo/assets/index.js +++ b/demo/assets/index.js @@ -14,6 +14,22 @@ _src: false }; + defaults.highlight = function (str, lang) { + if (!defaults._highlight) { return ''; } + + var hljs = window.hljs; + if (lang && hljs.getLanguage(lang)) { + try { + //console.log(lang, hljs.highlight(lang, str)); + return hljs.highlight(lang, str).value; + } catch (__) {} + } + + try { return hljs.highlightAuto(str).value; } catch (__) {} + + return ''; + }; + function mdInit() { mdHtml = new window.Remarkable(defaults); mdSrc = new window.Remarkable(defaults); diff --git a/lib/defaults.js b/lib/defaults.js index e5a7e8e..c2bec4f 100644 --- a/lib/defaults.js +++ b/lib/defaults.js @@ -7,5 +7,6 @@ module.exports = { html: false, xhtml: false, breaks: false, - langprefix: 'language-' + langprefix: 'language-', + highlight: function (/*str*/) { return ''; } }; diff --git a/lib/lexer_inline.js b/lib/lexer_inline.js index ff3507d..428273e 100644 --- a/lib/lexer_inline.js +++ b/lib/lexer_inline.js @@ -18,8 +18,6 @@ rules.push(require('./lexer_inline/text')); rules.push(require('./lexer_inline/newline')); rules.push(require('./lexer_inline/escape')); rules.push(require('./lexer_inline/backticks')); -// -// rules.push(require('./lexer_inline/emphasis')); rules.push(require('./lexer_inline/links')); rules.push(require('./lexer_inline/autolink')); diff --git a/lib/renderer.js b/lib/renderer.js index 3fbc74c..dd91a85 100644 --- a/lib/renderer.js +++ b/lib/renderer.js @@ -53,17 +53,21 @@ rules.code = function (tokens, idx /*, options*/) { rules.fence = function (tokens, idx, options) { var token = tokens[idx]; - var langMark = ''; + var langClass = ''; var langPrefix = options.langprefix || ''; - var params; + var params, langName = ''; + var highlighted; if (token.params) { params = token.params.split(/ +/g); - langMark = ' class="' + langPrefix + escapeHtml(replaceEntities(unescapeMd(params[0]))) + '"'; + langName = escapeHtml(replaceEntities(unescapeMd(params[0]))); + langClass = ' class="' + langPrefix + langName + '"'; } - return '
'
-        + escapeHtml(token.content)
+  highlighted = options.highlight(token.content, langName) || escapeHtml(token.content);
+
+  return  '
'
+        + highlighted
         + '
' + getBreak(tokens, idx); };