Skip to content

Commit

Permalink
initial LaTeX support (resolves #1)
Browse files Browse the repository at this point in the history
This adds support for LaTeX rendering using KaTeX, a fast and popular
library. Since this is a custom extension to Markdown, all of this is
non-standard and doesn't work 100%, e.g. if you include math in some
<pre> code, it will get converted to HTML there (and shown to the user).

But otherwise it's pretty good, just use $ or $$ for inline or
block-based math, respectively.
  • Loading branch information
kokes committed Oct 5, 2019
1 parent 4acb4d3 commit 1c0d733
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
15 changes: 12 additions & 3 deletions lib/nbv.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 || {};
Expand Down Expand Up @@ -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;
}
Expand Down
5 changes: 5 additions & 0 deletions viewer.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
<script src='https://cdnjs.cloudflare.com/ajax/libs/prism/1.5.1/prism.min.js' data-manual></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/prism/1.5.1/components/prism-python.min.js' data-manual></script>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css" integrity="sha384-zB1R0rpPzHqg7Kpt0Aljp8JPLqbXI3bhnPWROx27a9N0Ll6ZP/+DiW/UqRcLbRjq" crossorigin="anonymous">

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.js" integrity="sha384-y23I5Q6l+B6vatafAwxRu/0oK/79VlbSz7Q9aiSZUvyWYIYsd+qj+o24G5ZU2zJz" crossorigin="anonymous"></script>


<style type='text/css'>
body {
font: 0.8em Arial, sans-serif;
Expand Down

0 comments on commit 1c0d733

Please sign in to comment.