@izumisy/md-react-preview 0.1.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/LICENSE +21 -0
- package/README.md +116 -0
- package/app/index.html +41 -0
- package/app/src/app.tsx +219 -0
- package/app/src/code-block.tsx +182 -0
- package/app/src/main.tsx +9 -0
- package/app/src/mdx-components.tsx +103 -0
- package/app/src/preview-block.tsx +217 -0
- package/app/src/theme.tsx +45 -0
- package/app/src/virtual-modules.d.ts +26 -0
- package/dist/cli.d.mts +1 -0
- package/dist/cli.mjs +59 -0
- package/dist/index.d.mts +3370 -0
- package/dist/index.mjs +2 -0
- package/dist/server-C2ZxWhHj.mjs +275 -0
- package/package.json +60 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 IzumiSy
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
# @izumisy/md-react-preview
|
|
2
|
+
|
|
3
|
+
CLI and programmatic API for md-react-preview — a zero-config component previewer for React projects.
|
|
4
|
+
|
|
5
|
+
Drop Markdown files into `docs/` with `` ```tsx preview `` fenced blocks and get a Vite-powered dev server with live component previews and syntax-highlighted source code.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pnpm add -D @izumisy/md-react-preview
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## CLI
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npx mrp dev # Start dev server (default port 3040)
|
|
17
|
+
npx mrp build # Build static output
|
|
18
|
+
npx mrp preview # Preview the production build locally
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Writing Previews
|
|
22
|
+
|
|
23
|
+
Create a Markdown file under `docs/`:
|
|
24
|
+
|
|
25
|
+
````md
|
|
26
|
+
---
|
|
27
|
+
title: Button
|
|
28
|
+
description: A versatile button component.
|
|
29
|
+
---
|
|
30
|
+
|
|
31
|
+
## Default
|
|
32
|
+
|
|
33
|
+
```tsx preview
|
|
34
|
+
import { Button } from "./Button"
|
|
35
|
+
|
|
36
|
+
<Button variant="default">Click me</Button>
|
|
37
|
+
```
|
|
38
|
+
````
|
|
39
|
+
|
|
40
|
+
Each `` ```tsx preview `` block is rendered as a live component alongside collapsible, syntax-highlighted source code. Previews run in isolated iframes so host styles never leak.
|
|
41
|
+
|
|
42
|
+
### Preview Modes
|
|
43
|
+
|
|
44
|
+
| Mode | Syntax | Behavior |
|
|
45
|
+
|------|--------|----------|
|
|
46
|
+
| **inline** (default) | `` ```tsx preview `` | Rendered inline within the page |
|
|
47
|
+
| **standalone** | `` ```tsx preview standalone `` | Rendered in a dedicated full-viewport page at `/__preview/{blockId}` |
|
|
48
|
+
|
|
49
|
+
Standalone mode is useful for portal/overlay components (Dropdown, Sheet, Dialog) and full-page layouts that need the entire viewport.
|
|
50
|
+
|
|
51
|
+
### Block Options
|
|
52
|
+
|
|
53
|
+
Options can be set as `key="value"` pairs or boolean flags in the fence meta:
|
|
54
|
+
|
|
55
|
+
````md
|
|
56
|
+
```tsx preview wrap="row" height="200"
|
|
57
|
+
<Button>A</Button>
|
|
58
|
+
<Button>B</Button>
|
|
59
|
+
```
|
|
60
|
+
````
|
|
61
|
+
|
|
62
|
+
| Option | Description |
|
|
63
|
+
|--------|-------------|
|
|
64
|
+
| `wrap` | Layout mode: `"row"` (horizontal with gap) or `"column"` (vertical with gap) |
|
|
65
|
+
| `height` | Fixed iframe height (e.g. `"200"`) |
|
|
66
|
+
| `standalone` | Boolean flag — render in a separate full-viewport page |
|
|
67
|
+
|
|
68
|
+
### Frontmatter
|
|
69
|
+
|
|
70
|
+
| Field | Description |
|
|
71
|
+
|-------|-------------|
|
|
72
|
+
| `title` | Display name in the sidebar and page header |
|
|
73
|
+
| `description` | Short description shown below the title |
|
|
74
|
+
|
|
75
|
+
## Configuration
|
|
76
|
+
|
|
77
|
+
Create a `mrp.config.ts` at your project root:
|
|
78
|
+
|
|
79
|
+
```ts
|
|
80
|
+
import { defineConfig } from "@izumisy/md-react-preview";
|
|
81
|
+
|
|
82
|
+
export default defineConfig({
|
|
83
|
+
title: "My Component Library",
|
|
84
|
+
glob: "docs/**/*.md", // default
|
|
85
|
+
previewCss: "./src/globals.css", // optional
|
|
86
|
+
vite: {
|
|
87
|
+
plugins: [], // extra Vite plugins
|
|
88
|
+
},
|
|
89
|
+
});
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
| Option | Description |
|
|
93
|
+
|--------|-------------|
|
|
94
|
+
| `title` | Sidebar header and HTML `<title>` |
|
|
95
|
+
| `glob` | Glob pattern for Markdown files (default `docs/**/*.md`) |
|
|
96
|
+
| `previewCss` | CSS file imported into preview blocks (e.g. your design system's stylesheet) |
|
|
97
|
+
| `vite.plugins` | Additional Vite plugins (e.g. `@tailwindcss/vite`) |
|
|
98
|
+
|
|
99
|
+
## Programmatic API
|
|
100
|
+
|
|
101
|
+
```ts
|
|
102
|
+
import { startDev, runBuild, defineConfig, createPreviewerViteConfig } from "@izumisy/md-react-preview";
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
| Export | Description |
|
|
106
|
+
|--------|-------------|
|
|
107
|
+
| `defineConfig(config)` | Type helper for `mrp.config.ts` |
|
|
108
|
+
| `startDev(options)` | Start the Vite dev server |
|
|
109
|
+
| `runBuild(options)` | Run the production build |
|
|
110
|
+
| `runPreview(options)` | Preview the production build |
|
|
111
|
+
| `createPreviewerViteConfig(options)` | Generate the Vite `InlineConfig` used internally |
|
|
112
|
+
| `extractPreviewBlocks(source)` | Parse `` ```tsx preview `` blocks from a Markdown string |
|
|
113
|
+
|
|
114
|
+
## License
|
|
115
|
+
|
|
116
|
+
MIT
|
package/app/index.html
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8" />
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
6
|
+
<title>md-react-preview</title>
|
|
7
|
+
<style>
|
|
8
|
+
*, *::before, *::after { box-sizing: border-box; margin: 0; }
|
|
9
|
+
html, html.light {
|
|
10
|
+
--ms-bg: #ffffff;
|
|
11
|
+
--ms-fg: #1a1a1a;
|
|
12
|
+
--ms-fg-muted: #888888;
|
|
13
|
+
--ms-border: #e5e5e5;
|
|
14
|
+
--ms-sidebar-active: #f0f0f0;
|
|
15
|
+
--ms-code-bg: #f5f5f5;
|
|
16
|
+
}
|
|
17
|
+
html.dark {
|
|
18
|
+
--ms-bg: #1a1a1a;
|
|
19
|
+
--ms-fg: #e5e5e5;
|
|
20
|
+
--ms-fg-muted: #777777;
|
|
21
|
+
--ms-border: #333333;
|
|
22
|
+
--ms-sidebar-active: #2a2a2a;
|
|
23
|
+
--ms-code-bg: #2a2a2a;
|
|
24
|
+
}
|
|
25
|
+
@media (prefers-color-scheme: dark) {
|
|
26
|
+
html:not(.light) {
|
|
27
|
+
--ms-bg: #1a1a1a;
|
|
28
|
+
--ms-fg: #e5e5e5;
|
|
29
|
+
--ms-fg-muted: #777777;
|
|
30
|
+
--ms-border: #333333;
|
|
31
|
+
--ms-sidebar-active: #2a2a2a;
|
|
32
|
+
--ms-code-bg: #2a2a2a;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
</style>
|
|
36
|
+
</head>
|
|
37
|
+
<body>
|
|
38
|
+
<div id="root"></div>
|
|
39
|
+
<script type="module" src="/src/main.tsx"></script>
|
|
40
|
+
</body>
|
|
41
|
+
</html>
|
package/app/src/app.tsx
ADDED
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import { useCallback, useEffect, useRef, useSyncExternalStore } from "react";
|
|
2
|
+
import { MDXProvider } from "@mdx-js/react";
|
|
3
|
+
import { entries, type PreviewEntry } from "virtual:previewer-entries";
|
|
4
|
+
import { mdxComponents } from "./mdx-components";
|
|
5
|
+
import { ThemeProvider, useTheme } from "./theme";
|
|
6
|
+
|
|
7
|
+
const title: string = __PREVIEWER_TITLE__;
|
|
8
|
+
|
|
9
|
+
function getPathname() {
|
|
10
|
+
return decodeURIComponent(window.location.pathname).replace(/^\//, "");
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function usePathname() {
|
|
14
|
+
const subscribe = useCallback((cb: () => void) => {
|
|
15
|
+
window.addEventListener("popstate", cb);
|
|
16
|
+
return () => window.removeEventListener("popstate", cb);
|
|
17
|
+
}, []);
|
|
18
|
+
return useSyncExternalStore(subscribe, getPathname);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function Sidebar({
|
|
22
|
+
entries: items,
|
|
23
|
+
selected,
|
|
24
|
+
onSelect,
|
|
25
|
+
}: {
|
|
26
|
+
entries: PreviewEntry[];
|
|
27
|
+
selected: string | null;
|
|
28
|
+
onSelect: (name: string) => void;
|
|
29
|
+
}) {
|
|
30
|
+
return (
|
|
31
|
+
<nav
|
|
32
|
+
style={{
|
|
33
|
+
width: 220,
|
|
34
|
+
borderRight: "1px solid var(--ms-border)",
|
|
35
|
+
padding: "12px 0",
|
|
36
|
+
overflowY: "auto",
|
|
37
|
+
flexShrink: 0,
|
|
38
|
+
}}
|
|
39
|
+
>
|
|
40
|
+
{items.map((entry) => (
|
|
41
|
+
<button
|
|
42
|
+
key={entry.name}
|
|
43
|
+
onClick={() => onSelect(entry.name)}
|
|
44
|
+
style={{
|
|
45
|
+
display: "block",
|
|
46
|
+
width: "100%",
|
|
47
|
+
textAlign: "left",
|
|
48
|
+
padding: "6px 16px",
|
|
49
|
+
fontSize: 14,
|
|
50
|
+
border: "none",
|
|
51
|
+
cursor: "pointer",
|
|
52
|
+
backgroundColor: selected === entry.name ? "var(--ms-sidebar-active)" : "transparent",
|
|
53
|
+
fontWeight: selected === entry.name ? 600 : 400,
|
|
54
|
+
color: "var(--ms-fg)",
|
|
55
|
+
fontFamily: "inherit",
|
|
56
|
+
}}
|
|
57
|
+
>
|
|
58
|
+
{entry.name}
|
|
59
|
+
</button>
|
|
60
|
+
))}
|
|
61
|
+
</nav>
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function PreviewContent({ entry }: { entry: PreviewEntry }) {
|
|
66
|
+
const { Component } = entry;
|
|
67
|
+
const scrollRef = useRef<HTMLDivElement>(null);
|
|
68
|
+
|
|
69
|
+
useEffect(() => {
|
|
70
|
+
scrollRef.current?.scrollTo(0, 0);
|
|
71
|
+
}, [entry.name]);
|
|
72
|
+
|
|
73
|
+
return (
|
|
74
|
+
<div
|
|
75
|
+
ref={scrollRef}
|
|
76
|
+
style={{
|
|
77
|
+
flex: 1,
|
|
78
|
+
overflowY: "auto",
|
|
79
|
+
scrollbarWidth: "none",
|
|
80
|
+
}}
|
|
81
|
+
>
|
|
82
|
+
<div
|
|
83
|
+
style={{
|
|
84
|
+
maxWidth: 860,
|
|
85
|
+
margin: "0 auto",
|
|
86
|
+
padding: "24px 40px 80px",
|
|
87
|
+
}}
|
|
88
|
+
>
|
|
89
|
+
<Component />
|
|
90
|
+
</div>
|
|
91
|
+
</div>
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function Header() {
|
|
96
|
+
const { colorScheme, toggle } = useTheme();
|
|
97
|
+
return (
|
|
98
|
+
<header
|
|
99
|
+
style={{
|
|
100
|
+
display: "flex",
|
|
101
|
+
alignItems: "center",
|
|
102
|
+
justifyContent: "space-between",
|
|
103
|
+
padding: "0 20px",
|
|
104
|
+
height: 48,
|
|
105
|
+
borderBottom: "1px solid var(--ms-border)",
|
|
106
|
+
flexShrink: 0,
|
|
107
|
+
}}
|
|
108
|
+
>
|
|
109
|
+
<span style={{ fontSize: 15, fontWeight: 700 }}>{title}</span>
|
|
110
|
+
<button
|
|
111
|
+
onClick={toggle}
|
|
112
|
+
aria-label={`Switch to ${colorScheme === "light" ? "dark" : "light"} theme`}
|
|
113
|
+
style={{
|
|
114
|
+
background: "none",
|
|
115
|
+
border: "none",
|
|
116
|
+
cursor: "pointer",
|
|
117
|
+
padding: 4,
|
|
118
|
+
color: "var(--ms-fg)",
|
|
119
|
+
display: "flex",
|
|
120
|
+
alignItems: "center",
|
|
121
|
+
}}
|
|
122
|
+
>
|
|
123
|
+
{colorScheme === "light" ? (
|
|
124
|
+
<svg
|
|
125
|
+
width="18"
|
|
126
|
+
height="18"
|
|
127
|
+
viewBox="0 0 24 24"
|
|
128
|
+
fill="none"
|
|
129
|
+
stroke="currentColor"
|
|
130
|
+
strokeWidth="2"
|
|
131
|
+
strokeLinecap="round"
|
|
132
|
+
strokeLinejoin="round"
|
|
133
|
+
>
|
|
134
|
+
<path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z" />
|
|
135
|
+
</svg>
|
|
136
|
+
) : (
|
|
137
|
+
<svg
|
|
138
|
+
width="18"
|
|
139
|
+
height="18"
|
|
140
|
+
viewBox="0 0 24 24"
|
|
141
|
+
fill="none"
|
|
142
|
+
stroke="currentColor"
|
|
143
|
+
strokeWidth="2"
|
|
144
|
+
strokeLinecap="round"
|
|
145
|
+
strokeLinejoin="round"
|
|
146
|
+
>
|
|
147
|
+
<circle cx="12" cy="12" r="5" />
|
|
148
|
+
<line x1="12" y1="1" x2="12" y2="3" />
|
|
149
|
+
<line x1="12" y1="21" x2="12" y2="23" />
|
|
150
|
+
<line x1="4.22" y1="4.22" x2="5.64" y2="5.64" />
|
|
151
|
+
<line x1="18.36" y1="18.36" x2="19.78" y2="19.78" />
|
|
152
|
+
<line x1="1" y1="12" x2="3" y2="12" />
|
|
153
|
+
<line x1="21" y1="12" x2="23" y2="12" />
|
|
154
|
+
<line x1="4.22" y1="19.78" x2="5.64" y2="18.36" />
|
|
155
|
+
<line x1="18.36" y1="5.64" x2="19.78" y2="4.22" />
|
|
156
|
+
</svg>
|
|
157
|
+
)}
|
|
158
|
+
</button>
|
|
159
|
+
</header>
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export function App() {
|
|
164
|
+
const pathname = usePathname();
|
|
165
|
+
const selected = pathname || entries[0]?.name || null;
|
|
166
|
+
const current = entries.find((e: PreviewEntry) => e.name === selected);
|
|
167
|
+
|
|
168
|
+
// Redirect bare "/" to the first entry's path
|
|
169
|
+
useEffect(() => {
|
|
170
|
+
if (!pathname && entries[0]) {
|
|
171
|
+
history.replaceState(null, "", `/${encodeURIComponent(entries[0].name)}`);
|
|
172
|
+
}
|
|
173
|
+
}, [pathname]);
|
|
174
|
+
|
|
175
|
+
const setSelected = useCallback((name: string) => {
|
|
176
|
+
history.pushState(null, "", `/${encodeURIComponent(name)}`);
|
|
177
|
+
// pushState doesn't fire popstate, so dispatch one manually to trigger re-render
|
|
178
|
+
window.dispatchEvent(new PopStateEvent("popstate"));
|
|
179
|
+
}, []);
|
|
180
|
+
|
|
181
|
+
return (
|
|
182
|
+
<ThemeProvider>
|
|
183
|
+
<MDXProvider components={mdxComponents}>
|
|
184
|
+
<div
|
|
185
|
+
style={{
|
|
186
|
+
display: "flex",
|
|
187
|
+
flexDirection: "column",
|
|
188
|
+
height: "100vh",
|
|
189
|
+
fontFamily: "system-ui, sans-serif",
|
|
190
|
+
backgroundColor: "var(--ms-bg)",
|
|
191
|
+
color: "var(--ms-fg)",
|
|
192
|
+
}}
|
|
193
|
+
>
|
|
194
|
+
<Header />
|
|
195
|
+
<div style={{ display: "flex", flex: 1, overflow: "hidden" }}>
|
|
196
|
+
<Sidebar entries={entries} selected={selected} onSelect={setSelected} />
|
|
197
|
+
{current ? (
|
|
198
|
+
<PreviewContent entry={current} />
|
|
199
|
+
) : (
|
|
200
|
+
<div
|
|
201
|
+
style={{
|
|
202
|
+
flex: 1,
|
|
203
|
+
display: "flex",
|
|
204
|
+
alignItems: "center",
|
|
205
|
+
justifyContent: "center",
|
|
206
|
+
color: "var(--ms-fg-muted)",
|
|
207
|
+
}}
|
|
208
|
+
>
|
|
209
|
+
{entries.length === 0
|
|
210
|
+
? "No .md files found."
|
|
211
|
+
: "Select a component from the sidebar."}
|
|
212
|
+
</div>
|
|
213
|
+
)}
|
|
214
|
+
</div>
|
|
215
|
+
</div>
|
|
216
|
+
</MDXProvider>
|
|
217
|
+
</ThemeProvider>
|
|
218
|
+
);
|
|
219
|
+
}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { useCallback, useEffect, useState } from "react";
|
|
2
|
+
import { createHighlighterCore, type HighlighterCore } from "shiki/core";
|
|
3
|
+
import { createJavaScriptRegexEngine } from "shiki/engine/javascript";
|
|
4
|
+
import { useTheme } from "./theme";
|
|
5
|
+
|
|
6
|
+
let highlighterPromise: Promise<HighlighterCore> | undefined;
|
|
7
|
+
|
|
8
|
+
function getHighlighter(): Promise<HighlighterCore> {
|
|
9
|
+
if (!highlighterPromise) {
|
|
10
|
+
highlighterPromise = createHighlighterCore({
|
|
11
|
+
themes: [import("@shikijs/themes/github-dark"), import("@shikijs/themes/github-light")],
|
|
12
|
+
langs: [
|
|
13
|
+
import("@shikijs/langs/tsx"),
|
|
14
|
+
import("@shikijs/langs/jsx"),
|
|
15
|
+
import("@shikijs/langs/typescript"),
|
|
16
|
+
import("@shikijs/langs/javascript"),
|
|
17
|
+
import("@shikijs/langs/css"),
|
|
18
|
+
import("@shikijs/langs/html"),
|
|
19
|
+
import("@shikijs/langs/json"),
|
|
20
|
+
import("@shikijs/langs/shellscript"),
|
|
21
|
+
],
|
|
22
|
+
engine: createJavaScriptRegexEngine(),
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
return highlighterPromise;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function CopyButton({ text }: { text: string }) {
|
|
29
|
+
const [copied, setCopied] = useState(false);
|
|
30
|
+
|
|
31
|
+
const handleCopy = useCallback(() => {
|
|
32
|
+
navigator.clipboard.writeText(text).then(() => {
|
|
33
|
+
setCopied(true);
|
|
34
|
+
setTimeout(() => setCopied(false), 2000);
|
|
35
|
+
});
|
|
36
|
+
}, [text]);
|
|
37
|
+
|
|
38
|
+
return (
|
|
39
|
+
<button
|
|
40
|
+
onClick={handleCopy}
|
|
41
|
+
aria-label="Copy code"
|
|
42
|
+
style={{
|
|
43
|
+
position: "absolute",
|
|
44
|
+
top: 6,
|
|
45
|
+
right: 6,
|
|
46
|
+
display: "flex",
|
|
47
|
+
alignItems: "center",
|
|
48
|
+
justifyContent: "center",
|
|
49
|
+
width: 24,
|
|
50
|
+
height: 24,
|
|
51
|
+
border: "1px solid var(--ms-border)",
|
|
52
|
+
borderRadius: 4,
|
|
53
|
+
backgroundColor: "var(--ms-bg)",
|
|
54
|
+
cursor: "pointer",
|
|
55
|
+
color: "var(--ms-fg-muted)",
|
|
56
|
+
opacity: 0.6,
|
|
57
|
+
transition: "opacity 0.15s",
|
|
58
|
+
}}
|
|
59
|
+
onMouseEnter={(e) => {
|
|
60
|
+
e.currentTarget.style.opacity = "1";
|
|
61
|
+
}}
|
|
62
|
+
onMouseLeave={(e) => {
|
|
63
|
+
e.currentTarget.style.opacity = "0.6";
|
|
64
|
+
}}
|
|
65
|
+
>
|
|
66
|
+
{copied ? (
|
|
67
|
+
<svg
|
|
68
|
+
width="14"
|
|
69
|
+
height="14"
|
|
70
|
+
viewBox="0 0 16 16"
|
|
71
|
+
fill="none"
|
|
72
|
+
stroke="currentColor"
|
|
73
|
+
strokeWidth="1.5"
|
|
74
|
+
strokeLinecap="round"
|
|
75
|
+
strokeLinejoin="round"
|
|
76
|
+
>
|
|
77
|
+
<path d="M13.25 4.75l-6.5 6.5-3.5-3.5" />
|
|
78
|
+
</svg>
|
|
79
|
+
) : (
|
|
80
|
+
<svg
|
|
81
|
+
width="14"
|
|
82
|
+
height="14"
|
|
83
|
+
viewBox="0 0 16 16"
|
|
84
|
+
fill="none"
|
|
85
|
+
stroke="currentColor"
|
|
86
|
+
strokeWidth="1.5"
|
|
87
|
+
strokeLinecap="round"
|
|
88
|
+
strokeLinejoin="round"
|
|
89
|
+
>
|
|
90
|
+
<rect x="5.5" y="5.5" width="8" height="8" rx="1.5" />
|
|
91
|
+
<path d="M10.5 5.5V3a1.5 1.5 0 00-1.5-1.5H3A1.5 1.5 0 001.5 3v6A1.5 1.5 0 003 10.5h2.5" />
|
|
92
|
+
</svg>
|
|
93
|
+
)}
|
|
94
|
+
</button>
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export function CodeBlock({
|
|
99
|
+
children,
|
|
100
|
+
className,
|
|
101
|
+
noBorderRadius,
|
|
102
|
+
}: {
|
|
103
|
+
children: string;
|
|
104
|
+
className?: string;
|
|
105
|
+
noBorderRadius?: boolean;
|
|
106
|
+
}) {
|
|
107
|
+
const lang = className?.replace("language-", "") ?? "";
|
|
108
|
+
const radius = noBorderRadius ? 0 : 8;
|
|
109
|
+
const border = noBorderRadius ? "none" : "1px solid var(--ms-border)";
|
|
110
|
+
const { colorScheme } = useTheme();
|
|
111
|
+
const [html, setHtml] = useState<string | null>(null);
|
|
112
|
+
|
|
113
|
+
useEffect(() => {
|
|
114
|
+
if (!lang) return;
|
|
115
|
+
let cancelled = false;
|
|
116
|
+
getHighlighter()
|
|
117
|
+
.then(async (highlighter) => {
|
|
118
|
+
if (!highlighter.getLoadedLanguages().includes(lang)) {
|
|
119
|
+
// Language not pre-loaded – fall back to plain text
|
|
120
|
+
return null;
|
|
121
|
+
}
|
|
122
|
+
return highlighter.codeToHtml(children, {
|
|
123
|
+
lang,
|
|
124
|
+
theme: colorScheme === "dark" ? "github-dark" : "github-light",
|
|
125
|
+
transformers: [
|
|
126
|
+
{
|
|
127
|
+
pre(node) {
|
|
128
|
+
node.properties.style = "margin:0;padding:10px";
|
|
129
|
+
},
|
|
130
|
+
},
|
|
131
|
+
],
|
|
132
|
+
});
|
|
133
|
+
})
|
|
134
|
+
.then((result) => {
|
|
135
|
+
if (!cancelled && result) setHtml(result);
|
|
136
|
+
});
|
|
137
|
+
return () => {
|
|
138
|
+
cancelled = true;
|
|
139
|
+
};
|
|
140
|
+
}, [children, lang, colorScheme]);
|
|
141
|
+
|
|
142
|
+
if (html) {
|
|
143
|
+
return (
|
|
144
|
+
<div
|
|
145
|
+
style={{
|
|
146
|
+
position: "relative",
|
|
147
|
+
background: "var(--ms-code-bg)",
|
|
148
|
+
borderRadius: radius,
|
|
149
|
+
overflowX: "auto",
|
|
150
|
+
overflowY: "hidden",
|
|
151
|
+
fontSize: 13,
|
|
152
|
+
lineHeight: 1.5,
|
|
153
|
+
border,
|
|
154
|
+
}}
|
|
155
|
+
>
|
|
156
|
+
<CopyButton text={children} />
|
|
157
|
+
<div dangerouslySetInnerHTML={{ __html: html }} />
|
|
158
|
+
</div>
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
return (
|
|
163
|
+
<div style={{ position: "relative" }}>
|
|
164
|
+
<CopyButton text={children} />
|
|
165
|
+
<pre
|
|
166
|
+
style={{
|
|
167
|
+
background: "var(--ms-code-bg)",
|
|
168
|
+
padding: 10,
|
|
169
|
+
borderRadius: radius,
|
|
170
|
+
overflowX: "auto",
|
|
171
|
+
overflowY: "hidden",
|
|
172
|
+
fontSize: 13,
|
|
173
|
+
lineHeight: 1.5,
|
|
174
|
+
border,
|
|
175
|
+
margin: 0,
|
|
176
|
+
}}
|
|
177
|
+
>
|
|
178
|
+
<code data-language={lang}>{children}</code>
|
|
179
|
+
</pre>
|
|
180
|
+
</div>
|
|
181
|
+
);
|
|
182
|
+
}
|
package/app/src/main.tsx
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { type ReactNode, Children } from "react";
|
|
2
|
+
import { CodeBlock } from "./code-block";
|
|
3
|
+
import { PreviewBlock } from "./preview-block";
|
|
4
|
+
|
|
5
|
+
function slugify(children: ReactNode): string {
|
|
6
|
+
const text = Children.toArray(children)
|
|
7
|
+
.map((child) => (typeof child === "string" ? child : ""))
|
|
8
|
+
.join("");
|
|
9
|
+
return text
|
|
10
|
+
.toLowerCase()
|
|
11
|
+
.replace(/[^a-z0-9\u3000-\u9fff]+/g, "-")
|
|
12
|
+
.replace(/^-|-$/g, "");
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const mdxComponents = {
|
|
16
|
+
h1: ({ children }: { children?: ReactNode }) => (
|
|
17
|
+
<h1 style={{ fontSize: 28, fontWeight: 700, margin: "0 0 16px" }}>{children}</h1>
|
|
18
|
+
),
|
|
19
|
+
h2: ({ children }: { children?: ReactNode }) => {
|
|
20
|
+
const id = slugify(children);
|
|
21
|
+
return (
|
|
22
|
+
<h2
|
|
23
|
+
id={id}
|
|
24
|
+
style={{
|
|
25
|
+
fontSize: 20,
|
|
26
|
+
fontWeight: 600,
|
|
27
|
+
margin: "24px 0 12px",
|
|
28
|
+
borderBottom: "1px solid var(--ms-border)",
|
|
29
|
+
paddingBottom: 8,
|
|
30
|
+
}}
|
|
31
|
+
>
|
|
32
|
+
{children}
|
|
33
|
+
</h2>
|
|
34
|
+
);
|
|
35
|
+
},
|
|
36
|
+
h3: ({ children }: { children?: ReactNode }) => {
|
|
37
|
+
const id = slugify(children);
|
|
38
|
+
return (
|
|
39
|
+
<h3 id={id} style={{ fontSize: 16, fontWeight: 600, margin: "20px 0 8px" }}>
|
|
40
|
+
{children}
|
|
41
|
+
</h3>
|
|
42
|
+
);
|
|
43
|
+
},
|
|
44
|
+
p: ({ children }: { children?: ReactNode }) => (
|
|
45
|
+
<p style={{ margin: "8px 0", lineHeight: 1.6 }}>{children}</p>
|
|
46
|
+
),
|
|
47
|
+
pre: ({
|
|
48
|
+
children,
|
|
49
|
+
}: {
|
|
50
|
+
children?: React.ReactElement<{ children?: string; className?: string }>;
|
|
51
|
+
}) => {
|
|
52
|
+
const code = children?.props?.children ?? "";
|
|
53
|
+
const className = children?.props?.className ?? "";
|
|
54
|
+
return <CodeBlock className={className}>{code}</CodeBlock>;
|
|
55
|
+
},
|
|
56
|
+
table: ({ children }: { children?: ReactNode }) => (
|
|
57
|
+
<table
|
|
58
|
+
style={{
|
|
59
|
+
borderCollapse: "collapse",
|
|
60
|
+
width: "100%",
|
|
61
|
+
margin: "16px 0",
|
|
62
|
+
fontSize: 14,
|
|
63
|
+
}}
|
|
64
|
+
>
|
|
65
|
+
{children}
|
|
66
|
+
</table>
|
|
67
|
+
),
|
|
68
|
+
thead: ({ children }: { children?: ReactNode }) => <thead>{children}</thead>,
|
|
69
|
+
tbody: ({ children }: { children?: ReactNode }) => <tbody>{children}</tbody>,
|
|
70
|
+
tr: ({ children }: { children?: ReactNode }) => (
|
|
71
|
+
<tr style={{ borderBottom: "1px solid var(--ms-border)" }}>{children}</tr>
|
|
72
|
+
),
|
|
73
|
+
th: ({ children }: { children?: ReactNode }) => (
|
|
74
|
+
<th
|
|
75
|
+
style={{
|
|
76
|
+
background: "var(--ms-code-bg)",
|
|
77
|
+
padding: "8px 12px",
|
|
78
|
+
textAlign: "left" as const,
|
|
79
|
+
borderBottom: "2px solid var(--ms-border)",
|
|
80
|
+
fontWeight: 600,
|
|
81
|
+
}}
|
|
82
|
+
>
|
|
83
|
+
{children}
|
|
84
|
+
</th>
|
|
85
|
+
),
|
|
86
|
+
td: ({ children }: { children?: ReactNode }) => (
|
|
87
|
+
<td style={{ padding: "8px 12px", verticalAlign: "top" as const }}>{children}</td>
|
|
88
|
+
),
|
|
89
|
+
code: ({ children }: { children?: ReactNode }) => (
|
|
90
|
+
<code
|
|
91
|
+
style={{
|
|
92
|
+
background: "var(--ms-code-bg)",
|
|
93
|
+
padding: "2px 6px",
|
|
94
|
+
borderRadius: 4,
|
|
95
|
+
fontSize: 13,
|
|
96
|
+
fontFamily: "monospace",
|
|
97
|
+
}}
|
|
98
|
+
>
|
|
99
|
+
{children}
|
|
100
|
+
</code>
|
|
101
|
+
),
|
|
102
|
+
PreviewBlock,
|
|
103
|
+
};
|