Skip to content

Commit 69c2d06

Browse files
authored
fix: footnote definition and list rendering (#32)
1 parent 53e3d9f commit 69c2d06

File tree

6 files changed

+57
-37
lines changed

6 files changed

+57
-37
lines changed

markdown/01_markdown_sample.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# React Native Remark
22

3-
# h1 Heading 8-)
3+
# h1 Heading
44
## h2 Heading
55
### h3 Heading
66
#### h4 Heading
@@ -78,6 +78,13 @@ Start numbering with offset:
7878
57. foo
7979
1. bar
8080

81+
List with heading
82+
83+
- # Title
84+
- ## Sub title
85+
- ### Test
86+
- body
87+
8188

8289
## Code
8390

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { BlockContent, DefinitionContent, FootnoteDefinition } from "mdast";
22
import { Fragment, ReactNode } from "react";
3-
import { Text } from "react-native";
3+
import { Text, View } from "react-native";
44

55
import { useMarkdownContext } from "../context";
6+
import { mergeStyles } from "../themes/themes";
67
import { RendererArgs } from "./renderers";
78

89
export const FootnoteDefinitionRenderer = ({
@@ -11,23 +12,32 @@ export const FootnoteDefinitionRenderer = ({
1112
const { renderers, styles } = useMarkdownContext();
1213
const { BlockContentRenderer, DefinitionContentRenderer } = renderers;
1314

15+
const mergedStyles = mergeStyles(styles.paragraph, {
16+
fontWeight: "500",
17+
textDecorationLine: "underline",
18+
});
19+
1420
return (
15-
<Text style={styles.footnoteDefinition}>
16-
<Text>[{node.identifier}]: </Text>
17-
{node.children.map((child, idx) => (
18-
<Fragment key={idx}>
19-
<BlockContentRenderer
20-
node={child as BlockContent}
21-
index={idx}
22-
parent={node}
23-
/>
24-
<DefinitionContentRenderer
25-
node={child as DefinitionContent}
26-
index={idx}
27-
parent={node}
28-
/>
29-
</Fragment>
30-
))}
31-
</Text>
21+
<View style={{ flexDirection: "row" }}>
22+
<View>
23+
<Text style={mergedStyles}>[{node.identifier}]: </Text>
24+
</View>
25+
<View style={{ flex: 1 }}>
26+
{node.children.map((child, idx) => (
27+
<Fragment key={idx}>
28+
<BlockContentRenderer
29+
node={child as BlockContent}
30+
index={idx}
31+
parent={node}
32+
/>
33+
<DefinitionContentRenderer
34+
node={child as DefinitionContent}
35+
index={idx}
36+
parent={node}
37+
/>
38+
</Fragment>
39+
))}
40+
</View>
41+
</View>
3242
);
3343
};

src/renderers/list.tsx

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { BlockContent, DefinitionContent, List, ListItem } from "mdast";
2-
import { Fragment, ReactNode } from "react";
2+
import { Fragment, ReactNode, useMemo } from "react";
33
import { Text, View } from "react-native";
44

55
import { useMarkdownContext } from "../context";
@@ -29,20 +29,30 @@ export const ListItemRenderer = ({
2929
const { renderers, styles } = useMarkdownContext();
3030
const { BlockContentRenderer, DefinitionContentRenderer } = renderers;
3131

32-
const markerStyle = mergeStyles(styles.paragraph, {
33-
marginRight: 5,
34-
});
35-
3632
const list = parent?.type === "list" ? (parent as List) : null;
3733
const itemNumber = (list?.start ?? 1) + (index ?? 0);
3834

35+
const markerStyle = useMemo(() => {
36+
const defaultStyle = mergeStyles(styles.paragraph, {
37+
fontWeight: "500",
38+
});
39+
const firstItem = node.children[0];
40+
if (!firstItem) return defaultStyle;
41+
if (firstItem.type === "heading") {
42+
return styles.heading?.(firstItem.depth);
43+
}
44+
return defaultStyle;
45+
}, [styles, node]);
46+
3947
return (
4048
<View style={{ flexDirection: "row" }}>
41-
{list?.ordered ? (
42-
<Text style={markerStyle}>{itemNumber}.</Text>
43-
) : (
44-
<Text style={markerStyle}></Text>
45-
)}
49+
<View style={{ marginRight: 5 }}>
50+
{list?.ordered ? (
51+
<Text style={markerStyle}>{itemNumber}.</Text>
52+
) : (
53+
<Text style={markerStyle}></Text>
54+
)}
55+
</View>
4656
<View style={styles.listItem}>
4757
{node.children.map((child, idx) => (
4858
<Fragment key={idx}>

src/renderers/text.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { RendererArgs } from "./renderers";
88
export const TextRenderer = ({ node }: RendererArgs<MdText>): ReactNode => {
99
const { styles } = useMarkdownContext();
1010

11-
const value = node.value || "";
11+
const value = (node.value || "").replace(/\n/g, " ");
1212
if (!value) return null;
1313
return <Text style={styles.text}>{value}</Text>;
1414
};

src/themes/default.tsx

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,6 @@ export const defaultTheme: Theme = {
6262
emphasis: {
6363
fontStyle: "italic",
6464
},
65-
footnoteDefinition: {
66-
color: light.darkColor,
67-
},
6865
footnoteReference: {
6966
fontStyle: "italic",
7067
fontSize: 10,
@@ -127,9 +124,6 @@ export const defaultTheme: Theme = {
127124
color: dark.primaryColor,
128125
},
129126
},
130-
footnoteDefinition: {
131-
color: dark.darkColor,
132-
},
133127
footnoteReference: {
134128
color: dark.darkColor,
135129
},

src/themes/themes.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ export interface Styles {
1515
container: ViewStyle;
1616
delete: TextStyle;
1717
emphasis: TextStyle;
18-
footnoteDefinition: TextStyle;
1918
footnoteReference: TextStyle;
2019
heading: (level: number) => TextStyle;
2120
image: ImageStyle;

0 commit comments

Comments
 (0)