Skip to content

Commit

Permalink
feat(core/markdown): allow syntax highlight off main thread (#1066)
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcos Cáceres committed Feb 2, 2017
1 parent 3b964df commit 2ccd482
Showing 1 changed file with 1 addition and 35 deletions.
36 changes: 1 addition & 35 deletions js/core/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,49 +46,15 @@
define([
"deps/marked",
"core/utils",
"deps/highlight",
"deps/beautify-html",
"core/beautify-options",
], function(marked, utils, hljs, beautify, beautifyOps) {
var defaultLanguages = Object.freeze([
"css",
"html",
"http",
"js",
"json",
"markdown",
"xml",
]);

hljs.configure({
tabReplace: " ", // 2 spaces
});
], function(marked, utils, beautify, beautifyOps) {

marked.setOptions({
sanitize: false,
gfm: true,
highlight: makeHighlightHelper(),
});

function makeHighlightHelper() {
var div = document.createElement("div");
return function(code, language) {
var leftPadding = utils.calculateLeftPad(code);
var normalizedCode;
if (leftPadding) {
var leftPaddingMatcher = new RegExp("^ {" + leftPadding + "}", "gm");
normalizedCode = code.replace(leftPaddingMatcher, "");
} else {
normalizedCode = code;
}
div.innerHTML = normalizedCode;
var cleanCode = div.textContent;
var possibleLanguages = [].concat(language || defaultLanguages);
var highlightedCode = hljs.highlightAuto(cleanCode, possibleLanguages);
return highlightedCode.value;
};
}

function toHTML(text) {
var normalizedLeftPad = utils.normalizePadding(text);
// As markdown is pulled from HTML, > is already escaped and
Expand Down

0 comments on commit 2ccd482

Please sign in to comment.