asciidoctor-prism-extension
Version:
Highlight Asciidoc code listings with Prism.js, server side, with no front-end dependencies.
102 lines (69 loc) • 2.88 kB
Markdown
> Highlight Asciidoc code listings with Prism.js, server side, with no front-end dependencies.
This module is intended to be used with [Asciidoctor.js][], and Node.js (v10+). It adds a new syntax highlighter, to be used when converting Asciidoc to HTML5 documents. The syntax highlighting happens during the document conversion.

```bash
$ npm install @asciidoctor/core asciidoctor-prism-extension
```
```js
const asciidoctor = require('@asciidoctor/core')();
const prismExtension = require('asciidoctor-prism-extension');
asciidoctor.SyntaxHighlighter.register('prism', prismExtension);
```
This extension relies on [Asciidoc attributes](asciidoc-attributes) to affect the **theme** and the **highlighted syntaxes**.
```asciidoc
= Document
:source-highlighter: prism
:prism-languages: bash,docker,jsx
...
```
| Attribute | Default | Possible values
| --- | --- | ---
| `prism-theme` | `prism.css` | [Any filename in this list][prism-themes].
| `prism-languages` | `asciidoc,bash,json,markdown,typescript,yaml` | [Any supported language][prism-languages].
Theme can be disabled with the negated attribute (`!` character):
```asciidoc
= Document
:source-highlighter: prism
:prism-theme!:
:prism-languages: bash,docker,jsx
...
```
Then you convert as usual:
```js
asciidoctor.convertFile('document.adoc', {
to_file: 'document.html',
backend: 'html5'
});
```
**Note**: attributes can be specified at this stage too. [Have a look at Asciidoctor.js API][asciidoc-api] to learn more about it.
Author your documents as you would regularly do. The second argument of a _code listing_ hints the language to the syntax highlighter.
In the following example, `yaml` is going to be used to colour the output:
```asciidoc
[]
.example.yml
----
language: node_js
node_js: node
script: npm test
----
```
[][] is a highly configurable **syntax highlighter**.
[](LICENSE).
[]: https://prismjs.com
[]: https://github.com/PrismJS/prism/tree/master/themes
[]: https://prismjs.com/#languages-list
[]: https://github.com/asciidoctor/asciidoctor.js/
[]: https://asciidoctor.org
[]: https://github.com/asciidoctor/asciidoctor.js/blob/master/docs/manual.adoc#api
[]: https://asciidoctor.org/docs/user-manual/#source-code-blocks
[]: https://asciidoctor.org/docs/user-manual/#attributes