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

feat(rehype-shikiji): support metastring from node.properties #114

Merged
merged 5 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
13 changes: 11 additions & 2 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,8 +117,8 @@ const rehypeShikijiFromHighlighter: Plugin<[HighlighterGeneric<any, any>, Rehype
return
}

const attrs = (head.data as any)?.meta
const meta = parseMetaString?.(attrs, node, tree) || {}
const attrs = head.data?.meta ?? head.properties.metastring
const meta = (attrs && parseMetaString?.(attrs, node, tree)) || {}
antfu marked this conversation as resolved.
Show resolved Hide resolved

const codeOptions: CodeToHastOptions = {
...rest,
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')
})
Loading
Loading