@@ -11,6 +11,7 @@ const parseCodeFenceInfo = require('./parseCodeFenceInfo');
1111const parseCodeSpanInfo = require ( './parseCodeSpanInfo' ) ;
1212const createSchemaCustomization = require ( './graphql/createSchemaCustomization' ) ;
1313const getCodeBlockGraphQLDataFromRegistry = require ( './graphql/getCodeBlockDataFromRegistry' ) ;
14+ const getCodeSpanGraphQLDataFromRegistry = require ( './graphql/getCodeSpanDataFromRegistry' ) ;
1415const { registerCodeBlock, registerCodeSpan } = require ( './registerCodeNode' ) ;
1516const { createHash } = require ( 'crypto' ) ;
1617const { setChildNodes } = require ( './cacheUtils' ) ;
@@ -63,25 +64,30 @@ function createPlugin() {
6364
6465 /** @type {(MDASTNode<'code'> | MDASTNode<'inlineCode'>)[] } */
6566 const nodes = [ ] ;
66- visit ( markdownAST , ( { type } ) => type === 'code' || type === 'inlineCode' , node => {
67- nodes . push ( node ) ;
68- } ) ;
67+ visit (
68+ markdownAST ,
69+ ( { type } ) => type === 'code' || type === 'inlineCode' ,
70+ node => {
71+ nodes . push ( node ) ;
72+ }
73+ ) ;
6974
7075 // 2. For each code fence found, parse its header, determine what themes it will use,
7176 // and register its contents with a central code block registry, performing tokenization
7277 // along the way.
7378
74- /** @type {grvsc.gql.GRVSCCodeBlock[] } */
79+ /** @type {( grvsc.gql.GRVSCCodeBlock | grvsc.gql.GRVSCCodeSpan) [] } */
7580 const graphQLNodes = [ ] ;
76- /** @type {CodeNodeRegistry<MDASTNode> } */
81+ /** @type {CodeNodeRegistry<MDASTNode<'code' | 'inlineCode'> > } */
7782 const codeNodeRegistry = createCodeNodeRegistry ( ) ;
7883 for ( const node of nodes ) {
7984 /** @type {string } */
8085 const text = node . value || ( node . children && node . children [ 0 ] && node . children [ 0 ] . value ) ;
8186 if ( ! text ) continue ;
82- const { languageName, meta, text : parsedText = text } = node . type === 'code'
83- ? parseCodeFenceInfo ( node . lang ? node . lang . toLowerCase ( ) : '' , node . meta )
84- : parseCodeSpanInfo ( text , inlineCode . marker ) ;
87+ const { languageName, meta, text : parsedText = text } =
88+ node . type === 'code'
89+ ? parseCodeFenceInfo ( node . lang ? node . lang . toLowerCase ( ) : '' , node . meta )
90+ : parseCodeSpanInfo ( text , inlineCode . marker ) ;
8591
8692 if ( node . type === 'inlineCode' && ! languageName ) {
8793 continue ;
@@ -138,7 +144,7 @@ function createPlugin() {
138144 }
139145 }
140146
141- // 3. For each code block registered, convert its tokenization and theme data
147+ // 3. For each code block/span registered, convert its tokenization and theme data
142148 // to a GraphQL-compatible representation, including HTML renderings. At the same
143149 // time, change the original code fence Markdown node to an HTML node and set
144150 // its value to the HTML rendering contained in the GraphQL node.
@@ -153,7 +159,8 @@ function createPlugin() {
153159 ) ;
154160
155161 // Update Markdown node
156- node . type = 'html' ;
162+ /** @type {MDASTNode } */
163+ ( node ) . type = 'html' ;
157164 node . value = graphQLNode . html ;
158165
159166 // Push GraphQL node
@@ -182,6 +189,38 @@ function createPlugin() {
182189 }
183190 } ) ;
184191
192+ codeNodeRegistry . forEachCodeSpan ( ( codeSpan , node ) => {
193+ const graphQLNode = getCodeSpanGraphQLDataFromRegistry ( codeNodeRegistry , node , codeSpan , getClassName ) ;
194+
195+ // Update Markdown node
196+ /** @type {MDASTNode } */
197+ ( node ) . type = 'html' ;
198+ node . value = graphQLNode . html ;
199+
200+ // Push GraphQL node
201+ graphQLNodes . push ( {
202+ ...graphQLNode ,
203+ id : createNodeId ( `GRVSCCodeSpan-${ markdownNode . id } -${ codeSpan . index } ` ) ,
204+ parent : markdownNode . id ,
205+ internal : {
206+ type : 'GRVSCCodeSpan' ,
207+ contentDigest : createHash ( 'md5' )
208+ . update ( JSON . stringify ( graphQLNode ) )
209+ . digest ( 'hex' )
210+ }
211+ } ) ;
212+
213+ function getClassName ( ) {
214+ return typeof inlineCode . className === 'function'
215+ ? inlineCode . className ( {
216+ language : codeSpan . languageName ,
217+ markdownNode,
218+ node
219+ } )
220+ : inlineCode . className ;
221+ }
222+ } ) ;
223+
185224 // 4. Generate CSS rules for each theme used by one or more code blocks in the registry,
186225 // then append that CSS to the Markdown AST in an HTML node.
187226
0 commit comments