An open API service indexing awesome lists of open source software.

https://github.com/s9e/hljs-loader

Loader for the highlight.js library
https://github.com/s9e/hljs-loader

Last synced: 3 months ago
JSON representation

Loader for the highlight.js library

Awesome Lists containing this project

README

        

hljs-loader is a small (3 KB) script that can be added to any webpage to automatically load and run [highlight.js](https://highlightjs.org/) on all `pre > code` blocks. It automatically loads additional languages specified via the first `lang-*` or `language-*` class name of each code block.

### Usage

Add this anywhere to your HTML page:

```html

```

### Configuration

No configuration is required but a number of options are available and can be set on the `script` element that loads this script.

```html

```


data-hljs-observe

The value should be a CSS selector such as body or #content. The first element that matches this selector will be observed for changes via a MutationObserver instance and new code blocks will be automatically highlighted.

data-hljs-options

The value should be a JSON-encoded object that will be passed to hljs.configure().

data-hljs-style

Name of the highlight.js style to load. If set to none, no style will be loaded. Defaults to default.

data-hljs-url

The root URL used for loading highlight.js files. Defaults to jsDelivr's URL.

### API

By default, all code blocks are highlighted when the library is loaded and you do not need to call the library explicitly. However, there may be some situations where you want to manually trigger highlighting or observe a section of the page for new code blocks.


hljsLoader.highlightBlocks(element)

Highlight all blocks in element's subtree. New languages may be loaded and some blocks may be highlighted asynchronously.

```js
hljsLoader.highlightBlocks(document.body);
```

hljsLoader.observe(selector)

Observe the first element that matches given CSS selector and automatically highlight new code blocks.

```js
hljsLoader.observe('#id');
```

hljsLoader.disconnect()

Disconnect the observer and stop looking for new code blocks.