@thedatagrid/docs-kit 0.0.0-stage → 0.2.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/CHANGELOG.md +20 -0
- package/README.md +224 -2
- package/dist/code/demoFiles.d.ts +14 -0
- package/dist/code/demoFiles.d.ts.map +1 -0
- package/dist/code/demoFiles.js +46 -0
- package/dist/code/demoFiles.js.map +1 -0
- package/dist/code/heights.d.ts +17 -0
- package/dist/code/heights.d.ts.map +1 -0
- package/dist/code/heights.js +40 -0
- package/dist/code/heights.js.map +1 -0
- package/dist/code/highlightSpec.d.ts +34 -0
- package/dist/code/highlightSpec.d.ts.map +1 -0
- package/dist/code/highlightSpec.js +119 -0
- package/dist/code/highlightSpec.js.map +1 -0
- package/dist/code/imports.d.ts +5 -0
- package/dist/code/imports.d.ts.map +1 -0
- package/dist/code/imports.js +17 -0
- package/dist/code/imports.js.map +1 -0
- package/dist/code/index.d.ts +8 -0
- package/dist/code/index.d.ts.map +1 -0
- package/dist/code/index.js +8 -0
- package/dist/code/index.js.map +1 -0
- package/dist/code/lineRanges.d.ts +5 -0
- package/dist/code/lineRanges.d.ts.map +1 -0
- package/dist/code/lineRanges.js +35 -0
- package/dist/code/lineRanges.js.map +1 -0
- package/dist/code/regions.d.ts +53 -0
- package/dist/code/regions.d.ts.map +1 -0
- package/dist/code/regions.js +237 -0
- package/dist/code/regions.js.map +1 -0
- package/dist/code/shiki.d.ts +58 -0
- package/dist/code/shiki.d.ts.map +1 -0
- package/dist/code/shiki.js +244 -0
- package/dist/code/shiki.js.map +1 -0
- package/dist/demos/build.d.ts +17 -0
- package/dist/demos/build.d.ts.map +1 -0
- package/dist/demos/build.js +426 -0
- package/dist/demos/build.js.map +1 -0
- package/dist/demos/cli.d.ts +3 -0
- package/dist/demos/cli.d.ts.map +1 -0
- package/dist/demos/cli.js +102 -0
- package/dist/demos/cli.js.map +1 -0
- package/dist/demos/client.d.ts +27 -0
- package/dist/demos/client.d.ts.map +1 -0
- package/dist/demos/client.js +61 -0
- package/dist/demos/client.js.map +1 -0
- package/dist/demos/compile/core.d.ts +64 -0
- package/dist/demos/compile/core.d.ts.map +1 -0
- package/dist/demos/compile/core.js +174 -0
- package/dist/demos/compile/core.js.map +1 -0
- package/dist/demos/config.d.ts +108 -0
- package/dist/demos/config.d.ts.map +1 -0
- package/dist/demos/config.js +4 -0
- package/dist/demos/config.js.map +1 -0
- package/dist/demos/devEvents.d.ts +13 -0
- package/dist/demos/devEvents.d.ts.map +1 -0
- package/dist/demos/devEvents.js +45 -0
- package/dist/demos/devEvents.js.map +1 -0
- package/dist/demos/html.d.ts +21 -0
- package/dist/demos/html.d.ts.map +1 -0
- package/dist/demos/html.js +57 -0
- package/dist/demos/html.js.map +1 -0
- package/dist/demos/index.d.ts +8 -0
- package/dist/demos/index.d.ts.map +1 -0
- package/dist/demos/index.js +6 -0
- package/dist/demos/index.js.map +1 -0
- package/dist/demos/pages.d.ts +15 -0
- package/dist/demos/pages.d.ts.map +1 -0
- package/dist/demos/pages.js +71 -0
- package/dist/demos/pages.js.map +1 -0
- package/dist/demos/runtime/iframe.d.ts +50 -0
- package/dist/demos/runtime/iframe.d.ts.map +1 -0
- package/dist/demos/runtime/iframe.js +245 -0
- package/dist/demos/runtime/iframe.js.map +1 -0
- package/dist/demos/runtime/react.d.ts +3 -0
- package/dist/demos/runtime/react.d.ts.map +1 -0
- package/dist/demos/runtime/react.js +17 -0
- package/dist/demos/runtime/react.js.map +1 -0
- package/dist/demos/slug.d.ts +3 -0
- package/dist/demos/slug.d.ts.map +1 -0
- package/dist/demos/slug.js +11 -0
- package/dist/demos/slug.js.map +1 -0
- package/dist/demos/vendor.d.ts +32 -0
- package/dist/demos/vendor.d.ts.map +1 -0
- package/dist/demos/vendor.js +127 -0
- package/dist/demos/vendor.js.map +1 -0
- package/dist/demos/worker/compile.worker.d.ts +2 -0
- package/dist/demos/worker/compile.worker.d.ts.map +1 -0
- package/dist/demos/worker/compile.worker.js +27 -0
- package/dist/demos/worker/compile.worker.js.map +1 -0
- package/dist/editor/install.d.ts +15 -0
- package/dist/editor/install.d.ts.map +1 -0
- package/dist/editor/install.js +71 -0
- package/dist/editor/install.js.map +1 -0
- package/dist/fork/codesandbox.d.ts +39 -0
- package/dist/fork/codesandbox.d.ts.map +1 -0
- package/dist/fork/codesandbox.js +156 -0
- package/dist/fork/codesandbox.js.map +1 -0
- package/dist/fork/index.d.ts +2 -0
- package/dist/fork/index.d.ts.map +1 -0
- package/dist/fork/index.js +2 -0
- package/dist/fork/index.js.map +1 -0
- package/dist/react/Demo.d.ts +80 -0
- package/dist/react/Demo.d.ts.map +1 -0
- package/dist/react/Demo.js +156 -0
- package/dist/react/Demo.js.map +1 -0
- package/dist/react/DemoIframe.d.ts +22 -0
- package/dist/react/DemoIframe.d.ts.map +1 -0
- package/dist/react/DemoIframe.js +123 -0
- package/dist/react/DemoIframe.js.map +1 -0
- package/dist/react/DemoShell.d.ts +47 -0
- package/dist/react/DemoShell.d.ts.map +1 -0
- package/dist/react/DemoShell.js +341 -0
- package/dist/react/DemoShell.js.map +1 -0
- package/dist/react/devEvents.d.ts +17 -0
- package/dist/react/devEvents.d.ts.map +1 -0
- package/dist/react/devEvents.js +30 -0
- package/dist/react/devEvents.js.map +1 -0
- package/dist/react/editor.d.ts +17 -0
- package/dist/react/editor.d.ts.map +1 -0
- package/dist/react/editor.js +158 -0
- package/dist/react/editor.js.map +1 -0
- package/dist/react/fork.d.ts +10 -0
- package/dist/react/fork.d.ts.map +1 -0
- package/dist/react/fork.js +30 -0
- package/dist/react/fork.js.map +1 -0
- package/dist/react/index.d.ts +4 -0
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/index.js +4 -0
- package/dist/react/index.js.map +1 -0
- package/dist/react/styles.css +471 -0
- package/dist/remark/index.d.ts +3 -0
- package/dist/remark/index.d.ts.map +1 -0
- package/dist/remark/index.js +3 -0
- package/dist/remark/index.js.map +1 -0
- package/dist/remark/meta.d.ts +7 -0
- package/dist/remark/meta.d.ts.map +1 -0
- package/dist/remark/meta.js +20 -0
- package/dist/remark/meta.js.map +1 -0
- package/dist/remark/remarkDemoFences.d.ts +77 -0
- package/dist/remark/remarkDemoFences.d.ts.map +1 -0
- package/dist/remark/remarkDemoFences.js +259 -0
- package/dist/remark/remarkDemoFences.js.map +1 -0
- package/editor-extension/README.md +23 -0
- package/editor-extension/extension.js +40 -0
- package/editor-extension/links.js +137 -0
- package/editor-extension/package.json +19 -0
- package/package.json +107 -4
- package/src/code/demoFiles.ts +54 -0
- package/src/code/heights.ts +47 -0
- package/src/code/highlightSpec.ts +139 -0
- package/src/code/imports.ts +18 -0
- package/src/code/index.ts +46 -0
- package/src/code/lineRanges.ts +31 -0
- package/src/code/regions.ts +270 -0
- package/src/code/shiki.ts +271 -0
- package/src/demos/build.ts +477 -0
- package/src/demos/cli.ts +97 -0
- package/src/demos/client.ts +82 -0
- package/src/demos/compile/core.ts +228 -0
- package/src/demos/config.ts +106 -0
- package/src/demos/devEvents.ts +45 -0
- package/src/demos/html.ts +74 -0
- package/src/demos/index.ts +25 -0
- package/src/demos/pages.ts +79 -0
- package/src/demos/runtime/iframe.ts +262 -0
- package/src/demos/runtime/react.ts +19 -0
- package/src/demos/slug.ts +10 -0
- package/src/demos/vendor.ts +142 -0
- package/src/demos/worker/compile.worker.ts +35 -0
- package/src/editor/install.ts +85 -0
- package/src/fork/codesandbox.ts +189 -0
- package/src/fork/index.ts +10 -0
- package/src/react/Demo.tsx +250 -0
- package/src/react/DemoIframe.tsx +165 -0
- package/src/react/DemoShell.tsx +592 -0
- package/src/react/devEvents.ts +34 -0
- package/src/react/editor.ts +178 -0
- package/src/react/fork.ts +31 -0
- package/src/react/index.ts +10 -0
- package/src/react/styles.css +471 -0
- package/src/remark/index.ts +2 -0
- package/src/remark/meta.ts +20 -0
- package/src/remark/remarkDemoFences.ts +330 -0
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { visit } from 'unist-util-visit';
|
|
4
|
+
import { collectDemoFiles } from "../code/demoFiles.js";
|
|
5
|
+
import { formatHighlightSpec } from "../code/highlightSpec.js";
|
|
6
|
+
import { parseDemoHeight } from "../code/heights.js";
|
|
7
|
+
import { formatLineRanges } from "../code/lineRanges.js";
|
|
8
|
+
import { applySourceReplacements, extractCodeMarkers, sliceRegion, stripCodeMarkers } from "../code/regions.js";
|
|
9
|
+
import { parseFenceMeta } from "./meta.js";
|
|
10
|
+
const TYPECHECKED_LANGS = new Set(['ts', 'tsx', 'js', 'jsx', 'typescript', 'javascript']);
|
|
11
|
+
/**
|
|
12
|
+
* Remark plugin for file-based fences.
|
|
13
|
+
*
|
|
14
|
+
* ```tsx file="basic.demo.tsx" live height="large" title="Basic grid"
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* - `file="..."` is relative to the page's folder; `file="/docs/x.tsx"` to the
|
|
18
|
+
* content root. A missing file throws at compile time, so a bad reference
|
|
19
|
+
* fails the build.
|
|
20
|
+
* - `live` replaces the fence with `<Demo name="…">` (prebuilt iframe preview
|
|
21
|
+
* + code tabs). The file must be a demo entry (`*.demo.tsx`) inside the
|
|
22
|
+
* content folder. `live=off` shows the code tab first.
|
|
23
|
+
* - `region="name"` shows a `#region name` … `#endregion` slice (dedented).
|
|
24
|
+
* With `live`, the preview runs the whole demo and only the entry's code tab
|
|
25
|
+
* shows the region; editing and forking use full files.
|
|
26
|
+
* - Highlights live in the source as comments, stripped from the shown code:
|
|
27
|
+
* `// #highlight label` … `// #endhighlight`, or a trailing
|
|
28
|
+
* `code // #highlight label` (`{/* #highlight *\/}` in JSX children).
|
|
29
|
+
* `{1-3}` meta highlights are rejected.
|
|
30
|
+
* - `lines` adds a line-number gutter (original line numbers with `region`).
|
|
31
|
+
* - `files=off` hides the file names and tabs of the files the entry imports.
|
|
32
|
+
* - `title="..."` renders a card with the title as the snippet name.
|
|
33
|
+
* - `height="small|default|large|auto|<px>"`; a live fence defaults to
|
|
34
|
+
* `liveHeight` (`auto`).
|
|
35
|
+
*
|
|
36
|
+
* Fences can also live inside `<Demo>`, next to `<DemoTitle>` /
|
|
37
|
+
* `<DemoDescription>`, which render as the card header; the fence's props are
|
|
38
|
+
* hoisted onto the `<Demo>`. A fence without `live` there makes a code-only card.
|
|
39
|
+
*/
|
|
40
|
+
export function remarkDemoFences(options) {
|
|
41
|
+
const { contentDir, dependencies, claim, banInlineCode = true, staticFences = 'shiki-meta', demoSuffixes = ['.demo.tsx'], componentName = 'Demo', sourceReplacements, liveHeight = 'auto', } = options;
|
|
42
|
+
const folder = options.folder.replace(/^\/+|\/+$/g, '');
|
|
43
|
+
const pageDir = path.join(contentDir, folder);
|
|
44
|
+
/** `/docs/x.tsx` is relative to the content root, anything else to the page. */
|
|
45
|
+
const resolveFile = (file) => file.startsWith('/') ? path.join(contentDir, file) : path.resolve(pageDir, file);
|
|
46
|
+
const addDependency = (absPath) => {
|
|
47
|
+
dependencies?.add(path.relative(contentDir, absPath).split(path.sep).join('/'));
|
|
48
|
+
};
|
|
49
|
+
return () => (tree) => {
|
|
50
|
+
visit(tree, 'code', (node, index, parent) => {
|
|
51
|
+
if (index == null || !parent)
|
|
52
|
+
return;
|
|
53
|
+
const inDemo = parent.type === 'mdxJsxFlowElement' && parent.name === componentName;
|
|
54
|
+
const meta = node.meta || '';
|
|
55
|
+
const props = parseFenceMeta(meta);
|
|
56
|
+
if (!inDemo && claim && !claim({ lang: node.lang ?? null, meta, props, inDemo }))
|
|
57
|
+
return;
|
|
58
|
+
const context = `\`\`\`${node.lang ?? ''} ${meta} in "${path.join(path.basename(contentDir), folder)}"`;
|
|
59
|
+
if (/\{[^}]*\}/.test(meta.replace(/"[^"]*"/g, '""'))) {
|
|
60
|
+
throw new Error(`${context}: \`{…}\` line highlights are not supported — mark the lines in the ` +
|
|
61
|
+
`source file with \`// #highlight label\` … \`// #endhighlight\`, or a trailing ` +
|
|
62
|
+
`\`// #highlight label\` on a single line`);
|
|
63
|
+
}
|
|
64
|
+
const file = props.file;
|
|
65
|
+
if (props.live && !('height' in props))
|
|
66
|
+
props.height = liveHeight;
|
|
67
|
+
if ('range' in props) {
|
|
68
|
+
throw new Error(`${context}: \`range\` was renamed to \`region="region-name"\``);
|
|
69
|
+
}
|
|
70
|
+
if (banInlineCode && TYPECHECKED_LANGS.has(node.lang ?? '') && typeof file !== 'string') {
|
|
71
|
+
throw new Error(`${context}: inline ${node.lang} snippets are not allowed — put the code in a ` +
|
|
72
|
+
`co-located *.snippet.tsx (type-checked by tsc) and reference it with ` +
|
|
73
|
+
`\`file="x.snippet.tsx" region="region-name"\``);
|
|
74
|
+
}
|
|
75
|
+
if (!inDemo && typeof file !== 'string' && typeof props.title !== 'string')
|
|
76
|
+
return;
|
|
77
|
+
if ('height' in props) {
|
|
78
|
+
try {
|
|
79
|
+
parseDemoHeight(typeof props.height === 'string' ? props.height : '');
|
|
80
|
+
}
|
|
81
|
+
catch (error) {
|
|
82
|
+
throw new Error(`${context}: ${error instanceof Error ? error.message : String(error)}`);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
let source = node.value ?? '';
|
|
86
|
+
if (typeof file === 'string') {
|
|
87
|
+
const absPath = resolveFile(file);
|
|
88
|
+
if (!fs.existsSync(absPath) || !fs.statSync(absPath).isFile()) {
|
|
89
|
+
throw new Error(`${context}: file not found: ${path.relative(contentDir, absPath)}`);
|
|
90
|
+
}
|
|
91
|
+
source = applySourceReplacements(fs.readFileSync(absPath, 'utf8').trimEnd(), sourceReplacements);
|
|
92
|
+
if (props.live) {
|
|
93
|
+
for (const { filePath } of collectDemoFiles(absPath))
|
|
94
|
+
addDependency(filePath);
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
addDependency(absPath);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
let originalLines;
|
|
101
|
+
if (typeof props.region === 'string') {
|
|
102
|
+
if (typeof file !== 'string')
|
|
103
|
+
throw new Error(`${context}: \`region\` requires \`file="..."\``);
|
|
104
|
+
const sliced = sliceRegion(source, props.region, context);
|
|
105
|
+
source = sliced.source;
|
|
106
|
+
originalLines = sliced.originalLines;
|
|
107
|
+
}
|
|
108
|
+
// Live demos strip markers per file in <Demo>.
|
|
109
|
+
let highlight = null;
|
|
110
|
+
// without `#highlight label` rows: what a card's copy button copies
|
|
111
|
+
let copyCode = null;
|
|
112
|
+
if (typeof file === 'string' && !props.live) {
|
|
113
|
+
const stripped = stripCodeMarkers(source);
|
|
114
|
+
const marked = extractCodeMarkers(source, context, originalLines);
|
|
115
|
+
source = marked.source;
|
|
116
|
+
if (stripped !== source)
|
|
117
|
+
copyCode = stripped;
|
|
118
|
+
if (originalLines)
|
|
119
|
+
originalLines = marked.originalLines;
|
|
120
|
+
if (marked.highlight.lines.length)
|
|
121
|
+
highlight = formatHighlightSpec(marked.highlight);
|
|
122
|
+
}
|
|
123
|
+
const originalLinesAttr = originalLines ? formatLineRanges(originalLines) || null : null;
|
|
124
|
+
if (props.live) {
|
|
125
|
+
if (props.live !== true && props.live !== 'off') {
|
|
126
|
+
throw new Error(`${context}: \`live\` accepts no value (preview shown first) or ` +
|
|
127
|
+
`\`live=off\` (preview available, code shown first)`);
|
|
128
|
+
}
|
|
129
|
+
const suffix = typeof file === 'string' ? demoSuffixes.find((s) => file.endsWith(s)) : undefined;
|
|
130
|
+
const fromRoot = typeof file === 'string' ? path.relative(contentDir, resolveFile(file)) : '..';
|
|
131
|
+
if (typeof file !== 'string' || !suffix || fromRoot.startsWith('..') || path.isAbsolute(fromRoot)) {
|
|
132
|
+
throw new Error(`${context}: live demos must reference a ${demoSuffixes.map((s) => `*${s}`).join(' / ')} ` +
|
|
133
|
+
`file inside the content folder (it has to match a prebuilt demo bundle)`);
|
|
134
|
+
}
|
|
135
|
+
const attrs = { name: file.slice(0, -suffix.length) };
|
|
136
|
+
if (props.live === 'off')
|
|
137
|
+
attrs.startTab = 'code';
|
|
138
|
+
if (props.files === 'off')
|
|
139
|
+
attrs.hideFiles = 'true';
|
|
140
|
+
if (typeof props.region === 'string')
|
|
141
|
+
attrs.region = props.region;
|
|
142
|
+
if (typeof props.height === 'string')
|
|
143
|
+
attrs.height = props.height;
|
|
144
|
+
const heading = inDemo ? demoTitleFromElement(parent) : null;
|
|
145
|
+
if (heading)
|
|
146
|
+
attrs.title = heading;
|
|
147
|
+
else if (typeof props.title === 'string')
|
|
148
|
+
attrs.title = props.title;
|
|
149
|
+
if (props.lines)
|
|
150
|
+
attrs.lines = 'true';
|
|
151
|
+
if (props.edit === 'off')
|
|
152
|
+
attrs.editable = 'false';
|
|
153
|
+
if (inDemo) {
|
|
154
|
+
assertSingleSnippet(parent, context);
|
|
155
|
+
for (const [attrName, value] of Object.entries(attrs))
|
|
156
|
+
setAttribute(parent, attrName, value);
|
|
157
|
+
parent.children.splice(index, 1);
|
|
158
|
+
return index;
|
|
159
|
+
}
|
|
160
|
+
parent.children[index] = element(componentName, attrs);
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
const cardAttrs = (snippetTitle) => {
|
|
164
|
+
const attrs = { code: source, lang: node.lang || 'tsx' };
|
|
165
|
+
if (snippetTitle)
|
|
166
|
+
attrs.codeTitle = snippetTitle;
|
|
167
|
+
if (copyCode)
|
|
168
|
+
attrs.copyCode = copyCode;
|
|
169
|
+
if (typeof props.height === 'string')
|
|
170
|
+
attrs.height = props.height;
|
|
171
|
+
if (highlight)
|
|
172
|
+
attrs.highlight = highlight;
|
|
173
|
+
if (props.lines)
|
|
174
|
+
attrs.lines = 'true';
|
|
175
|
+
if (originalLinesAttr)
|
|
176
|
+
attrs.originalLines = originalLinesAttr;
|
|
177
|
+
return attrs;
|
|
178
|
+
};
|
|
179
|
+
if (inDemo) {
|
|
180
|
+
// Code-only demo: hand the snippet to <Demo> and drop the fence.
|
|
181
|
+
assertSingleSnippet(parent, context);
|
|
182
|
+
const snippetTitle = typeof props.title === 'string' ? props.title : props.files === 'off' ? null : (file ?? null);
|
|
183
|
+
const attrs = cardAttrs(typeof snippetTitle === 'string' ? snippetTitle : null);
|
|
184
|
+
const heading = demoTitleFromElement(parent);
|
|
185
|
+
if (heading)
|
|
186
|
+
attrs.title = heading;
|
|
187
|
+
else if (typeof props.title === 'string')
|
|
188
|
+
attrs.title = props.title;
|
|
189
|
+
for (const [attrName, value] of Object.entries(attrs))
|
|
190
|
+
setAttribute(parent, attrName, value);
|
|
191
|
+
parent.children.splice(index, 1);
|
|
192
|
+
return index;
|
|
193
|
+
}
|
|
194
|
+
if (typeof props.title === 'string' || staticFences === 'card') {
|
|
195
|
+
parent.children[index] = element(componentName, cardAttrs(typeof props.title === 'string' ? props.title : null));
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
// Standalone static snippet: inline the referenced file's source; a
|
|
199
|
+
// markdown-level shiki pipeline highlights it like any code block.
|
|
200
|
+
node.value = source;
|
|
201
|
+
node.meta =
|
|
202
|
+
[
|
|
203
|
+
highlight ? `{${highlight}}` : null,
|
|
204
|
+
props.lines ? 'lines' : null,
|
|
205
|
+
originalLinesAttr ? `originalLines="${originalLinesAttr}"` : null,
|
|
206
|
+
typeof props.height === 'string' ? `height="${props.height}"` : null,
|
|
207
|
+
]
|
|
208
|
+
.filter(Boolean)
|
|
209
|
+
.join(' ') || null;
|
|
210
|
+
node.data = { ...node.data, docsKit: true };
|
|
211
|
+
});
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
function element(name, attrs) {
|
|
215
|
+
return {
|
|
216
|
+
type: 'mdxJsxFlowElement',
|
|
217
|
+
name,
|
|
218
|
+
attributes: Object.entries(attrs).map(([attrName, value]) => ({
|
|
219
|
+
type: 'mdxJsxAttribute',
|
|
220
|
+
name: attrName,
|
|
221
|
+
value,
|
|
222
|
+
})),
|
|
223
|
+
children: [],
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
function hasAttribute(el, name) {
|
|
227
|
+
return (el.attributes || []).some((attr) => attr.type === 'mdxJsxAttribute' && attr.name === name);
|
|
228
|
+
}
|
|
229
|
+
/** Adds an attribute unless it's already set on the element in the markdown. */
|
|
230
|
+
function setAttribute(el, name, value) {
|
|
231
|
+
if (hasAttribute(el, name))
|
|
232
|
+
return;
|
|
233
|
+
el.attributes = el.attributes || [];
|
|
234
|
+
el.attributes.push({ type: 'mdxJsxAttribute', name, value });
|
|
235
|
+
}
|
|
236
|
+
function assertSingleSnippet(demoElement, context) {
|
|
237
|
+
if (hasAttribute(demoElement, 'name') || hasAttribute(demoElement, 'code')) {
|
|
238
|
+
throw new Error(`${context}: a <Demo> can contain only one code block`);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
function jsxText(node) {
|
|
242
|
+
if (!node)
|
|
243
|
+
return '';
|
|
244
|
+
if (node.type === 'text' || node.type === 'mdxTextExpression')
|
|
245
|
+
return node.value || '';
|
|
246
|
+
return (node.children || []).map(jsxText).join('');
|
|
247
|
+
}
|
|
248
|
+
/** Plain text of a wrapping `<DemoTitle>`, if the fence lives inside `<Demo>`. */
|
|
249
|
+
function demoTitleFromElement(demoElement) {
|
|
250
|
+
for (const child of demoElement.children || []) {
|
|
251
|
+
if (child.type === 'mdxJsxFlowElement' && child.name === 'DemoTitle') {
|
|
252
|
+
const text = jsxText(child).trim();
|
|
253
|
+
if (text)
|
|
254
|
+
return text;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
return null;
|
|
258
|
+
}
|
|
259
|
+
//# sourceMappingURL=remarkDemoFences.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"remarkDemoFences.js","sourceRoot":"","sources":["../../src/remark/remarkDemoFences.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AAEzC,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AACxD,OAAO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,UAAU,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,iBAAiB,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC;AAC/D,CAAC;AAED,SAAS,mBAAmB,CAAC,WAAgB,EAAE,OAAe;IAC5D,IAAI,YAAY,CAAC,WAAW,EAAE,MAAM,CAAC,IAAI,YAAY,CAAC,WAAW,EAAE,MAAM,CAAC,EAAE,CAAC;QAC3E,MAAM,IAAI,KAAK,CAAC,GAAG,OAAO,4CAA4C,CAAC,CAAC;IAC1E,CAAC;AACH,CAAC;AAED,SAAS,OAAO,CAAC,IAAS;IACxB,IAAI,CAAC,IAAI;QAAE,OAAO,EAAE,CAAC;IACrB,IAAI,IAAI,CAAC,IAAI,KAAK,MAAM,IAAI,IAAI,CAAC,IAAI,KAAK,mBAAmB;QAAE,OAAO,IAAI,CAAC,KAAK,IAAI,EAAE,CAAC;IACvF,OAAO,CAAC,IAAI,CAAC,QAAQ,IAAI,EAAE,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;AACrD,CAAC;AAED,kFAAkF;AAClF,SAAS,oBAAoB,CAAC,WAAgB;IAC5C,KAAK,MAAM,KAAK,IAAI,WAAW,CAAC,QAAQ,IAAI,EAAE,EAAE,CAAC;QAC/C,IAAI,KAAK,CAAC,IAAI,KAAK,mBAAmB,IAAI,KAAK,CAAC,IAAI,KAAK,WAAW,EAAE,CAAC;YACrE,MAAM,IAAI,GAAG,OAAO,CAAC,KAAK,CAAC,CAAC,IAAI,EAAE,CAAC;YACnC,IAAI,IAAI;gBAAE,OAAO,IAAI,CAAC;QACxB,CAAC;IACH,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# docs-kit links
|
|
2
|
+
|
|
3
|
+
Cmd/Ctrl+click the `file="…"` of a code fence in a docs page to open that file.
|
|
4
|
+
When the fence has `region="name"`, the file opens at its `#region name` line.
|
|
5
|
+
|
|
6
|
+
````md
|
|
7
|
+
```tsx file="basic.demo.tsx" live
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
```ts file="/docs/learn/sorting/sorting.snippet.tsx" region="sort-info"
|
|
11
|
+
```
|
|
12
|
+
````
|
|
13
|
+
|
|
14
|
+
The paths resolve like they do in `@thedatagrid/docs-kit`'s `remarkDemoFences`:
|
|
15
|
+
`x.demo.tsx` resolves from the page's folder, and `/docs/…` from the site's
|
|
16
|
+
content root. The content root is `docsKit.contentDir`, or else
|
|
17
|
+
`docsRoot.source`, in the nearest `package.json` that has one.
|
|
18
|
+
|
|
19
|
+
Install it from a project that depends on `@thedatagrid/docs-kit`:
|
|
20
|
+
|
|
21
|
+
```sh
|
|
22
|
+
npx docs-kit editor install # VS Code and/or Cursor, whichever are installed
|
|
23
|
+
```
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
/**
|
|
3
|
+
* docs-kit links for VS Code and Cursor: Cmd/Ctrl+click the `file="…"` of a
|
|
4
|
+
* docs code fence to open that file, at its `region="…"` when one is set.
|
|
5
|
+
* Installed with `docs-kit editor install` (README → Editor extension).
|
|
6
|
+
*/
|
|
7
|
+
const vscode = require('vscode');
|
|
8
|
+
const { fenceLinks } = require('./links.js');
|
|
9
|
+
|
|
10
|
+
/** @type {import('vscode').DocumentLinkProvider} */
|
|
11
|
+
const provider = {
|
|
12
|
+
provideDocumentLinks(document) {
|
|
13
|
+
return fenceLinks(document.getText(), document.fileName).map((link) => {
|
|
14
|
+
const target = vscode.Uri.file(link.target).with({
|
|
15
|
+
fragment: link.targetLine == null ? '' : `L${link.targetLine + 1}`,
|
|
16
|
+
});
|
|
17
|
+
const documentLink = new vscode.DocumentLink(
|
|
18
|
+
new vscode.Range(link.line, link.start, link.line, link.end),
|
|
19
|
+
target,
|
|
20
|
+
);
|
|
21
|
+
documentLink.tooltip = link.tooltip;
|
|
22
|
+
return documentLink;
|
|
23
|
+
});
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
/** @param {import('vscode').ExtensionContext} context */
|
|
28
|
+
function activate(context) {
|
|
29
|
+
context.subscriptions.push(
|
|
30
|
+
vscode.languages.registerDocumentLinkProvider(
|
|
31
|
+
[
|
|
32
|
+
{ scheme: 'file', language: 'markdown' },
|
|
33
|
+
{ scheme: 'file', language: 'mdx' },
|
|
34
|
+
],
|
|
35
|
+
provider,
|
|
36
|
+
),
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
module.exports = { activate, deactivate() {} };
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
/**
|
|
3
|
+
* Fence links for the docs-kit editor extension, without the `vscode` API (so
|
|
4
|
+
* it can be unit tested). Same rules as `remarkDemoFences`:
|
|
5
|
+
*
|
|
6
|
+
* - `file="x.demo.tsx"` resolves from the page's folder;
|
|
7
|
+
* - `file="/docs/a/x.demo.tsx"` resolves from the site's content root;
|
|
8
|
+
* - `region="name"` targets the line of that region's `#region name` marker.
|
|
9
|
+
*
|
|
10
|
+
* The content root is `docsKit.contentDir` or `docsRoot.source` (the
|
|
11
|
+
* generate-docs-meta config) of the nearest package.json that has one.
|
|
12
|
+
*/
|
|
13
|
+
const fs = require('node:fs');
|
|
14
|
+
const path = require('node:path');
|
|
15
|
+
|
|
16
|
+
/** A fence opener with an info string: ```lang meta (or ~~~). */
|
|
17
|
+
const FENCE_RE = /^(\s*(?:`{3,}|~{3,})[^\s`]*[ \t]+)(.*)$/;
|
|
18
|
+
|
|
19
|
+
/** @param {string} name */
|
|
20
|
+
function attributeRe(name) {
|
|
21
|
+
return new RegExp(`(^|\\s)${name}=(?:"([^"]*)"|'([^']*)'|([^\\s"']+))`);
|
|
22
|
+
}
|
|
23
|
+
const FILE_RE = attributeRe('file');
|
|
24
|
+
const REGION_RE = attributeRe('region');
|
|
25
|
+
|
|
26
|
+
/** @param {string} file */
|
|
27
|
+
function isFile(file) {
|
|
28
|
+
try {
|
|
29
|
+
return fs.statSync(file).isFile();
|
|
30
|
+
} catch {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* The content root of the docs site `fromDir` is in, or null.
|
|
37
|
+
* @param {string} fromDir
|
|
38
|
+
*/
|
|
39
|
+
function contentRootOf(fromDir) {
|
|
40
|
+
for (let dir = fromDir; ; dir = path.dirname(dir)) {
|
|
41
|
+
const manifest = path.join(dir, 'package.json');
|
|
42
|
+
if (isFile(manifest)) {
|
|
43
|
+
try {
|
|
44
|
+
const json = JSON.parse(fs.readFileSync(manifest, 'utf8'));
|
|
45
|
+
const source = json.docsKit?.contentDir ?? json.docsRoot?.source;
|
|
46
|
+
if (typeof source === 'string') return path.resolve(dir, source);
|
|
47
|
+
} catch {
|
|
48
|
+
// not JSON: keep looking further up
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
if (path.dirname(dir) === dir) return null;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** @param {string} text */
|
|
56
|
+
const escapeRe = (text) => text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* 0-based line of the `#region <name>` marker in `file`, or null. Matches the
|
|
60
|
+
* markers `sliceRegion` accepts: `// #region`, `/* #region *\/`,
|
|
61
|
+
* `{/* #region *\/}` and `<!-- #region -->`.
|
|
62
|
+
* @param {string} file
|
|
63
|
+
* @param {string} name
|
|
64
|
+
*/
|
|
65
|
+
function regionLine(file, name) {
|
|
66
|
+
const n = escapeRe(name);
|
|
67
|
+
const markers = [
|
|
68
|
+
new RegExp(`^\\s*//\\s*#?region\\s+${n}\\s*$`),
|
|
69
|
+
new RegExp(`^\\s*\\{?/\\*\\s*#region\\s+${n}\\s*\\*/\\}?\\s*$`),
|
|
70
|
+
new RegExp(`^\\s*<!--\\s*#?region\\s+${n}\\s*-->`),
|
|
71
|
+
];
|
|
72
|
+
const lines = fs.readFileSync(file, 'utf8').split('\n');
|
|
73
|
+
const index = lines.findIndex((line) => markers.some((marker) => marker.test(line)));
|
|
74
|
+
return index === -1 ? null : index;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* @typedef {{
|
|
79
|
+
* line: number, start: number, end: number,
|
|
80
|
+
* target: string, targetLine: number | null, tooltip: string,
|
|
81
|
+
* }} FenceLink
|
|
82
|
+
* `line`/`start`/`end`: 0-based position of the `file` value on the page;
|
|
83
|
+
* `targetLine`: 0-based line to open the target at.
|
|
84
|
+
*/
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Links for the `file="…"` values of the fences in a page. Files that don't
|
|
88
|
+
* exist get no link.
|
|
89
|
+
* @param {string} text the page source
|
|
90
|
+
* @param {string} fileName the page's absolute path
|
|
91
|
+
* @returns {FenceLink[]}
|
|
92
|
+
*/
|
|
93
|
+
function fenceLinks(text, fileName) {
|
|
94
|
+
const pageDir = path.dirname(fileName);
|
|
95
|
+
/** @type {string | null | undefined} */
|
|
96
|
+
let contentRoot;
|
|
97
|
+
/** @type {FenceLink[]} */
|
|
98
|
+
const links = [];
|
|
99
|
+
text.split('\n').forEach((lineText, line) => {
|
|
100
|
+
const fence = FENCE_RE.exec(lineText.replace(/\r$/, ''));
|
|
101
|
+
if (!fence) return;
|
|
102
|
+
const [, opener, meta] = fence;
|
|
103
|
+
const file = FILE_RE.exec(meta);
|
|
104
|
+
if (!file) return;
|
|
105
|
+
const value = file[2] ?? file[3] ?? file[4];
|
|
106
|
+
if (!value) return;
|
|
107
|
+
let target;
|
|
108
|
+
if (value.startsWith('/')) {
|
|
109
|
+
if (contentRoot === undefined) contentRoot = contentRootOf(pageDir);
|
|
110
|
+
if (!contentRoot) return;
|
|
111
|
+
target = path.join(contentRoot, value);
|
|
112
|
+
} else {
|
|
113
|
+
target = path.resolve(pageDir, value);
|
|
114
|
+
}
|
|
115
|
+
if (!isFile(target)) return;
|
|
116
|
+
|
|
117
|
+
const start = opener.length + file.index + file[0].indexOf(value, file[1].length + 'file='.length);
|
|
118
|
+
const region = REGION_RE.exec(meta);
|
|
119
|
+
const regionName = region ? (region[2] ?? region[3] ?? region[4]) : undefined;
|
|
120
|
+
const targetLine = regionName ? regionLine(target, regionName) : null;
|
|
121
|
+
links.push({
|
|
122
|
+
line,
|
|
123
|
+
start,
|
|
124
|
+
end: start + value.length,
|
|
125
|
+
target,
|
|
126
|
+
targetLine,
|
|
127
|
+
tooltip: regionName
|
|
128
|
+
? targetLine == null
|
|
129
|
+
? `Open ${value} (no #region ${regionName} in it)`
|
|
130
|
+
: `Open ${value} at #region ${regionName}`
|
|
131
|
+
: `Open ${value}`,
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
return links;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
module.exports = { fenceLinks, contentRootOf, regionLine };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "docs-kit-links",
|
|
3
|
+
"displayName": "docs-kit links",
|
|
4
|
+
"description": "Cmd/Ctrl+click file=\"…\" in a docs code fence to open the file, at its region=\"…\" when set. Part of @thedatagrid/docs-kit.",
|
|
5
|
+
"version": "0.0.0",
|
|
6
|
+
"publisher": "thedatagrid",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "https://github.com/thedatagrid/docs-kit.git"
|
|
11
|
+
},
|
|
12
|
+
"engines": {
|
|
13
|
+
"vscode": "^1.80.0"
|
|
14
|
+
},
|
|
15
|
+
"categories": ["Other"],
|
|
16
|
+
"main": "./extension.js",
|
|
17
|
+
"activationEvents": ["onLanguage:markdown", "onLanguage:mdx"],
|
|
18
|
+
"contributes": {}
|
|
19
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,109 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thedatagrid/docs-kit",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Docs tooling: type-checked file snippets with regions and highlights, prebuilt and editable live demos, CodeSandbox forks",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "TheDataGrid",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/thedatagrid/docs-kit.git",
|
|
11
|
+
"directory": "packages/docs-kit"
|
|
12
|
+
},
|
|
13
|
+
"sideEffects": [
|
|
14
|
+
"*.css"
|
|
15
|
+
],
|
|
16
|
+
"exports": {
|
|
17
|
+
"./code": {
|
|
18
|
+
"types": "./dist/code/index.d.ts",
|
|
19
|
+
"default": "./dist/code/index.js"
|
|
20
|
+
},
|
|
21
|
+
"./remark": {
|
|
22
|
+
"types": "./dist/remark/index.d.ts",
|
|
23
|
+
"default": "./dist/remark/index.js"
|
|
24
|
+
},
|
|
25
|
+
"./demos": {
|
|
26
|
+
"types": "./dist/demos/index.d.ts",
|
|
27
|
+
"default": "./dist/demos/index.js"
|
|
28
|
+
},
|
|
29
|
+
"./demos/cli": "./dist/demos/cli.js",
|
|
30
|
+
"./demos/client": {
|
|
31
|
+
"types": "./dist/demos/client.d.ts",
|
|
32
|
+
"default": "./dist/demos/client.js"
|
|
33
|
+
},
|
|
34
|
+
"./react": {
|
|
35
|
+
"types": "./dist/react/index.d.ts",
|
|
36
|
+
"default": "./dist/react/index.js"
|
|
37
|
+
},
|
|
38
|
+
"./react/styles.css": "./dist/react/styles.css",
|
|
39
|
+
"./fork": {
|
|
40
|
+
"types": "./dist/fork/index.d.ts",
|
|
41
|
+
"default": "./dist/fork/index.js"
|
|
42
|
+
},
|
|
43
|
+
"./package.json": "./package.json"
|
|
44
|
+
},
|
|
45
|
+
"typesVersions": {
|
|
46
|
+
"*": {
|
|
47
|
+
"demos/client": [
|
|
48
|
+
"dist/demos/client.d.ts"
|
|
49
|
+
],
|
|
50
|
+
"*": [
|
|
51
|
+
"dist/*/index.d.ts"
|
|
52
|
+
]
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
"bin": {
|
|
56
|
+
"docs-kit": "./dist/demos/cli.js"
|
|
57
|
+
},
|
|
58
|
+
"files": [
|
|
59
|
+
"dist",
|
|
60
|
+
"src",
|
|
61
|
+
"editor-extension",
|
|
62
|
+
"README.md",
|
|
63
|
+
"CHANGELOG.md"
|
|
64
|
+
],
|
|
65
|
+
"scripts": {
|
|
66
|
+
"build": "rm -rf dist && tsc -p tsconfig.build.json && node scripts/copy-assets.mjs",
|
|
67
|
+
"dev": "tsc -p tsconfig.build.json --watch",
|
|
68
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
69
|
+
"test": "vitest run"
|
|
70
|
+
},
|
|
71
|
+
"dependencies": {
|
|
72
|
+
"@codemirror/autocomplete": "^6.20.1",
|
|
73
|
+
"@codemirror/commands": "^6.11.1",
|
|
74
|
+
"@codemirror/lang-css": "^6.3.1",
|
|
75
|
+
"@codemirror/lang-javascript": "^6.2.5",
|
|
76
|
+
"@codemirror/language": "^6.13.1",
|
|
77
|
+
"@codemirror/search": "^6.6.0",
|
|
78
|
+
"@codemirror/state": "^6.7.6",
|
|
79
|
+
"@codemirror/view": "^6.43.15",
|
|
80
|
+
"@lezer/highlight": "^1.2.5",
|
|
81
|
+
"chokidar": "^4.0.3",
|
|
82
|
+
"esbuild": "0.28.2",
|
|
83
|
+
"esbuild-wasm": "0.28.2",
|
|
84
|
+
"lz-string": "^1.5.0",
|
|
85
|
+
"shiki": "^4.6.0",
|
|
86
|
+
"unist-util-visit": "^5.2.0"
|
|
87
|
+
},
|
|
88
|
+
"peerDependencies": {
|
|
89
|
+
"react": ">=18",
|
|
90
|
+
"react-dom": ">=18"
|
|
91
|
+
},
|
|
92
|
+
"devDependencies": {
|
|
93
|
+
"@mdx-js/mdx": "^3.1.1",
|
|
94
|
+
"@types/node": "^22.18.0",
|
|
95
|
+
"@types/react": "^19.2.2",
|
|
96
|
+
"@types/react-dom": "^19.2.1",
|
|
97
|
+
"playwright-core": "1.64.1",
|
|
98
|
+
"react": "^19.2.0",
|
|
99
|
+
"react-dom": "^19.2.0",
|
|
100
|
+
"remark-gfm": "^4.0.1",
|
|
101
|
+
"remark-mdx": "^3.1.1"
|
|
102
|
+
},
|
|
103
|
+
"engines": {
|
|
104
|
+
"node": ">=22.18.0"
|
|
105
|
+
},
|
|
106
|
+
"publishConfig": {
|
|
107
|
+
"access": "public"
|
|
108
|
+
}
|
|
109
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
|
|
4
|
+
const IMPORT_RE =
|
|
5
|
+
/(?:import|export)\s[^'"]*?from\s+['"](\.\.?\/[^'"]+)['"]|import\s*\(?\s*['"](\.\.?\/[^'"]+)['"]/g;
|
|
6
|
+
|
|
7
|
+
export const RESOLVE_EXTENSIONS = ['', '.tsx', '.ts', '.jsx', '.js', '.mjs', '.css', '.json'];
|
|
8
|
+
|
|
9
|
+
function resolveRelativeImport(fromDir: string, specifier: string): string | null {
|
|
10
|
+
const base = path.resolve(fromDir, specifier);
|
|
11
|
+
for (const ext of RESOLVE_EXTENSIONS) {
|
|
12
|
+
const candidate = base + ext;
|
|
13
|
+
if (fs.existsSync(candidate) && fs.statSync(candidate).isFile()) return candidate;
|
|
14
|
+
}
|
|
15
|
+
for (const ext of RESOLVE_EXTENSIONS.slice(1)) {
|
|
16
|
+
const candidate = path.join(base, `index${ext}`);
|
|
17
|
+
if (fs.existsSync(candidate)) return candidate;
|
|
18
|
+
}
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export type DemoSourceFile = {
|
|
23
|
+
/** Absolute path. */
|
|
24
|
+
filePath: string;
|
|
25
|
+
source: string;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The demo file plus every file it (transitively) pulls in through relative
|
|
30
|
+
* imports, entry first. They are shown as extra tabs in the code view and are
|
|
31
|
+
* the files a browser rebuild and a CodeSandbox fork need.
|
|
32
|
+
*/
|
|
33
|
+
export function collectDemoFiles(entryFile: string): DemoSourceFile[] {
|
|
34
|
+
const ordered: DemoSourceFile[] = [];
|
|
35
|
+
const visited = new Set<string>();
|
|
36
|
+
|
|
37
|
+
const visit = (filePath: string) => {
|
|
38
|
+
if (visited.has(filePath)) return;
|
|
39
|
+
visited.add(filePath);
|
|
40
|
+
const source = fs.readFileSync(filePath, 'utf8');
|
|
41
|
+
ordered.push({ filePath, source });
|
|
42
|
+
if (!/\.(?:[cm]?[jt]sx?)$/.test(filePath)) return;
|
|
43
|
+
for (const match of source.matchAll(IMPORT_RE)) {
|
|
44
|
+
const specifier = match[1] ?? match[2];
|
|
45
|
+
const resolved = resolveRelativeImport(path.dirname(filePath), specifier);
|
|
46
|
+
if (resolved) visit(resolved);
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
visit(entryFile);
|
|
51
|
+
return ordered;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export { bareImports } from './imports.ts';
|