-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathEnrichedMarkdownText.tsx
More file actions
164 lines (143 loc) · 4.24 KB
/
EnrichedMarkdownText.tsx
File metadata and controls
164 lines (143 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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
import {
useState,
useEffect,
useMemo,
Fragment,
type CSSProperties,
} from 'react';
import type { EnrichedMarkdownTextProps } from '../types/MarkdownTextProps.web';
import { normalizeMarkdownStyle } from '../normalizeMarkdownStyle.web';
import {
zeroTrailingMargins,
parseErrorFallbackStyle,
buildStyles,
} from './styles';
import { parseMarkdown } from './parseMarkdown';
import { RenderNode } from './renderers';
import type { ASTNode, RendererCallbacks, RenderCapabilities } from './types';
import { indexTaskItems, markInlineImages } from './utils';
import { loadKaTeX } from './katex';
import type { KaTeXInstance } from './katex';
export const EnrichedMarkdownText = ({
markdown,
markdownStyle = {},
md4cFlags = {},
onLinkPress,
onLinkLongPress,
onTaskListItemPress,
allowTrailingMargin = false,
containerStyle,
selectable = true,
dir,
selectionColor,
...rest
}: EnrichedMarkdownTextProps) => {
const normalizedStyle = useMemo(
() => normalizeMarkdownStyle(markdownStyle),
[markdownStyle]
);
const [ast, setAst] = useState<ASTNode | null>(null);
const [katex, setKatex] = useState<KaTeXInstance | null>(null);
const [parseError, setParseError] = useState<boolean>(false);
const { underline = false, latexMath = true } = md4cFlags;
useEffect(() => {
let cancelled = false;
const katexPromise = latexMath ? loadKaTeX() : Promise.resolve(null);
Promise.all([
parseMarkdown(markdown, { underline, latexMath }),
katexPromise,
])
.then(([result, katexInstance]) => {
if (!cancelled) {
indexTaskItems(result);
markInlineImages(result);
setParseError(false);
setKatex(katexInstance);
setAst(result);
}
})
.catch((error) => {
if (!cancelled) {
if (__DEV__) {
console.error('[EnrichedMarkdownText] Parse failed:', error);
}
setParseError(true);
setAst(null);
setKatex(null);
}
});
return () => {
cancelled = true;
};
}, [markdown, underline, latexMath]);
const callbacks = useMemo<RendererCallbacks>(
() => ({ onLinkPress, onLinkLongPress, onTaskListItemPress }),
[onLinkPress, onLinkLongPress, onTaskListItemPress]
);
const capabilities = useMemo<RenderCapabilities>(() => ({ katex }), [katex]);
const lastChildStyle = useMemo(
() =>
allowTrailingMargin
? normalizedStyle
: zeroTrailingMargins(normalizedStyle),
[normalizedStyle, allowTrailingMargin]
);
const styles = useMemo(() => buildStyles(normalizedStyle), [normalizedStyle]);
const lastChildStyles = useMemo(
() => buildStyles(lastChildStyle),
[lastChildStyle]
);
const wrapperStyle = useMemo<CSSProperties>(
() => ({
display: 'flex',
flexDirection: 'column',
...(containerStyle as CSSProperties),
...(selectable ? undefined : { userSelect: 'none' }),
...(selectionColor
? ({ ['--enrm-selection-bg']: selectionColor } as CSSProperties)
: null),
}),
[containerStyle, selectable, selectionColor]
);
const selectionStyle = selectionColor ? (
<style>{`[data-enriched-markdown-text] ::selection {
background-color: var(--enrm-selection-bg);
}`}</style>
) : null;
if (parseError) {
return (
<Fragment>
{selectionStyle}
<div
data-enriched-markdown-text
style={wrapperStyle}
dir={dir}
{...rest}
>
<pre style={parseErrorFallbackStyle}>{markdown}</pre>
</div>
</Fragment>
);
}
if (!ast) return null;
const children = ast.children ?? [];
const lastIdx = children.length - 1;
return (
<Fragment>
{selectionStyle}
<div data-enriched-markdown-text style={wrapperStyle} dir={dir} {...rest}>
{children.map((child, index) => (
<RenderNode
key={`${child.type}-${index}`}
node={child}
style={index === lastIdx ? lastChildStyle : normalizedStyle}
styles={index === lastIdx ? lastChildStyles : styles}
callbacks={callbacks}
capabilities={capabilities}
/>
))}
</div>
</Fragment>
);
};
export default EnrichedMarkdownText;