Skip to content

Commit

Permalink
v0.6.0: Fix KaTeX support, expand sanitization
Browse files Browse the repository at this point in the history
  • Loading branch information
jsvine committed Jan 9, 2021
1 parent 1695f1f commit 960aa4f
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 21 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# notebook.js `v0.5.2`
# notebook.js `v0.6.0`

Notebook.js parses raw [Jupyter](http://jupyter.org/)/[IPython](http://ipython.org/) notebooks, and lets you render them as HTML. See a __[working demo here](https://jsvine.github.io/nbpreview/)__.

Expand Down Expand Up @@ -101,8 +101,9 @@ The function should at least return the original `text` value if it cannot perfo
Notebook.js currently doesn't support all of MathJax's syntaxes (MathML, AsciiMath, LaTeX). In the browser, however, it does support a significant subset of LaTeX via [KaTeX](https://github.com/Khan/KaTeX). To enable this functionality, the webpage must have the following JavaScript and CSS libraries (or their equivalents, from other sources) loaded:

- `https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.12.0/katex.min.js`
- `https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.12.0/katex.min.css`
- `https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.12.0/contrib/auto-render.min.js`
- `https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.12.0/katex.min.css`
- [KaTeX fonts](https://katex.org/docs/font.html)

## Styling Rendered Notebooks

Expand All @@ -123,3 +124,4 @@ Many thanks to the following users for catching bugs, fixing typos, and proposin
- [@dereklieu](https://github.com/dereklieu)
- [@micmcg](https://github.com/micmcg)
- [@mhrvol](https://github.com/mhrvol)
- [@ingodahn](https://github.com/ingodahn)
35 changes: 19 additions & 16 deletions notebook.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// notebook.js 0.5.2
// notebook.js 0.6.0
// http://github.com/jsvine/notebookjs
// notebook.js may be freely distributed under the MIT license.
(function () {
var VERSION = "0.5.2";
var VERSION = "0.6.0";
var root = this;
var isBrowser = root.window !== undefined;
var doc;
Expand Down Expand Up @@ -255,35 +255,38 @@
}
};

var math_delimiters = [
{left: "$$", right: "$$", display: true},
{left: "\\[", right: "\\]", display: true},
{left: "\\(", right: "\\)", display: false},
{left: "$", right: "$", display: false}
];

nb.Cell.prototype.renderers = {
markdown: function () {
var el = makeElement("div", [ "cell", "markdown-cell" ]);
el.innerHTML = nb.markdown(joinText(this.raw.source));

/* Requires to render KaTeX
'https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.10.0/katex.min.js',
'https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.10.0/katex.min.css',
'https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.10.0/contrib/auto-render.min.js',
*/
var joined = joinText(this.raw.source);

// Pre-render math via KaTeX's auto-render extension, if available
if (root.renderMathInElement != null) {
root.renderMathInElement(el, {delimiters: [
{left: "$$", right: "$$", display: true},
{left: "\\[", right: "\\]", display: true},
{left: "\\(", right: "\\)", display: false},
{left: "$", right: "$", display: false}
]});
el.innerHTML = nb.sanitizer(joined);
root.renderMathInElement(el, { delimiters: math_delimiters });
el.innerHTML = nb.sanitizer(nb.markdown(el.innerHTML));
} else {
el.innerHTML = nb.sanitizer(nb.markdown(joined));
}

return el;
},
heading: function () {
var el = makeElement("h" + this.raw.level, [ "cell", "heading-cell" ]);
el.innerHTML = joinText(this.raw.source);
el.innerHTML = nb.sanitizer(joinText(this.raw.source));
return el;
},
raw: function () {
var el = makeElement("div", [ "cell", "raw-cell" ]);
el.innerHTML = joinText(this.raw.source);
el.innerHTML = escapeHTML(joinText(this.raw.source));
return el;
},
code: function () {
Expand Down
2 changes: 1 addition & 1 deletion notebook.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "notebookjs",
"version": "0.5.2",
"version": "0.6.0",
"description": "Parse and render IPython/Jupyter notebooks.",
"main": "notebook.js",
"homepage": "https://github.com/jsvine/notebookjs",
Expand Down

0 comments on commit 960aa4f

Please sign in to comment.