@@ -8,6 +8,10 @@ import { defineConfig, loadEnv, type Plugin } from 'vite';
8
8
import Inspect from 'vite-plugin-inspect' ;
9
9
import { examplesData , playgroundData , rawSource , tutorialData } from './vite.repl-apps' ;
10
10
import { sourceResolver } from './vite.source-resolver' ;
11
+ import shikiRehype from '@shikijs/rehype' ;
12
+ import { transformerMetaHighlight , transformerMetaWordHighlight } from '@shikijs/transformers' ;
13
+ import { transformerColorizedBrackets } from '@shikijs/colorized-brackets' ;
14
+ import type { ShikiTransformer } from '@shikijs/types' ;
11
15
12
16
const PUBLIC_QWIK_INSIGHTS_KEY = loadEnv ( '' , '.' , 'PUBLIC' ) . PUBLIC_QWIK_INSIGHTS_KEY ;
13
17
const docsDir = new URL ( import . meta. url ) . pathname ;
@@ -52,9 +56,45 @@ const muteWarningsPlugin = (warningsToIgnore: string[][]): Plugin => {
52
56
} ;
53
57
} ;
54
58
55
- export default defineConfig ( async ( ) => {
56
- const { default : rehypePrettyCode } = await import ( 'rehype-pretty-code' ) ;
59
+ function transformerShowEmptyLines ( ) : ShikiTransformer {
60
+ return {
61
+ line ( node ) {
62
+ if ( node . children . length === 0 ) {
63
+ node . children = [ { type : 'text' , value : ' ' } ] ;
64
+ return node ;
65
+ }
66
+ } ,
67
+ } ;
68
+ }
69
+
70
+ function transformerMetaShowTitle ( ) : ShikiTransformer {
71
+ return {
72
+ root ( node ) {
73
+ const meta = this . options . meta ?. __raw ;
74
+ if ( ! meta ) {
75
+ return ;
76
+ }
77
+ const titleMatch = meta . match ( / t i t l e = " ( [ ^ " ] * ) " / ) ;
78
+ if ( ! titleMatch ) {
79
+ return ;
80
+ }
81
+ const title = titleMatch [ 1 ] ?? '' ;
82
+ if ( title . length > 0 ) {
83
+ node . children . unshift ( {
84
+ type : 'element' ,
85
+ tagName : 'div' ,
86
+ properties : {
87
+ class : 'shiki-title' ,
88
+ } ,
89
+ children : [ { type : 'text' , value : title } ] ,
90
+ } ) ;
91
+ }
92
+ meta . replace ( titleMatch [ 0 ] , '' ) ;
93
+ } ,
94
+ } ;
95
+ }
57
96
97
+ export default defineConfig ( async ( ) => {
58
98
const routesDir = resolve ( 'src' , 'routes' ) ;
59
99
return {
60
100
dev : {
@@ -113,46 +153,16 @@ export default defineConfig(async () => {
113
153
mdx : {
114
154
rehypePlugins : [
115
155
[
116
- rehypePrettyCode as any ,
156
+ shikiRehype ,
117
157
{
118
158
theme : 'dark-plus' ,
119
- onVisitLine ( node : any ) {
120
- // Prevent lines from collapsing in `display: grid` mode, and
121
- // allow empty lines to be copy/pasted
122
- if ( node . children . length === 0 ) {
123
- node . children = [ { type : 'text' , value : ' ' } ] ;
124
- }
125
- } ,
126
- onVisitHighlightedLine ( node : any ) {
127
- // Each line node by default has `class="line"`.
128
- if ( node . properties . className ) {
129
- node . properties . className . push ( 'line--highlighted' ) ;
130
- }
131
- } ,
132
- onVisitHighlightedWord ( node : any , id : string ) {
133
- // Each word node has no className by default.
134
- node . properties . className = [ 'word' ] ;
135
- if ( id ) {
136
- const backgroundColor = {
137
- a : 'rgb(196 42 94 / 59%)' ,
138
- b : 'rgb(0 103 163 / 56%)' ,
139
- c : 'rgb(100 50 255 / 35%)' ,
140
- } [ id ] ;
141
-
142
- const color = {
143
- a : 'rgb(255 225 225 / 100%)' ,
144
- b : 'rgb(175 255 255 / 100%)' ,
145
- c : 'rgb(225 200 255 / 100%)' ,
146
- } [ id ] ;
147
- if ( node . properties [ 'data-rehype-pretty-code-wrapper' ] ) {
148
- node . children . forEach ( ( childNode : any ) => {
149
- childNode . properties . style = `` ;
150
- childNode . properties . className = '' ;
151
- } ) ;
152
- }
153
- node . properties . style = `background-color: ${ backgroundColor } ; color: ${ color } ;` ;
154
- }
155
- } ,
159
+ transformers : [
160
+ transformerMetaHighlight ( ) ,
161
+ transformerMetaWordHighlight ( ) ,
162
+ transformerColorizedBrackets ( ) ,
163
+ transformerShowEmptyLines ( ) ,
164
+ transformerMetaShowTitle ( ) ,
165
+ ] ,
156
166
} ,
157
167
] ,
158
168
] ,
0 commit comments