Browse Source

Demo: simplify highlighting

pull/186/head
Vitaly Puzrin 10 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) { defaults.highlight = function (str, lang) {
if (!defaults._highlight || !window.hljs) { return ''; } if (!defaults._highlight || !window.hljs) { return ''; }
var hljs = window.hljs; var hljs = window.hljs,
esc = mdHtml.utils.escapeHtml;
if (lang && hljs.getLanguage(lang)) { if (lang && hljs.getLanguage(lang)) {
try { try {
return hljs.highlight(lang, str).value; return '<pre class="hljs"><code>' +
hljs.highlight(lang, str).value +
'</code></pre>';
} catch (__) { /**/ } } catch (__) { /**/ }
} }
try { /* try {
return hljs.highlightAuto(str).value; var result = hljs.highlightAuto(str);
} catch (__) { /**/ } 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) { 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: // Inject line numbers for sync scroll. Notes:
// //
@ -124,8 +107,8 @@ function mdInit() {
var line; var line;
if (tokens[idx].map && tokens[idx].level === 0) { if (tokens[idx].map && tokens[idx].level === 0) {
line = tokens[idx].map[0]; line = tokens[idx].map[0];
tokens[idx].attrPush([ 'class', 'line' ]); tokens[idx].attrJoin('class', 'line');
tokens[idx].attrPush([ 'data-line', String(line) ]); tokens[idx].attrSet('data-line', String(line));
} }
return slf.renderToken(tokens, idx, options, env, slf); return slf.renderToken(tokens, idx, options, env, slf);
} }

10
support/demo_template/index.styl

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

Loading…
Cancel
Save