Skip to content

Commit

Permalink
Add plainText option
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jul 4, 2024
1 parent 0da15b8 commit c9d7aaf
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const listFormat = new Intl.ListFormat('en', {type: 'disjunction'})

/** @type {Readonly<Options>} */
const emptyOptions = {}
/** @type {ReadonlyArray<never>} */
const emptyPlainText = []

const prefix = 'language-'

Expand All @@ -30,6 +32,7 @@ const max = 4
export default function rehypeStarryNight(options) {
const settings = options || emptyOptions
const grammars = settings.grammars || common
const plainText = settings.plainText || emptyPlainText
const starryNightPromise = createStarryNight(grammars, settings)
const names = grammars.flatMap(function (d) {
return d.names
Expand Down Expand Up @@ -67,7 +70,9 @@ export default function rehypeStarryNight(options) {
const language = languageClass.slice(prefix.length)
const scope = starryNight.flagToScope(language)

if (scope) {
if (plainText.includes(language)) {
// Empty.
} else if (scope) {
const fragment = starryNight.highlight(toString(node), scope)
node.children = /** @type {Array<ElementContent>} */ (
fragment.children
Expand Down
4 changes: 4 additions & 0 deletions lib/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ export interface Options extends StarryNightOptions {
* Grammars to support (default: `common`).
*/
grammars?: ReadonlyArray<Grammar> | null | undefined
/**
* List of language names to not highlight (default: `[]`).
*/
plainText?: ReadonlyArray<string> | null | undefined
}
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ Configuration for `rehype-starry-night`.

* `grammars?` (`ReadonlyArray<Grammar> | null | undefined`)
— grammars to support (default: `common`)
* `plainText?` (`ReadonlyArray<string> | null | undefined`)
— list of language names to not highlight (default: `[]`)

### `rehypeStarryNight(options) (default)`

Expand Down
16 changes: 16 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,22 @@ alert(hi)
'1:6-1:47: Unexpected unknown language `hypescript` defined with `language-` class, expected a known name; did you mean `typescript` or `cakescript`'
])
})

await t.test('should ignore languages in `plainText`', async function () {
const file = await unified()
.use(rehypeParse, {fragment: true})
.use(rehypeStarryNight, {plainText: ['hypescript', 'javascript']})
.use(rehypeStringify)
.process(
'<pre><code class="language-javascript">"hi"</code></pre>\n<pre><code class="language-hypescript"></code></pre>'
)

assert.equal(
String(file),
'<pre><code class="language-javascript">"hi"</code></pre>\n<pre><code class="language-hypescript"></code></pre>'
)
assert.deepEqual(file.messages.map(String), [])
})
})

test('fixtures', async function (t) {
Expand Down

0 comments on commit c9d7aaf

Please sign in to comment.