@@ -10,6 +10,65 @@ const createTranslateNode = (text) => ({
1010 value : `<Translate text="${ escapeQuotes ( text ) } " />` ,
1111} )
1212
13+ // Helper function to handle text with code blocks
14+ const handleTextWithCode = ( text ) => {
15+ // If the text is already wrapped in HTML tags, return as is
16+ if ( text . startsWith ( "<" ) && text . endsWith ( ">" ) ) {
17+ return { type : "html" , value : text }
18+ }
19+
20+ // Split text into parts: text and code blocks
21+ const parts = [ ]
22+ let currentText = ""
23+ let inCode = false
24+ let codeContent = ""
25+
26+ for ( let i = 0 ; i < text . length ; i ++ ) {
27+ const char = text [ i ]
28+
29+ if ( char === "`" ) {
30+ if ( inCode ) {
31+ // End of code block
32+ if ( currentText . trim ( ) ) {
33+ parts . push ( createTranslateNode ( currentText . trim ( ) ) )
34+ currentText = ""
35+ }
36+ parts . push ( { type : "html" , value : `<code>${ codeContent } </code>` } )
37+ codeContent = ""
38+ inCode = false
39+ } else {
40+ // Start of code block
41+ if ( currentText . trim ( ) ) {
42+ parts . push ( createTranslateNode ( currentText . trim ( ) ) )
43+ currentText = ""
44+ }
45+ inCode = true
46+ }
47+ continue
48+ }
49+
50+ if ( inCode ) {
51+ codeContent += char
52+ } else {
53+ currentText += char
54+ }
55+ }
56+
57+ // Handle any remaining text
58+ if ( currentText . trim ( ) ) {
59+ parts . push ( createTranslateNode ( currentText . trim ( ) ) )
60+ }
61+
62+ // If we only have one part and it's a translate node, return it directly
63+ if ( parts . length === 1 && parts [ 0 ] . type === "html" && parts [ 0 ] . value . startsWith ( "<Translate" ) ) {
64+ return parts [ 0 ]
65+ }
66+
67+ // Otherwise, combine all parts
68+ const combinedHtml = parts . map ( p => p . value ) . join ( " " )
69+ return { type : "html" , value : combinedHtml }
70+ }
71+
1372export function remarkTranslate ( ) {
1473 return ( tree , file ) => {
1574 // Skip processing if file is CHANGELOG.md
@@ -38,7 +97,7 @@ export function remarkTranslate() {
3897 // but preserves the original text for ID generation
3998 const wrapperNode = {
4099 type : "html" ,
41- value : `<span data-heading-text="${ escapeQuotes ( headingText ) } ">${ createTranslateNode ( headingText ) . value } </span>` ,
100+ value : `<span data-heading-text="${ escapeQuotes ( headingText ) } ">${ handleTextWithCode ( headingText ) . value } </span>` ,
42101 }
43102
44103 // Replace text nodes with our wrapper
@@ -66,13 +125,12 @@ export function remarkTranslate() {
66125 // For each element in the cell
67126 cell . children . forEach ( ( child , index ) => {
68127 if ( child . type === "text" && child . value . trim ( ) ) {
69- // Replace text with Translate component
70- cell . children [ index ] = createTranslateNode ( child . value . trim ( ) )
128+ cell . children [ index ] = handleTextWithCode ( child . value . trim ( ) )
71129 } else if ( child . type === "paragraph" && child . children ) {
72130 // For paragraphs inside cells
73131 child . children . forEach ( ( grandChild , grandIndex ) => {
74132 if ( grandChild . type === "text" && grandChild . value . trim ( ) ) {
75- child . children [ grandIndex ] = createTranslateNode ( grandChild . value . trim ( ) )
133+ child . children [ grandIndex ] = handleTextWithCode ( grandChild . value . trim ( ) )
76134 }
77135 } )
78136 } else if ( child . type === "inlineCode" ) {
@@ -94,29 +152,38 @@ export function remarkTranslate() {
94152 // Process paragraphs
95153 visit ( tree , "paragraph" , ( node ) => {
96154 if ( node . children && node . children . length ) {
97- node . children . forEach ( ( child , index ) => {
155+ let i = 0
156+ while ( i < node . children . length ) {
157+ const child = node . children [ i ]
98158 if ( child . type === "text" ) {
99159 // Split text by line breaks to preserve formatting
100160 const lines = child . value . split ( / ( \n + ) / )
101161 if ( lines . length > 1 ) {
102162 // If there are line breaks, create multiple translate nodes
103163 const newNodes = [ ]
104- lines . forEach ( ( line , i ) => {
105- if ( i % 2 === 0 && line . trim ( ) ) {
164+ lines . forEach ( ( line , lineIndex ) => {
165+ if ( lineIndex % 2 === 0 && line . trim ( ) ) {
106166 // Text content
107- newNodes . push ( createTranslateNode ( line ) )
167+ newNodes . push ( handleTextWithCode ( line ) )
108168 } else {
109169 // Line breaks
110170 newNodes . push ( { type : "text" , value : line } )
111171 }
112172 } )
113- node . children . splice ( index , 1 , ...newNodes )
173+ node . children . splice ( i , 1 , ...newNodes )
174+ i += newNodes . length
114175 } else if ( child . value . trim ( ) ) {
115176 // Simple text without line breaks
116- node . children [ index ] = createTranslateNode ( child . value )
177+ const nodes = [ handleTextWithCode ( child . value ) ]
178+ node . children . splice ( i , 1 , ...nodes )
179+ i += nodes . length
180+ } else {
181+ i ++
117182 }
183+ } else {
184+ i ++
118185 }
119- } )
186+ }
120187 }
121188 } )
122189
@@ -125,11 +192,17 @@ export function remarkTranslate() {
125192 if ( node . children && node . children . length ) {
126193 node . children . forEach ( ( child ) => {
127194 if ( child . type === "paragraph" && child . children ) {
128- child . children . forEach ( ( grandChild , index ) => {
195+ let i = 0
196+ while ( i < child . children . length ) {
197+ const grandChild = child . children [ i ]
129198 if ( grandChild . type === "text" && grandChild . value . trim ( ) ) {
130- child . children [ index ] = createTranslateNode ( grandChild . value )
199+ const nodes = [ handleTextWithCode ( grandChild . value ) ]
200+ child . children . splice ( i , 1 , ...nodes )
201+ i += nodes . length
202+ } else {
203+ i ++
131204 }
132- } )
205+ }
133206 }
134207 } )
135208 }
@@ -138,11 +211,17 @@ export function remarkTranslate() {
138211 // Process emphasis and strong
139212 visit ( tree , [ "emphasis" , "strong" ] , ( node ) => {
140213 if ( node . children && node . children . length ) {
141- node . children . forEach ( ( child , index ) => {
214+ let i = 0
215+ while ( i < node . children . length ) {
216+ const child = node . children [ i ]
142217 if ( child . type === "text" && child . value . trim ( ) ) {
143- node . children [ index ] = createTranslateNode ( child . value )
218+ const nodes = [ handleTextWithCode ( child . value ) ]
219+ node . children . splice ( i , 1 , ...nodes )
220+ i += nodes . length
221+ } else {
222+ i ++
144223 }
145- } )
224+ }
146225 }
147226 } )
148227
@@ -151,11 +230,17 @@ export function remarkTranslate() {
151230 if ( node . children && node . children . length ) {
152231 node . children . forEach ( ( child ) => {
153232 if ( child . type === "paragraph" && child . children ) {
154- child . children . forEach ( ( grandChild , index ) => {
233+ let i = 0
234+ while ( i < child . children . length ) {
235+ const grandChild = child . children [ i ]
155236 if ( grandChild . type === "text" && grandChild . value . trim ( ) ) {
156- child . children [ index ] = createTranslateNode ( grandChild . value )
237+ const nodes = [ handleTextWithCode ( grandChild . value ) ]
238+ child . children . splice ( i , 1 , ...nodes )
239+ i += nodes . length
240+ } else {
241+ i ++
157242 }
158- } )
243+ }
159244 }
160245 } )
161246 }
0 commit comments