Skip to content
This repository has been archived by the owner on Feb 7, 2024. It is now read-only.

Commit

Permalink
feat(rehype-shikiji): support metastring from node.properties (#114)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Fu <[email protected]>
Co-authored-by: Anthony Fu <[email protected]>
  • Loading branch information
3 people authored Jan 25, 2024
1 parent 32f8a6f commit 3b0cb7b
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/rehype-shikiji/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"unist-util-visit": "^5.0.0"
},
"devDependencies": {
"rehype-raw": "^7.0.0",
"rehype-stringify": "^10.0.0",
"remark-parse": "^11.0.0",
"remark-rehype": "^11.1.0"
Expand Down
17 changes: 13 additions & 4 deletions packages/rehype-shikiji/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ export type RehypeShikijiCoreOptions =
& CodeOptionsMeta
& RehypeShikijiExtraOptions

declare module 'hast' {
interface Data {
meta?: string
}
interface Properties {
metastring?: string
}
}

const rehypeShikijiFromHighlighter: Plugin<[HighlighterGeneric<any, any>, RehypeShikijiCoreOptions], Root> = function (
highlighter,
options,
Expand Down Expand Up @@ -108,16 +117,16 @@ const rehypeShikijiFromHighlighter: Plugin<[HighlighterGeneric<any, any>, Rehype
return
}

const attrs = (head.data as any)?.meta
const meta = parseMetaString?.(attrs, node, tree) || {}
const metaString = head.data?.meta ?? head.properties.metastring ?? ''
const meta = parseMetaString?.(metaString, node, tree) || {}

const codeOptions: CodeToHastOptions = {
...rest,
lang: language.slice(prefix.length),
meta: {
...rest.meta,
...meta,
__raw: attrs,
__raw: metaString,
},
}

Expand All @@ -132,7 +141,7 @@ const rehypeShikijiFromHighlighter: Plugin<[HighlighterGeneric<any, any>, Rehype
})
}

if (highlightLines && typeof attrs === 'string') {
if (highlightLines && typeof metaString === 'string') {
codeOptions.transformers ||= []
codeOptions.transformers.push(
transformerMetaHighlight({
Expand Down
37 changes: 37 additions & 0 deletions packages/rehype-shikiji/test/core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ import fs from 'node:fs/promises'
import { unified } from 'unified'
import remarkParse from 'remark-parse'
import remarkRehype from 'remark-rehype'
import rehypeRaw from 'rehype-raw'
import rehypeStringify from 'rehype-stringify'
import { visit } from 'unist-util-visit'
import { expect, it } from 'vitest'

import { getHighlighter } from 'shikiji'
import type { Root } from 'hast'
import rehypeShikijiFromHighlighter from '../src/core'

it('run', async () => {
Expand All @@ -30,3 +33,37 @@ it('run', async () => {

expect(file.toString()).toMatchFileSnapshot('./fixtures/a.core.out.html')
})

it('run with rehype-raw', async () => {
const highlighter = await getHighlighter({
themes: [
'vitesse-light',
],
langs: [
'javascript',
],
})

const rehypeMetaString = () => (tree: Root) => {
visit(tree, 'element', (node) => {
if (node.tagName === 'code' && node.data?.meta) {
node.properties ??= {}
node.properties.metastring = node.data.meta
}
})
}

const file = unified()
.use(remarkParse)
.use(remarkRehype)
.use(rehypeMetaString)
.use(rehypeRaw)
.use(rehypeShikijiFromHighlighter, highlighter, {
theme: 'vitesse-light',
highlightLines: true,
})
.use(rehypeStringify)
.processSync(await fs.readFile(new URL('./fixtures/a.md', import.meta.url)))

expect(file.toString()).toMatchFileSnapshot('./fixtures/a.core.out.html')
})
11 changes: 11 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3b0cb7b

Please sign in to comment.