Skip to content

Commit

Permalink
fix(minor): don't render empty links
Browse files Browse the repository at this point in the history
This ensures that the text styling is not messed up.
  • Loading branch information
gmsgowtham committed May 18, 2023
1 parent 93094a3 commit 7f763c4
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/lib/Parser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ class Parser {
});
}
case "link": {
// Don't render anchors without text and children
if (token.text.trim.length < 1 && token.tokens.length < 1) {
return null;
}

// Note: Linking Images (https://www.markdownguide.org/basic-syntax/#linking-images) are wrapped
// in paragraph token, so will be handled via `getNormalizedSiblingNodesForBlockAndInlineTokens`
const linkStyle = {
Expand Down
12 changes: 12 additions & 0 deletions src/lib/__tests__/Markdown.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,18 @@ describe("Links", () => {
const tree = r.toJSON();
expect(tree).toMatchSnapshot();
});
it("Links without text, (no render)", () => {
const r = render(
<Markdown
value={
"Table of Contents[](https://mastersoftwaretesting.com/testing-fundamentals/software-testing-101-what-is-software-testing#table-of-contents)\n-------------------------------------------------------------------------------------------------------------------------------------------\n"
}
/>,
);
expect(screen.queryByText("Table of Contents")).toBeTruthy();
const tree = r.toJSON();
expect(tree).toMatchSnapshot();
});
});

// https://www.markdownguide.org/basic-syntax/#images-1
Expand Down
106 changes: 106 additions & 0 deletions src/lib/__tests__/__snapshots__/Markdown.spec.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5038,6 +5038,112 @@ See the section on
</RCTScrollView>
`;

exports[`Links Links without text, (no render) 1`] = `
<RCTScrollView
data={
[
<Text
selectable={true}
style={
{
"borderBottomColor": "#d0d7de",
"borderBottomWidth": 1,
"color": "#333333",
"fontSize": 28,
"fontWeight": "500",
"lineHeight": 36,
"marginVertical": 8,
"paddingBottom": 4,
}
}
>
<Text
selectable={true}
style={
{
"borderBottomColor": "#d0d7de",
"borderBottomWidth": 1,
"color": "#333333",
"fontSize": 28,
"fontWeight": "500",
"lineHeight": 36,
"marginVertical": 8,
"paddingBottom": 4,
}
}
>
Table of Contents
</Text>
</Text>,
]
}
getItem={[Function]}
getItemCount={[Function]}
initialNumToRender={8}
keyExtractor={[Function]}
maxToRenderPerBatch={8}
onContentSizeChange={[Function]}
onLayout={[Function]}
onMomentumScrollBegin={[Function]}
onMomentumScrollEnd={[Function]}
onScroll={[Function]}
onScrollBeginDrag={[Function]}
onScrollEndDrag={[Function]}
removeClippedSubviews={false}
renderItem={[Function]}
scrollEventThrottle={50}
stickyHeaderIndices={[]}
style={
{
"backgroundColor": "#ffffff",
}
}
viewabilityConfigCallbackPairs={[]}
>
<View>
<View
onFocusCapture={[Function]}
onLayout={[Function]}
style={null}
>
<Text
selectable={true}
style={
{
"borderBottomColor": "#d0d7de",
"borderBottomWidth": 1,
"color": "#333333",
"fontSize": 28,
"fontWeight": "500",
"lineHeight": 36,
"marginVertical": 8,
"paddingBottom": 4,
}
}
>
<Text
selectable={true}
style={
{
"borderBottomColor": "#d0d7de",
"borderBottomWidth": 1,
"color": "#333333",
"fontSize": 28,
"fontWeight": "500",
"lineHeight": 36,
"marginVertical": 8,
"paddingBottom": 4,
}
}
>
Table of Contents
</Text>
</Text>
</View>
</View>
</RCTScrollView>
`;

exports[`Links Titles 1`] = `
<RCTScrollView
data={
Expand Down

0 comments on commit 7f763c4

Please sign in to comment.