@@ -3,7 +3,6 @@ import type { BlockContent, Parent, Root } from 'mdast';
3
3
import type { Plugin , Transformer } from 'unified' ;
4
4
import { visit } from 'unist-util-visit' ;
5
5
import type { BuildVisitor } from 'unist-util-visit/complex-types' ;
6
- import { isMDXFile } from './utils/isMDX' ;
7
6
import { makeComponentNode } from './utils/makeComponentNode' ;
8
7
9
8
const CodeSnippetTagname = 'AutoImportedCodeSnippet' ;
@@ -59,88 +58,78 @@ declare module 'mdast' {
59
58
}
60
59
61
60
export function remarkCodeSnippets ( ) : Plugin < [ ] , Root > {
62
- const makeVisitor =
63
- ( format : 'md' | 'mdx' ) : BuildVisitor < Root , 'code' > =>
64
- ( code , index , parent ) => {
65
- if ( index === null || parent === null ) return ;
66
- const isMDX = format === 'mdx' ;
67
-
68
- // Parse optional meta information after the opening code fence,
69
- // trying to get a meta title and an array of highlighted lines
70
- const { title : metaTitle , lineMarkings, inlineMarkings } = parseMeta ( code . meta || '' ) ;
71
- let title = metaTitle ;
72
-
73
- // Preprocess the code
74
- const { preprocessedCode, extractedFileName, removedLineIndex, removedLineCount } =
75
- preprocessCode (
76
- code . value ,
77
- code . lang || '' ,
78
- // Only try to extract a file name from the code if no meta title was found above
79
- title === undefined
80
- ) ;
81
- code . value = preprocessedCode ;
82
- if ( extractedFileName ) {
83
- title = extractedFileName ;
84
- }
85
-
86
- // If there was no title in the meta information or in the code, check if the previous
87
- // Markdown paragraph contains a file name that we can use as a title
88
- if ( title === undefined && index > 0 ) {
89
- // Check the previous node to see if it matches our requirements
90
- const prev = parent . children [ index - 1 ] ;
91
- const strongContent =
92
- // The previous node must be a paragraph...
93
- prev . type === 'paragraph' &&
94
- // ...it must contain exactly one child with strong formatting...
95
- prev . children . length === 1 &&
96
- prev . children [ 0 ] . type === 'strong' &&
97
- // ...this child must also contain exactly one child
98
- prev . children [ 0 ] . children . length === 1 &&
99
- // ...which is the result of this expression
100
- prev . children [ 0 ] . children [ 0 ] ;
101
-
102
- // Require the strong content to be either raw text or inline code and retrieve its value
103
- const prevParaStrongTextValue =
104
- strongContent && strongContent . type === 'text' && strongContent . value ;
105
- const prevParaStrongCodeValue =
106
- strongContent && strongContent . type === 'inlineCode' && strongContent . value ;
107
- const potentialFileName = prevParaStrongTextValue || prevParaStrongCodeValue ;
61
+ const visitor : BuildVisitor < Root , 'code' > = ( code , index , parent ) => {
62
+ if ( index === null || parent === null ) return ;
63
+
64
+ // Parse optional meta information after the opening code fence,
65
+ // trying to get a meta title and an array of highlighted lines
66
+ const { title : metaTitle , lineMarkings, inlineMarkings } = parseMeta ( code . meta || '' ) ;
67
+ let title = metaTitle ;
68
+
69
+ // Preprocess the code
70
+ const { preprocessedCode, extractedFileName, removedLineIndex, removedLineCount } =
71
+ preprocessCode (
72
+ code . value ,
73
+ code . lang || '' ,
74
+ // Only try to extract a file name from the code if no meta title was found above
75
+ title === undefined
76
+ ) ;
77
+ code . value = preprocessedCode ;
78
+ if ( extractedFileName ) {
79
+ title = extractedFileName ;
80
+ }
108
81
109
- // Check if it's a file name
110
- const matches = potentialFileName && FileNameCommentRegExp . exec ( `// ${ potentialFileName } ` ) ;
111
- if ( matches ) {
112
- // Yes, store the file name and replace the paragraph with an empty node
113
- title = matches [ 2 ] ;
114
- parent . children [ index - 1 ] = {
115
- type : 'html' ,
116
- value : '' ,
117
- } ;
118
- }
82
+ // If there was no title in the meta information or in the code, check if the previous
83
+ // Markdown paragraph contains a file name that we can use as a title
84
+ if ( title === undefined && index > 0 ) {
85
+ // Check the previous node to see if it matches our requirements
86
+ const prev = parent . children [ index - 1 ] ;
87
+ const strongContent =
88
+ // The previous node must be a paragraph...
89
+ prev . type === 'paragraph' &&
90
+ // ...it must contain exactly one child with strong formatting...
91
+ prev . children . length === 1 &&
92
+ prev . children [ 0 ] . type === 'strong' &&
93
+ // ...this child must also contain exactly one child
94
+ prev . children [ 0 ] . children . length === 1 &&
95
+ // ...which is the result of this expression
96
+ prev . children [ 0 ] . children [ 0 ] ;
97
+
98
+ // Require the strong content to be either raw text or inline code and retrieve its value
99
+ const prevParaStrongTextValue =
100
+ strongContent && strongContent . type === 'text' && strongContent . value ;
101
+ const prevParaStrongCodeValue =
102
+ strongContent && strongContent . type === 'inlineCode' && strongContent . value ;
103
+ const potentialFileName = prevParaStrongTextValue || prevParaStrongCodeValue ;
104
+
105
+ // Check if it's a file name
106
+ const matches = potentialFileName && FileNameCommentRegExp . exec ( `// ${ potentialFileName } ` ) ;
107
+ if ( matches ) {
108
+ // Yes, store the file name and replace the paragraph with an empty node
109
+ title = matches [ 2 ] ;
110
+ parent . children [ index - 1 ] = {
111
+ type : 'html' ,
112
+ value : '' ,
113
+ } ;
119
114
}
115
+ }
120
116
121
- const attributes = {
122
- lang : code . lang ,
123
- title : encodeMarkdownStringProp ( title ) ,
124
- removedLineIndex,
125
- removedLineCount,
126
- lineMarkings : encodeMarkdownStringArrayProp ( lineMarkings ) ,
127
- inlineMarkings : encodeMarkdownStringArrayProp ( inlineMarkings ) ,
128
- } ;
129
-
130
- const codeSnippetWrapper = makeComponentNode (
131
- CodeSnippetTagname ,
132
- { mdx : isMDX , attributes } ,
133
- code
134
- ) ;
135
-
136
- parent . children . splice ( index , 1 , codeSnippetWrapper ) ;
117
+ const attributes = {
118
+ lang : code . lang ,
119
+ title : encodeMarkdownStringProp ( title ) ,
120
+ removedLineIndex,
121
+ removedLineCount,
122
+ lineMarkings : encodeMarkdownStringArrayProp ( lineMarkings ) ,
123
+ inlineMarkings : encodeMarkdownStringArrayProp ( inlineMarkings ) ,
137
124
} ;
138
125
139
- const mdVisitor = makeVisitor ( 'md' ) ;
140
- const mdxVisitor = makeVisitor ( 'mdx' ) ;
126
+ const codeSnippetWrapper = makeComponentNode ( CodeSnippetTagname , { attributes } , code ) ;
127
+
128
+ parent . children . splice ( index , 1 , codeSnippetWrapper ) ;
129
+ } ;
141
130
142
- const transformer : Transformer < Root > = ( tree , file ) => {
143
- visit ( tree , 'code' , isMDXFile ( file ) ? mdxVisitor : mdVisitor ) ;
131
+ const transformer : Transformer < Root > = ( tree ) => {
132
+ visit ( tree , 'code' , visitor ) ;
144
133
} ;
145
134
146
135
return function attacher ( ) {
0 commit comments