mirror of
https://github.com/jupyter/notebook.git
synced 2025-03-19 13:20:36 +08:00
use CodeMirror.runMode to highlight in markdown
instead of highlight.js
This commit is contained in:
parent
388fec4f96
commit
29e49329e2
@ -11,7 +11,8 @@ define([
|
||||
'services/sessions/session',
|
||||
'notebook/js/celltoolbar',
|
||||
'components/marked/lib/marked',
|
||||
'highlight',
|
||||
'codemirror/lib/codemirror',
|
||||
'codemirror/addon/runmode/runmode',
|
||||
'notebook/js/mathjaxutils',
|
||||
'base/js/keyboard',
|
||||
'notebook/js/tooltip',
|
||||
@ -29,7 +30,8 @@ define([
|
||||
session,
|
||||
celltoolbar,
|
||||
marked,
|
||||
hljs,
|
||||
CodeMirror,
|
||||
runMode,
|
||||
mathjaxutils,
|
||||
keyboard,
|
||||
tooltip,
|
||||
@ -83,19 +85,37 @@ define([
|
||||
marked.setOptions({
|
||||
gfm : true,
|
||||
tables: true,
|
||||
langPrefix: "language-",
|
||||
highlight: function(code, lang) {
|
||||
// FIXME: probably want central config for CodeMirror theme when we have js config
|
||||
langPrefix: "cm-s-ipython language-",
|
||||
highlight: function(code, lang, callback) {
|
||||
if (!lang) {
|
||||
// no language, no highlight
|
||||
return code;
|
||||
if (callback) {
|
||||
callback(null, code);
|
||||
return;
|
||||
} else {
|
||||
return code;
|
||||
}
|
||||
}
|
||||
var highlighted;
|
||||
try {
|
||||
highlighted = hljs.highlight(lang, code, false);
|
||||
} catch(err) {
|
||||
highlighted = hljs.highlightAuto(code);
|
||||
}
|
||||
return highlighted.value;
|
||||
utils.requireCodeMirrorMode(lang, function () {
|
||||
var el = document.createElement("div");
|
||||
mode = CodeMirror.getMode({}, lang);
|
||||
if (!mode) {
|
||||
console.log("No CodeMirror mode: " + lang);
|
||||
callback(null, code);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
CodeMirror.runMode(code, mode, el);
|
||||
callback(null, el.innerHTML);
|
||||
} catch (err) {
|
||||
console.log("Failed to highlight " + lang + " code", error);
|
||||
callback(err, code);
|
||||
}
|
||||
}, function (err) {
|
||||
console.log("No CodeMirror mode: " + lang);
|
||||
callback(err, code);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -552,9 +552,10 @@ define([
|
||||
var text_and_math = mathjaxutils.remove_math(markdown);
|
||||
var text = text_and_math[0];
|
||||
var math = text_and_math[1];
|
||||
var html = marked.parser(marked.lexer(text));
|
||||
html = mathjaxutils.replace_math(html, math);
|
||||
toinsert.append(html);
|
||||
marked(text, function (err, html) {
|
||||
html = mathjaxutils.replace_math(html, math);
|
||||
toinsert.append(html);
|
||||
});
|
||||
element.append(toinsert);
|
||||
return toinsert;
|
||||
};
|
||||
|
@ -241,34 +241,35 @@ define([
|
||||
MarkdownCell.prototype.render = function () {
|
||||
var cont = TextCell.prototype.render.apply(this);
|
||||
if (cont) {
|
||||
var that = this;
|
||||
var text = this.get_text();
|
||||
var math = null;
|
||||
if (text === "") { text = this.placeholder; }
|
||||
var text_and_math = mathjaxutils.remove_math(text);
|
||||
text = text_and_math[0];
|
||||
math = text_and_math[1];
|
||||
var html = marked.parser(marked.lexer(text));
|
||||
html = mathjaxutils.replace_math(html, math);
|
||||
html = security.sanitize_html(html);
|
||||
html = $($.parseHTML(html));
|
||||
// add anchors to headings
|
||||
// console.log(html);
|
||||
html.find(":header").addBack(":header").each(function (i, h) {
|
||||
h = $(h);
|
||||
var hash = h.text().replace(/ /g, '-');
|
||||
h.attr('id', hash);
|
||||
h.append(
|
||||
$('<a/>')
|
||||
.addClass('anchor-link')
|
||||
.attr('href', '#' + hash)
|
||||
.text('¶')
|
||||
);
|
||||
})
|
||||
// links in markdown cells should open in new tabs
|
||||
html.find("a[href]").not('[href^="#"]').attr("target", "_blank");
|
||||
this.set_rendered(html);
|
||||
this.typeset();
|
||||
this.events.trigger("rendered.MarkdownCell", {cell: this})
|
||||
marked(text, function (err, html) {
|
||||
html = mathjaxutils.replace_math(html, math);
|
||||
html = security.sanitize_html(html);
|
||||
html = $($.parseHTML(html));
|
||||
// add anchors to headings
|
||||
html.find(":header").addBack(":header").each(function (i, h) {
|
||||
h = $(h);
|
||||
var hash = h.text().replace(/ /g, '-');
|
||||
h.attr('id', hash);
|
||||
h.append(
|
||||
$('<a/>')
|
||||
.addClass('anchor-link')
|
||||
.attr('href', '#' + hash)
|
||||
.text('¶')
|
||||
);
|
||||
});
|
||||
// links in markdown cells should open in new tabs
|
||||
html.find("a[href]").not('[href^="#"]').attr("target", "_blank");
|
||||
that.set_rendered(html);
|
||||
that.typeset();
|
||||
that.events.trigger("rendered.MarkdownCell", {cell: that});
|
||||
});
|
||||
}
|
||||
return cont;
|
||||
};
|
||||
|
@ -27,7 +27,6 @@
|
||||
bootstrap: 'components/bootstrap/js/bootstrap.min',
|
||||
bootstraptour: 'components/bootstrap-tour/build/js/bootstrap-tour.min',
|
||||
jqueryui: 'components/jquery-ui/ui/minified/jquery-ui.min',
|
||||
highlight: 'components/highlight.js/build/highlight.pack',
|
||||
moment: "components/moment/moment",
|
||||
codemirror: 'components/codemirror',
|
||||
termjs: "components/term.js/src/term",
|
||||
@ -52,10 +51,7 @@
|
||||
jqueryui: {
|
||||
deps: ["jquery"],
|
||||
exports: "$"
|
||||
},
|
||||
highlight: {
|
||||
exports: "hljs"
|
||||
},
|
||||
}
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
Loading…
x
Reference in New Issue
Block a user