Skip to content

Commit

Permalink
Merge pull request #15 from bradhowes/master
Browse files Browse the repository at this point in the history
Add support for a custom highlighter function.
  • Loading branch information
jsvine authored May 6, 2018
2 parents 587f7ef + d137a66 commit cfa4831
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
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.

0 comments on commit cfa4831

Please sign in to comment.