react-native-marked 1.6.3 → 1.6.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/commonjs/components/MDImage.js +1 -1
- package/dist/commonjs/components/MDImage.js.map +1 -1
- package/dist/commonjs/lib/Markdown.js.map +1 -1
- package/dist/commonjs/lib/Parser.js +27 -24
- package/dist/commonjs/lib/Parser.js.map +1 -1
- package/dist/commonjs/lib/Renderer.js.map +1 -1
- package/dist/commonjs/theme/colors.js +10 -10
- package/dist/commonjs/theme/colors.js.map +1 -1
- package/dist/commonjs/theme/spacing.js.map +1 -1
- package/dist/commonjs/theme/styles.js +12 -12
- package/dist/commonjs/theme/styles.js.map +1 -1
- package/dist/commonjs/utils/handlers.js.map +1 -1
- package/dist/commonjs/utils/string.js.map +1 -1
- package/dist/commonjs/utils/url.js +3 -3
- package/dist/commonjs/utils/url.js.map +1 -1
- package/dist/module/components/MDImage.js +4 -4
- package/dist/module/components/MDImage.js.map +1 -1
- package/dist/module/index.js +1 -1
- package/dist/module/lib/Markdown.js +5 -5
- package/dist/module/lib/Markdown.js.map +1 -1
- package/dist/module/lib/Parser.js +29 -26
- package/dist/module/lib/Parser.js.map +1 -1
- package/dist/module/lib/Renderer.js +8 -8
- package/dist/module/lib/Renderer.js.map +1 -1
- package/dist/module/theme/colors.js +10 -10
- package/dist/module/theme/colors.js.map +1 -1
- package/dist/module/theme/spacing.js.map +1 -1
- package/dist/module/theme/styles.js +15 -15
- package/dist/module/theme/styles.js.map +1 -1
- package/dist/module/utils/handlers.js +1 -1
- package/dist/module/utils/handlers.js.map +1 -1
- package/dist/module/utils/string.js.map +1 -1
- package/dist/module/utils/url.js +3 -3
- package/dist/module/utils/url.js.map +1 -1
- package/dist/typescript/components/MDImage.d.ts +2 -2
- package/dist/typescript/components/MDImage.d.ts.map +1 -1
- package/dist/typescript/index.d.ts +1 -1
- package/dist/typescript/lib/Markdown.d.ts +2 -2
- package/dist/typescript/lib/Parser.d.ts +4 -4
- package/dist/typescript/lib/Parser.d.ts.map +1 -1
- package/dist/typescript/lib/Renderer.d.ts +2 -2
- package/dist/typescript/lib/Renderer.d.ts.map +1 -1
- package/dist/typescript/lib/types.d.ts +4 -4
- package/dist/typescript/lib/types.d.ts.map +1 -1
- package/dist/typescript/theme/colors.d.ts +2 -2
- package/dist/typescript/theme/spacing.d.ts +1 -1
- package/dist/typescript/theme/styles.d.ts +2 -2
- package/dist/typescript/theme/styles.d.ts.map +1 -1
- package/dist/typescript/theme/types.d.ts +3 -3
- package/dist/typescript/theme/types.d.ts.map +1 -1
- package/package.json +7 -57
- package/src/components/MDImage.tsx +19 -19
- package/src/index.ts +1 -1
- package/src/lib/Markdown.tsx +40 -40
- package/src/lib/Parser.tsx +241 -240
- package/src/lib/Renderer.tsx +108 -108
- package/src/lib/types.ts +11 -11
- package/src/theme/colors.ts +16 -16
- package/src/theme/spacing.ts +5 -5
- package/src/theme/styles.ts +126 -126
- package/src/theme/types.ts +25 -25
- package/src/utils/handlers.ts +6 -6
- package/src/utils/string.ts +1 -1
- package/src/utils/url.ts +14 -14
package/src/lib/Parser.tsx
CHANGED
|
@@ -1,255 +1,256 @@
|
|
|
1
|
-
import type { ReactNode } from
|
|
2
|
-
import type { TextStyle, ViewStyle, ImageStyle } from
|
|
3
|
-
import type { marked } from
|
|
4
|
-
import Renderer from
|
|
5
|
-
import type { MarkedStyles } from
|
|
6
|
-
import type { ParserOptions } from
|
|
7
|
-
import { getValidURL } from
|
|
1
|
+
import type { ReactNode } from "react";
|
|
2
|
+
import type { TextStyle, ViewStyle, ImageStyle } from "react-native";
|
|
3
|
+
import type { marked } from "marked";
|
|
4
|
+
import Renderer from "./Renderer";
|
|
5
|
+
import type { MarkedStyles } from "../theme/types";
|
|
6
|
+
import type { ParserOptions } from "./types";
|
|
7
|
+
import { getValidURL } from "./../utils/url";
|
|
8
8
|
|
|
9
9
|
class Parser {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
10
|
+
private renderer;
|
|
11
|
+
private styles: MarkedStyles;
|
|
12
|
+
private headingStylesMap: Record<number, TextStyle | undefined>;
|
|
13
|
+
private baseUrl: string;
|
|
14
|
+
constructor(options: ParserOptions) {
|
|
15
|
+
this.styles = { ...options.styles };
|
|
16
|
+
this.baseUrl = options.baseUrl ?? "";
|
|
17
|
+
this.renderer = new Renderer();
|
|
18
|
+
this.headingStylesMap = {
|
|
19
|
+
1: this.styles.h1,
|
|
20
|
+
2: this.styles.h2,
|
|
21
|
+
3: this.styles.h3,
|
|
22
|
+
4: this.styles.h4,
|
|
23
|
+
5: this.styles.h5,
|
|
24
|
+
6: this.styles.h6,
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
27
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
28
|
+
parse(tokens: marked.Token[]) {
|
|
29
|
+
const elements: ReactNode[] = tokens.map((token) => {
|
|
30
|
+
switch (token.type) {
|
|
31
|
+
case "space": {
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
case "paragraph": {
|
|
35
|
+
const paragraphChildren =
|
|
36
|
+
this.getNormalizedSiblingNodesForBlockAndInlineTokens(
|
|
37
|
+
token.tokens,
|
|
38
|
+
this.styles.text,
|
|
39
|
+
);
|
|
40
40
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
41
|
+
return this.renderer.getViewNode(
|
|
42
|
+
paragraphChildren,
|
|
43
|
+
this.styles.paragraph,
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
case "blockquote": {
|
|
47
|
+
return this.renderer.getBlockquoteNode(
|
|
48
|
+
this.parse(token.tokens),
|
|
49
|
+
this.styles.blockquote,
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
case "heading": {
|
|
53
|
+
const styles = this.headingStylesMap[token.depth];
|
|
54
|
+
return this.renderer.getTextNode(
|
|
55
|
+
this.parseInline(token.tokens, styles),
|
|
56
|
+
styles,
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
case "code": {
|
|
60
|
+
return this.renderer.getCodeBlockNode(
|
|
61
|
+
token.text,
|
|
62
|
+
this.styles.code,
|
|
63
|
+
this.styles.em,
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
case "hr": {
|
|
67
|
+
return this.renderer.getViewNode(null, this.styles.hr);
|
|
68
|
+
}
|
|
69
|
+
case "list": {
|
|
70
|
+
const li = token.items.map((item) => {
|
|
71
|
+
const children = item.tokens.map((cItem) => {
|
|
72
|
+
if (cItem.type === "text") {
|
|
73
|
+
/* getViewNode since tokens could contain a block like elements (i.e. img) */
|
|
74
|
+
const childTokens = (cItem as marked.Tokens.Text).tokens || [];
|
|
75
|
+
const listChildren =
|
|
76
|
+
this.getNormalizedSiblingNodesForBlockAndInlineTokens(
|
|
77
|
+
childTokens,
|
|
78
|
+
this.styles.li,
|
|
79
|
+
);
|
|
80
|
+
return this.renderer.getViewNode(listChildren, this.styles.li);
|
|
81
|
+
}
|
|
82
82
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
83
|
+
/* Parse the nested token */
|
|
84
|
+
return this.parse([cItem]);
|
|
85
|
+
});
|
|
86
|
+
return this.renderer.getViewNode(children, this.styles.li);
|
|
87
|
+
});
|
|
88
|
+
return this.renderer.getListNode(
|
|
89
|
+
token.ordered,
|
|
90
|
+
li,
|
|
91
|
+
this.styles.list,
|
|
92
|
+
this.styles.li,
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
default:
|
|
96
|
+
return this.parseInline([token]);
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
return elements.filter((element) => element !== null);
|
|
100
|
+
}
|
|
101
101
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
102
|
+
parseInline(
|
|
103
|
+
tokens: marked.Token[],
|
|
104
|
+
styles?: ViewStyle | TextStyle | ImageStyle,
|
|
105
|
+
) {
|
|
106
|
+
const elements: ReactNode[] = tokens.map((token) => {
|
|
107
|
+
switch (token.type) {
|
|
108
|
+
case "escape": {
|
|
109
|
+
return this.renderer.getTextNode(token.text, {
|
|
110
|
+
...this.styles.text,
|
|
111
|
+
...styles,
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
case "link": {
|
|
115
|
+
// Note: Linking Images (https://www.markdownguide.org/basic-syntax/#linking-images) are wrapped
|
|
116
|
+
// in paragraph token, so will be handled via `getNormalizedSiblingNodesForBlockAndInlineTokens`
|
|
117
|
+
const linkStyle = {
|
|
118
|
+
...styles,
|
|
119
|
+
...this.styles.link, // To override color property
|
|
120
|
+
};
|
|
121
|
+
const href = getValidURL(this.baseUrl, token.href);
|
|
122
|
+
return this.renderer.getTextLinkNode(
|
|
123
|
+
this.parseInline(token.tokens, linkStyle),
|
|
124
|
+
href,
|
|
125
|
+
linkStyle,
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
case "image": {
|
|
129
|
+
return this.renderer.getImageNode(
|
|
130
|
+
token.href,
|
|
131
|
+
token.text || token.title,
|
|
132
|
+
this.styles.image,
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
case "strong": {
|
|
136
|
+
const boldStyle = {
|
|
137
|
+
...this.styles.strong,
|
|
138
|
+
...styles,
|
|
139
|
+
};
|
|
140
|
+
return this.renderer.getTextNode(
|
|
141
|
+
this.parseInline(token.tokens, boldStyle),
|
|
142
|
+
boldStyle,
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
case "em": {
|
|
146
|
+
const italicStyle = {
|
|
147
|
+
...this.styles.em,
|
|
148
|
+
...styles,
|
|
149
|
+
};
|
|
150
|
+
return this.renderer.getTextNode(
|
|
151
|
+
this.parseInline(token.tokens, italicStyle),
|
|
152
|
+
italicStyle,
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
case "codespan": {
|
|
156
|
+
return this.renderer.getTextNode(token.text, {
|
|
157
|
+
...this.styles.codespan,
|
|
158
|
+
...styles,
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
case "br": {
|
|
162
|
+
return this.renderer.getTextNode("\n", {});
|
|
163
|
+
}
|
|
164
|
+
case "del": {
|
|
165
|
+
const strikethroughStyle = {
|
|
166
|
+
...this.styles.strikethrough,
|
|
167
|
+
...styles,
|
|
168
|
+
};
|
|
169
|
+
return this.renderer.getTextNode(
|
|
170
|
+
this.parseInline(token.tokens, strikethroughStyle),
|
|
171
|
+
strikethroughStyle,
|
|
172
|
+
);
|
|
173
|
+
}
|
|
174
|
+
case "text":
|
|
175
|
+
case "html": {
|
|
176
|
+
if (token.type === "html") {
|
|
177
|
+
console.warn(
|
|
178
|
+
"react-native-marked: rendering html from markdown is not supported",
|
|
179
|
+
);
|
|
180
|
+
}
|
|
180
181
|
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
182
|
+
return this.renderer.getTextNode(token.raw, {
|
|
183
|
+
...this.styles.text,
|
|
184
|
+
...styles,
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
default: {
|
|
188
|
+
console.warn(
|
|
189
|
+
`react-native-marked: token with '${token.type}' type was not found.`,
|
|
190
|
+
);
|
|
191
|
+
return null;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
return elements.filter((element) => element !== null);
|
|
196
|
+
}
|
|
196
197
|
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
198
|
+
private getNormalizedSiblingNodesForBlockAndInlineTokens = (
|
|
199
|
+
tokens: marked.Token[],
|
|
200
|
+
textStyle?: TextStyle,
|
|
201
|
+
): ReactNode[] => {
|
|
202
|
+
let tokenRenderQueue: marked.Token[] = [];
|
|
203
|
+
const siblingNodes: ReactNode[] = [];
|
|
204
|
+
tokens.forEach((t) => {
|
|
205
|
+
/**
|
|
206
|
+
* To avoid inlining images
|
|
207
|
+
* Currently supports images, link images
|
|
208
|
+
* Note: to be extend for other token types
|
|
209
|
+
*/
|
|
210
|
+
if (
|
|
211
|
+
t.type === "image" ||
|
|
212
|
+
(t.type === "link" &&
|
|
213
|
+
t.tokens &&
|
|
214
|
+
t.tokens[0] &&
|
|
215
|
+
t.tokens[0].type === "image")
|
|
216
|
+
) {
|
|
217
|
+
// Render existing inline tokens in the queue
|
|
218
|
+
const parsed = this.parseInline(tokenRenderQueue);
|
|
219
|
+
if (parsed.length > 0) {
|
|
220
|
+
siblingNodes.push(this.renderer.getTextNode(parsed, textStyle));
|
|
221
|
+
}
|
|
221
222
|
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
223
|
+
// Render the current block token
|
|
224
|
+
if (t.type === "image") {
|
|
225
|
+
siblingNodes.push(this.parseInline([t]));
|
|
226
|
+
} else if (t.type === "link") {
|
|
227
|
+
const imageToken = t.tokens[0] as marked.Tokens.Image;
|
|
228
|
+
const href = getValidURL(this.baseUrl, t.href);
|
|
229
|
+
siblingNodes.push(
|
|
230
|
+
this.renderer.getImageLinkNode(
|
|
231
|
+
href,
|
|
232
|
+
imageToken.href,
|
|
233
|
+
imageToken.text || imageToken.title,
|
|
234
|
+
this.styles.image,
|
|
235
|
+
),
|
|
236
|
+
);
|
|
237
|
+
}
|
|
237
238
|
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
239
|
+
tokenRenderQueue = [];
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
tokenRenderQueue = [...tokenRenderQueue, t];
|
|
243
|
+
});
|
|
243
244
|
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
245
|
+
/* Remaining temp tokens if any */
|
|
246
|
+
if (tokenRenderQueue.length > 0) {
|
|
247
|
+
siblingNodes.push(
|
|
248
|
+
this.renderer.getTextNode(this.parseInline(tokenRenderQueue), {}),
|
|
249
|
+
);
|
|
250
|
+
}
|
|
250
251
|
|
|
251
|
-
|
|
252
|
-
|
|
252
|
+
return siblingNodes;
|
|
253
|
+
};
|
|
253
254
|
}
|
|
254
255
|
|
|
255
256
|
export default Parser;
|