-
Notifications
You must be signed in to change notification settings - Fork 449
Expand file tree
/
Copy pathCodeWalkthrough.tsx
More file actions
151 lines (142 loc) · 5.32 KB
/
Copy pathCodeWalkthrough.tsx
File metadata and controls
151 lines (142 loc) · 5.32 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
import React, { useMemo, useState } from 'react';
import clsx from 'clsx';
import { usePrismTheme } from '@docusaurus/theme-common';
import Highlight, { defaultProps, Language } from 'prism-react-renderer';
import { parseCodeTitle, parseCodeWalkthrough } from './codeWalkthroughParser';
import styles from './styles.module.css';
type Props = {
children: string;
className?: string;
language?: string;
metastring?: string;
title?: string;
};
function normalizeLanguage(language?: string, className?: string): string {
const fromClassName = className?.match(/language-([^\s]+)/)?.[1];
return (language || fromClassName || 'text').toLowerCase();
}
function resolveHighlightLanguage(language: string): string {
if (defaultProps.Prism.languages[language]) return language;
if (language === 'solidity' || language === 'rust') return 'clike';
if (language === 'shell' || language === 'sh') return 'bash';
return 'text';
}
function isActiveLine(lineNumber: number, startLine: number, endLine: number): boolean {
return lineNumber >= startLine && lineNumber <= endLine;
}
export function CodeWalkthrough({
children,
className,
language: languageProp,
metastring,
title,
}: Props) {
const parsed = useMemo(() => parseCodeWalkthrough(children), [children]);
const language = normalizeLanguage(languageProp, className);
const highlightLanguage = resolveHighlightLanguage(language);
const prismTheme = usePrismTheme();
const [activeIndex, setActiveIndex] = useState(0);
const activeStep = parsed.steps[activeIndex];
const resolvedTitle = parseCodeTitle(metastring, title);
const copyCode = async () => {
if (typeof navigator === 'undefined' || !navigator.clipboard) return;
await navigator.clipboard.writeText(parsed.code);
};
if (parsed.steps.length === 0) {
return (
<pre className={className}>
<code>{children}</code>
</pre>
);
}
return (
<div className={styles.codeWalkthrough}>
<div className={styles.walkthroughCodePane}>
<div className={styles.walkthroughCodeHeader}>
<span>{resolvedTitle || language}</span>
<button type="button" onClick={copyCode} className={styles.secondaryButton}>
Copy
</button>
</div>
<Highlight
Prism={defaultProps.Prism}
theme={prismTheme}
code={parsed.code}
language={highlightLanguage as Language}
>
{({ className: highlightClassName, style, tokens, getLineProps, getTokenProps }) => (
<pre
className={clsx(highlightClassName, styles.walkthroughPre, 'thin-scrollbar')}
style={style}
tabIndex={0}
>
<code>
{tokens.map((line, index) => {
const lineNumber = index + 1;
const lineProps = getLineProps({ line });
const { key: lineKey, ...linePropsWithoutKey } = lineProps;
return (
<span
key={lineKey || index}
{...linePropsWithoutKey}
className={clsx(
lineProps.className,
styles.walkthroughLine,
activeStep &&
isActiveLine(lineNumber, activeStep.startLine, activeStep.endLine) &&
styles.walkthroughLineActive,
)}
>
<span className={styles.walkthroughLineNumber}>{lineNumber}</span>
<span className={styles.walkthroughLineContent}>
{line.map((token, key) => {
const tokenProps = getTokenProps({ token });
const { key: tokenKey, ...tokenPropsWithoutKey } = tokenProps;
return <span key={tokenKey || key} {...tokenPropsWithoutKey} />;
})}
</span>
</span>
);
})}
</code>
</pre>
)}
</Highlight>
</div>
<div className={styles.walkthroughSteps}>
{parsed.steps.map((step, index) => (
<button
key={step.id}
type="button"
className={clsx(styles.walkthroughStep, index === activeIndex && styles.activeStep)}
onClick={() => setActiveIndex(index)}
>
<span className={styles.stepKicker}>
{index + 1} / {parsed.steps.length}
</span>
<span className={styles.stepTitle}>{step.title}</span>
{step.body && <span className={styles.stepBody}>{step.body}</span>}
</button>
))}
<div className={styles.walkthroughControls}>
<button
type="button"
className={styles.secondaryButton}
disabled={activeIndex === 0}
onClick={() => setActiveIndex((value) => Math.max(0, value - 1))}
>
Previous
</button>
<button
type="button"
className={styles.primaryButton}
disabled={activeIndex === parsed.steps.length - 1}
onClick={() => setActiveIndex((value) => Math.min(parsed.steps.length - 1, value + 1))}
>
Next
</button>
</div>
</div>
</div>
);
}