/
code.tsx
331 lines (295 loc) · 10.3 KB
/
code.tsx
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import { css } from '@emotion/react';
import { mergeClasses, theme, Themes, typography } from '@expo/styleguide';
import { borderRadius, spacing } from '@expo/styleguide-base';
import { FileCode01Icon, LayoutAlt01Icon, Server03Icon } from '@expo/styleguide-icons';
import { Language, Prism } from 'prism-react-renderer';
import * as React from 'react';
import tippy, { roundArrow } from 'tippy.js';
import { useCodeBlockSettingsContext } from '~/providers/CodeBlockSettingsProvider';
import { Snippet } from '~/ui/components/Snippet/Snippet';
import { SnippetContent } from '~/ui/components/Snippet/SnippetContent';
import { SnippetHeader } from '~/ui/components/Snippet/SnippetHeader';
import { CopyAction } from '~/ui/components/Snippet/actions/CopyAction';
import { SettingsAction } from '~/ui/components/Snippet/actions/SettingsAction';
import { CODE } from '~/ui/components/Text';
import { TextTheme } from '~/ui/components/Text/types';
// @ts-ignore Jest ESM issue https://github.com/facebook/jest/issues/9430
const { default: testTippy } = tippy;
// Read more: https://github.com/FormidableLabs/prism-react-renderer#custom-language-support
async function initPrism() {
(typeof global !== 'undefined' ? global : window).Prism = Prism;
await import('~/ui/components/Snippet/prism-bash' as Language);
await import('prismjs/components/prism-diff' as Language);
await import('prismjs/components/prism-groovy' as Language);
await import('prismjs/components/prism-ini' as Language);
await import('prismjs/components/prism-java' as Language);
await import('prismjs/components/prism-json' as Language);
await import('prismjs/components/prism-objectivec' as Language);
await import('prismjs/components/prism-properties' as Language);
await import('prismjs/components/prism-ruby' as Language);
}
await initPrism();
const attributes = {
'data-text': true,
};
type Props = {
className?: string;
};
export function cleanCopyValue(value: string) {
return value
.replace(/\/\*\s?@(info[^*]+|end|hide[^*]+).?\*\//g, '')
.replace(/#\s?@(info[^#]+|end|hide[^#]+).?#/g, '')
.replace(/<!--\s?@(info[^<>]+|end|hide[^<>]+).?-->/g, '')
.replace(/^ +\r?\n|\n +\r?$/gm, '');
}
function escapeHtml(text: string) {
return text.replace(/"/g, '"');
}
function replaceXmlCommentsWithAnnotations(value: string) {
return value
.replace(
/<span class="token (comment|plain-text)"><!-- @info (.*?)--><\/span>\s*/g,
(match, type, content) => {
return content
? `<span class="code-annotation with-tooltip" data-tippy-content="${escapeHtml(
content
)}">`
: '<span class="code-annotation">';
}
)
.replace(
/<span class="token (comment|plain-text)"><!-- @hide (.*?)--><\/span>\s*/g,
(match, type, content) => {
return `<span><span class="code-hidden">%%placeholder-start%%</span><span class="code-placeholder">${escapeHtml(
content
)}</span><span class="code-hidden">%%placeholder-end%%</span><span class="code-hidden">`;
}
)
.replace(/\s*<span class="token (comment|plain-text)"><!-- @end --><\/span>/g, '</span>');
}
function replaceHashCommentsWithAnnotations(value: string) {
return value
.replace(
/<span class="token (comment|plain-text)"># @info (.*?)#<\/span>\s*/g,
(match, type, content) => {
return content
? `<span class="code-annotation with-tooltip" data-tippy-content="${escapeHtml(
content
)}">`
: '<span class="code-annotation">';
}
)
.replace(
/<span class="token (comment|plain-text)"># @hide (.*?)#<\/span>\s*/g,
(match, type, content) => {
return `<span><span class="code-hidden">%%placeholder-start%%</span><span class="code-placeholder">${escapeHtml(
content
)}</span><span class="code-hidden">%%placeholder-end%%</span><span class="code-hidden">`;
}
)
.replace(/\s*<span class="token (comment|plain-text)"># @end #<\/span>/g, '</span>');
}
function replaceSlashCommentsWithAnnotations(value: string) {
return value
.replace(
/<span class="token (comment|plain-text)">([\n\r\s]*)\/\* @info (.*?)\*\/[\n\r\s]*<\/span>\s*/g,
(match, type, beforeWhitespace, content) => {
return content
? `${beforeWhitespace}<span class="code-annotation with-tooltip" data-tippy-content="${escapeHtml(
content
)}">`
: `${beforeWhitespace}<span class="code-annotation">`;
}
)
.replace(
/<span class="token (comment|plain-text)">([\n\r\s]*)\/\* @hide (.*?)\*\/([\n\r\s]*)<\/span>\s*/g,
(match, type, beforeWhitespace, content, afterWhitespace) => {
return `<span><span class="code-hidden">%%placeholder-start%%</span><span class="code-placeholder">${beforeWhitespace}${escapeHtml(
content
)}${afterWhitespace}</span><span class="code-hidden">%%placeholder-end%%</span><span class="code-hidden">`;
}
)
.replace(
/\s*<span class="token (comment|plain-text)">[\n\r\s]*\/\* @end \*\/([\n\r\s]*)<\/span>/g,
(match, type, afterWhitespace) => `</span>${afterWhitespace}`
);
}
function parseValue(value: string) {
if (value.startsWith('@@@')) {
const valueChunks = value.split('@@@');
return {
title: valueChunks[1],
value: valueChunks[2],
};
}
return {
value,
};
}
export function Code({ className, children }: React.PropsWithChildren<Props>) {
const { preferredTheme, wordWrap } = useCodeBlockSettingsContext();
React.useEffect(() => {
const tippyFunc = testTippy || tippy;
tippyFunc('.code-annotation.with-tooltip', {
allowHTML: true,
theme: 'expo',
placement: 'top',
arrow: roundArrow,
interactive: true,
offset: [0, 20],
appendTo: document.body,
});
}, []);
// note(simek): MDX dropped `inlineCode` pseudo-tag, and we need to relay on `pre` and `code` now,
// which results in this nesting mess, we should fix it in the future
const rootProps =
className && className.startsWith('language')
? { className, children }
: (React.Children.toArray(children)[0] as JSX.Element)?.props;
const value = parseValue(rootProps?.children?.toString() || '');
let html = value.value;
// mdx will add the class `language-foo` to codeblocks with the tag `foo`
// if this class is present, we want to slice out `language-`
let lang = rootProps.className && rootProps.className.slice(9).toLowerCase();
// Allow for code blocks without a language.
if (lang) {
if (lang in remapLanguages) {
lang = remapLanguages[lang];
}
const grammar = Prism.languages[lang as keyof typeof Prism.languages];
if (!grammar) {
throw new Error(`docs currently do not support language: ${lang}`);
}
html = Prism.highlight(html, grammar, lang as Language);
if (['properties', 'ruby', 'bash', 'yaml'].includes(lang)) {
html = replaceHashCommentsWithAnnotations(html);
} else if (['xml', 'html'].includes(lang)) {
html = replaceXmlCommentsWithAnnotations(html);
} else {
html = replaceSlashCommentsWithAnnotations(html);
}
}
return value?.title ? (
<Snippet>
<SnippetHeader title={value.title} Icon={getIconForFile(value.title)}>
<CopyAction text={cleanCopyValue(value.value)} />
<SettingsAction />
</SnippetHeader>
<SnippetContent className="p-0">
<pre
css={STYLES_CODE_CONTAINER}
className={mergeClasses(wordWrap && '!whitespace-pre-wrap !break-words')}
{...attributes}>
<code
css={STYLES_CODE_BLOCK}
dangerouslySetInnerHTML={{ __html: html.replace(/^@@@.+@@@/g, '') }}
/>
</pre>
</SnippetContent>
</Snippet>
) : (
<pre
css={[STYLES_CODE_CONTAINER, STYLES_CODE_CONTAINER_BLOCK]}
className={mergeClasses(
preferredTheme === Themes.DARK && 'dark-theme',
wordWrap && '!whitespace-pre-wrap !break-words',
'last:mb-0'
)}
{...attributes}>
<code css={STYLES_CODE_BLOCK} dangerouslySetInnerHTML={{ __html: html }} />
</pre>
);
}
const STYLES_CODE_BLOCK = css`
${typography.body.code};
color: ${theme.text.default};
white-space: inherit;
padding: 0;
margin: 0;
.code-annotation {
transition: 200ms ease all;
transition-property: text-shadow, opacity;
text-shadow:
${theme.palette.yellow7} 0 0 10px,
${theme.palette.yellow7} 0 0 10px,
${theme.palette.yellow7} 0 0 10px,
${theme.palette.yellow7} 0 0 10px;
}
.code-annotation.with-tooltip:hover {
cursor: pointer;
animation: none;
opacity: 0.8;
}
.code-hidden {
display: none;
}
.code-placeholder {
opacity: 0.5;
}
`;
const STYLES_CODE_CONTAINER_BLOCK = css`
border: 1px solid ${theme.border.secondary};
padding: 16px;
margin: 16px 0;
background-color: ${theme.background.subtle};
`;
const STYLES_CODE_CONTAINER = css`
white-space: pre;
overflow: auto;
-webkit-overflow-scrolling: touch;
line-height: 120%;
border-radius: ${borderRadius.sm}px;
padding: ${spacing[4]}px;
table &:last-child {
margin-bottom: 0;
}
`;
const remapLanguages: Record<string, string> = {
'objective-c': 'objc',
sh: 'bash',
rb: 'ruby',
};
const codeBlockContainerStyle = {
margin: 0,
padding: `3px 6px`,
};
const codeBlockInlineStyle = {
padding: 4,
};
const codeBlockInlineContainerStyle = {
display: 'inline-flex',
padding: 0,
};
type CodeBlockProps = React.PropsWithChildren<{ inline?: boolean; theme?: TextTheme }>;
export const CodeBlock = ({ children, theme, inline = false }: CodeBlockProps) => {
const Element = inline ? 'span' : 'pre';
return (
<Element
css={[
STYLES_CODE_CONTAINER,
codeBlockContainerStyle,
inline && codeBlockInlineContainerStyle,
]}
{...attributes}>
<CODE
theme={theme}
css={[
STYLES_CODE_BLOCK,
inline && codeBlockInlineStyle,
{ fontSize: '80%' },
theme && { color: 'inherit' },
]}>
{children}
</CODE>
</Element>
);
};
function getIconForFile(filename: string) {
if (/_layout\.[jt]sx?$/.test(filename)) {
return LayoutAlt01Icon;
}
if (/\+api\.[jt]sx?$/.test(filename)) {
return Server03Icon;
}
return FileCode01Icon;
}