Improve syntax highlighting performance by moving it to finish view
This commit is contained in:
parent
521f76f7ad
commit
1d1eedce7e
2 changed files with 32 additions and 15 deletions
|
@ -435,6 +435,33 @@ function finishView(view) {
|
||||||
height: '400px'
|
height: '400px'
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
//syntax highlighting
|
||||||
|
view.find("pre.raw").removeClass("raw")
|
||||||
|
.each(function (key, value) {
|
||||||
|
var langDiv = $(value).find('code.hljs');
|
||||||
|
if (langDiv.length > 0) {
|
||||||
|
var reallang = langDiv[0].className.replace('hljs', '').trim();
|
||||||
|
var codeDiv = $(value).find('.code');
|
||||||
|
var code = "";
|
||||||
|
if (codeDiv.length > 0) code = codeDiv.html();
|
||||||
|
else code = langDiv.html();
|
||||||
|
code = md.utils.unescapeAll(code);
|
||||||
|
if (reallang == "tiddlywiki" || reallang == "mediawiki") {
|
||||||
|
var result = {
|
||||||
|
value: Prism.highlight(code, Prism.languages.wiki)
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
var languages = hljs.listLanguages();
|
||||||
|
if (languages.indexOf(reallang) == -1) {
|
||||||
|
var result = hljs.highlightAuto(code);
|
||||||
|
} else {
|
||||||
|
var result = hljs.highlight(reallang, code);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (codeDiv.length > 0) codeDiv.html(result.value);
|
||||||
|
else langDiv.html(result.value);
|
||||||
|
}
|
||||||
|
});
|
||||||
//render title
|
//render title
|
||||||
document.title = renderTitle(view);
|
document.title = renderTitle(view);
|
||||||
}
|
}
|
||||||
|
@ -772,19 +799,9 @@ function highlightRender(code, lang) {
|
||||||
} else if (lang == 'mermaid') {
|
} else if (lang == 'mermaid') {
|
||||||
return '<div class="mermaid raw">' + code + '</div>';
|
return '<div class="mermaid raw">' + code + '</div>';
|
||||||
}
|
}
|
||||||
var reallang = lang.replace(/\=$|\=\d+$|\=\+$/, '');
|
var result = {
|
||||||
if (reallang == "tiddlywiki" || reallang == "mediawiki") {
|
value: code
|
||||||
var result = {
|
};
|
||||||
value: Prism.highlight(code, Prism.languages.wiki)
|
|
||||||
};
|
|
||||||
} else {
|
|
||||||
var languages = hljs.listLanguages();
|
|
||||||
if (languages.indexOf(reallang) == -1) {
|
|
||||||
var result = hljs.highlightAuto(code);
|
|
||||||
} else {
|
|
||||||
var result = hljs.highlight(reallang, code);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
var showlinenumbers = /\=$|\=\d+$|\=\+$/.test(lang);
|
var showlinenumbers = /\=$|\=\d+$|\=\+$/.test(lang);
|
||||||
if (showlinenumbers) {
|
if (showlinenumbers) {
|
||||||
var startnumber = 1;
|
var startnumber = 1;
|
||||||
|
@ -878,7 +895,7 @@ md.renderer.rules.fence = function (tokens, idx, options, env, self) {
|
||||||
return highlighted + '\n';
|
return highlighted + '\n';
|
||||||
}
|
}
|
||||||
|
|
||||||
return '<pre><code' + self.renderAttrs(token) + '>'
|
return '<pre class="raw"><code' + self.renderAttrs(token) + '>'
|
||||||
+ highlighted
|
+ highlighted
|
||||||
+ '</code></pre>\n';
|
+ '</code></pre>\n';
|
||||||
};
|
};
|
||||||
|
|
|
@ -75,7 +75,7 @@ md.renderer.rules.fence = function (tokens, idx, options, env, self) {
|
||||||
if (tokens[idx].map && tokens[idx].level === 0) {
|
if (tokens[idx].map && tokens[idx].level === 0) {
|
||||||
var startline = tokens[idx].map[0] + 1;
|
var startline = tokens[idx].map[0] + 1;
|
||||||
var endline = tokens[idx].map[1];
|
var endline = tokens[idx].map[1];
|
||||||
return '<pre class="part" data-startline="' + startline + '" data-endline="' + endline + '"><code' + self.renderAttrs(token) + '>'
|
return '<pre class="part raw" data-startline="' + startline + '" data-endline="' + endline + '"><code' + self.renderAttrs(token) + '>'
|
||||||
+ highlighted
|
+ highlighted
|
||||||
+ '</code></pre>\n';
|
+ '</code></pre>\n';
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue