From 506b6a775fa0019236a7bccccea86914b4c815fb Mon Sep 17 00:00:00 2001 From: Yann Defretin Date: Fri, 1 Nov 2019 15:58:14 +0100 Subject: [PATCH 1/2] push add latex support --- nbviewer.js/README.md | 10 ++++ nbviewer.js/lib/nbv.js | 15 ++++- nbviewer.js/viewer.html | 118 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 137 insertions(+), 6 deletions(-) diff --git a/nbviewer.js/README.md b/nbviewer.js/README.md index f43b178..483d873 100644 --- a/nbviewer.js/README.md +++ b/nbviewer.js/README.md @@ -8,6 +8,16 @@ I thought it could be easier and more lightweight. So I hacked together this cli [**Try a live demo**](https://kokes.github.io/nbviewer.js/viewer.html) +#### NEW: Rendering Github notebooks + +You can now render notebooks hosted on Github. You can copy and paste their URL in the viewer, linked above, or you can save this following link as a bookmark: + +``` +javascript:(function(){location.href="https://kokes.github.io/nbviewer.js/viewer.html#"+btoa(location.href);})(); +``` + +Clicking this while on Github, looking at a notebook, will launch our nbviewer with this notebook rendered here instead. You'll also get a permanent link for you to share. + #### Usage There are two ways one can use this. You can use the library itself, there is just a single public method, you call `nbv.render(data, target)`, where `data` is the JSON representation of your Jupyter notebook and `target` is the node where the notebook is to be rendered. diff --git a/nbviewer.js/lib/nbv.js b/nbviewer.js/lib/nbv.js index 331b955..0e6e207 100644 --- a/nbviewer.js/lib/nbv.js +++ b/nbviewer.js/lib/nbv.js @@ -5,8 +5,8 @@ var nbv = (function() { var st = {}; // settings function render_ipynb(obj, target, settings) { - if (!window.marked || !window.Prism) { - console.error('expecting libraries marked.js and Prism.js to be present'); + if (!window.marked || !window.Prism || !window.katex) { + console.error('expecting libraries marked.js, Prism.js and KaTeX to be present'); return; } st = settings || {}; @@ -291,9 +291,18 @@ var nbv = (function() { return cn; } + function latexer(match, m1, offset, string) { + return katex.renderToString(m1, { + throwOnError: false, // we do not want to stop rendering because of bad LaTeX + }); + } + function handle_mdown(cell) { var el = d.createElement('div'); - el.innerHTML = marked(cell.source.join('')); + var source = cell.source.join(''); + var latexed = source.replace(/\$\$([\s\S]+?)\$\$/g, latexer); // block-based math + latexed = latexed.replace(/\$(.+?)\$/g, latexer); // inline math + el.innerHTML = marked(latexed); return el; } diff --git a/nbviewer.js/viewer.html b/nbviewer.js/viewer.html index 39faedc..25a5417 100644 --- a/nbviewer.js/viewer.html +++ b/nbviewer.js/viewer.html @@ -9,6 +9,11 @@ + + + + + @@ -50,7 +73,13 @@
-
Drag and drop Jupyter notebooks anywhere here
+ +
Drag and drop Jupyter notebooks anywhere here...
+ or paste a link from Github:
+ +
+ +
@@ -58,12 +87,30 @@ + + + + +