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 inline code highlighting #3180

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Parser:
- adds `title.class` sub-scope support (#3078) [Josh Goebel][]
- adds `title.function` sub-scope support (#3078) [Josh Goebel][]
- adds `beforeMatch` compiler extension (#3078) [Josh Goebel][]
- adds `allCodeSelector` option (#3180) [James Edington][]
JamesTheAwesomeDude marked this conversation as resolved.
Show resolved Hide resolved

Grammars:

Expand Down Expand Up @@ -129,6 +130,7 @@ Dev Improvements:
[NullVoxPopuli]: https://github.com/NullVoxPopuli
[Mike Watling]: https://github.com/Pickysaurus
[Nico Abram]:https://github.com/nico-abram
joshgoebel marked this conversation as resolved.
Show resolved Hide resolved
[James Edington]: http://www.ishygddt.xyz/


## Version 10.7.1
Expand Down
5 changes: 3 additions & 2 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ Configures global options:
* ``languages``: an array of language names and aliases restricting auto detection to only these languages.
* ``languageDetectRe``: a regex to configure how CSS class names map to language (allows class names like say `color-as-php` vs the default of `language-php`, etc.)
* ``noHighlightRe``: a regex to configure which CSS classes are to be skipped completely.
* ``allCodeSelector``: a CSS selector to configure which elements are affected by ``hljs.highlightAll``. Defaults to ``'pre code'``.

Accepts an object representing options with the values to updated. Other options don't change
::
Expand All @@ -111,7 +112,7 @@ Accepts an object representing options with the values to updated. Other options
highlightAll
------------

Applies highlighting to all ``<pre><code>...</code></pre>`` blocks on a page.
Applies highlighting to all elements on a page matching ``allCodeSelector``.
JamesTheAwesomeDude marked this conversation as resolved.
Show resolved Hide resolved
This can be called before or after the page's ``onload`` event has fired.


Expand All @@ -120,7 +121,7 @@ initHighlighting

*Deprecated as of 10.6:* Please use ``highlightAll()`` instead.

Applies highlighting to all ``<pre><code>...</code></pre>`` blocks on a page.
Applies highlighting to all elements on a page matching ``allCodeSelector``.
JamesTheAwesomeDude marked this conversation as resolved.
Show resolved Hide resolved


initHighlightingOnLoad
Expand Down
8 changes: 5 additions & 3 deletions src/highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ const HLJS = function(hljs) {
languages: null,
// beta configuration options, subject to change, welcome to discuss
// https://github.com/highlightjs/highlight.js/issues/1086
__emitter: TokenTreeEmitter
__emitter: TokenTreeEmitter,
// https://github.com/highlightjs/highlight.js/pull/3180
joshgoebel marked this conversation as resolved.
Show resolved Hide resolved
allCodeSelector: 'pre code',
JamesTheAwesomeDude marked this conversation as resolved.
Show resolved Hide resolved
};

/* Utility functions */
Expand Down Expand Up @@ -773,14 +775,14 @@ const HLJS = function(hljs) {
/**
* auto-highlights all pre>code elements on the page
*/
function highlightAll(selector='pre code') {
function highlightAll() {
// if we are called too early in the loading process
if (document.readyState === "loading") {
wantsHighlight = true;
return;
}

const blocks = document.querySelectorAll(selector);
const blocks = document.querySelectorAll(options.allCodeSelector);
blocks.forEach(highlightElement);
}

Expand Down