Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for a custom highlighter function. #15

Merged
merged 1 commit into from
May 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,49 @@ To support other Markdown or ANSI-coloring engines, set `nb.markdown` and/or `nb

## Code-Highlighting

Notebook.js plays well with code-highlighting libraries. See [NBPreview](https://github.com/jsvine/nbpreview) for an example of how to add support for your preferred highlighter.
Notebook.js plays well with code-highlighting libraries. See [NBPreview](https://github.com/jsvine/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](http://prismjs.com) during page generation for a static site:

```js
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);
};
```

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

Expand Down
7 changes: 4 additions & 3 deletions notebook.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
prefix: "nb-",
markdown: getMarkdown() || ident,
ansi: getAnsi() || ident,
highlighter: ident,
VERSION: VERSION
};

Expand All @@ -81,7 +82,7 @@
var lang = this.cell.raw.language || m.language || m.language_info.name;
code_el.setAttribute("data-language", lang);
code_el.className = "lang-" + lang;
code_el.innerHTML = escapeHTML(joinText(this.raw));
code_el.innerHTML = nb.highlighter(escapeHTML(joinText(this.raw)), pre_el, code_el, lang);
pre_el.appendChild(code_el);
holder.appendChild(pre_el);
this.el = holder;
Expand Down Expand Up @@ -169,7 +170,7 @@
var render_error = function () {
var el = makeElement("pre", [ "pyerr" ]);
var raw = this.raw.traceback.join("\n");
el.innerHTML = nb.ansi(escapeHTML(raw));
el.innerHTML = nb.highlighter(nb.ansi(escapeHTML(raw)), el);
return el;
};

Expand All @@ -188,7 +189,7 @@
"stream": function () {
var el = makeElement("pre", [ (this.raw.stream || this.raw.name) ]);
var raw = joinText(this.raw.text);
el.innerHTML = nb.ansi(escapeHTML(raw));
el.innerHTML = nb.highlighter(nb.ansi(escapeHTML(raw)), el);
return el;
}
};
Expand Down
2 changes: 1 addition & 1 deletion notebook.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.