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

enh(parser) simplify nohighlight #2374

Merged
merged 11 commits into from Feb 6, 2020
3 changes: 2 additions & 1 deletion CHANGES.md
Expand Up @@ -10,7 +10,7 @@ New themes:

Core Changes:

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

Language Improvements:

Expand All @@ -21,6 +21,7 @@ Developer Tools:
- none.

[Taufik Nurrohman]: https://github.com/taufik-nurrohman
[Josh Goebel]: https://github.com/yyyc514


## Version 9.18.0
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -176,7 +176,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.
* ``languagePrefixRe``: allows you to configure the css language prefix (to allow class names like, `color-as-php`, etc.)
joshgoebel marked this conversation as resolved.
Show resolved Hide resolved
* ``noHighlightRe``: allows you to configure which CSS classes are considered as "do not highlight" flags

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 @@ -41,9 +41,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 @@ -55,6 +53,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,
languagePrefixRe: /\blang(?:uage)?-([\w-]+)\b/i,
classPrefix: 'hljs-',
tabReplace: null,
useBR: false,
Expand All @@ -81,7 +81,7 @@ https://highlightjs.org/
}

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

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

// language-* takes precedence over non-prefixed class names.
match = languagePrefixRe.exec(classes);
match = options.languagePrefixRe.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);
});
});