@@ -3,7 +3,7 @@ import { bundledLanguages, getHighlighter } from 'shikiji'
3
3
import { toString } from 'hast-util-to-string'
4
4
import { visit } from 'unist-util-visit'
5
5
import type { Plugin } from 'unified'
6
- import type { Root } from 'hast'
6
+ import type { Element , Root } from 'hast'
7
7
import { parseHighlightLines } from '../../shared/line-highlight'
8
8
9
9
export type RehypeShikijiOptions = CodeOptionsThemes < BuiltinTheme > & {
@@ -20,11 +20,28 @@ export type RehypeShikijiOptions = CodeOptionsThemes<BuiltinTheme> & {
20
20
* @default true
21
21
*/
22
22
highlightLines ?: boolean | string
23
+
24
+ /**
25
+ * Extra meta data to pass to the highlighter
26
+ */
27
+ meta ?: Record < string , any >
28
+
29
+ /**
30
+ * Custom meta string parser
31
+ * Return an object to merge with `meta`
32
+ */
33
+ parseMetaString ?: (
34
+ metaString : string ,
35
+ node : Element ,
36
+ tree : Root
37
+ ) => Record < string , any > | undefined | null
23
38
}
24
39
25
40
const rehypeShikiji : Plugin < [ RehypeShikijiOptions ] , Root > = function ( options = { } as any ) {
26
41
const {
27
42
highlightLines = true ,
43
+ parseMetaString,
44
+ ...rest
28
45
} = options
29
46
30
47
const prefix = 'language-'
@@ -64,12 +81,18 @@ const rehypeShikiji: Plugin<[RehypeShikijiOptions], Root> = function (options =
64
81
return
65
82
66
83
const code = toString ( head as any )
84
+ const attrs = ( head . data as any ) ?. meta
85
+ const meta = parseMetaString ?.( attrs , node , tree ) || { }
86
+
67
87
const codeOptions : CodeToHastOptions = {
68
- ...options ,
88
+ ...rest ,
69
89
lang : language . slice ( prefix . length ) ,
90
+ meta : {
91
+ ...rest . meta ,
92
+ ...meta ,
93
+ } ,
70
94
}
71
95
72
- const attrs = ( head . data as any ) ?. meta
73
96
if ( highlightLines && typeof attrs === 'string' ) {
74
97
const lines = parseHighlightLines ( attrs )
75
98
if ( lines ) {
0 commit comments