Skip to content

Commit 88dd613

Browse files
committed
fix(minor): avoid rendering duplicate nested text nodes
ref: #522
1 parent dd0c41e commit 88dd613

File tree

4 files changed

+82
-565
lines changed

4 files changed

+82
-565
lines changed

src/lib/Parser.tsx

+36-3
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,7 @@ class Parser {
5757
case "heading": {
5858
const styles = this.headingStylesMap[token.depth];
5959

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)) {
6361
return this.renderer.heading(token.text, styles, token.depth);
6462
}
6563

@@ -133,6 +131,11 @@ class Parser {
133131
fontStyle: this.styles.link?.fontStyle,
134132
};
135133
const href = getValidURL(this.baseUrl, token.href);
134+
135+
if (this.hasDuplicateTextChildToken(token)) {
136+
return this.renderer.link(token.text, href, linkStyle);
137+
}
138+
136139
const children = this._parse(token.tokens, linkStyle);
137140
return this.renderer.link(children, href, linkStyle);
138141
}
@@ -148,6 +151,10 @@ class Parser {
148151
...this.styles.strong,
149152
...styles,
150153
};
154+
if (this.hasDuplicateTextChildToken(token)) {
155+
return this.renderer.strong(token.text, boldStyle);
156+
}
157+
151158
const children = this._parse(token.tokens, boldStyle);
152159
return this.renderer.strong(children, boldStyle);
153160
}
@@ -156,6 +163,10 @@ class Parser {
156163
...this.styles.em,
157164
...styles,
158165
};
166+
if (this.hasDuplicateTextChildToken(token)) {
167+
return this.renderer.em(token.text, italicStyle);
168+
}
169+
159170
const children = this._parse(token.tokens, italicStyle);
160171
return this.renderer.em(children, italicStyle);
161172
}
@@ -173,6 +184,10 @@ class Parser {
173184
...this.styles.strikethrough,
174185
...styles,
175186
};
187+
if (this.hasDuplicateTextChildToken(token)) {
188+
return this.renderer.del(token.text, strikethroughStyle);
189+
}
190+
176191
const children = this._parse(token.tokens, strikethroughStyle);
177192
return this.renderer.del(children, strikethroughStyle);
178193
}
@@ -282,6 +297,24 @@ class Parser {
282297

283298
return siblingNodes;
284299
}
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+
}
285318
}
286319

287320
export default Parser;

src/lib/Renderer.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,11 @@ class Renderer implements RendererInterface {
123123
return <MDImage key={key} uri={uri} alt={alt} style={style} />;
124124
}
125125

126-
strong(children: ReactNode[], styles?: TextStyle): ReactNode {
126+
strong(children: string | ReactNode[], styles?: TextStyle): ReactNode {
127127
return this.getTextNode(children, styles);
128128
}
129129

130-
em(children: ReactNode[], styles?: TextStyle): ReactNode {
130+
em(children: string | ReactNode[], styles?: TextStyle): ReactNode {
131131
return this.getTextNode(children, styles);
132132
}
133133

@@ -139,7 +139,7 @@ class Renderer implements RendererInterface {
139139
return this.getTextNode("\n", {});
140140
}
141141

142-
del(children: ReactNode[], styles?: TextStyle): ReactNode {
142+
del(children: string | ReactNode[], styles?: TextStyle): ReactNode {
143143
return this.getTextNode(children, styles);
144144
}
145145

0 commit comments

Comments
 (0)