Skip to content

Commit

Permalink
Remove ignoreMissing option, make it default
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Sep 5, 2023
1 parent d72c765 commit dc3823d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 54 deletions.
36 changes: 17 additions & 19 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@
* @property {boolean | null | undefined} [detect=false]
* Detect the programming language on code without a language class (default:
* `false`).
* @property {boolean | null | undefined} [ignoreMissing=false]
* Swallow errors for missing languages (default: `false`); unregistered
* syntaxes normally throw an error when used; pass `ignoreMissing: true` to
* swallow those errors and ignore code with unknown code languages.
* @property {Record<string, LanguageFn> | null | undefined} [languages={}]
* Register more languages (optional); each key/value pair passed as arguments
* to `lowlight.registerLanguage`.
Expand Down Expand Up @@ -52,7 +48,6 @@ export default function rehypeHighlight(options) {
const settings = options || emptyOptions
const aliases = settings.aliases
const detect = settings.detect || false
const ignoreMissing = settings.ignoreMissing || false
const languages = settings.languages || common
const plainText = settings.plainText
const prefix = settings.prefix
Expand Down Expand Up @@ -117,22 +112,25 @@ export default function rehypeHighlight(options) {
? lowlight.highlight(lang, toText(parent), {prefix})
: lowlight.highlightAuto(toText(parent), {prefix, subset})
} catch (error) {
const exception = /** @type {Error} */ (error)

if (
lang &&
(!ignoreMissing || !/Unknown language/.test(exception.message))
) {
file.fail('Cannot highlight as `' + lang + '`, it’s not registered', {
ancestors: [parent, node],
cause: exception,
place: node.position,
ruleId: 'missing-language',
source: 'rehype-highlight'
})
const cause = /** @type {Error} */ (error)

if (lang && /Unknown language/.test(cause.message)) {
file.message(
'Cannot highlight as `' + lang + '`, it’s not registered',
{
ancestors: [parent, node],
cause,
place: node.position,
ruleId: 'missing-language',
source: 'rehype-highlight'
}
)

/* c8 ignore next 5 -- throw arbitrary hljs errors */
return
}

return
throw cause
}

if (!lang && result.data && result.data.language) {
Expand Down
2 changes: 0 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -468,8 +468,6 @@ abide by its terms.

[register-alias]: https://github.com/wooorm/lowlight#lowregisteraliasname-alias

[register-language]: https://github.com/wooorm/lowlight#lowregisterlanguagename-syntax

[highlight-js]: https://github.com/isagalaev/highlight.js

[rehype-sanitize]: https://github.com/rehypejs/rehype-sanitize
Expand Down
38 changes: 5 additions & 33 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,32 +291,10 @@ test('rehypeHighlight', async function (t) {
)
})

await t.test('should throw on missing languages', async function () {
try {
await rehype()
.data('settings', {fragment: true})
.use(rehypeHighlight)
.process(
[
'<h1>Hello World!</h1>',
'',
'<pre><code class="lang-foobar">var name = "World";',
'console.log("Hello, " + name + "!")</code></pre>'
].join('\n')
)
assert.fail()
} catch (error) {
assert.match(
String(error),
/Cannot highlight as `foobar`, its not registered/
)
}
})

await t.test('should ignore missing languages', async function () {
await t.test('should warn on missing languages', async function () {
const file = await rehype()
.data('settings', {fragment: true})
.use(rehypeHighlight, {ignoreMissing: true})
.use(rehypeHighlight)
.process(
[
'<h1>Hello World!</h1>',
Expand All @@ -326,15 +304,9 @@ test('rehypeHighlight', async function (t) {
].join('\n')
)

assert.equal(
String(file),
[
'<h1>Hello World!</h1>',
'',
'<pre><code class="hljs lang-foobar">var name = "World";',
'console.log("Hello, " + name + "!")</code></pre>'
].join('\n')
)
assert.deepEqual(file.messages.map(String), [
'3:6-4:43: Cannot highlight as `foobar`, it’s not registered'
])
})

await t.test(
Expand Down

0 comments on commit dc3823d

Please sign in to comment.