Skip to content

Commit 3458ebb

Browse files
committed
initial rework
1 parent f92295a commit 3458ebb

File tree

5 files changed

+326
-246
lines changed

5 files changed

+326
-246
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-markdown-display",
3-
"version": "5.1.1",
3+
"version": "6.0.0",
44
"description": "Markdown renderer for react-native, with CommonMark spec support + adds syntax extensions & sugar (URL autolinking, typographer), originally created by Mient-jan Stelling as react-native-markdown-renderer",
55
"main": "src/index.js",
66
"types": "src/index.d.ts",

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ const Markdown = React.memo(
139139
rules = null,
140140
plugins = [],
141141
style = null,
142-
mergeStyle = false,
142+
mergeStyle = true,
143143
markdownit = MarkdownIt({
144144
typographer: true,
145145
}),

src/lib/AstRenderer.js

Lines changed: 47 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,37 @@ export default class AstRenderer {
7575
return this.renderNode(value, parents);
7676
});
7777

78-
// text node type is a special case because it is always the 'last' element in a chain
78+
// render any special types of nodes that have different renderRule function signatures
7979

80-
if (
81-
node.type === 'text' ||
82-
node.type === 'list_item' ||
83-
node.type === 'code_inline' ||
84-
node.type === 'code_block' ||
85-
node.type === 'fence'
86-
) {
87-
// we build up a style object for text types, this effectively grabs the styles from parents and
88-
// applies them in order of priority parent (most) to child (least) priority
89-
// so that if we overwride the text style, it does not overwrite a header1 style, for instance.
80+
if (node.type === 'link' || node.type === 'blocklink') {
81+
return renderFunction(
82+
node,
83+
children,
84+
parentNodes,
85+
this._style,
86+
this._onLinkPress,
87+
);
88+
}
89+
90+
if (node.type === 'image') {
91+
return renderFunction(
92+
node,
93+
children,
94+
parentNodes,
95+
this._style,
96+
this._allowedImageHandlers,
97+
this._defaultImageHandler,
98+
);
99+
}
100+
101+
// We are at the bottom of some tree - grab all the parent styles
102+
// this effectively grabs the styles from parents and
103+
// applies them in order of priority parent (least) to child (most)
104+
// to allow styling global, then lower down things individually
105+
106+
// we have to handle list_item seperately here because they have some child
107+
// pseudo classes that need the additional style props from parents passed down to them
108+
if (children.length === 0 || node.type === 'list_item') {
90109
const styleObj = {};
91110

92111
for (let a = parentNodes.length - 1; a > -1; a--) {
@@ -107,6 +126,22 @@ export default class AstRenderer {
107126
...refStyle,
108127
...StyleSheet.flatten(this._style[parentNodes[a].type]),
109128
};
129+
130+
// workaround for list_items and their content cascading down the tree
131+
if (parentNodes[a].type === 'list_item') {
132+
let contentStyle = {};
133+
134+
if (parentNodes[a + 1].type === 'bullet_list') {
135+
contentStyle = this._style.bullet_list_content;
136+
} else if (parentNodes[a + 1].type === 'ordered_list') {
137+
contentStyle = this._style.ordered_list_content;
138+
}
139+
140+
refStyle = {
141+
...refStyle,
142+
...StyleSheet.flatten(contentStyle),
143+
};
144+
}
110145
}
111146

112147
// then work out if any of them are text styles that should be used in the end.
@@ -122,29 +157,6 @@ export default class AstRenderer {
122157
return renderFunction(node, children, parentNodes, this._style, styleObj);
123158
}
124159

125-
// render any special types of nodes that have different renderRule function signatures
126-
127-
if (node.type === 'link' || node.type === 'blocklink') {
128-
return renderFunction(
129-
node,
130-
children,
131-
parentNodes,
132-
this._style,
133-
this._onLinkPress,
134-
);
135-
}
136-
137-
if (node.type === 'image') {
138-
return renderFunction(
139-
node,
140-
children,
141-
parentNodes,
142-
this._style,
143-
this._allowedImageHandlers,
144-
this._defaultImageHandler,
145-
);
146-
}
147-
148160
// cull top level children
149161

150162
if (
@@ -167,7 +179,7 @@ export default class AstRenderer {
167179
* @return {*}
168180
*/
169181
render = nodes => {
170-
const root = {type: 'root', key: getUniqueID(), children: nodes};
182+
const root = {type: 'body', key: getUniqueID(), children: nodes};
171183
return this.renderNode(root, [], true);
172184
};
173185
}

0 commit comments

Comments
 (0)