@@ -35,34 +35,64 @@ function extractCodePlaceholders(content: string): {
3535 content : string ;
3636 codePlaceholders : Record < string , string > ;
3737} {
38- const ast = parseMdast ( content ) ;
39- const placeholderableElements : ( keyof RootContentMap ) [ ] = [
40- "code" ,
41- "inlineCode" ,
42- ] ;
38+ let shouldSearchForPlaceholders = true ;
4339 let finalContent = content ;
4440 const codePlaceholders : Record < string , string > = { } ;
4541
46- traverseMdast ( ast , ( _node ) => {
47- if ( ! placeholderableElements . includes ( _node . type as any ) ) {
48- return ;
49- }
50- const node = _node as Code | InlineCode ;
51- const nodeContent = node . value ;
42+ while ( shouldSearchForPlaceholders ) {
43+ const ast = parseMdast ( finalContent ) ;
5244
53- const nodeContentHash = md5 ( nodeContent ) ;
54- const placeholderId = `__PLACEHOLDER_ ${ nodeContentHash } __` ;
45+ traverseMdast ( ast , ( _node ) => {
46+ shouldSearchForPlaceholders = false ;
5547
56- const nodeContentStart = node . position ?. start . offset ;
57- const nodeContentEnd = node . position ?. end . offset ;
48+ if ( _node . type === "code" ) {
49+ const node = _node as Code ;
5850
59- if ( ! nodeContentStart || ! nodeContentEnd ) {
60- return ;
61- }
51+ const nodeContentStartLine = node . position ?. start . line ;
52+ const nodeContentEndLine = node . position ?. end . line ;
6253
63- codePlaceholders [ placeholderId ] = nodeContent ;
64- finalContent = finalContent . split ( nodeContent ) . join ( placeholderId ) ;
65- } ) ;
54+ if ( ! nodeContentStartLine || ! nodeContentEndLine ) {
55+ return ;
56+ }
57+
58+ const nodeContentPreStartLine = nodeContentStartLine - 1 ;
59+ const nodeContentPostEndLine = nodeContentEndLine + 1 ;
60+
61+ const nodeContent = finalContent
62+ . split ( "\n" )
63+ . slice ( nodeContentPreStartLine , nodeContentPostEndLine )
64+ . join ( "\n" ) ;
65+
66+ const nodeContentHash = md5 ( nodeContent ) ;
67+ const placeholderId = `__PLACEHOLDER_${ nodeContentHash } __` ;
68+
69+ codePlaceholders [ placeholderId ] = nodeContent ;
70+ finalContent = finalContent . replace ( nodeContent , placeholderId ) ;
71+ shouldSearchForPlaceholders = true ;
72+ } else if ( _node . type === "inlineCode" ) {
73+ const node = _node as InlineCode ;
74+
75+ const nodeContentStartIndex = node . position ?. start . offset ;
76+ const nodeContentEndIndex = node . position ?. end . offset ;
77+
78+ if ( ! nodeContentStartIndex || ! nodeContentEndIndex ) {
79+ return ;
80+ }
81+
82+ const nodeContent = finalContent . slice (
83+ nodeContentStartIndex ,
84+ nodeContentEndIndex ,
85+ ) ;
86+
87+ const nodeContentHash = md5 ( nodeContent ) ;
88+ const placeholderId = `__PLACEHOLDER_${ nodeContentHash } __` ;
89+
90+ codePlaceholders [ placeholderId ] = nodeContent ;
91+ finalContent = finalContent . replace ( nodeContent , placeholderId ) ;
92+ shouldSearchForPlaceholders = true ;
93+ }
94+ } ) ;
95+ }
6696
6797 return {
6898 content : finalContent ,
0 commit comments