react-native-marked 1.6.12 → 1.6.13

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.
@@ -26,173 +26,174 @@ class Parser {
26
26
  }
27
27
 
28
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
-
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
-
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);
29
+ return this._parse(tokens);
100
30
  }
101
31
 
102
- parseInline(
32
+ private _parse(
103
33
  tokens: marked.Token[],
104
34
  styles?: ViewStyle | TextStyle | ImageStyle,
105
35
  ) {
106
36
  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,
37
+ return this._parseToken(token, styles);
38
+ });
39
+ return elements.filter((element) => element !== null);
40
+ }
41
+
42
+ private _parseToken(
43
+ token: marked.Token,
44
+ styles?: ViewStyle | TextStyle | ImageStyle,
45
+ ): ReactNode {
46
+ switch (token.type) {
47
+ case "space": {
48
+ return null;
49
+ }
50
+ case "paragraph": {
51
+ const paragraphChildren =
52
+ this.getNormalizedSiblingNodesForBlockAndInlineTokens(
53
+ token.tokens,
54
+ this.styles.text,
172
55
  );
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
- }
181
56
 
182
- return this.renderer.getTextNode(token.raw, {
183
- ...this.styles.text,
184
- ...styles,
57
+ return this.renderer.getViewNode(
58
+ paragraphChildren,
59
+ this.styles.paragraph,
60
+ );
61
+ }
62
+ case "blockquote": {
63
+ return this.renderer.getBlockquoteNode(
64
+ this._parse(token.tokens),
65
+ this.styles.blockquote,
66
+ );
67
+ }
68
+ case "heading": {
69
+ const styles = this.headingStylesMap[token.depth];
70
+ return this.renderer.getTextNode(
71
+ this._parse(token.tokens, styles),
72
+ styles,
73
+ );
74
+ }
75
+ case "code": {
76
+ return this.renderer.getCodeBlockNode(
77
+ token.text,
78
+ this.styles.code,
79
+ this.styles.em,
80
+ );
81
+ }
82
+ case "hr": {
83
+ return this.renderer.getViewNode(null, this.styles.hr);
84
+ }
85
+ case "list": {
86
+ const li = token.items.map((item) => {
87
+ const children = item.tokens.map((cItem) => {
88
+ if (cItem.type === "text") {
89
+ /* getViewNode since tokens could contain a block like elements (i.e. img) */
90
+ const childTokens = (cItem as marked.Tokens.Text).tokens || [];
91
+ const listChildren =
92
+ this.getNormalizedSiblingNodesForBlockAndInlineTokens(
93
+ childTokens,
94
+ this.styles.li,
95
+ );
96
+ return this.renderer.getViewNode(listChildren, this.styles.li);
97
+ }
98
+
99
+ /* Parse the nested token */
100
+ return this._parseToken(cItem);
185
101
  });
186
- }
187
- default: {
102
+ return this.renderer.getViewNode(children, this.styles.li);
103
+ });
104
+ return this.renderer.getListNode(
105
+ token.ordered,
106
+ li,
107
+ this.styles.list,
108
+ this.styles.li,
109
+ );
110
+ }
111
+ case "escape": {
112
+ return this.renderer.getTextNode(token.text, {
113
+ ...this.styles.text,
114
+ ...styles,
115
+ });
116
+ }
117
+ case "link": {
118
+ // Note: Linking Images (https://www.markdownguide.org/basic-syntax/#linking-images) are wrapped
119
+ // in paragraph token, so will be handled via `getNormalizedSiblingNodesForBlockAndInlineTokens`
120
+ const linkStyle = {
121
+ ...styles,
122
+ ...this.styles.link, // To override color property
123
+ };
124
+ const href = getValidURL(this.baseUrl, token.href);
125
+ return this.renderer.getTextLinkNode(
126
+ this._parse(token.tokens, linkStyle),
127
+ href,
128
+ linkStyle,
129
+ );
130
+ }
131
+ case "image": {
132
+ return this.renderer.getImageNode(
133
+ token.href,
134
+ token.text || token.title,
135
+ this.styles.image,
136
+ );
137
+ }
138
+ case "strong": {
139
+ const boldStyle = {
140
+ ...this.styles.strong,
141
+ ...styles,
142
+ };
143
+ return this.renderer.getTextNode(
144
+ this._parse(token.tokens, boldStyle),
145
+ boldStyle,
146
+ );
147
+ }
148
+ case "em": {
149
+ const italicStyle = {
150
+ ...this.styles.em,
151
+ ...styles,
152
+ };
153
+ return this.renderer.getTextNode(
154
+ this._parse(token.tokens, italicStyle),
155
+ italicStyle,
156
+ );
157
+ }
158
+ case "codespan": {
159
+ return this.renderer.getTextNode(token.text, {
160
+ ...this.styles.codespan,
161
+ ...styles,
162
+ });
163
+ }
164
+ case "br": {
165
+ return this.renderer.getTextNode("\n", {});
166
+ }
167
+ case "del": {
168
+ const strikethroughStyle = {
169
+ ...this.styles.strikethrough,
170
+ ...styles,
171
+ };
172
+ return this.renderer.getTextNode(
173
+ this._parse(token.tokens, strikethroughStyle),
174
+ strikethroughStyle,
175
+ );
176
+ }
177
+ case "text":
178
+ case "html": {
179
+ if (token.type === "html") {
188
180
  console.warn(
189
- `react-native-marked: token with '${token.type}' type was not found.`,
181
+ "react-native-marked: rendering html from markdown is not supported",
190
182
  );
191
- return null;
192
183
  }
184
+
185
+ return this.renderer.getTextNode(token.raw, {
186
+ ...this.styles.text,
187
+ ...styles,
188
+ });
193
189
  }
194
- });
195
- return elements.filter((element) => element !== null);
190
+ default: {
191
+ console.warn(
192
+ `react-native-marked: token with '${token.type}' type was not found.`,
193
+ );
194
+ return null;
195
+ }
196
+ }
196
197
  }
