### Node.js Usage: Install notebookjs Source: https://github.com/jsvine/notebookjs/blob/master/README.md Command to install the notebookjs package using npm for Node.js environments. ```sh npm install notebookjs ``` -------------------------------- ### Custom Code Highlighting with Prismjs Source: https://github.com/jsvine/notebookjs/blob/master/README.md Provides an example of integrating Prismjs for code highlighting by defining a custom `highlighter` function and assigning it to `nb.highlighter`. The function handles language loading and highlighting. ```javascript var Prism = require('prismjs'); var highlighter = function(code, lang) { if (typeof lang === 'undefined') lang = 'markup'; if (!Prism.languages.hasOwnProperty(lang)) { try { require('prismjs/components/prism-' + lang + '.js'); } catch (e) { console.warn('** failed to load Prism lang: ' + lang); Prism.languages[lang] = false; } } return Prism.languages[lang] ? Prism.highlight(code, Prism.languages[lang]) : code; }; var nb = require("notebookjs"); nb.highlighter = function(text, pre, code, lang) { var language = lang || 'text'; pre.className = 'language-' + language; if (typeof code != 'undefined') { code.className = 'language-' + language; } return highlighter(text, language); }; ``` -------------------------------- ### Node.js Usage: Parse and Render Notebook Source: https://github.com/jsvine/notebookjs/blob/master/README.md Shows how to read a notebook file, parse it, render it, and output the HTML in a Node.js application. ```javascript var fs = require("fs"); var nb = require("notebookjs"); var ipynb = JSON.parse(fs.readFileSync("path/to/notebook.ipynb")); var notebook = nb.parse(ipynb); console.log(notebook.render().outerHTML); ``` -------------------------------- ### Browser Usage: Parse and Render Notebook Source: https://github.com/jsvine/notebookjs/blob/master/README.md Demonstrates parsing raw notebook JSON and rendering it to the DOM in a browser environment. ```javascript var notebook = nb.parse(JSON.parse(raw_ipynb_json_string)); var rendered = notebook.render(); document.body.appendChild(rendered); ``` -------------------------------- ### Browser Usage: Include notebook.js Source: https://github.com/jsvine/notebookjs/blob/master/README.md How to include the notebook.js library in a web page using a script tag. ```html ``` -------------------------------- ### Customizing Markdown and ANSI Coloring Source: https://github.com/jsvine/notebookjs/blob/master/README.md Explains how to set custom functions for Markdown rendering and ANSI text coloring by assigning them to `nb.markdown` and `nb.ansi` respectively. ```javascript // 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. // Example: // nb.markdown = function(text) { return customMarkdownRenderer(text); }; // nb.ansi = function(text) { return customAnsiRenderer(text); }; ``` -------------------------------- ### Enabling JavaScript Execution in Notebooks Source: https://github.com/jsvine/notebookjs/blob/master/README.md Explains how to enable the execution of JavaScript code within notebook cells by setting the `nb.executeJavaScript` option to `true`. This should only be used for trusted notebooks due to potential XSS risks. ```javascript var nb = require("notebookjs"); // Run the JavaScript in notebook. Ensure you only use this for trusted notebooks nb.executeJavaScript = true; var notebook = nb.parse(ipynb); ``` -------------------------------- ### Customizing HTML Sanitization Source: https://github.com/jsvine/notebookjs/blob/master/README.md Details how to set a custom HTML sanitizer function by assigning it to `nb.sanitizer`. Setting it to an identity function disables sanitization. ```javascript // 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; }; // Example: // nb.sanitizer = function(htmlString) { return DOMPurify.sanitize(htmlString); }; ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.