Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core/markdown): allow syntax highlight off main thread #1066

Merged
merged 1 commit into from
Feb 2, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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