react-native-marked 1.0.0-alpha.1 → 1.0.0
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 +23 -7
- 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 +38 -131
- package/dist/commonjs/lib/Parser.js.map +1 -1
- package/dist/commonjs/lib/Renderer.js +22 -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 +39 -130
- package/dist/module/lib/Parser.js.map +1 -1
- package/dist/module/lib/Renderer.js +19 -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 +10 -5
- package/src/components/MDImage.tsx +5 -28
- package/src/lib/Markdown.tsx +28 -10
- package/src/lib/Parser.tsx +163 -0
- package/src/lib/Renderer.tsx +32 -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
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import type { marked } from 'marked';
|
|
2
|
+
import type { ImageStyle, StyleProp, TextStyle, ViewStyle } from 'react-native';
|
|
3
|
+
import Renderer from './Renderer';
|
|
4
|
+
import type { MarkedStyles } from '../theme/types';
|
|
5
|
+
|
|
6
|
+
interface ParserOptions {
|
|
7
|
+
styles?: MarkedStyles;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
class Parser {
|
|
11
|
+
private renderer;
|
|
12
|
+
private styles: MarkedStyles;
|
|
13
|
+
private headingStylesMap: Record<number, TextStyle | undefined>;
|
|
14
|
+
constructor(options: ParserOptions) {
|
|
15
|
+
this.styles = { ...options.styles };
|
|
16
|
+
this.renderer = new Renderer();
|
|
17
|
+
this.headingStylesMap = {
|
|
18
|
+
1: this.styles.h1,
|
|
19
|
+
2: this.styles.h2,
|
|
20
|
+
3: this.styles.h3,
|
|
21
|
+
4: this.styles.h4,
|
|
22
|
+
5: this.styles.h5,
|
|
23
|
+
6: this.styles.h6,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
parse(tokens: marked.Token[]) {
|
|
28
|
+
const elements: React.ReactNode[] = tokens.map((token) => {
|
|
29
|
+
switch (token.type) {
|
|
30
|
+
case 'space': {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
case 'paragraph': {
|
|
34
|
+
let tempTokens: marked.Token[] = [];
|
|
35
|
+
const paragraphChildren: React.ReactNode[] = [];
|
|
36
|
+
token.tokens.forEach((t) => {
|
|
37
|
+
if (t.type === 'image') {
|
|
38
|
+
paragraphChildren.push(
|
|
39
|
+
this.renderer.getTextNode(
|
|
40
|
+
this.parseInline(tempTokens),
|
|
41
|
+
this.styles.text
|
|
42
|
+
)
|
|
43
|
+
);
|
|
44
|
+
paragraphChildren.push(this.parseInline([t]));
|
|
45
|
+
tempTokens = [];
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
tempTokens = [...tempTokens, t];
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
if (tempTokens.length > 0) {
|
|
52
|
+
paragraphChildren.push(
|
|
53
|
+
this.renderer.getTextNode(this.parseInline(tempTokens), {})
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
|
|
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] ?? this.styles.text;
|
|
70
|
+
return this.renderer.getTextNode(token.text, styles);
|
|
71
|
+
}
|
|
72
|
+
case 'code': {
|
|
73
|
+
return this.renderer.getCodeBlockNode(
|
|
74
|
+
token.text,
|
|
75
|
+
this.styles.code,
|
|
76
|
+
this.styles.em
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
case 'hr': {
|
|
80
|
+
return this.renderer.getViewNode(null, this.styles.hr);
|
|
81
|
+
}
|
|
82
|
+
case 'list': {
|
|
83
|
+
const li = token.items.map((item) => item.text);
|
|
84
|
+
return this.renderer.getListNode(
|
|
85
|
+
token.ordered,
|
|
86
|
+
li,
|
|
87
|
+
this.styles.list,
|
|
88
|
+
this.styles.li
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
default:
|
|
92
|
+
console.warn(`Token with '${token.type}' type was not found.`);
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
return elements;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
parseInline(
|
|
100
|
+
tokens: marked.Token[],
|
|
101
|
+
styles: StyleProp<ViewStyle | TextStyle | ImageStyle> = {}
|
|
102
|
+
) {
|
|
103
|
+
const elements: React.ReactNode[] = tokens.map((token) => {
|
|
104
|
+
if (!token) return null;
|
|
105
|
+
|
|
106
|
+
switch (token.type) {
|
|
107
|
+
case 'escape': {
|
|
108
|
+
return this.renderer.getTextNode(token.text, [
|
|
109
|
+
styles,
|
|
110
|
+
this.styles.text,
|
|
111
|
+
]);
|
|
112
|
+
}
|
|
113
|
+
case 'html': {
|
|
114
|
+
console.log('html', token);
|
|
115
|
+
return null;
|
|
116
|
+
}
|
|
117
|
+
case 'link': {
|
|
118
|
+
return this.renderer.getLinkNode(token.text, token.href, [
|
|
119
|
+
styles,
|
|
120
|
+
this.styles.link,
|
|
121
|
+
]);
|
|
122
|
+
}
|
|
123
|
+
case 'image': {
|
|
124
|
+
return this.renderer.getImageNode(token.href);
|
|
125
|
+
}
|
|
126
|
+
case 'strong': {
|
|
127
|
+
return this.renderer.getTextNode(
|
|
128
|
+
this.parseInline(token.tokens, this.styles.strong),
|
|
129
|
+
[styles, this.styles.strong]
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
case 'em': {
|
|
133
|
+
return this.renderer.getTextNode(
|
|
134
|
+
this.parseInline(token.tokens, this.styles.em),
|
|
135
|
+
[styles, this.styles.em]
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
case 'codespan': {
|
|
139
|
+
return this.renderer.getTextNode(token.text, this.styles.codespan);
|
|
140
|
+
}
|
|
141
|
+
case 'br': {
|
|
142
|
+
return this.renderer.getTextNode('\n', {});
|
|
143
|
+
}
|
|
144
|
+
case 'del': {
|
|
145
|
+
return null;
|
|
146
|
+
}
|
|
147
|
+
case 'text': {
|
|
148
|
+
return this.renderer.getTextNode(token.raw, [
|
|
149
|
+
styles,
|
|
150
|
+
this.styles.text,
|
|
151
|
+
]);
|
|
152
|
+
}
|
|
153
|
+
default: {
|
|
154
|
+
console.warn(`Token with '${token.type}' type was not found.`);
|
|
155
|
+
return null;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
return elements;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export default Parser;
|
package/src/lib/Renderer.tsx
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
2
|
import { Linking, ScrollView, View, ViewStyle } from 'react-native';
|
|
3
|
-
import
|
|
4
|
-
import
|
|
3
|
+
import { Text, StyleProp, TextStyle } from 'react-native';
|
|
4
|
+
import MarkedList from '@jsamr/react-native-li';
|
|
5
|
+
import Disc from '@jsamr/counter-style/presets/disc';
|
|
6
|
+
import Decimal from '@jsamr/counter-style/presets/decimal';
|
|
5
7
|
import MDImage from './../components/MDImage';
|
|
8
|
+
|
|
6
9
|
import { generateRandomString } from '../utils/string';
|
|
7
10
|
|
|
8
11
|
class Renderer {
|
|
@@ -31,7 +34,10 @@ class Renderer {
|
|
|
31
34
|
);
|
|
32
35
|
};
|
|
33
36
|
|
|
34
|
-
|
|
37
|
+
getViewNode(
|
|
38
|
+
children: React.ReactNode[] | null,
|
|
39
|
+
styles: StyleProp<ViewStyle>
|
|
40
|
+
) {
|
|
35
41
|
return (
|
|
36
42
|
<View key={generateRandomString()} style={styles}>
|
|
37
43
|
{children}
|
|
@@ -39,7 +45,7 @@ class Renderer {
|
|
|
39
45
|
);
|
|
40
46
|
}
|
|
41
47
|
|
|
42
|
-
|
|
48
|
+
getCodeBlockNode(
|
|
43
49
|
text: string,
|
|
44
50
|
containerStyle: StyleProp<ViewStyle>,
|
|
45
51
|
textStyle: StyleProp<TextStyle>
|
|
@@ -51,7 +57,7 @@ class Renderer {
|
|
|
51
57
|
);
|
|
52
58
|
}
|
|
53
59
|
|
|
54
|
-
|
|
60
|
+
getBlockquoteNode(children: React.ReactNode[], styles: StyleProp<ViewStyle>) {
|
|
55
61
|
return (
|
|
56
62
|
<View key={generateRandomString()} style={styles}>
|
|
57
63
|
{children}
|
|
@@ -59,16 +65,29 @@ class Renderer {
|
|
|
59
65
|
);
|
|
60
66
|
}
|
|
61
67
|
|
|
62
|
-
|
|
63
|
-
return (
|
|
64
|
-
<Text key={generateRandomString()} style={styles}>
|
|
65
|
-
{text}
|
|
66
|
-
</Text>
|
|
67
|
-
);
|
|
68
|
+
getImageNode(uri: string) {
|
|
69
|
+
return <MDImage key={generateRandomString()} uri={uri} />;
|
|
68
70
|
}
|
|
69
71
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
+
getListNode(
|
|
73
|
+
ordered: boolean,
|
|
74
|
+
li: string[],
|
|
75
|
+
listStyle: ViewStyle | undefined,
|
|
76
|
+
textStyle: TextStyle | undefined
|
|
77
|
+
) {
|
|
78
|
+
return (
|
|
79
|
+
<MarkedList
|
|
80
|
+
counterRenderer={ordered ? Decimal : Disc}
|
|
81
|
+
markerTextStyle={textStyle}
|
|
82
|
+
markerBoxStyle={listStyle}
|
|
83
|
+
>
|
|
84
|
+
{li.map((text, index) => (
|
|
85
|
+
<Text key={index} style={textStyle}>
|
|
86
|
+
{text}
|
|
87
|
+
</Text>
|
|
88
|
+
))}
|
|
89
|
+
</MarkedList>
|
|
90
|
+
);
|
|
72
91
|
}
|
|
73
92
|
}
|
|
74
93
|
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { ColorKeys } from './types';
|
|
2
|
+
|
|
3
|
+
const colors: Record<'light' | 'dark', Record<ColorKeys, string>> = {
|
|
4
|
+
light: {
|
|
5
|
+
background: '#ffffff',
|
|
6
|
+
code: '#f6f8fa',
|
|
7
|
+
link: '#58a6ff',
|
|
8
|
+
text: '#333333',
|
|
9
|
+
border: '#d0d7de',
|
|
10
|
+
},
|
|
11
|
+
dark: {
|
|
12
|
+
background: '#000000',
|
|
13
|
+
code: '#161b22',
|
|
14
|
+
link: '#58a6ff',
|
|
15
|
+
text: '#ffffff',
|
|
16
|
+
border: '#30363d',
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export default colors;
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { ColorSchemeName, StyleSheet } from 'react-native';
|
|
2
|
+
import { getTheme } from './theme';
|
|
3
|
+
import type { MarkedStyles, Theme } from './types';
|
|
4
|
+
|
|
5
|
+
const getFontStyles = (theme: Theme) => {
|
|
6
|
+
return StyleSheet.create({
|
|
7
|
+
regular: {
|
|
8
|
+
fontSize: 16,
|
|
9
|
+
lineHeight: 24,
|
|
10
|
+
color: theme.colors.text,
|
|
11
|
+
},
|
|
12
|
+
heading: {
|
|
13
|
+
fontWeight: '500',
|
|
14
|
+
color: theme.colors.text,
|
|
15
|
+
},
|
|
16
|
+
});
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const getStyles = (
|
|
20
|
+
userStyles?: MarkedStyles,
|
|
21
|
+
colorScheme?: ColorSchemeName
|
|
22
|
+
): MarkedStyles => {
|
|
23
|
+
const theme = getTheme(colorScheme || 'light');
|
|
24
|
+
const fontStyle = getFontStyles(theme);
|
|
25
|
+
const defaultStyles = StyleSheet.create<MarkedStyles>({
|
|
26
|
+
container: {
|
|
27
|
+
backgroundColor: theme.colors.background,
|
|
28
|
+
},
|
|
29
|
+
em: {
|
|
30
|
+
...fontStyle.regular,
|
|
31
|
+
fontStyle: 'italic',
|
|
32
|
+
},
|
|
33
|
+
strong: {
|
|
34
|
+
...fontStyle.regular,
|
|
35
|
+
fontWeight: 'bold',
|
|
36
|
+
},
|
|
37
|
+
text: { ...fontStyle.regular },
|
|
38
|
+
paragraph: {
|
|
39
|
+
...fontStyle.regular,
|
|
40
|
+
paddingVertical: 8,
|
|
41
|
+
},
|
|
42
|
+
link: {
|
|
43
|
+
...fontStyle.regular,
|
|
44
|
+
fontStyle: 'italic',
|
|
45
|
+
color: theme.colors.link,
|
|
46
|
+
},
|
|
47
|
+
blockquote: {
|
|
48
|
+
borderLeftColor: theme.colors.border,
|
|
49
|
+
paddingLeft: 16,
|
|
50
|
+
borderLeftWidth: 5,
|
|
51
|
+
opacity: 0.8,
|
|
52
|
+
},
|
|
53
|
+
h1: {
|
|
54
|
+
fontSize: 32,
|
|
55
|
+
lineHeight: 40,
|
|
56
|
+
fontWeight: 'bold',
|
|
57
|
+
marginVertical: 8,
|
|
58
|
+
letterSpacing: 0,
|
|
59
|
+
},
|
|
60
|
+
h2: {
|
|
61
|
+
...fontStyle.heading,
|
|
62
|
+
fontSize: 28,
|
|
63
|
+
lineHeight: 36,
|
|
64
|
+
marginVertical: 8,
|
|
65
|
+
},
|
|
66
|
+
h3: {
|
|
67
|
+
...fontStyle.heading,
|
|
68
|
+
fontSize: 24,
|
|
69
|
+
lineHeight: 32,
|
|
70
|
+
marginVertical: 4,
|
|
71
|
+
},
|
|
72
|
+
h4: {
|
|
73
|
+
...fontStyle.heading,
|
|
74
|
+
fontSize: 22,
|
|
75
|
+
lineHeight: 28,
|
|
76
|
+
marginVertical: 4,
|
|
77
|
+
},
|
|
78
|
+
h5: {
|
|
79
|
+
...fontStyle.regular,
|
|
80
|
+
...fontStyle.heading,
|
|
81
|
+
marginVertical: 2,
|
|
82
|
+
},
|
|
83
|
+
h6: {
|
|
84
|
+
...fontStyle.heading,
|
|
85
|
+
fontSize: 14,
|
|
86
|
+
lineHeight: 20,
|
|
87
|
+
marginVertical: 2,
|
|
88
|
+
},
|
|
89
|
+
codespan: {
|
|
90
|
+
...fontStyle.regular,
|
|
91
|
+
fontStyle: 'italic',
|
|
92
|
+
backgroundColor: theme.colors.code,
|
|
93
|
+
fontWeight: '300',
|
|
94
|
+
},
|
|
95
|
+
code: {
|
|
96
|
+
padding: 16,
|
|
97
|
+
backgroundColor: theme.colors.code,
|
|
98
|
+
minWidth: '100%',
|
|
99
|
+
},
|
|
100
|
+
hr: {
|
|
101
|
+
borderBottomWidth: 1,
|
|
102
|
+
borderBottomColor: theme.colors.border,
|
|
103
|
+
marginVertical: 4,
|
|
104
|
+
},
|
|
105
|
+
li: {
|
|
106
|
+
...fontStyle.regular,
|
|
107
|
+
flexShrink: 1,
|
|
108
|
+
},
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
return {
|
|
112
|
+
...defaultStyles,
|
|
113
|
+
...userStyles,
|
|
114
|
+
};
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
export default getStyles;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { TextStyle, ViewStyle } from 'react-native';
|
|
2
|
+
|
|
3
|
+
export interface MarkedStyles {
|
|
4
|
+
container?: ViewStyle;
|
|
5
|
+
em?: TextStyle;
|
|
6
|
+
strong?: TextStyle;
|
|
7
|
+
text?: TextStyle;
|
|
8
|
+
paragraph?: ViewStyle;
|
|
9
|
+
link?: TextStyle;
|
|
10
|
+
blockquote?: ViewStyle;
|
|
11
|
+
h1?: TextStyle;
|
|
12
|
+
h2?: TextStyle;
|
|
13
|
+
h3?: TextStyle;
|
|
14
|
+
h4?: TextStyle;
|
|
15
|
+
h5?: TextStyle;
|
|
16
|
+
h6?: TextStyle;
|
|
17
|
+
codespan?: TextStyle;
|
|
18
|
+
code?: ViewStyle;
|
|
19
|
+
hr?: ViewStyle;
|
|
20
|
+
list?: ViewStyle;
|
|
21
|
+
li?: TextStyle;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export type ColorKeys = 'background' | 'code' | 'link' | 'text' | 'border';
|
|
25
|
+
|
|
26
|
+
export interface Theme {
|
|
27
|
+
colors: Record<ColorKeys, string>;
|
|
28
|
+
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"names":[],"sources":["types.ts"],"sourcesContent":["import type { ImageStyle, TextStyle, ViewStyle } from 'react-native';\n\nexport interface MarkedStyles {\n em?: TextStyle;\n strong?: TextStyle;\n text?: TextStyle;\n paragraph?: ViewStyle;\n link?: TextStyle;\n blockquoteText?: TextStyle;\n blockquoteContainer?: ViewStyle;\n h1?: TextStyle;\n h2?: TextStyle;\n h3?: TextStyle;\n h4?: TextStyle;\n h5?: TextStyle;\n h6?: TextStyle;\n image?: ImageStyle;\n codespan?: TextStyle;\n code?: ViewStyle;\n}\n\nexport interface ParserOptions {\n contentWidth: number;\n styles?: MarkedStyles;\n}\n"],"mappings":""}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"names":[],"sources":["types.ts"],"sourcesContent":["import type { ImageStyle, TextStyle, ViewStyle } from 'react-native';\n\nexport interface MarkedStyles {\n em?: TextStyle;\n strong?: TextStyle;\n text?: TextStyle;\n paragraph?: ViewStyle;\n link?: TextStyle;\n blockquoteText?: TextStyle;\n blockquoteContainer?: ViewStyle;\n h1?: TextStyle;\n h2?: TextStyle;\n h3?: TextStyle;\n h4?: TextStyle;\n h5?: TextStyle;\n h6?: TextStyle;\n image?: ImageStyle;\n codespan?: TextStyle;\n code?: ViewStyle;\n}\n\nexport interface ParserOptions {\n contentWidth: number;\n styles?: MarkedStyles;\n}\n"],"mappings":""}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import type { ImageStyle, TextStyle, ViewStyle } from 'react-native';
|
|
2
|
-
export interface MarkedStyles {
|
|
3
|
-
em?: TextStyle;
|
|
4
|
-
strong?: TextStyle;
|
|
5
|
-
text?: TextStyle;
|
|
6
|
-
paragraph?: ViewStyle;
|
|
7
|
-
link?: TextStyle;
|
|
8
|
-
blockquoteText?: TextStyle;
|
|
9
|
-
blockquoteContainer?: ViewStyle;
|
|
10
|
-
h1?: TextStyle;
|
|
11
|
-
h2?: TextStyle;
|
|
12
|
-
h3?: TextStyle;
|
|
13
|
-
h4?: TextStyle;
|
|
14
|
-
h5?: TextStyle;
|
|
15
|
-
h6?: TextStyle;
|
|
16
|
-
image?: ImageStyle;
|
|
17
|
-
codespan?: TextStyle;
|
|
18
|
-
code?: ViewStyle;
|
|
19
|
-
}
|
|
20
|
-
export interface ParserOptions {
|
|
21
|
-
contentWidth: number;
|
|
22
|
-
styles?: MarkedStyles;
|
|
23
|
-
}
|
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;
|