Skip to content

Commit f20ec2e

Browse files
authored
Merge pull request #524 from gmsgowtham/fix/nested-heading
fix(minor): avoid wrapping heading inside two text nodes
2 parents fd4115a + 911663c commit f20ec2e

File tree

4 files changed

+203
-299
lines changed

4 files changed

+203
-299
lines changed

examples/react-native-marked-sample/src/const.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const MD_STRING = `
22
# h1 Heading
33
## h2 Heading
4+
## ~~_h2 Heading with Emphasis_~~
45
### h3 Heading
56
#### h4 Heading
67
##### h5 Heading

src/lib/Parser.tsx

+7
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ class Parser {
5656
}
5757
case "heading": {
5858
const styles = this.headingStylesMap[token.depth];
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") {
63+
return this.renderer.heading(token.text, styles, token.depth);
64+
}
65+
5966
const children = this._parse(token.tokens, styles);
6067
return this.renderer.heading(children, styles, token.depth);
6168
}

src/lib/__tests__/Markdown.spec.tsx

+6
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ describe("Headings", () => {
7272
const tree = r.toJSON();
7373
expect(tree).toMatchSnapshot();
7474
});
75+
it("Heading with text emphasis", () => {
76+
const r = render(<Markdown value={"## ~~_Heading level 2_~~"} />);
77+
expect(screen.queryByText("Heading level 2")).toBeTruthy();
78+
const tree = r.toJSON();
79+
expect(tree).toMatchSnapshot();
80+
});
7581
});
7682

7783
// https://www.markdownguide.org/basic-syntax/#paragraphs-1

0 commit comments

Comments
 (0)