Changeset View
Changeset View
Standalone View
Standalone View
swh/web/assets/src/bundles/webapp/notebook-rendering.js
- This file was added.
/** | |||||
* Copyright (C) 2019 The Software Heritage developers | |||||
* See the AUTHORS file at the top-level directory of this distribution | |||||
* License: GNU Affero General Public License version 3, or any later version | |||||
* See top-level LICENSE file for more information | |||||
*/ | |||||
import 'script-loader!notebookjs'; | |||||
import AnsiUp from 'ansi_up'; | |||||
import './notebook.css'; | |||||
const ansiup = new AnsiUp(); | |||||
export async function renderNotebook(nbJsonUrl, domElt) { | |||||
let showdown = await import(/* webpackChunkName: "showdown" */ 'utils/showdown'); | |||||
await import(/* webpackChunkName: "highlightjs" */ 'utils/highlightjs'); | |||||
vlorentz: yum | |||||
function renderMarkdown(text) { | |||||
let converter = new showdown.Converter({ | |||||
tables: true, | |||||
literalMidWordUnderscores: true | |||||
}); | |||||
// showdown will remove some LaTex escaping sequences when | |||||
Not Done Inline ActionsJS is weird... vlorentz: JS is weird... | |||||
Done Inline ActionsTell me about it anlambert: Tell me about it | |||||
// converting md to html. So we use the following hack to keep | |||||
// them in the html output and avoid MathJax typesetting errors | |||||
for (let specialLaTexChar of ['{', '}', '#', '%', '&', '_']) { | |||||
text = text.replace('\\' + specialLaTexChar, | |||||
'\\\\' + specialLaTexChar); | |||||
} | |||||
// line breaks in LaTex array need also special escaping | |||||
text = text.replace('\\\\', '\\\\\\\\'); | |||||
let rendered = converter.makeHtml(text); | |||||
return rendered; | |||||
} | |||||
function highlightCode(text, preElt, codeElt, lang) { | |||||
if (lang) { | |||||
return hljs.highlight(lang, text).value; | |||||
} else { | |||||
return text; | |||||
} | |||||
} | |||||
function renderAnsi(text) { | |||||
return ansiup.ansi_to_html(text); | |||||
} | |||||
nb.markdown = renderMarkdown; | |||||
nb.highlighter = highlightCode; | |||||
nb.ansi = renderAnsi; | |||||
function initMathJax() { | |||||
// same config as in nbviewer | |||||
window.MathJax = { | |||||
TeX: { | |||||
equationNumbers: { | |||||
autoNumber: 'AMS', | |||||
Not Done Inline ActionsDo we use transpiling to older JS versions? If not, this will break on not-so-old browsers. vlorentz: Do we use transpiling to older JS versions? If not, this will break on not-so-old browsers. | |||||
Done Inline ActionsOf course we do anlambert: Of course we do | |||||
useLabelIds: true | |||||
} | |||||
}, | |||||
tex2jax: { | |||||
inlineMath: [ ['$', '$'], ['\\(', '\\)'] ], | |||||
displayMath: [ ['$$', '$$'], ['\\[', '\\]'] ], | |||||
processEscapes: true, | |||||
processEnvironments: true | |||||
}, | |||||
displayAlign: 'center', | |||||
'HTML-CSS': { | |||||
styles: {'.MathJax_Display': {'margin': 0}}, | |||||
linebreaks: { automatic: true } | |||||
} | |||||
}; | |||||
// MathJax is not easily webpackable in its current version | |||||
// (https://github.com/mathjax/MathJax/issues/1629) | |||||
// and is quite a monster regarding the number of files to distribute. | |||||
// So we will load it through a CDN for commodity of use here. | |||||
let head = document.getElementsByTagName('head')[0]; | |||||
let script = document.createElement('script'); | |||||
script.type = 'text/javascript'; | |||||
script.src = 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS_HTML'; | |||||
Not Done Inline ActionsWhy don't we want these underscores to be interprested by showdown? vlorentz: Why don't we want these underscores to be interprested by showdown? | |||||
Done Inline ActionsTo get correct math typesetting when using MathJax. LaTeX formula containing substrings like { A }_{ 1 } will be turned by showdown to { A }<em>{ 1 } anlambert: To get correct math typesetting when using MathJax.
LaTeX formula containing substrings like… | |||||
head.appendChild(script); | |||||
} | |||||
fetch(nbJsonUrl) | |||||
.then(response => response.json()) | |||||
.then(nbJson => { | |||||
// load MathJax library for math typesetting | |||||
initMathJax(); | |||||
// give some time to MathJax to load | |||||
setTimeout(() => { | |||||
// parse and render the notebook | |||||
let notebook = nb.parse(nbJson); | |||||
let rendered = notebook.render(); | |||||
// insert rendered notebook in the DOM | |||||
$(domElt).append(rendered); | |||||
// execute math typesetting | |||||
MathJax.Hub.Queue(['Typeset', MathJax.Hub]); | |||||
}, 1000); | |||||
}); | |||||
} | |||||
Not Done Inline ActionsLet's open a task to remember that vlorentz: Let's open a task to remember that | |||||
Done Inline Actionsanlambert: T1680 |
yum