From 3bb179c7b72b9260e4f8752d047d9f338572cb2a Mon Sep 17 00:00:00 2001 From: Josh Goebel Date: Thu, 6 Feb 2020 16:28:31 -0500 Subject: [PATCH] enh(parser) simplify nohighlight (#2374) * (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. --- CHANGES.md | 3 ++- README.md | 2 +- VERSION_10_BREAKING.md | 2 ++ docs/api.rst | 2 ++ src/highlight.js | 10 +++++----- test/fixtures/index.html | 10 ---------- test/special/noHighlight.js | 35 ++++------------------------------- 7 files changed, 16 insertions(+), 48 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 44e7160830..0491d5be76 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -10,7 +10,7 @@ New themes: Core Changes: -- none. +- make `noHighlightRe` and `languagePrefixRe` configurable (#2374) [Josh Goebel][] Language Improvements: @@ -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 diff --git a/README.md b/README.md index 347a813fe1..05a34c0b0a 100644 --- a/README.md +++ b/README.md @@ -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 | | diff --git a/VERSION_10_BREAKING.md b/VERSION_10_BREAKING.md index 4466fce6d7..1a6473e86d 100644 --- a/VERSION_10_BREAKING.md +++ b/VERSION_10_BREAKING.md @@ -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][] diff --git a/docs/api.rst b/docs/api.rst index aaa0588b9e..646b1d2f67 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -74,6 +74,8 @@ Configures global options: * ``useBR``: a flag to generate ``
`` tags instead of new-line characters in the output, useful when code is marked up using a non-``
`` 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
 ::
diff --git a/src/highlight.js b/src/highlight.js
index 7afe48ff19..201a8287f9 100644
--- a/src/highlight.js
+++ b/src/highlight.js
@@ -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.
@@ -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,
@@ -82,7 +82,7 @@ https://highlightjs.org/
   }
 
   function isNotHighlighted(language) {
-    return noHighlightRe.test(language);
+    return options.noHighlightRe.test(language);
   }
 
   function blockLanguage(block) {
@@ -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) {
diff --git a/test/fixtures/index.html b/test/fixtures/index.html
index aba3e89982..99a45bdf71 100644
--- a/test/fixtures/index.html
+++ b/test/fixtures/index.html
@@ -71,12 +71,6 @@
 
<div id="contents">
   <p>Hello, World!
 </div>
-
<div id="contents">
-  <p>Hello, World!
-</div>
-
<div id="contents">
-  <p>Hello, World!
-</div>
Computer output
@@ -90,10 +84,6 @@
for x in [1, 2, 3]: count(x)
- -
var x = 'foo';
-
var x = 'foo';
- diff --git a/test/special/noHighlight.js b/test/special/noHighlight.js index 57301fbe62..de085ddb47 100644 --- a/test/special/noHighlight.js +++ b/test/special/noHighlight.js @@ -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); - }); });