Browse Source

Added syntax highlight support for fenced blocks

pull/14/head
Vitaly Puzrin 10 years ago
parent
commit
58f2b8c37e
  1. 8
      README.md
  2. 16
      demo/assets/index.js
  3. 3
      lib/defaults.js
  4. 2
      lib/lexer_inline.js
  5. 14
      lib/renderer.js

8
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; }
});
```

16
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);

3
lib/defaults.js

@ -7,5 +7,6 @@ module.exports = {
html: false,
xhtml: false,
breaks: false,
langprefix: 'language-'
langprefix: 'language-',
highlight: function (/*str*/) { return ''; }
};

2
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'));

14
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 '<pre><code' + langMark + '>'
+ escapeHtml(token.content)
highlighted = options.highlight(token.content, langName) || escapeHtml(token.content);
return '<pre><code' + langClass + '>'
+ highlighted
+ '</code></pre>' + getBreak(tokens, idx);
};

Loading…
Cancel
Save