From 2c5a4ec5938c3eae9a707c8ccd59bea50e79e30c Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Wed, 12 Oct 2022 19:24:17 +0200 Subject: [PATCH] Remove detection by default Previously, even without a class on `code`, its programming language was inferred, and it was highlighted anyway. It was possible to turn this by passing `subset: false`, an overload of an option that also has another job. This commit reverts this behavior, and removes the overload in favor of a new option: pass `detect: true` to automatically detect the programming language of code blocks. The default, `detect: false`, does not do that. If you had `subset: false`, you can remove that line. If you want the previous behavior, pass `detect: true`. Closes GH-21. --- lib/index.js | 14 ++++++++------ readme.md | 33 +++++++++++++++++---------------- test.js | 42 ++++++++++++++++++++++++++++++------------ 3 files changed, 55 insertions(+), 34 deletions(-) diff --git a/lib/index.js b/lib/index.js index 69bdace..65ab8df 100644 --- a/lib/index.js +++ b/lib/index.js @@ -9,9 +9,11 @@ * Configuration (optional). * @property {string} [prefix='hljs-'] * Prefix to use before classes. - * @property {boolean|Array} [subset] + * @property {boolean} [detect=false] + * Whether to detect the programming language on code without a language + * class. + * @property {Array} [subset] * Scope of languages to check when auto-detecting (default: all languages). - * Pass `false` to not highlight code without language classes. * @property {boolean} [ignoreMissing=false] * Swallow errors for missing languages. * By default, unregistered syntaxes throw an error when they are used. @@ -41,7 +43,8 @@ const own = {}.hasOwnProperty * @type {import('unified').Plugin<[Options?] | Array, Root>} */ export default function rehypeHighlight(options = {}) { - const {aliases, languages, prefix, plainText, ignoreMissing, subset} = options + const {aliases, languages, prefix, plainText, ignoreMissing, subset, detect} = + options let name = 'hljs' if (aliases) { @@ -83,7 +86,7 @@ export default function rehypeHighlight(options = {}) { if ( lang === false || - (!lang && subset === false) || + (!lang && !detect) || (lang && plainText && plainText.includes(lang)) ) { return @@ -103,8 +106,7 @@ export default function rehypeHighlight(options = {}) { try { result = lang ? lowlight.highlight(lang, toText(parent), {prefix}) - : // @ts-expect-error: we checked that `subset` is not a boolean. - lowlight.highlightAuto(toText(parent), {prefix, subset}) + : lowlight.highlightAuto(toText(parent), {prefix, subset}) } catch (error) { const exception = /** @type {Error} */ (error) if (!ignoreMissing || !/Unknown language/.test(exception.message)) { diff --git a/readme.md b/readme.md index 9cef8a5..0c7d1d0 100644 --- a/readme.md +++ b/readme.md @@ -45,10 +45,11 @@ them. You can specify the code language (such as Python) with a `language-*` or `lang-*` class, where the `*` can be for example `js` (so `language-js`), `md`, `css`, etc. -By default, even without a class, all `
` is highlighted by
-automatically detecting which code language it seems to be.
-You can prevent that with a `no-highlight` or `nohighlight` class on the
-`` or by passing `options.subset: false`.
+By default, code without such a language class is not highlighted.
+Pass `detect: true` to detect their programming language and highlight the code
+anyway.
+You can still prevent specific blocks from being highlighted with a
+`no-highlight` or `nohighlight` class on the ``.
 
 **unified** is a project that transforms content with abstract syntax trees
 (ASTs).
@@ -113,16 +114,12 @@ import {read} from 'to-vfile'
 import {rehype} from 'rehype'
 import rehypeHighlight from 'rehype-highlight'
 
-main()
-
-async function main() {
-  const file = await rehype()
-    .data('settings', {fragment: true})
-    .use(rehypeHighlight)
-    .process(await read('example.html'))
+const file = await rehype()
+  .data('settings', {fragment: true})
+  .use(rehypeHighlight)
+  .process(await read('example.html'))
 
-  console.log(String(file))
-}
+console.log(String(file))
 ```
 
 Now running `node example.js` yields:
@@ -151,11 +148,15 @@ Configuration (optional).
 
 Prefix to use before classes (`string`, default: `'hljs-'`).
 
+###### `options.detect`
+
+Whether to detect the programming language on code without a language class
+(`boolean`, default: `false`).
+
 ###### `options.subset`
 
-Scope of languages to check when automatically detecting (`boolean` or
-`Array`, default: all languages).
-Pass `false` to not highlight code without language classes.
+Languages to check when automatically detecting (`Array`, default: all
+languages).
 
 ###### `options.plainText`
 
diff --git a/test.js b/test.js
index f70e06d..cec2e75 100644
--- a/test.js
+++ b/test.js
@@ -10,7 +10,7 @@ test('rehypeHighlight', (t) => {
   t.equal(
     rehype()
       .data('settings', {fragment: true})
-      .use(rehypeHighlight)
+      .use(rehypeHighlight, {detect: true})
       .processSync(
         ['

Hello World!

', '', '
'].join('\n') ) @@ -33,18 +33,36 @@ test('rehypeHighlight', (t) => { ].join('\n') ) .toString(), + ['

Hello World!

', '', '
"use strict";
'].join( + '\n' + ), + 'should not highlight (no class)' + ) + + t.equal( + rehype() + .data('settings', {fragment: true}) + .use(rehypeHighlight, {detect: true}) + .processSync( + [ + '

Hello World!

', + '', + '
"use strict";
' + ].join('\n') + ) + .toString(), [ '

Hello World!

', '', '
"use strict";
' ].join('\n'), - 'should highlight (no class)' + 'should highlight (`detect`, no class)' ) t.equal( rehype() .data('settings', {fragment: true}) - .use(rehypeHighlight, {subset: ['arduino']}) + .use(rehypeHighlight, {detect: true, subset: ['arduino']}) .processSync( [ '

Hello World!

', @@ -58,13 +76,13 @@ test('rehypeHighlight', (t) => { '', '
"use strict";
' ].join('\n'), - 'should highlight (no class, subset)' + 'should highlight (detect, no class, subset)' ) t.equal( rehype() .data('settings', {fragment: true}) - .use(rehypeHighlight, {subset: false}) + .use(rehypeHighlight, {detect: false}) .processSync( [ '

Hello World!

', @@ -76,13 +94,13 @@ test('rehypeHighlight', (t) => { ['

Hello World!

', '', '
"use strict";
'].join( '\n' ), - 'should not highlight (no class, subset: false)' + 'should not highlight (`detect: false`, no class)' ) t.equal( rehype() .data('settings', {fragment: true}) - .use(rehypeHighlight, {prefix: 'foo'}) + .use(rehypeHighlight, {prefix: 'foo', detect: true}) .processSync( [ '

Hello World!

', @@ -102,7 +120,7 @@ test('rehypeHighlight', (t) => { t.equal( rehype() .data('settings', {fragment: true}) - .use(rehypeHighlight, {prefix: 'foo-'}) + .use(rehypeHighlight, {prefix: 'foo-', detect: true}) .processSync( [ '

Hello World!

', @@ -296,7 +314,7 @@ test('rehypeHighlight', (t) => { t.equal( rehype() .data('settings', {fragment: true}) - .use(rehypeHighlight, {subset: ['cpp']}) + .use(rehypeHighlight, {subset: ['cpp'], detect: true}) .processSync(`
def add(a, b):\n  return a + b
`) .toString(), '
def add(a, b):\n  return a + b
', @@ -364,7 +382,7 @@ test('rehypeHighlight', (t) => { t.equal( rehype() .data('settings', {fragment: true}) - .use(rehypeHighlight) + .use(rehypeHighlight, {detect: true}) .processSync( [ '

Hello World!

', @@ -389,7 +407,7 @@ test('rehypeHighlight', (t) => { [ '

Hello World!

', '', - '
"use strict";
console.log("very strict")
' + '
"use strict";
console.log("very strict")
' ].join('\n') ) .toString(), @@ -421,7 +439,7 @@ test('rehypeHighlight', (t) => { [ '

Hello World!

', '', - '
test normal text
' + '
test normal text
' ].join('\n') ) .toString(),