On Node.js, notebook.js uses marked for Markdown rendering, and ansi_up for ANSI-coloring.
The browser-based version does not, however, ship with those libraries, so you must <script>-include or require them before initializing notebook.js.
To support other Markdown or ANSI-coloring engines, set nb.markdown and/or nb.ansi to functions that accept raw text and return rendered text.
HTML and Markdown Sanitization
On Node.js, notebook.js runs all HTML and Markdown outputs through DOMPurify. The browser-based version, however, does not ship with this library; to enable the default behavior, you must <script>-include or require it before initializing notebook.js.
Alternative sanitizers can be passed by setting nb.sanitizer to a function that accepts a raw HTML string and returns a sanitized version. (To disable sanitization, set nb.sanitizer = function (x) { return x; };.)
Code-Highlighting
Notebook.js plays well with code-highlighting libraries. See NBPreview for an example of how to add support for your preferred highlighter. However, if you wish to inject your own highlighting, you can install a custom highlighter function by adding it under the highlighter name in an notebookjs instance. For instance, here is an implementation which colorizes languages using Prismjs during page generation for a static site:
A highlighter function takes up to four arguments:
text -- text of the cell to highlight
pre -- the DOM <pre> node that holds the cell
code -- the DOM <code> node that holds the cell (if undefined then text is not code)
lang -- the language of the code in the cell (if undefined then text is not code)
The function should at least return the original text value if it cannot perform any highlighting.
MathJax / LaTeX / KaTeX
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. To enable this functionality, the webpage must have the following JavaScript and CSS libraries (or their equivalents, from other sources) loaded:
The HTML rendered by notebook.js (intentionally) does not contain any styling. But each key element has fairly straightfoward CSS classes that make styling your notebooks a cinch. See nbpreview's stylesheet for an example implementation.
Thanks
Many thanks to the following users for catching bugs, fixing typos, and proposing useful features:
请发表评论