@@ -57,9 +57,7 @@ class Parser {
57
57
case "heading" : {
58
58
const styles = this . headingStylesMap [ token . depth ] ;
59
59
60
- // To avoid duplicate text node nesting when there are no child tokens with text emphasis (i.e., italic)
61
- // ref: https://github.com/gmsgowtham/react-native-marked/issues/522
62
- if ( token . tokens . length === 1 && token . tokens [ 0 ] ?. type === "text" ) {
60
+ if ( this . hasDuplicateTextChildToken ( token ) ) {
63
61
return this . renderer . heading ( token . text , styles , token . depth ) ;
64
62
}
65
63
@@ -133,6 +131,11 @@ class Parser {
133
131
fontStyle : this . styles . link ?. fontStyle ,
134
132
} ;
135
133
const href = getValidURL ( this . baseUrl , token . href ) ;
134
+
135
+ if ( this . hasDuplicateTextChildToken ( token ) ) {
136
+ return this . renderer . link ( token . text , href , linkStyle ) ;
137
+ }
138
+
136
139
const children = this . _parse ( token . tokens , linkStyle ) ;
137
140
return this . renderer . link ( children , href , linkStyle ) ;
138
141
}
@@ -148,6 +151,10 @@ class Parser {
148
151
...this . styles . strong ,
149
152
...styles ,
150
153
} ;
154
+ if ( this . hasDuplicateTextChildToken ( token ) ) {
155
+ return this . renderer . strong ( token . text , boldStyle ) ;
156
+ }
157
+
151
158
const children = this . _parse ( token . tokens , boldStyle ) ;
152
159
return this . renderer . strong ( children , boldStyle ) ;
153
160
}
@@ -156,6 +163,10 @@ class Parser {
156
163
...this . styles . em ,
157
164
...styles ,
158
165
} ;
166
+ if ( this . hasDuplicateTextChildToken ( token ) ) {
167
+ return this . renderer . em ( token . text , italicStyle ) ;
168
+ }
169
+
159
170
const children = this . _parse ( token . tokens , italicStyle ) ;
160
171
return this . renderer . em ( children , italicStyle ) ;
161
172
}
@@ -173,6 +184,10 @@ class Parser {
173
184
...this . styles . strikethrough ,
174
185
...styles ,
175
186
} ;
187
+ if ( this . hasDuplicateTextChildToken ( token ) ) {
188
+ return this . renderer . del ( token . text , strikethroughStyle ) ;
189
+ }
190
+
176
191
const children = this . _parse ( token . tokens , strikethroughStyle ) ;
177
192
return this . renderer . del ( children , strikethroughStyle ) ;
178
193
}
@@ -282,6 +297,24 @@ class Parser {
282
297
283
298
return siblingNodes ;
284
299
}
300
+
301
+ // To avoid duplicate text node nesting when there are no child tokens with text emphasis (i.e., italic)
302
+ // ref: https://github.com/gmsgowtham/react-native-marked/issues/522
303
+ private hasDuplicateTextChildToken ( token : Token ) : boolean {
304
+ if ( ! ( "tokens" in token ) ) {
305
+ return false ;
306
+ }
307
+
308
+ if (
309
+ token . tokens &&
310
+ token . tokens . length === 1 &&
311
+ token . tokens [ 0 ] ?. type === "text"
312
+ ) {
313
+ return true ;
314
+ }
315
+
316
+ return false ;
317
+ }
285
318
}
286
319
287
320
export default Parser ;
0 commit comments