Browse Source

Demo: simplify highlighting

pull/186/head
Vitaly Puzrin 9 years ago
parent
commit
14eba9197e
  1. 47
      support/demo_template/index.js
  2. 10
      support/demo_template/index.styl

47
support/demo_template/index.js

@ -24,18 +24,25 @@ var defaults = {
defaults.highlight = function (str, lang) {
if (!defaults._highlight || !window.hljs) { return ''; }
var hljs = window.hljs;
var hljs = window.hljs,
esc = mdHtml.utils.escapeHtml;
if (lang && hljs.getLanguage(lang)) {
try {
return hljs.highlight(lang, str).value;
return '<pre class="hljs"><code>' +
hljs.highlight(lang, str).value +
'</code></pre>';
} catch (__) { /**/ }
}
try {
return hljs.highlightAuto(str).value;
} catch (__) { /**/ }
/* try {
var result = hljs.highlightAuto(str);
return '<pre class="hljs ' + esc(result.language) + '"><code>' +
result.value +
'</code></pre>';
} catch (__) {} */
return '';
return '<pre class="hljs"><code>' + esc(str) + '</code></pre>';
};
function setOptionClass(name, val) {
@ -91,30 +98,6 @@ function mdInit() {
};
mdHtml.renderer.rules.fence = function (tokens, idx, options, env, slf) {
var escapeHtml = mdHtml.utils.escapeHtml,
unescapeAll = mdHtml.utils.unescapeAll,
token = tokens[idx],
info = token.info ? unescapeAll(token.info).trim() : '',
langName = '',
highlighted;
if (info) {
langName = info.split(/\s+/g)[0];
token.attrPush([ 'class', options.langPrefix + langName ]);
}
if (options.highlight) {
highlighted = options.highlight(token.content, langName) || escapeHtml(token.content);
} else {
highlighted = escapeHtml(token.content);
}
return '<pre class="hljs"><code' + slf.renderAttrs(token) + '>'
+ highlighted
+ '</code></pre>\n';
};
//
// Inject line numbers for sync scroll. Notes:
//
@ -124,8 +107,8 @@ function mdInit() {
var line;
if (tokens[idx].map && tokens[idx].level === 0) {
line = tokens[idx].map[0];
tokens[idx].attrPush([ 'class', 'line' ]);
tokens[idx].attrPush([ 'data-line', String(line) ]);
tokens[idx].attrJoin('class', 'line');
tokens[idx].attrSet('data-line', String(line));
}
return slf.renderToken(tokens, idx, options, env, slf);
}

10
support/demo_template/index.styl

@ -77,10 +77,12 @@ body
a:last-child
padding-right 30px
/*.hljs
background none
padding 0
*/
// twbs fix
.hljs
padding 9.5px
code
white-space pre
.footnotes
-moz-column-count 2

Loading…
Cancel
Save