Skip to content

Commit

Permalink
Update lowlight
Browse files Browse the repository at this point in the history
If you passed `options.languages`, that now replaces the default
of `common`.
If you want common languages and more languages, pass:
`{...common, ...yourOtherLanguages}`.
  • Loading branch information
wooorm committed Sep 4, 2023
1 parent cad037f commit d72c765
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 32 deletions.
36 changes: 12 additions & 24 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/**
* @typedef {import('hast').Element} Element
* @typedef {import('hast').ElementContent} ElementContent
* @typedef {import('hast').Root} Root
*
* @typedef {import('lowlight').Root} LowlightRoot
* @typedef {import('lowlight/lib/core.js').HighlightSyntax} HighlightSyntax
* To do: expose from `lowlight` root.
* @typedef {import('lowlight').LanguageFn} LanguageFn
*
* @typedef {import('vfile').VFile} VFile
*/
Expand All @@ -21,7 +20,7 @@
* 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, HighlightSyntax> | null | undefined} [languages={}]
* @property {Record<string, LanguageFn> | null | undefined} [languages={}]
* Register more languages (optional); each key/value pair passed as arguments
* to `lowlight.registerLanguage`.
* @property {Array<string> | null | undefined} [plainText=[]]
Expand All @@ -35,7 +34,7 @@
*/

import {toText} from 'hast-util-to-text'
import {lowlight} from 'lowlight'
import {common, createLowlight} from 'lowlight'
import {visit} from 'unist-util-visit'

/** @type {Options} */
Expand All @@ -54,27 +53,18 @@ export default function rehypeHighlight(options) {
const aliases = settings.aliases
const detect = settings.detect || false
const ignoreMissing = settings.ignoreMissing || false
const languages = settings.languages
const languages = settings.languages || common
const plainText = settings.plainText
const prefix = settings.prefix
const subset = settings.subset
let name = 'hljs'

const lowlight = createLowlight(languages)

if (aliases) {
lowlight.registerAlias(aliases)
}

if (languages) {
/** @type {string} */
let key

for (key in languages) {
if (Object.hasOwn(languages, key)) {
lowlight.registerLanguage(key, languages[key])
}
}
}

if (prefix) {
const pos = prefix.indexOf('-')
name = pos > -1 ? prefix.slice(0, pos) : prefix
Expand Down Expand Up @@ -119,15 +109,13 @@ export default function rehypeHighlight(options) {
node.properties.className.unshift(name)
}

/** @type {LowlightRoot} */
/** @type {Root} */
let result

try {
result = lang
? // @ts-expect-error: to do: lowlight types are wrong.
lowlight.highlight(lang, toText(parent), {prefix})
: // @ts-expect-error: to do: lowlight types are wrong.
lowlight.highlightAuto(toText(parent), {prefix, subset})
? lowlight.highlight(lang, toText(parent), {prefix})
: lowlight.highlightAuto(toText(parent), {prefix, subset})
} catch (error) {
const exception = /** @type {Error} */ (error)

Expand All @@ -147,12 +135,12 @@ export default function rehypeHighlight(options) {
return
}

if (!lang && result.data.language) {
if (!lang && result.data && result.data.language) {
node.properties.className.push('language-' + result.data.language)
}

if (result.children.length > 0) {
node.children = result.children
node.children = /** @type {Array<ElementContent>} */ (result.children)
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"dependencies": {
"@types/hast": "^3.0.0",
"hast-util-to-text": "^4.0.0",
"lowlight": "^2.0.0",
"lowlight": "^3.0.0",
"unist-util-visit": "^5.0.0",
"vfile": "^6.0.0"
},
Expand Down
5 changes: 2 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,8 @@ Passed to [`lowlight.registerAlias`][register-alias].

###### `options.languages`

Register more languages (`Record<string, Function>`, default: `{}`).
Each key/value pair passed as arguments to
[`lowlight.registerLanguage`][register-language].
Register languages (`Record<string, Function>`, default: [`common`][common]).
Passed to `createLowlight`.

## Example

Expand Down
8 changes: 4 additions & 4 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/**
* @typedef {import('lowlight/lib/core.js').HighlightSyntax} HighlightSyntax
* To do: expose from lowlight root?
* @typedef {import('lowlight').LanguageFn} LanguageFn
*/

import assert from 'node:assert/strict'
import test from 'node:test'
import {common} from 'lowlight'
import {rehype} from 'rehype'
import rehypeHighlight from './index.js'

Expand Down Expand Up @@ -489,7 +489,7 @@ test('rehypeHighlight', async function (t) {
await t.test('should register languages', async function () {
const file = await rehype()
.data('settings', {fragment: true})
.use(rehypeHighlight, {languages: {test: testLang}})
.use(rehypeHighlight, {languages: {...common, test: testLang}})
.process(
[
'<h1>Hello World!</h1>',
Expand All @@ -508,7 +508,7 @@ test('rehypeHighlight', async function (t) {
)

/**
* @type {HighlightSyntax}
* @type {LanguageFn}
*/
function testLang() {
return {
Expand Down

0 comments on commit d72c765

Please sign in to comment.