myst-to-react 0.1.14

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 ADDED
@@ -0,0 +1,6 @@
1
+ # myst-to-react
2
+
3
+ [![myst-to-react on npm](https://img.shields.io/npm/v/myst-to-react.svg)](https://www.npmjs.com/package/myst-to-react)
4
+ [![MIT License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/curvenote/curvenote/blob/main/LICENSE)
5
+
6
+ Convert a MyST AST to React.
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "myst-to-react",
3
+ "version": "0.1.14",
4
+ "main": "./src/index.tsx",
5
+ "types": "./src/index.tsx",
6
+ "files": [
7
+ "src"
8
+ ],
9
+ "license": "MIT",
10
+ "scripts": {
11
+ "lint": "eslint \"src/**/*.ts*\" \"src/**/*.tsx\" -c ./.eslintrc.js",
12
+ "lint:format": "prettier --check \"src/**/*.{ts,tsx,md}\""
13
+ },
14
+ "dependencies": {
15
+ "@curvenote/blocks": "*",
16
+ "@curvenote/connect": "^0.0.6",
17
+ "@curvenote/nbtx": "^0.1.11",
18
+ "@curvenote/ui-providers": "*",
19
+ "@headlessui/react": "^1.6.5",
20
+ "@heroicons/react": "^1.0.6",
21
+ "@popperjs/core": "^2.11.5",
22
+ "@remix-run/react": "^1.6.3",
23
+ "ansi-to-react": "^6.1.6",
24
+ "classnames": "^2.3.1",
25
+ "mermaid": "^9.1.6",
26
+ "myst-spec": "^0.0.4",
27
+ "myst-to-tex": "*",
28
+ "myst-transforms": "*",
29
+ "mystjs": "^0.0.13",
30
+ "nanoid": "^4.0.0",
31
+ "react-dom": "^17.0.2",
32
+ "react-popper": "^2.3.0",
33
+ "react-redux": "^8.0.2",
34
+ "react-syntax-highlighter": "^15.5.0",
35
+ "swr": "^1.3.0",
36
+ "unist-util-select": "^4.0.1"
37
+ },
38
+ "devDependencies": {
39
+ "@types/mermaid": "^8.2.9",
40
+ "@types/react": "^17.0.37",
41
+ "@types/react-dom": "^17.0.11",
42
+ "eslint": "^8.21.0",
43
+ "eslint-config-curvenote": "*",
44
+ "react": "^17.0.2",
45
+ "tsconfig": "*",
46
+ "typescript": "latest"
47
+ }
48
+ }
@@ -0,0 +1,183 @@
1
+ import type * as spec from 'myst-spec';
2
+ import React, { useState } from 'react';
3
+ import type { NodeRenderer } from './types';
4
+ import {
5
+ InformationCircleIcon,
6
+ ExclamationIcon as OExclamationIcon,
7
+ SpeakerphoneIcon,
8
+ PencilAltIcon,
9
+ ArrowCircleRightIcon,
10
+ } from '@heroicons/react/outline';
11
+ import {
12
+ ExclamationIcon as SExclamationIcon,
13
+ ExclamationCircleIcon as SExclamationCircleIcon,
14
+ XCircleIcon,
15
+ LightBulbIcon,
16
+ LightningBoltIcon,
17
+ ChevronRightIcon,
18
+ } from '@heroicons/react/solid';
19
+ import classNames from 'classnames';
20
+ // import { AdmonitionKind } from 'mystjs';
21
+
22
+ // TODO: get this from myst-spec?
23
+ enum AdmonitionKind {
24
+ admonition = 'admonition',
25
+ attention = 'attention',
26
+ caution = 'caution',
27
+ danger = 'danger',
28
+ error = 'error',
29
+ important = 'important',
30
+ hint = 'hint',
31
+ note = 'note',
32
+ seealso = 'seealso',
33
+ tip = 'tip',
34
+ warning = 'warning',
35
+ }
36
+
37
+ type ColorAndKind = {
38
+ kind: AdmonitionKind;
39
+ color: 'blue' | 'green' | 'yellow' | 'red';
40
+ };
41
+
42
+ function getClasses(className?: string) {
43
+ const classes =
44
+ className
45
+ ?.split(' ')
46
+ .map((s) => s.trim().toLowerCase())
47
+ .filter((s) => !!s) ?? [];
48
+ return [...new Set(classes)];
49
+ }
50
+
51
+ function getFirstKind({
52
+ kind,
53
+ classes = [],
54
+ }: {
55
+ kind?: AdmonitionKind | string;
56
+ classes?: string[];
57
+ }): ColorAndKind {
58
+ if (kind === AdmonitionKind.note || classes.includes('note')) {
59
+ return { kind: AdmonitionKind.note, color: 'blue' };
60
+ }
61
+ if (kind === AdmonitionKind.important || classes.includes('important')) {
62
+ return { kind: AdmonitionKind.important, color: 'blue' };
63
+ }
64
+ if (kind === AdmonitionKind.hint || classes.includes('hint')) {
65
+ return { kind: AdmonitionKind.hint, color: 'green' };
66
+ }
67
+ if (kind === AdmonitionKind.seealso || classes.includes('seealso')) {
68
+ return { kind: AdmonitionKind.seealso, color: 'green' };
69
+ }
70
+ if (kind === AdmonitionKind.tip || classes.includes('tip')) {
71
+ return { kind: AdmonitionKind.tip, color: 'green' };
72
+ }
73
+ if (kind === AdmonitionKind.attention || classes.includes('attention')) {
74
+ return { kind: AdmonitionKind.attention, color: 'yellow' };
75
+ }
76
+ if (kind === AdmonitionKind.warning || classes.includes('warning')) {
77
+ return { kind: AdmonitionKind.warning, color: 'yellow' };
78
+ }
79
+ if (kind === AdmonitionKind.caution || classes.includes('caution')) {
80
+ return { kind: AdmonitionKind.caution, color: 'yellow' };
81
+ }
82
+ if (kind === AdmonitionKind.danger || classes.includes('danger')) {
83
+ return { kind: AdmonitionKind.danger, color: 'red' };
84
+ }
85
+ if (kind === AdmonitionKind.error || classes.includes('error')) {
86
+ return { kind: AdmonitionKind.error, color: 'red' };
87
+ }
88
+ return { kind: AdmonitionKind.note, color: 'blue' };
89
+ }
90
+
91
+ const iconClass = 'h-8 w-8 inline-block pl-2 mr-2 -translate-y-[1px]';
92
+
93
+ function AdmonitionIcon({ kind }: { kind: AdmonitionKind }) {
94
+ if (kind === AdmonitionKind.note) return <InformationCircleIcon className={iconClass} />;
95
+ if (kind === AdmonitionKind.caution) return <OExclamationIcon className={iconClass} />;
96
+ if (kind === AdmonitionKind.warning) return <SExclamationIcon className={iconClass} />;
97
+ if (kind === AdmonitionKind.danger) return <SExclamationCircleIcon className={iconClass} />;
98
+ if (kind === AdmonitionKind.error) return <XCircleIcon className={iconClass} />;
99
+ if (kind === AdmonitionKind.attention) return <SpeakerphoneIcon className={iconClass} />;
100
+ if (kind === AdmonitionKind.tip) return <PencilAltIcon className={iconClass} />;
101
+ if (kind === AdmonitionKind.hint) return <LightBulbIcon className={iconClass} />;
102
+ if (kind === AdmonitionKind.important) return <LightningBoltIcon className={iconClass} />;
103
+ if (kind === AdmonitionKind.seealso) return <ArrowCircleRightIcon className={iconClass} />;
104
+ return <InformationCircleIcon className={iconClass} />;
105
+ }
106
+
107
+ export const AdmonitionTitle: NodeRenderer<spec.AdmonitionTitle> = (node, children) => {
108
+ return children;
109
+ };
110
+
111
+ function Admonition({
112
+ title,
113
+ kind,
114
+ color,
115
+ dropdown,
116
+ children,
117
+ }: ColorAndKind & { title: React.ReactNode; children: React.ReactNode[]; dropdown: boolean }) {
118
+ const [open, setOpen] = useState(false);
119
+
120
+ return (
121
+ <aside
122
+ className={classNames(
123
+ 'admonition rounded-md my-4 border-l-4 shadow-md dark:shadow-2xl dark:shadow-neutral-900 overflow-hidden',
124
+ {
125
+ 'border-blue-500': color === 'blue',
126
+ 'border-green-600': color === 'green',
127
+ 'border-amber-600': color === 'yellow',
128
+ 'border-red-600': color === 'red',
129
+ },
130
+ )}
131
+ >
132
+ <p
133
+ className={classNames('admonition-header m-0 text-lg font-medium py-1', {
134
+ 'text-blue-600 bg-blue-50 dark:bg-slate-900': color === 'blue',
135
+ 'text-green-600 bg-green-50 dark:bg-slate-900': color === 'green',
136
+ 'text-amber-600 bg-amber-50 dark:bg-slate-900': color === 'yellow',
137
+ 'text-red-600 bg-red-50 dark:bg-slate-900': color === 'red',
138
+ 'cursor-pointer hover:shadow-[inset_0_0_0px_20px_#00000003] dark:hover:shadow-[inset_0_0_0px_20px_#FFFFFF03]':
139
+ dropdown,
140
+ })}
141
+ onClick={dropdown ? () => setOpen(!open) : undefined}
142
+ >
143
+ <AdmonitionIcon kind={kind} />
144
+ <span className="text-neutral-900 dark:text-white">
145
+ {dropdown && (
146
+ <span className="block float-right font-thin text-sm text-neutral-700 dark:text-neutral-200">
147
+ {!open && 'Click to show'}
148
+ <ChevronRightIcon
149
+ className={classNames(iconClass, 'transition-transform', {
150
+ 'rotate-90 -translate-y-[5px]': open,
151
+ })}
152
+ />
153
+ </span>
154
+ )}
155
+ {title}
156
+ </span>
157
+ </p>
158
+ {(!dropdown || open) && (
159
+ <div className="px-4 py-1 bg-gray-50 dark:bg-stone-800">{children}</div>
160
+ )}
161
+ </aside>
162
+ );
163
+ }
164
+
165
+ export const AdmonitionRenderer: NodeRenderer<spec.Admonition> = (node, children) => {
166
+ const [title, ...rest] = children as any[];
167
+ const classes = getClasses(node.class);
168
+ const { kind, color } = getFirstKind({ kind: node.kind, classes });
169
+ const isDropdown = classes.includes('dropdown');
170
+
171
+ return (
172
+ <Admonition key={node.key} title={title} kind={kind} color={color} dropdown={isDropdown}>
173
+ {rest}
174
+ </Admonition>
175
+ );
176
+ };
177
+
178
+ const ADMONITION_RENDERERS = {
179
+ admonition: AdmonitionRenderer,
180
+ admonitionTitle: AdmonitionTitle,
181
+ };
182
+
183
+ export default ADMONITION_RENDERERS;
package/src/basic.tsx ADDED
@@ -0,0 +1,217 @@
1
+ import type * as spec from 'myst-spec';
2
+ import { HashLink } from './heading';
3
+ import type { NodeRenderer } from './types';
4
+
5
+ type TableExts = {
6
+ rowspan?: number;
7
+ colspan?: number;
8
+ };
9
+
10
+ type Delete = {
11
+ type: 'delete';
12
+ };
13
+
14
+ type Underline = {
15
+ type: 'underline';
16
+ };
17
+
18
+ type SmallCaps = {
19
+ type: 'smallcaps';
20
+ };
21
+
22
+ type DefinitionList = {
23
+ type: 'definitionList';
24
+ };
25
+
26
+ type DefinitionTerm = {
27
+ type: 'definitionTerm';
28
+ };
29
+
30
+ type DefinitionDescription = {
31
+ type: 'definitionDescription';
32
+ };
33
+
34
+ type CaptionNumber = {
35
+ type: 'captionNumber';
36
+ kind: string;
37
+ identifier: string;
38
+ };
39
+
40
+ type BasicNodeRenderers = {
41
+ strong: NodeRenderer<spec.Strong>;
42
+ emphasis: NodeRenderer<spec.Emphasis>;
43
+ link: NodeRenderer<spec.Link>;
44
+ paragraph: NodeRenderer<spec.Paragraph>;
45
+ break: NodeRenderer<spec.Break>;
46
+ inlineMath: NodeRenderer<spec.InlineMath>;
47
+ math: NodeRenderer<spec.Math>;
48
+ list: NodeRenderer<spec.List>;
49
+ listItem: NodeRenderer<spec.ListItem>;
50
+ container: NodeRenderer<spec.Container>;
51
+ caption: NodeRenderer<spec.Caption>;
52
+ blockquote: NodeRenderer<spec.Blockquote>;
53
+ thematicBreak: NodeRenderer<spec.ThematicBreak>;
54
+ subscript: NodeRenderer<spec.Subscript>;
55
+ superscript: NodeRenderer<spec.Superscript>;
56
+ abbreviation: NodeRenderer<spec.Abbreviation>;
57
+ // Tables
58
+ table: NodeRenderer<spec.Table>;
59
+ tableRow: NodeRenderer<spec.TableRow>;
60
+ tableCell: NodeRenderer<spec.TableCell & TableExts>;
61
+ // Comment
62
+ comment: NodeRenderer<spec.Comment>;
63
+ mystComment: NodeRenderer<spec.Comment>;
64
+ // Our additions
65
+ captionNumber: NodeRenderer<CaptionNumber>;
66
+ delete: NodeRenderer<Delete>;
67
+ underline: NodeRenderer<Underline>;
68
+ smallcaps: NodeRenderer<SmallCaps>;
69
+ // definitions
70
+ definitionList: NodeRenderer<DefinitionList>;
71
+ definitionTerm: NodeRenderer<DefinitionTerm>;
72
+ definitionDescription: NodeRenderer<DefinitionDescription>;
73
+ };
74
+
75
+ const BASIC_RENDERERS: BasicNodeRenderers = {
76
+ delete(node, children) {
77
+ return <del key={node.key}>{children}</del>;
78
+ },
79
+ strong(node, children) {
80
+ return <strong key={node.key}>{children}</strong>;
81
+ },
82
+ emphasis(node, children) {
83
+ return <em key={node.key}>{children}</em>;
84
+ },
85
+ underline(node, children) {
86
+ return (
87
+ <span key={node.key} style={{ textDecoration: 'underline' }}>
88
+ {children}
89
+ </span>
90
+ );
91
+ },
92
+ smallcaps(node, children) {
93
+ return (
94
+ <span key={node.key} style={{ fontVariant: 'small-caps' }}>
95
+ {children}
96
+ </span>
97
+ );
98
+ },
99
+ link(node, children) {
100
+ return (
101
+ <a key={node.key} target="_blank" href={node.url} rel="noreferrer">
102
+ {children}
103
+ </a>
104
+ );
105
+ },
106
+ paragraph(node, children) {
107
+ return <p key={node.key}>{children}</p>;
108
+ },
109
+ break(node) {
110
+ return <br key={node.key} />;
111
+ },
112
+ inlineMath(node) {
113
+ return <code key={node.key}>{node.value}</code>;
114
+ },
115
+ math(node) {
116
+ return <code key={node.key}>{node.value}</code>;
117
+ },
118
+ list(node, children) {
119
+ if (node.ordered) {
120
+ return (
121
+ <ol key={node.key} start={node.start || undefined}>
122
+ {children}
123
+ </ol>
124
+ );
125
+ }
126
+ return <ul key={node.key}>{children}</ul>;
127
+ },
128
+ listItem(node, children) {
129
+ return <li key={node.key}>{children}</li>;
130
+ },
131
+ container(node, children) {
132
+ return (
133
+ <figure key={node.key} id={node.html_id || node.identifier || node.key} className={node.kind}>
134
+ {children}
135
+ </figure>
136
+ );
137
+ },
138
+ caption(node, children) {
139
+ return (
140
+ <figcaption key={node.key} className="group">
141
+ {children}
142
+ </figcaption>
143
+ );
144
+ },
145
+ blockquote(node, children) {
146
+ return <blockquote key={node.key}>{children}</blockquote>;
147
+ },
148
+ thematicBreak(node) {
149
+ return <hr key={node.key} />;
150
+ },
151
+ captionNumber(node, children) {
152
+ function backwardsCompatibleLabel(value: string, kind?: string) {
153
+ const capital = kind?.slice(0, 1).toUpperCase() ?? 'F';
154
+ const body = kind?.slice(1) ?? 'igure';
155
+ return `${capital}${body}: ${children}`;
156
+ }
157
+ const label =
158
+ typeof children === 'string' ? backwardsCompatibleLabel(children, node.kind) : children;
159
+ const id = node.html_id || node.identifier || node.key;
160
+ return (
161
+ <span key={node.key} className="font-bold mr-1 select-none relative">
162
+ <HashLink id={id} align="left" kind={node.kind} />
163
+ {label}
164
+ </span>
165
+ );
166
+ },
167
+ table(node, children) {
168
+ return <table key={node.key}>{children}</table>;
169
+ },
170
+ tableRow(node, children) {
171
+ return <tr key={node.key}>{children}</tr>;
172
+ },
173
+ tableCell(node, children) {
174
+ const ifGreaterThanOne = (num?: number) => (num === 1 ? undefined : num);
175
+ const attrs = {
176
+ key: node.key,
177
+ rowSpan: ifGreaterThanOne(node.rowspan),
178
+ colSpan: ifGreaterThanOne(node.colspan),
179
+ };
180
+ if (node.header) return <th {...attrs}>{children}</th>;
181
+ return <td {...attrs}>{children}</td>;
182
+ },
183
+ subscript(node, children) {
184
+ return <sub key={node.key}>{children}</sub>;
185
+ },
186
+ superscript(node, children) {
187
+ return <sup key={node.key}>{children}</sup>;
188
+ },
189
+ abbreviation(node, children) {
190
+ return (
191
+ <abbr key={node.key} title={node.title}>
192
+ {children}
193
+ </abbr>
194
+ );
195
+ },
196
+ mystComment() {
197
+ return null;
198
+ },
199
+ comment() {
200
+ return null;
201
+ },
202
+ definitionList(node, children) {
203
+ return <dl key={node.key}>{children}</dl>;
204
+ },
205
+ definitionTerm(node, children) {
206
+ return (
207
+ <dt key={node.key}>
208
+ <strong>{children}</strong>
209
+ </dt>
210
+ );
211
+ },
212
+ definitionDescription(node, children) {
213
+ return <dd key={node.key}>{children}</dd>;
214
+ },
215
+ };
216
+
217
+ export default BASIC_RENDERERS;
package/src/cite.tsx ADDED
@@ -0,0 +1,94 @@
1
+ import classNames from 'classnames';
2
+ import { useReferences } from '@curvenote/ui-providers';
3
+ import type { NodeRenderer } from './types';
4
+ import { useState } from 'react';
5
+ import { ClickPopover } from './components/ClickPopover';
6
+ import { InlineError } from './inlineError';
7
+
8
+ function CiteChild({ label }: { label: string }) {
9
+ const references = useReferences();
10
+ const { html } = references?.cite?.data[label] ?? {};
11
+ return <div dangerouslySetInnerHTML={{ __html: html || '' }} />;
12
+ }
13
+
14
+ export const CiteGroup: NodeRenderer = (node, children) => {
15
+ return (
16
+ <span
17
+ key={node.key}
18
+ className={classNames('cite-group', {
19
+ narrative: node.kind === 'narrative',
20
+ parenthetical: node.kind === 'parenthetical',
21
+ })}
22
+ >
23
+ {children}
24
+ </span>
25
+ );
26
+ };
27
+
28
+ export const Cite: NodeRenderer = (node, children) => {
29
+ if (node.error) {
30
+ return <InlineError key={node.key} value={node.label} message={'Citation Not Found'} />;
31
+ }
32
+ return (
33
+ <ClickPopover key={node.key} card={<CiteChild label={node.label as string} />}>
34
+ {children}
35
+ </ClickPopover>
36
+ );
37
+ };
38
+
39
+ const HIDE_OVER_N_REFERENCES = 5;
40
+
41
+ export function Bibliography() {
42
+ const references = useReferences();
43
+ const { order, data } = references?.cite ?? {};
44
+ const filtered = order?.filter((l) => l);
45
+ const [hidden, setHidden] = useState(true);
46
+ if (!filtered || !data || filtered.length === 0) return null;
47
+ const refs = hidden ? filtered.slice(0, HIDE_OVER_N_REFERENCES) : filtered;
48
+ return (
49
+ <section>
50
+ {filtered.length > HIDE_OVER_N_REFERENCES && (
51
+ <button
52
+ onClick={() => setHidden(!hidden)}
53
+ className="float-right text-xs p-1 px-2 border rounded hover:border-blue-500 dark:hover:border-blue-400"
54
+ >
55
+ {hidden ? 'Show All' : 'Collapse'}
56
+ </button>
57
+ )}
58
+ <header className="text-lg font-semibold text-stone-900 dark:text-white">References</header>
59
+ <div className="text-xs mb-8 pl-3 text-stone-500 dark:text-stone-300">
60
+ <ol>
61
+ {refs.map((label) => {
62
+ const { html } = data[label];
63
+ return (
64
+ <li
65
+ key={label}
66
+ className="break-words"
67
+ id={`cite-${label}`}
68
+ dangerouslySetInnerHTML={{ __html: html || '' }}
69
+ />
70
+ );
71
+ })}
72
+ {filtered.length > HIDE_OVER_N_REFERENCES && (
73
+ <li className="list-none text-center">
74
+ <button
75
+ onClick={() => setHidden(!hidden)}
76
+ className="p-2 border rounded hover:border-blue-500 dark:hover:border-blue-400"
77
+ >
78
+ {hidden ? `Show all ${filtered.length} references` : 'Collapse references'}
79
+ </button>
80
+ </li>
81
+ )}
82
+ </ol>
83
+ </div>
84
+ </section>
85
+ );
86
+ }
87
+
88
+ const CITE_RENDERERS: Record<string, NodeRenderer> = {
89
+ citeGroup: CiteGroup,
90
+ cite: Cite,
91
+ bibliography: (node) => <Bibliography key={node.key}></Bibliography>,
92
+ };
93
+
94
+ export default CITE_RENDERERS;
package/src/code.tsx ADDED
@@ -0,0 +1,119 @@
1
+ import type { Code, InlineCode } from 'myst-spec';
2
+ import type { NodeRenderer } from './types';
3
+ import { useTheme } from '@curvenote/ui-providers';
4
+ import { LightAsync as SyntaxHighlighter } from 'react-syntax-highlighter';
5
+ import light from 'react-syntax-highlighter/dist/cjs/styles/hljs/xcode';
6
+ import dark from 'react-syntax-highlighter/dist/cjs/styles/hljs/vs2015';
7
+ import classNames from 'classnames';
8
+ import { CopyIcon } from './components/CopyIcon';
9
+
10
+ type Props = {
11
+ value: string;
12
+ lang?: string;
13
+ showCopy?: boolean;
14
+ showLineNumbers?: boolean;
15
+ startingLineNumber?: number;
16
+ emphasizeLines?: number[];
17
+ className?: string;
18
+ };
19
+
20
+ export function CodeBlock(props: Props) {
21
+ const { isLight } = useTheme();
22
+ const {
23
+ value,
24
+ lang,
25
+ emphasizeLines,
26
+ showLineNumbers,
27
+ className,
28
+ showCopy = true,
29
+ startingLineNumber = 1,
30
+ } = props;
31
+ const highlightLines = new Set(emphasizeLines);
32
+
33
+ return (
34
+ <div className={classNames('relative group not-prose overflow-auto', className)}>
35
+ <SyntaxHighlighter
36
+ language={lang}
37
+ startingLineNumber={startingLineNumber}
38
+ showLineNumbers={showLineNumbers}
39
+ style={isLight ? light : dark}
40
+ wrapLines
41
+ lineNumberContainerStyle={{
42
+ // This stops page content shifts
43
+ display: 'inline-block',
44
+ float: 'left',
45
+ minWidth: '1.25em',
46
+ paddingRight: '1em',
47
+ textAlign: 'right',
48
+ userSelect: 'none',
49
+ borderLeft: '4px solid transparent',
50
+ }}
51
+ lineProps={(line) => {
52
+ if (typeof line === 'boolean') return {};
53
+ return highlightLines.has(line)
54
+ ? ({
55
+ 'data-line-number': `${line}`,
56
+ 'data-highlight': 'true',
57
+ } as any)
58
+ : ({ 'data-line-number': `${line}` } as any);
59
+ }}
60
+ customStyle={{ padding: '0.8rem' }}
61
+ >
62
+ {value}
63
+ </SyntaxHighlighter>
64
+ {showCopy && (
65
+ <div className="absolute hidden top-1 right-1 group-hover:block">
66
+ <CopyIcon text={value} />
67
+ </div>
68
+ )}
69
+ </div>
70
+ );
71
+ }
72
+
73
+ const code: NodeRenderer<Code> = (node) => {
74
+ return (
75
+ <CodeBlock
76
+ key={node.key}
77
+ className="rounded shadow-md dark:shadow-2xl dark:shadow-neutral-900 my-8 text-sm border border-l-4 border-l-blue-400 border-gray-200 dark:border-l-blue-400 dark:border-gray-800"
78
+ value={node.value || ''}
79
+ lang={node.lang}
80
+ emphasizeLines={node.emphasizeLines}
81
+ showLineNumbers={node.showLineNumbers}
82
+ startingLineNumber={node.startingLineNumber}
83
+ />
84
+ );
85
+ };
86
+
87
+ function isColor(maybeColorHash: string): string | undefined {
88
+ if (!maybeColorHash || maybeColorHash.length > 9) return undefined;
89
+ if (!new Set([4, 7, 9]).has(maybeColorHash.length)) return undefined;
90
+ const match = /^#([0-9A-Fa-f]{3,8})$/.exec(maybeColorHash);
91
+ if (!match) return undefined;
92
+ const color = match[1];
93
+ return color;
94
+ }
95
+
96
+ const inlineCode: NodeRenderer<InlineCode> = (node, children) => {
97
+ if (isColor(node.value)) {
98
+ return (
99
+ <code
100
+ key={node.key}
101
+ className="bg-slate-100 text-slate-700 dark:bg-slate-800 dark:text-slate-100 px-1 rounded"
102
+ >
103
+ {children}
104
+ <span
105
+ style={{ backgroundColor: node.value }}
106
+ className="inline-block w-[10px] h-[10px] rounded-full ml-1"
107
+ ></span>
108
+ </code>
109
+ );
110
+ }
111
+ return <code key={node.key}>{children}</code>;
112
+ };
113
+
114
+ const CODE_RENDERERS = {
115
+ code,
116
+ inlineCode,
117
+ };
118
+
119
+ export default CODE_RENDERERS;