react-native-marked 1.0.0-alpha.3 → 1.0.2
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/README.md +7 -5
- package/dist/commonjs/components/MDImage.js +5 -22
- package/dist/commonjs/components/MDImage.js.map +1 -1
- package/dist/commonjs/lib/Markdown.js +19 -11
- package/dist/commonjs/lib/Markdown.js.map +1 -1
- package/dist/commonjs/lib/Parser.js +49 -130
- package/dist/commonjs/lib/Parser.js.map +1 -1
- package/dist/commonjs/lib/Renderer.js +19 -13
- package/dist/commonjs/lib/Renderer.js.map +1 -1
- package/dist/commonjs/theme/colors.js +25 -0
- package/dist/commonjs/theme/colors.js.map +1 -0
- package/dist/commonjs/theme/styles.js +113 -0
- package/dist/commonjs/theme/styles.js.map +1 -0
- package/dist/commonjs/theme/theme.js +19 -0
- package/dist/commonjs/theme/theme.js.map +1 -0
- package/dist/commonjs/{types → theme}/types.js +0 -0
- package/dist/commonjs/theme/types.js.map +1 -0
- package/dist/module/components/MDImage.js +6 -22
- package/dist/module/components/MDImage.js.map +1 -1
- package/dist/module/lib/Markdown.js +18 -12
- package/dist/module/lib/Markdown.js.map +1 -1
- package/dist/module/lib/Parser.js +50 -129
- package/dist/module/lib/Parser.js.map +1 -1
- package/dist/module/lib/Renderer.js +16 -13
- package/dist/module/lib/Renderer.js.map +1 -1
- package/dist/module/theme/colors.js +18 -0
- package/dist/module/theme/colors.js.map +1 -0
- package/dist/module/theme/styles.js +102 -0
- package/dist/module/theme/styles.js.map +1 -0
- package/dist/module/theme/theme.js +7 -0
- package/dist/module/theme/theme.js.map +1 -0
- package/dist/module/{types → theme}/types.js +0 -0
- package/dist/module/theme/types.js.map +1 -0
- package/dist/typescript/components/MDImage.d.ts +1 -2
- package/dist/typescript/lib/Markdown.d.ts +6 -5
- package/dist/typescript/lib/Parser.d.ts +7 -5
- package/dist/typescript/lib/Renderer.d.ts +6 -6
- package/dist/typescript/theme/colors.d.ts +3 -0
- package/dist/typescript/theme/styles.d.ts +4 -0
- package/dist/typescript/theme/theme.d.ts +3 -0
- package/dist/typescript/theme/types.d.ts +25 -0
- package/package.json +15 -9
- package/src/components/MDImage.tsx +5 -28
- package/src/lib/Markdown.tsx +28 -10
- package/src/lib/Parser.tsx +177 -0
- package/src/lib/Renderer.tsx +28 -13
- package/src/theme/colors.ts +20 -0
- package/src/theme/styles.ts +117 -0
- package/src/theme/theme.ts +9 -0
- package/src/theme/types.ts +28 -0
- package/dist/commonjs/types/types.js.map +0 -1
- package/dist/module/types/types.js.map +0 -1
- package/dist/typescript/types/types.d.ts +0 -23
- package/src/lib/Parser.ts +0 -256
- package/src/types/types.ts +0 -25
package/src/lib/Parser.ts
DELETED
|
@@ -1,256 +0,0 @@
|
|
|
1
|
-
import type { marked } from 'marked';
|
|
2
|
-
import {
|
|
3
|
-
ImageStyle,
|
|
4
|
-
StyleProp,
|
|
5
|
-
StyleSheet,
|
|
6
|
-
TextStyle,
|
|
7
|
-
ViewStyle,
|
|
8
|
-
} from 'react-native';
|
|
9
|
-
import Renderer from './Renderer';
|
|
10
|
-
import type { MarkedStyles, ParserOptions } from '../types/types';
|
|
11
|
-
|
|
12
|
-
class Parser {
|
|
13
|
-
private renderer;
|
|
14
|
-
private styles: MarkedStyles;
|
|
15
|
-
private options: ParserOptions;
|
|
16
|
-
constructor(options: ParserOptions) {
|
|
17
|
-
this.renderer = new Renderer();
|
|
18
|
-
this.options = options;
|
|
19
|
-
this.styles = {
|
|
20
|
-
...defaultStyles,
|
|
21
|
-
...options.styles,
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
parse(tokens: marked.Token[]) {
|
|
26
|
-
return this.parseBlocks(tokens);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
parseBlocks(
|
|
30
|
-
tokens: marked.Token[],
|
|
31
|
-
styleObj: StyleProp<ViewStyle | TextStyle | ImageStyle> = {}
|
|
32
|
-
) {
|
|
33
|
-
const elements: React.ReactNode[] = tokens.map((token) => {
|
|
34
|
-
switch (token.type) {
|
|
35
|
-
case 'space': {
|
|
36
|
-
return null;
|
|
37
|
-
}
|
|
38
|
-
case 'paragraph': {
|
|
39
|
-
let tempTokens: marked.Token[] = [];
|
|
40
|
-
const paragraphChildren: React.ReactNode[] = [];
|
|
41
|
-
token.tokens.forEach((t) => {
|
|
42
|
-
// TODO: move type checking value to const
|
|
43
|
-
if (t.type === 'image') {
|
|
44
|
-
paragraphChildren.push(
|
|
45
|
-
this.renderer.getTextNode(
|
|
46
|
-
this.parseInline(tempTokens),
|
|
47
|
-
styleObj
|
|
48
|
-
)
|
|
49
|
-
);
|
|
50
|
-
paragraphChildren.push(this.parseInline([t]));
|
|
51
|
-
tempTokens = [];
|
|
52
|
-
return;
|
|
53
|
-
}
|
|
54
|
-
tempTokens = [...tempTokens, t];
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
if (tempTokens.length > 0) {
|
|
58
|
-
paragraphChildren.push(
|
|
59
|
-
this.renderer.getTextNode(this.parseInline(tempTokens), styleObj)
|
|
60
|
-
);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
return this.renderer.getParagraph(paragraphChildren, [
|
|
64
|
-
styleObj,
|
|
65
|
-
this.styles.paragraph,
|
|
66
|
-
]);
|
|
67
|
-
}
|
|
68
|
-
case 'blockquote': {
|
|
69
|
-
return this.renderer.getBlockquote(
|
|
70
|
-
this.parseBlocks(token.tokens, this.styles.blockquoteText),
|
|
71
|
-
this.styles.blockquoteContainer
|
|
72
|
-
);
|
|
73
|
-
}
|
|
74
|
-
case 'heading': {
|
|
75
|
-
switch (token.depth) {
|
|
76
|
-
case 1:
|
|
77
|
-
return this.renderer.getHeading(token.text, this.styles.h1);
|
|
78
|
-
case 2:
|
|
79
|
-
return this.renderer.getHeading(token.text, this.styles.h2);
|
|
80
|
-
case 3:
|
|
81
|
-
return this.renderer.getHeading(token.text, this.styles.h3);
|
|
82
|
-
case 4:
|
|
83
|
-
return this.renderer.getHeading(token.text, this.styles.h4);
|
|
84
|
-
case 5:
|
|
85
|
-
return this.renderer.getHeading(token.text, this.styles.h5);
|
|
86
|
-
case 6:
|
|
87
|
-
return this.renderer.getHeading(token.text, this.styles.h6);
|
|
88
|
-
default:
|
|
89
|
-
return null;
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
case 'code': {
|
|
93
|
-
return this.renderer.getCodeBlock(
|
|
94
|
-
token.text,
|
|
95
|
-
this.styles.code,
|
|
96
|
-
this.styles.em
|
|
97
|
-
);
|
|
98
|
-
}
|
|
99
|
-
default:
|
|
100
|
-
return null;
|
|
101
|
-
}
|
|
102
|
-
});
|
|
103
|
-
return elements;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
parseInline(
|
|
107
|
-
tokens: marked.Token[],
|
|
108
|
-
styleObj: StyleProp<ViewStyle | TextStyle | ImageStyle> = {}
|
|
109
|
-
) {
|
|
110
|
-
const elements: React.ReactNode[] = tokens.map((token) => {
|
|
111
|
-
if (!token) return null;
|
|
112
|
-
|
|
113
|
-
switch (token.type) {
|
|
114
|
-
case 'escape': {
|
|
115
|
-
return this.renderer.getTextNode(token.text, [
|
|
116
|
-
styleObj,
|
|
117
|
-
this.styles.text,
|
|
118
|
-
]);
|
|
119
|
-
}
|
|
120
|
-
case 'html': {
|
|
121
|
-
console.log('html', token);
|
|
122
|
-
return null;
|
|
123
|
-
}
|
|
124
|
-
case 'link': {
|
|
125
|
-
return this.renderer.getLinkNode(token.text, token.href, [
|
|
126
|
-
styleObj,
|
|
127
|
-
this.styles.link,
|
|
128
|
-
]);
|
|
129
|
-
}
|
|
130
|
-
case 'image': {
|
|
131
|
-
return this.renderer.getImage(token.href, this.options.contentWidth);
|
|
132
|
-
}
|
|
133
|
-
case 'strong': {
|
|
134
|
-
return this.renderer.getTextNode(
|
|
135
|
-
this.parseInline(token.tokens, this.styles.strong),
|
|
136
|
-
[styleObj, this.styles.strong]
|
|
137
|
-
);
|
|
138
|
-
}
|
|
139
|
-
case 'em': {
|
|
140
|
-
return this.renderer.getTextNode(
|
|
141
|
-
this.parseInline(token.tokens, this.styles.em),
|
|
142
|
-
[styleObj, this.styles.em]
|
|
143
|
-
);
|
|
144
|
-
}
|
|
145
|
-
case 'codespan': {
|
|
146
|
-
return this.renderer.getTextNode(
|
|
147
|
-
` ${token.text} `,
|
|
148
|
-
this.styles.codespan
|
|
149
|
-
);
|
|
150
|
-
}
|
|
151
|
-
case 'br': {
|
|
152
|
-
return this.renderer.getTextNode('\n', {});
|
|
153
|
-
}
|
|
154
|
-
case 'del': {
|
|
155
|
-
return null;
|
|
156
|
-
}
|
|
157
|
-
case 'text': {
|
|
158
|
-
return this.renderer.getTextNode(token.raw, [
|
|
159
|
-
styleObj,
|
|
160
|
-
this.styles.text,
|
|
161
|
-
]);
|
|
162
|
-
}
|
|
163
|
-
default: {
|
|
164
|
-
const errMsg = 'Token with "' + token.type + '" type was not found.';
|
|
165
|
-
console.error(errMsg);
|
|
166
|
-
return null;
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
});
|
|
170
|
-
return elements;
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
// TODO: material typography for font size
|
|
175
|
-
const defaultStyles = StyleSheet.create<MarkedStyles>({
|
|
176
|
-
em: {
|
|
177
|
-
fontSize: 16,
|
|
178
|
-
lineHeight: 24,
|
|
179
|
-
fontStyle: 'italic',
|
|
180
|
-
},
|
|
181
|
-
strong: {
|
|
182
|
-
fontSize: 16,
|
|
183
|
-
lineHeight: 24,
|
|
184
|
-
fontWeight: 'bold',
|
|
185
|
-
},
|
|
186
|
-
text: {
|
|
187
|
-
fontSize: 16,
|
|
188
|
-
lineHeight: 24,
|
|
189
|
-
},
|
|
190
|
-
paragraph: {
|
|
191
|
-
fontSize: 16,
|
|
192
|
-
lineHeight: 24,
|
|
193
|
-
paddingVertical: 8,
|
|
194
|
-
},
|
|
195
|
-
link: {
|
|
196
|
-
fontStyle: 'italic',
|
|
197
|
-
fontSize: 16,
|
|
198
|
-
lineHeight: 24,
|
|
199
|
-
color: '#0074cc',
|
|
200
|
-
},
|
|
201
|
-
blockquoteText: {
|
|
202
|
-
color: '#6a737d',
|
|
203
|
-
},
|
|
204
|
-
blockquoteContainer: {
|
|
205
|
-
borderLeftColor: '#dfe2e5',
|
|
206
|
-
paddingLeft: 16,
|
|
207
|
-
borderLeftWidth: 5,
|
|
208
|
-
},
|
|
209
|
-
h1: {
|
|
210
|
-
fontSize: 32,
|
|
211
|
-
lineHeight: 40,
|
|
212
|
-
fontWeight: 'bold',
|
|
213
|
-
paddingVertical: 8,
|
|
214
|
-
letterSpacing: 0,
|
|
215
|
-
},
|
|
216
|
-
h2: {
|
|
217
|
-
fontSize: 28,
|
|
218
|
-
lineHeight: 36,
|
|
219
|
-
fontWeight: '500',
|
|
220
|
-
paddingVertical: 8,
|
|
221
|
-
},
|
|
222
|
-
h3: {
|
|
223
|
-
fontSize: 36,
|
|
224
|
-
lineHeight: 44,
|
|
225
|
-
fontWeight: '500',
|
|
226
|
-
},
|
|
227
|
-
h4: {
|
|
228
|
-
fontSize: 22,
|
|
229
|
-
lineHeight: 28,
|
|
230
|
-
fontWeight: '500',
|
|
231
|
-
},
|
|
232
|
-
h5: {
|
|
233
|
-
fontSize: 16,
|
|
234
|
-
lineHeight: 24,
|
|
235
|
-
fontWeight: '500',
|
|
236
|
-
},
|
|
237
|
-
h6: {
|
|
238
|
-
fontSize: 14,
|
|
239
|
-
lineHeight: 20,
|
|
240
|
-
fontWeight: '500',
|
|
241
|
-
},
|
|
242
|
-
codespan: {
|
|
243
|
-
fontSize: 16,
|
|
244
|
-
lineHeight: 24,
|
|
245
|
-
fontStyle: 'italic',
|
|
246
|
-
backgroundColor: '#afb8c133',
|
|
247
|
-
fontWeight: '300',
|
|
248
|
-
},
|
|
249
|
-
code: {
|
|
250
|
-
padding: 16,
|
|
251
|
-
backgroundColor: '#afb8c133',
|
|
252
|
-
minWidth: '100%',
|
|
253
|
-
},
|
|
254
|
-
});
|
|
255
|
-
|
|
256
|
-
export default Parser;
|
package/src/types/types.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import type { ImageStyle, TextStyle, ViewStyle } from 'react-native';
|
|
2
|
-
|
|
3
|
-
export interface MarkedStyles {
|
|
4
|
-
em?: TextStyle;
|
|
5
|
-
strong?: TextStyle;
|
|
6
|
-
text?: TextStyle;
|
|
7
|
-
paragraph?: ViewStyle;
|
|
8
|
-
link?: TextStyle;
|
|
9
|
-
blockquoteText?: TextStyle;
|
|
10
|
-
blockquoteContainer?: ViewStyle;
|
|
11
|
-
h1?: TextStyle;
|
|
12
|
-
h2?: TextStyle;
|
|
13
|
-
h3?: TextStyle;
|
|
14
|
-
h4?: TextStyle;
|
|
15
|
-
h5?: TextStyle;
|
|
16
|
-
h6?: TextStyle;
|
|
17
|
-
image?: ImageStyle;
|
|
18
|
-
codespan?: TextStyle;
|
|
19
|
-
code?: ViewStyle;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export interface ParserOptions {
|
|
23
|
-
contentWidth: number;
|
|
24
|
-
styles?: MarkedStyles;
|
|
25
|
-
}
|