Skip to content

Commit 0cf9eb6

Browse files
committed
Get markdown cells to work well with mathjax.
1 parent 18a228a commit 0cf9eb6

File tree

4 files changed

+26
-9
lines changed

4 files changed

+26
-9
lines changed

jupyter_notebook/static/phosphor-notebook/gulpfile.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ var tsSources = [
1919
"NotebookComponent",
2020
"nbformat",
2121
"demodata",
22+
"mathjaxutils",
2223
].map(function(name) {return "./src/" + name + ".ts"; });
2324

2425

jupyter_notebook/static/phosphor-notebook/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<link rel="stylesheet" href="components/codemirror/lib/codemirror.css">
77
<script src="components/marked/marked.min.js"></script>
88
<script src="/static/components/requirejs/require.js"></script>
9-
<script src="/static/components/MathJax/MathJax.js?config=TeX-AMS_HTML-full"></script>
9+
<script src="/static/components/MathJax/MathJax.js?config=TeX-AMS_HTML-full&delayStartupUntil=configured"></script>
1010
<link href="components/phosphor/dist/phosphor.css" rel="stylesheet" type="text/css">
1111
<!--<link href="build/index.css" rel="stylesheet" type="text/css">-->
1212
<script src="components/phosphor/dist/phosphor.js"></script>

jupyter_notebook/static/phosphor-notebook/src/NotebookComponent.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
///<reference path="../components/phosphor/dist/phosphor.d.ts"/>
22

33
import nbformat = require("./nbformat");
4+
import mathjaxutils = require("./mathjaxutils");
45
import DOM = phosphor.virtualdom.dom;
56
import Component = phosphor.virtualdom.Component;
67
import BaseComponent = phosphor.virtualdom.BaseComponent;
@@ -58,17 +59,30 @@ class JupyterErrorComponent extends Component<nbformat.JupyterError> {
5859
}
5960
export var JupyterError = createFactory(JupyterErrorComponent)
6061

61-
class MarkdownCellComponent extends BaseComponent<nbformat.MarkdownCell> {
62-
constructor(data: nbformat.MarkdownCell, children: Elem[]) {
63-
super(data, children);
64-
this.onUpdateRequest(undefined);
65-
}
62+
// customized renderer example from marked.js readme
63+
var renderer = new (<any>marked).Renderer();
64+
renderer.heading = function (text: string, level: number) {
65+
var escapedText = text.toLowerCase().replace(/[^\w]+/g, '-');
66+
return '<h' + level + '><a name="' +
67+
escapedText +
68+
'" class="anchor" href="#' +
69+
escapedText +
70+
'"><span class="header-link"></span></a>' +
71+
text + '</h' + level + '>';
72+
}
73+
// TODO: make links open new tabs
74+
// links in markdown cells should open in new tabs
75+
// html.find("a[href]").not('[href^="#"]').attr("target", "_blank");
6676

77+
78+
class MarkdownCellComponent extends BaseComponent<nbformat.MarkdownCell> {
6779
onUpdateRequest(msg: IMessage): void {
6880
// replace the innerHTML of the node with the rendered markdown
69-
var x = this.data.source;
70-
this.node.innerHTML = marked(typeof x === "string" ? x : x.join(''));
71-
MathJax.Hub.Queue(["Typeset", MathJax.Hub, this.node]);
81+
var t = mathjaxutils.remove_math(this.data.source);
82+
marked(t.html, { sanitize: true }, (err: any, html: string) => {
83+
this.node.innerHTML = mathjaxutils.replace_math(html, t.math);
84+
MathJax.Hub.Queue(["Typeset", MathJax.Hub, this.node]);
85+
});
7286
}
7387
}
7488
export var MarkdownCell = createFactory(MarkdownCellComponent)

jupyter_notebook/static/phosphor-notebook/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ import NotebookApp = require("./app");
22
import NotebookComponent = require("./NotebookComponent");
33
import render = phosphor.virtualdom.render;
44
import demo = require("./demodata")
5+
import mathjaxutils = require("./mathjaxutils");
56
export function main(): void {
67
// var notebook = new NotebookApp.NotebookApplication;
78
// notebook.run();
89
var test = document.getElementById('test');
10+
mathjaxutils.init();
911
render(NotebookComponent.Notebook(demo.notebook), test);
1012
};

0 commit comments

Comments
 (0)