react-native-marked 1.6.2 → 1.6.4

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.
Files changed (64) hide show
  1. package/dist/commonjs/components/MDImage.js +1 -1
  2. package/dist/commonjs/components/MDImage.js.map +1 -1
  3. package/dist/commonjs/lib/Markdown.js.map +1 -1
  4. package/dist/commonjs/lib/Parser.js +27 -24
  5. package/dist/commonjs/lib/Parser.js.map +1 -1
  6. package/dist/commonjs/lib/Renderer.js.map +1 -1
  7. package/dist/commonjs/theme/colors.js +10 -10
  8. package/dist/commonjs/theme/colors.js.map +1 -1
  9. package/dist/commonjs/theme/spacing.js.map +1 -1
  10. package/dist/commonjs/theme/styles.js +12 -12
  11. package/dist/commonjs/theme/styles.js.map +1 -1
  12. package/dist/commonjs/utils/handlers.js.map +1 -1
  13. package/dist/commonjs/utils/string.js.map +1 -1
  14. package/dist/commonjs/utils/url.js +3 -3
  15. package/dist/commonjs/utils/url.js.map +1 -1
  16. package/dist/module/components/MDImage.js +4 -4
  17. package/dist/module/components/MDImage.js.map +1 -1
  18. package/dist/module/index.js +1 -1
  19. package/dist/module/lib/Markdown.js +5 -5
  20. package/dist/module/lib/Markdown.js.map +1 -1
  21. package/dist/module/lib/Parser.js +29 -26
  22. package/dist/module/lib/Parser.js.map +1 -1
  23. package/dist/module/lib/Renderer.js +8 -8
  24. package/dist/module/lib/Renderer.js.map +1 -1
  25. package/dist/module/theme/colors.js +10 -10
  26. package/dist/module/theme/colors.js.map +1 -1
  27. package/dist/module/theme/spacing.js.map +1 -1
  28. package/dist/module/theme/styles.js +15 -15
  29. package/dist/module/theme/styles.js.map +1 -1
  30. package/dist/module/utils/handlers.js +1 -1
  31. package/dist/module/utils/handlers.js.map +1 -1
  32. package/dist/module/utils/string.js.map +1 -1
  33. package/dist/module/utils/url.js +3 -3
  34. package/dist/module/utils/url.js.map +1 -1
  35. package/dist/typescript/components/MDImage.d.ts +2 -2
  36. package/dist/typescript/components/MDImage.d.ts.map +1 -1
  37. package/dist/typescript/index.d.ts +1 -1
  38. package/dist/typescript/lib/Markdown.d.ts +2 -2
  39. package/dist/typescript/lib/Parser.d.ts +4 -4
  40. package/dist/typescript/lib/Parser.d.ts.map +1 -1
  41. package/dist/typescript/lib/Renderer.d.ts +2 -2
  42. package/dist/typescript/lib/Renderer.d.ts.map +1 -1
  43. package/dist/typescript/lib/types.d.ts +4 -4
  44. package/dist/typescript/lib/types.d.ts.map +1 -1
  45. package/dist/typescript/theme/colors.d.ts +2 -2
  46. package/dist/typescript/theme/spacing.d.ts +1 -1
  47. package/dist/typescript/theme/styles.d.ts +2 -2
  48. package/dist/typescript/theme/styles.d.ts.map +1 -1
  49. package/dist/typescript/theme/types.d.ts +3 -3
  50. package/dist/typescript/theme/types.d.ts.map +1 -1
  51. package/package.json +8 -58
  52. package/src/components/MDImage.tsx +19 -19
  53. package/src/index.ts +1 -1
  54. package/src/lib/Markdown.tsx +40 -40
  55. package/src/lib/Parser.tsx +241 -240
  56. package/src/lib/Renderer.tsx +108 -108
  57. package/src/lib/types.ts +11 -11
  58. package/src/theme/colors.ts +16 -16
  59. package/src/theme/spacing.ts +5 -5
  60. package/src/theme/styles.ts +126 -126
  61. package/src/theme/types.ts +25 -25
  62. package/src/utils/handlers.ts +6 -6
  63. package/src/utils/string.ts +1 -1
  64. package/src/utils/url.ts +14 -14
@@ -1,255 +1,256 @@
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';
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
- 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
- }
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
- 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
- );
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
- 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
- }
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
- /* 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
- }
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
- 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
- );
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
- return this.renderer.getTextNode(token.raw, {
182
- ...this.styles.text,
183
- ...styles,
184
- });
185
- }
186
- default: {
187
- console.warn(
188
- `react-native-marked: token with '${token.type}' type was not found.`
189
- );
190
- return null;
191
- }
192
- }
193
- });
194
- return elements.filter((element) => element !== null);
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
- private getNormalizedSiblingNodesForBlockAndInlineTokens = (
198
- tokens: marked.Token[],
199
- textStyle?: TextStyle
200
- ): ReactNode[] => {
201
- let tokenRenderQueue: marked.Token[] = [];
202
- const siblingNodes: ReactNode[] = [];
203
- tokens.forEach((t) => {
204
- /**
205
- * To avoid inlining images
206
- * Currently supports images, link images
207
- * Note: to be extend for other token types
208
- */
209
- if (
210
- t.type === 'image' ||
211
- (t.type === 'link' &&
212
- t.tokens &&
213
- t.tokens[0] &&
214
- t.tokens[0].type === 'image')
215
- ) {
216
- // Render existing inline tokens in the queue
217
- const parsed = this.parseInline(tokenRenderQueue);
218
- if (parsed.length > 0) {
219
- siblingNodes.push(this.renderer.getTextNode(parsed, textStyle));
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
- // Render the current block token
223
- if (t.type === 'image') {
224
- siblingNodes.push(this.parseInline([t]));
225
- } else if (t.type === 'link') {
226
- const imageToken = t.tokens[0] as marked.Tokens.Image;
227
- const href = getValidURL(this.baseUrl, t.href);
228
- siblingNodes.push(
229
- this.renderer.getImageLinkNode(
230
- href,
231
- imageToken.href,
232
- imageToken.text || imageToken.title,
233
- this.styles.image
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
- tokenRenderQueue = [];
239
- return;
240
- }
241
- tokenRenderQueue = [...tokenRenderQueue, t];
242
- });
239
+ tokenRenderQueue = [];
240
+ return;
241
+ }
242
+ tokenRenderQueue = [...tokenRenderQueue, t];
243
+ });
243
244
 
244
- /* Remaining temp tokens if any */
245
- if (tokenRenderQueue.length > 0) {
246
- siblingNodes.push(
247
- this.renderer.getTextNode(this.parseInline(tokenRenderQueue), {})
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
- return siblingNodes;
252
- };
252
+ return siblingNodes;
253
+ };
253
254
  }
254
255
 
255
256
  export default Parser;