Skip to content

Commit

Permalink
enh(parser) simplify nohighlight (#2374)
Browse files Browse the repository at this point in the history
* (chore) make `noHighlightRe` and `languagePrefixRe` configurable (languageDetectRe now)
* (chore) simply 'nohighlight' regex

- Now only `no-highlight` and `nohighlight` skip a block entirely
- `plain` will do nothing
- `text` is now an alias for `plaintext`

Closes #2363.
  • Loading branch information
joshgoebel committed Feb 6, 2020
1 parent 7be9dc8 commit 3bb179c
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 48 deletions.
3 changes: 2 additions & 1 deletion CHANGES.md
Expand Up @@ -10,7 +10,7 @@ New themes:

Core Changes:

- none.
- make `noHighlightRe` and `languagePrefixRe` configurable (#2374) [Josh Goebel][]

Language Improvements:

Expand All @@ -31,6 +31,7 @@ Developer Tools:
[Sam Miller]: https://github.com/smillerc
[Robert Riebisch]: https://github.com/bttrx
[Taufik Nurrohman]: https://github.com/taufik-nurrohman
[Josh Goebel]: https://github.com/yyyc514


## Version 9.18.1
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -177,7 +177,7 @@ The table below shows the full list of supported languages (and corresponding cl
| PHP | php, php3, php4, php5, php6, php7 | |
| Parser3 | parser3 | |
| Perl | perl, pl, pm | |
| Plaintext: no highlight | plaintext | |
| Plaintext | plaintext, txt, text | |
| Pony | pony | |
| PostgreSQL & PL/pgSQL | pgsql, postgres, postgresql | |
| PowerShell | powershell, ps, ps1 | |
Expand Down
2 changes: 2 additions & 0 deletions VERSION_10_BREAKING.md
Expand Up @@ -2,6 +2,8 @@

Incompatibilities:
- chore(parser): compressed version 9.x language definitions no longer supported (rebuild them minified) [Josh Goebel][]
- `nohightlight` and `no-highlight` are the only "ignore me" css classes now (`plain` and `text` no longer count)
(to get the old behavior you can customize `noHighlightRe`)

Renamed Language Files:
- chore(parser): rename `nimrod.js` to `nim.js` [Josh Goebel][]
Expand Down
2 changes: 2 additions & 0 deletions docs/api.rst
Expand Up @@ -74,6 +74,8 @@ Configures global options:
* ``useBR``: a flag to generate ``<br>`` tags instead of new-line characters in the output, useful when code is marked up using a non-``<pre>`` container.
* ``classPrefix``: a string prefix added before class names in the generated markup, used for backwards compatibility with stylesheets.
* ``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

Accepts an object representing options with the values to updated. Other options don't change
::
Expand Down
10 changes: 5 additions & 5 deletions src/highlight.js
Expand Up @@ -42,9 +42,7 @@ https://highlightjs.org/
var SAFE_MODE = true;

// Regular expressions used throughout the highlight.js library.
var noHighlightRe = /^(no-?highlight|plain|text)$/i,
languagePrefixRe = /\blang(?:uage)?-([\w-]+)\b/i,
fixMarkupRe = /((^(<[^>]+>|\t|)+|(?:\n)))/gm;
var fixMarkupRe = /((^(<[^>]+>|\t|)+|(?:\n)))/gm;

// The object will be assigned by the build tool. It used to synchronize API
// of external language files with minified version of the highlight.js library.
Expand All @@ -56,6 +54,8 @@ https://highlightjs.org/
// Global options used when within external APIs. This is modified when
// calling the `hljs.configure` function.
var options = {
noHighlightRe: /^(no-?highlight)$/i,
languageDetectRe: /\blang(?:uage)?-([\w-]+)\b/i,
classPrefix: 'hljs-',
tabReplace: null,
useBR: false,
Expand All @@ -82,7 +82,7 @@ https://highlightjs.org/
}

function isNotHighlighted(language) {
return noHighlightRe.test(language);
return options.noHighlightRe.test(language);
}

function blockLanguage(block) {
Expand All @@ -92,7 +92,7 @@ https://highlightjs.org/
classes += block.parentNode ? block.parentNode.className : '';

// language-* takes precedence over non-prefixed class names.
match = languagePrefixRe.exec(classes);
match = options.languageDetectRe.exec(classes);
if (match) {
var language = getLanguage(match[1]);
if (!language) {
Expand Down
10 changes: 0 additions & 10 deletions test/fixtures/index.html
Expand Up @@ -71,12 +71,6 @@
<pre><code class="no-highlight">&lt;div id="contents"&gt;
&lt;p&gt;Hello, World!
&lt;/div&gt;</code></pre>
<pre><code class="plain">&lt;div id="contents"&gt;
&lt;p&gt;Hello, World!
&lt;/div&gt;</code></pre>
<pre><code class="text">&lt;div id="contents"&gt;
&lt;p&gt;Hello, World!
&lt;/div&gt;</code></pre>

<!-- non-code <pre> -->
<pre><samp>Computer output</samp></pre>
Expand All @@ -90,10 +84,6 @@
<!-- unsupported prefixed language and supported unprefixed language -->
<pre><code class="python language-foo">for x in [1, 2, 3]: count(x)</code></pre>

<!-- class name with no highlighting keyword (eg: 'plain' or 'text') -->
<pre><code class="text-content javascript">var x = 'foo';</code></pre>
<pre><code class="clipboard-text javascript">var x = 'foo';</code></pre>

</div>

<!-- sub-languages -->
Expand Down
35 changes: 4 additions & 31 deletions test/special/noHighlight.js
Expand Up @@ -30,59 +30,32 @@ describe('no highlighting', () => {
actual.should.equal(expected);
});

it('should keep block unchanged (plain)', () => {
const expected = this.expected.html,
actual = this.blocks[2];

actual.should.equal(expected);
});

it('should keep block unchanged (text)', () => {
const expected = this.expected.html,
actual = this.blocks[3];

actual.should.equal(expected);
});

it('should skip pre tags without a child code tag', () => {
const expected = 'Computer output',
actual = this.blocks[4];
actual = this.blocks[2];

actual.should.equal(expected);
});

it('should keep block unchanged (unsupported language)', () => {
const expected = this.expected.python,
actual = this.blocks[5];
actual = this.blocks[3];

actual.should.equal(expected);
});

it('should keep block unchanged (unsupported lang)', () => {
const expected = this.expected.python,
actual = this.blocks[6];
actual = this.blocks[4];

actual.should.equal(expected);
});

it('should keep block unchanged (unsupported prefixed language)', () => {
const expected = this.expected.python,
actual = this.blocks[7];

actual.should.equal(expected);
});

it('should highlight class names containing text at the start', () => {
const expected = this.expected.javascript,
actual = this.blocks[8];
actual = this.blocks[5];

actual.should.equal(expected);
});

it('should highlight class names containing text at the end', () => {
const expected = this.expected.javascript,
actual = this.blocks[9];

actual.should.equal(expected);
});
});

0 comments on commit 3bb179c

Please sign in to comment.