197
198
 
198
199
  private getNormalizedSiblingNodesForBlockAndInlineTokens = (
@@ -215,14 +216,14 @@ class Parser {
215
216
  t.tokens[0].type === "image")
216
217
  ) {
217
218
  // Render existing inline tokens in the queue
218
- const parsed = this.parseInline(tokenRenderQueue);
219
+ const parsed = this._parse(tokenRenderQueue);
219
220
  if (parsed.length > 0) {
220
221
  siblingNodes.push(this.renderer.getTextNode(parsed, textStyle));
221
222
  }
222
223
 
223
224
  // Render the current block token
224
225
  if (t.type === "image") {
225
- siblingNodes.push(this.parseInline([t]));
226
+ siblingNodes.push(this._parseToken(t));
226
227
  } else if (t.type === "link") {
227
228
  const imageToken = t.tokens[0] as marked.Tokens.Image;
228
229
  const href = getValidURL(this.baseUrl, t.href);
@@ -245,7 +246,7 @@ class Parser {
245
246
  /* Remaining temp tokens if any */
246
247
  if (tokenRenderQueue.length > 0) {
247
248
  siblingNodes.push(
248
- this.renderer.getTextNode(this.parseInline(tokenRenderQueue), {}),
249
+ this.renderer.getTextNode(this._parse(tokenRenderQueue), {}),
249
250
  );
250
251
  }
251
252
 
@@ -76,9 +76,9 @@ class Renderer {
76
76
  ) {
77
77
  return (
78
78
  <ScrollView
79
+ horizontal
79
80
  key={generateRandomString()}
80
81
  contentContainerStyle={containerStyle}
81
- horizontal={true}
82
82
  >
83
83
  <Text style={textStyle}>{text}</Text>
84
84
  </ScrollView>