|
1 | 1 | ///<reference path="../components/phosphor/dist/phosphor.d.ts"/> |
2 | 2 |
|
3 | 3 | import nbformat = require("./nbformat"); |
| 4 | +import mathjaxutils = require("./mathjaxutils"); |
4 | 5 | import DOM = phosphor.virtualdom.dom; |
5 | 6 | import Component = phosphor.virtualdom.Component; |
6 | 7 | import BaseComponent = phosphor.virtualdom.BaseComponent; |
@@ -58,17 +59,30 @@ class JupyterErrorComponent extends Component<nbformat.JupyterError> { |
58 | 59 | } |
59 | 60 | export var JupyterError = createFactory(JupyterErrorComponent) |
60 | 61 |
|
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"); |
66 | 76 |
|
| 77 | + |
| 78 | +class MarkdownCellComponent extends BaseComponent<nbformat.MarkdownCell> { |
67 | 79 | onUpdateRequest(msg: IMessage): void { |
68 | 80 | // 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 | + }); |
72 | 86 | } |
73 | 87 | } |
74 | 88 | export var MarkdownCell = createFactory(MarkdownCellComponent) |
|
0 commit comments