sdocs 0.0.41 → 0.0.42
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 +26 -0
- package/dist/explorer/tree-builder.js +2 -4
- package/dist/explorer/views/ComponentView.svelte +85 -29
- package/dist/explorer/views/LayoutView.svelte +1 -1
- package/dist/explorer/views/PageView.svelte +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/server/app-gen.js +18 -21
- package/dist/server/discovery.d.ts +0 -2
- package/dist/server/discovery.js +0 -9
- package/dist/server/doc-model.d.ts +26 -0
- package/dist/server/doc-model.js +58 -0
- package/dist/server/page-markdown.d.ts +15 -0
- package/dist/server/page-markdown.js +104 -0
- package/dist/server/snippet-compiler.d.ts +18 -18
- package/dist/server/snippet-compiler.js +32 -29
- package/dist/types.d.ts +35 -19
- package/dist/vite.js +168 -99
- package/package.json +2 -1
- package/dist/server/meta-parser.d.ts +0 -11
- package/dist/server/meta-parser.js +0 -109
- package/dist/server/snippet-extractor.d.ts +0 -11
- package/dist/server/snippet-extractor.js +0 -83
- package/dist/server/toc-extractor.d.ts +0 -3
- package/dist/server/toc-extractor.js +0 -23
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,32 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [0.0.42] - 2026-07-04
|
|
11
|
+
|
|
12
|
+
### Changed
|
|
13
|
+
|
|
14
|
+
- **Breaking: `.sdoc` files are the block format.** A doc file is now
|
|
15
|
+
`<script>` on top, `[DOCS]`/`[PAGE]`/`[LAYOUT]` entity blocks in the
|
|
16
|
+
middle, `<style>` at the bottom — the language documented in the site's
|
|
17
|
+
Language section. Each entity is its own sidebar entry, so one file can
|
|
18
|
+
hold several. The `export const meta` convention, `{#snippet}` extraction,
|
|
19
|
+
auto-generated `Default` previews, and the `.page.sdoc`/`.layout.sdoc`
|
|
20
|
+
filename kinds are gone.
|
|
21
|
+
- **`[DOCS]` holds any number of previews.** Each
|
|
22
|
+
`[preview component={X} args={{…}}]` is self-contained; several render as
|
|
23
|
+
tabs, each a fully live panel with its own controls, extracted API tables,
|
|
24
|
+
and component source. `[example title="…"]` blocks are frozen and render
|
|
25
|
+
page-level below the tabs.
|
|
26
|
+
- **`[PAGE]` bodies are markdown** — GFM with `{expression}` interpolation
|
|
27
|
+
and Svelte component islands; code fences and inline code are inert. The
|
|
28
|
+
table of contents comes from the markdown headings.
|
|
29
|
+
- **Parsing never crashes the dev server.** Doc files parse through the
|
|
30
|
+
`sdocs/language` scanner; mistakes surface as file:line warnings and the
|
|
31
|
+
rest of the file keeps working.
|
|
32
|
+
- **Breaking for embedders:** `DocEntry` is reshaped (`previews`/`examples`/
|
|
33
|
+
`content` replace `snippets`/`componentData`), and preview URLs address
|
|
34
|
+
entities as `path#slug`.
|
|
35
|
+
|
|
10
36
|
## [0.0.41] - 2026-07-04
|
|
11
37
|
|
|
12
38
|
### Added
|
|
@@ -44,9 +44,7 @@ export function buildTree(docs, sidebar) {
|
|
|
44
44
|
// Create the item node
|
|
45
45
|
const itemPath = [...currentPath, itemName];
|
|
46
46
|
if (kind === 'component') {
|
|
47
|
-
const examples = doc.
|
|
48
|
-
?.filter((s) => s.name !== 'Default')
|
|
49
|
-
.map((s) => s.name) ?? [];
|
|
47
|
+
const examples = doc.examples?.map((s) => s.name) ?? [];
|
|
50
48
|
// Check if a folder node with this name already exists (created by a child doc)
|
|
51
49
|
const existing = parent.find((n) => n.name === itemName);
|
|
52
50
|
const componentNode = existing ?? {
|
|
@@ -195,7 +193,7 @@ export function findDocByPath(docs, path) {
|
|
|
195
193
|
// One extra segment → example
|
|
196
194
|
if (segments.length === path.length - 1 && segments.every((s, i) => s === path[i])) {
|
|
197
195
|
const snippetName = path[path.length - 1];
|
|
198
|
-
const hasSnippet = doc.
|
|
196
|
+
const hasSnippet = doc.examples?.some((s) => s.name === snippetName);
|
|
199
197
|
if (hasSnippet) {
|
|
200
198
|
return { doc, snippetName };
|
|
201
199
|
}
|
|
@@ -18,28 +18,40 @@
|
|
|
18
18
|
let { doc, snippetName, activeStylesheet }: Props = $props();
|
|
19
19
|
|
|
20
20
|
const meta = $derived(doc.meta);
|
|
21
|
-
const
|
|
21
|
+
const previews = $derived(doc.previews ?? []);
|
|
22
|
+
|
|
23
|
+
// Active preview tab (one full live panel per preview)
|
|
24
|
+
let activeIndex = $state(0);
|
|
25
|
+
$effect(() => {
|
|
26
|
+
doc;
|
|
27
|
+
activeIndex = 0;
|
|
28
|
+
});
|
|
29
|
+
const activePreview = $derived(
|
|
30
|
+
previews.length > 0 ? previews[Math.min(activeIndex, previews.length - 1)] : undefined,
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
const cd = $derived(activePreview?.componentData ?? null);
|
|
22
34
|
const componentName = $derived(
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
35
|
+
activePreview?.componentName ??
|
|
36
|
+
((meta.title ?? '').split('/').pop()?.trim() || 'Component'),
|
|
37
|
+
);
|
|
38
|
+
const exampleSnippets = $derived(doc.examples ?? []);
|
|
39
|
+
const focusedSnippet = $derived(
|
|
40
|
+
snippetName ? exampleSnippets.find((s) => s.name === snippetName) : null,
|
|
26
41
|
);
|
|
27
|
-
const snippets = $derived(doc.snippets ?? []);
|
|
28
|
-
const defaultSnippet = $derived(snippets.find((s) => s.name === 'Default'));
|
|
29
|
-
const exampleSnippets = $derived(snippets.filter((s) => s.name !== 'Default'));
|
|
30
|
-
const focusedSnippet = $derived(snippetName ? snippets.find((s) => s.name === snippetName) : null);
|
|
31
42
|
|
|
32
43
|
// Props/CSS controls state
|
|
33
44
|
let propValues = $state<Record<string, unknown>>({});
|
|
34
45
|
let cssValues = $state<Record<string, string>>({});
|
|
35
46
|
|
|
36
|
-
// Live wiring to the
|
|
47
|
+
// Live wiring to the active preview: method invocation + exported state values
|
|
37
48
|
let defaultPreview = $state<{ callMethod: (name: string) => void }>();
|
|
38
49
|
let liveStateValues = $state<Record<string, unknown>>({});
|
|
39
50
|
|
|
40
|
-
// Initialize from
|
|
51
|
+
// Initialize from the active preview's args (build new objects to avoid
|
|
52
|
+
// read+write loop); re-runs on doc and tab change.
|
|
41
53
|
$effect(() => {
|
|
42
|
-
propValues = { ...(
|
|
54
|
+
propValues = { ...(activePreview?.args ?? {}) };
|
|
43
55
|
const newCss: Record<string, string> = {};
|
|
44
56
|
if (cd?.cssProps) {
|
|
45
57
|
for (const cp of cd.cssProps) {
|
|
@@ -47,6 +59,7 @@
|
|
|
47
59
|
}
|
|
48
60
|
}
|
|
49
61
|
cssValues = newCss;
|
|
62
|
+
liveStateValues = {};
|
|
50
63
|
});
|
|
51
64
|
|
|
52
65
|
function handlePropChange(name: string, value: unknown) {
|
|
@@ -159,8 +172,8 @@
|
|
|
159
172
|
return snippetBody.slice(0, attrsStart) + attrs + closing + snippetBody.slice(tagEnd + 1);
|
|
160
173
|
}
|
|
161
174
|
|
|
162
|
-
// Initial values for diffing (computed once per doc change)
|
|
163
|
-
const initialProps = $derived(
|
|
175
|
+
// Initial values for diffing (computed once per doc/tab change)
|
|
176
|
+
const initialProps = $derived(activePreview?.args ?? {});
|
|
164
177
|
const initialCss = $derived.by(() => {
|
|
165
178
|
const css: Record<string, string> = {};
|
|
166
179
|
if (cd?.cssProps) {
|
|
@@ -172,8 +185,8 @@
|
|
|
172
185
|
});
|
|
173
186
|
|
|
174
187
|
const usageCode = $derived.by(() => {
|
|
175
|
-
if (
|
|
176
|
-
return patchSnippetCode(
|
|
188
|
+
if (activePreview?.snippet.body) {
|
|
189
|
+
return patchSnippetCode(activePreview.snippet.body, componentName, propValues, cssValues, initialProps, initialCss);
|
|
177
190
|
}
|
|
178
191
|
return generateFallbackCode(componentName, propValues, cssValues);
|
|
179
192
|
});
|
|
@@ -198,7 +211,7 @@
|
|
|
198
211
|
});
|
|
199
212
|
|
|
200
213
|
function handleReset() {
|
|
201
|
-
propValues = { ...(
|
|
214
|
+
propValues = { ...(activePreview?.args ?? {}) };
|
|
202
215
|
const newCss: Record<string, string> = {};
|
|
203
216
|
if (cd?.cssProps) {
|
|
204
217
|
for (const cp of cd.cssProps) {
|
|
@@ -290,19 +303,37 @@
|
|
|
290
303
|
{/if}
|
|
291
304
|
</div>
|
|
292
305
|
|
|
306
|
+
{#if previews.length > 1}
|
|
307
|
+
<div class="sdocs-preview-tabs" role="tablist">
|
|
308
|
+
{#each previews as preview, i (preview.snippet.slug)}
|
|
309
|
+
<button
|
|
310
|
+
class="sdocs-preview-tab"
|
|
311
|
+
class:active={i === activeIndex}
|
|
312
|
+
role="tab"
|
|
313
|
+
aria-selected={i === activeIndex}
|
|
314
|
+
onclick={() => (activeIndex = i)}
|
|
315
|
+
>
|
|
316
|
+
{preview.label}
|
|
317
|
+
</button>
|
|
318
|
+
{/each}
|
|
319
|
+
</div>
|
|
320
|
+
{/if}
|
|
321
|
+
|
|
293
322
|
<div class="sdocs-panels">
|
|
294
323
|
<!-- Showcase -->
|
|
295
|
-
{#if
|
|
296
|
-
|
|
297
|
-
<
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
324
|
+
{#if activePreview}
|
|
325
|
+
{#key activePreview.snippet.slug}
|
|
326
|
+
<div class="sdocs-preview-wrapper">
|
|
327
|
+
<PreviewFrame
|
|
328
|
+
bind:this={defaultPreview}
|
|
329
|
+
src={activePreview.snippet.previewUrl ?? ''}
|
|
330
|
+
props={propValues}
|
|
331
|
+
cssVars={cssValues}
|
|
332
|
+
{activeStylesheet}
|
|
333
|
+
onStateValues={(values) => (liveStateValues = values)}
|
|
334
|
+
/>
|
|
335
|
+
</div>
|
|
336
|
+
{/key}
|
|
306
337
|
|
|
307
338
|
<CollapsiblePanel title="Preview Code" defaultExpanded={false}>
|
|
308
339
|
<div class="sdocs-code-block">
|
|
@@ -414,11 +445,11 @@
|
|
|
414
445
|
{/each}
|
|
415
446
|
{/if}
|
|
416
447
|
|
|
417
|
-
{#if
|
|
448
|
+
{#if activePreview?.highlightedSource}
|
|
418
449
|
<hr class="sdocs-divider" />
|
|
419
450
|
<div class="sdocs-panels">
|
|
420
451
|
<CollapsiblePanel title="Component Source" defaultExpanded={false}>
|
|
421
|
-
<div class="sdocs-code-block">{@html
|
|
452
|
+
<div class="sdocs-code-block">{@html activePreview.highlightedSource}</div>
|
|
422
453
|
</CollapsiblePanel>
|
|
423
454
|
</div>
|
|
424
455
|
{/if}
|
|
@@ -476,6 +507,31 @@
|
|
|
476
507
|
opacity: 0.4;
|
|
477
508
|
cursor: default;
|
|
478
509
|
}
|
|
510
|
+
.sdocs-preview-tabs {
|
|
511
|
+
display: flex;
|
|
512
|
+
gap: 4px;
|
|
513
|
+
margin-bottom: 12px;
|
|
514
|
+
border-bottom: 1px solid var(--color-base-200);
|
|
515
|
+
}
|
|
516
|
+
.sdocs-preview-tab {
|
|
517
|
+
padding: 7px 14px;
|
|
518
|
+
border: none;
|
|
519
|
+
border-bottom: 2px solid transparent;
|
|
520
|
+
background: none;
|
|
521
|
+
font: inherit;
|
|
522
|
+
font-size: 13px;
|
|
523
|
+
font-weight: 500;
|
|
524
|
+
color: var(--color-base-500);
|
|
525
|
+
cursor: pointer;
|
|
526
|
+
}
|
|
527
|
+
.sdocs-preview-tab:hover {
|
|
528
|
+
color: var(--color-base-800);
|
|
529
|
+
}
|
|
530
|
+
.sdocs-preview-tab.active {
|
|
531
|
+
color: var(--color-base-900);
|
|
532
|
+
border-bottom-color: var(--color-accent-500, var(--color-base-900));
|
|
533
|
+
font-weight: 600;
|
|
534
|
+
}
|
|
479
535
|
.sdocs-view-header {
|
|
480
536
|
margin-bottom: 24px;
|
|
481
537
|
}
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
let { doc, activeStylesheet }: Props = $props();
|
|
11
11
|
|
|
12
12
|
const meta = $derived(doc.meta);
|
|
13
|
-
const contentSnippet = $derived(doc.
|
|
13
|
+
const contentSnippet = $derived(doc.content);
|
|
14
14
|
const toc = $derived(doc.toc ?? []);
|
|
15
15
|
|
|
16
16
|
function scrollToHeading(id: string) {
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export { sdocsPlugin } from './vite.js';
|
|
2
|
-
export type { SdocsConfig, ResolvedSdocsConfig, SdocMeta, DocEntry, ParsedProp, ParsedMethod, ParsedState, ParsedCssProp, ComponentData, ExtractedSnippet, } from './types.js';
|
|
2
|
+
export type { SdocsConfig, ResolvedSdocsConfig, SdocMeta, DocEntry, ParsedProp, ParsedMethod, ParsedState, ParsedCssProp, ComponentData, ExtractedSnippet, PreviewEntry, TocHeading, } from './types.js';
|
package/dist/server/app-gen.js
CHANGED
|
@@ -3,9 +3,10 @@ import { existsSync } from 'node:fs';
|
|
|
3
3
|
import { tmpdir } from 'node:os';
|
|
4
4
|
import { dirname, resolve, join } from 'node:path';
|
|
5
5
|
import { fileURLToPath } from 'node:url';
|
|
6
|
-
import { discoverDocFiles
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
6
|
+
import { discoverDocFiles } from './discovery.js';
|
|
7
|
+
import { parseSdoc } from '../language/index.js';
|
|
8
|
+
import { planEntitySnippets } from './doc-model.js';
|
|
9
|
+
import { encodeEntityId, setDocPathRoot } from './snippet-compiler.js';
|
|
9
10
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
10
11
|
/** Source Explorer directory in the installed package (dist/explorer, next to dist/server) */
|
|
11
12
|
function getExplorerSourceDir() {
|
|
@@ -141,23 +142,19 @@ function generatePreviewHtml(iframeVirtualId, css) {
|
|
|
141
142
|
</body>
|
|
142
143
|
</html>`;
|
|
143
144
|
}
|
|
144
|
-
/** Discover doc files and
|
|
145
|
+
/** Discover doc files and plan snippet slugs per entity (lightweight, no highlighting) */
|
|
145
146
|
async function discoverSnippets(config, cwd) {
|
|
146
147
|
const files = await discoverDocFiles(config.include, cwd);
|
|
147
148
|
const results = [];
|
|
148
149
|
for (const filePath of files) {
|
|
149
150
|
const source = await readFile(filePath, 'utf-8');
|
|
150
|
-
const
|
|
151
|
-
|
|
152
|
-
results.push({
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
if (!hasDefaultSnippet(snippets)) {
|
|
158
|
-
names.unshift('Default');
|
|
159
|
-
}
|
|
160
|
-
results.push({ filePath, snippetNames: names });
|
|
151
|
+
const doc = parseSdoc(source);
|
|
152
|
+
for (const entity of doc.entities) {
|
|
153
|
+
results.push({
|
|
154
|
+
filePath,
|
|
155
|
+
entitySlug: entity.slug,
|
|
156
|
+
snippetSlugs: planEntitySnippets(entity).map((s) => s.slug),
|
|
157
|
+
});
|
|
161
158
|
}
|
|
162
159
|
}
|
|
163
160
|
return results;
|
|
@@ -186,15 +183,15 @@ export async function generateBuildFiles(config, cwd) {
|
|
|
186
183
|
};
|
|
187
184
|
// Discover snippets and generate preview HTML pages
|
|
188
185
|
const docSnippets = await discoverSnippets(config, cwd);
|
|
189
|
-
for (const { filePath,
|
|
190
|
-
const encoded =
|
|
191
|
-
for (const
|
|
192
|
-
const iframeId = `/@sdocs/iframe/${encoded}/${
|
|
186
|
+
for (const { filePath, entitySlug, snippetSlugs } of docSnippets) {
|
|
187
|
+
const encoded = encodeEntityId(filePath, entitySlug);
|
|
188
|
+
for (const snippetSlug of snippetSlugs) {
|
|
189
|
+
const iframeId = `/@sdocs/iframe/${encoded}/${snippetSlug}.svelte`;
|
|
193
190
|
const previewDir = resolve(sdocsDir, 'previews', encoded);
|
|
194
191
|
await mkdir(previewDir, { recursive: true });
|
|
195
|
-
const previewPath = resolve(previewDir, `${
|
|
192
|
+
const previewPath = resolve(previewDir, `${snippetSlug}.html`);
|
|
196
193
|
await writeFile(previewPath, generatePreviewHtml(iframeId, config.css));
|
|
197
|
-
const inputKey = `preview-${encoded}-${
|
|
194
|
+
const inputKey = `preview-${encoded}-${snippetSlug}`;
|
|
198
195
|
inputs[inputKey] = previewPath;
|
|
199
196
|
}
|
|
200
197
|
}
|
|
@@ -1,6 +1,4 @@
|
|
|
1
1
|
/** Discover all .sdoc files matching the include patterns */
|
|
2
2
|
export declare function discoverDocFiles(include: string[], root: string): Promise<string[]>;
|
|
3
|
-
/** Determine the sdoc kind from the file path */
|
|
4
|
-
export declare function getSdocKind(filePath: string): 'component' | 'page' | 'layout';
|
|
5
3
|
/** Get the absolute path for a relative import */
|
|
6
4
|
export declare function resolveImportPath(importPath: string, fromFile: string): string;
|
package/dist/server/discovery.js
CHANGED
|
@@ -8,15 +8,6 @@ export async function discoverDocFiles(include, root) {
|
|
|
8
8
|
});
|
|
9
9
|
return files.sort();
|
|
10
10
|
}
|
|
11
|
-
/** Determine the sdoc kind from the file path */
|
|
12
|
-
export function getSdocKind(filePath) {
|
|
13
|
-
const name = filePath.split('/').pop() ?? '';
|
|
14
|
-
if (name.includes('.page.'))
|
|
15
|
-
return 'page';
|
|
16
|
-
if (name.includes('.layout.'))
|
|
17
|
-
return 'layout';
|
|
18
|
-
return 'component';
|
|
19
|
-
}
|
|
20
11
|
/** Get the absolute path for a relative import */
|
|
21
12
|
export function resolveImportPath(importPath, fromFile) {
|
|
22
13
|
const dir = fromFile.substring(0, fromFile.lastIndexOf('/'));
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared entity → snippet planning. Both the Vite plugin (URL generation)
|
|
3
|
+
* and the CLI app-gen (pre-computed rollup inputs) derive snippet slugs
|
|
4
|
+
* from this module, so build inputs always byte-match plugin output.
|
|
5
|
+
*/
|
|
6
|
+
import { type SdocEntity } from '../language/index.js';
|
|
7
|
+
export type SnippetRole = 'preview' | 'example' | 'content';
|
|
8
|
+
export interface PlannedSnippet {
|
|
9
|
+
name: string;
|
|
10
|
+
slug: string;
|
|
11
|
+
role: SnippetRole;
|
|
12
|
+
body: string;
|
|
13
|
+
}
|
|
14
|
+
/** URL-safe slug for an example snippet. The 'x-' prefix keeps example
|
|
15
|
+
* slugs disjoint from preview slugs and 'content'. */
|
|
16
|
+
export declare function exampleSlug(title: string): string;
|
|
17
|
+
/** URL-safe slug for a preview snippet (from its tab label). */
|
|
18
|
+
export declare function previewSlug(label: string): string;
|
|
19
|
+
/** The snippets one entity produces, in order: previews, then examples,
|
|
20
|
+
* or the single 'content' body for PAGE/LAYOUT. */
|
|
21
|
+
export declare function planEntitySnippets(entity: SdocEntity): PlannedSnippet[];
|
|
22
|
+
/** Extract import statements from the file-level script content. */
|
|
23
|
+
export declare function extractImports(scriptContent: string): string[];
|
|
24
|
+
/** Resolve a preview's component identifier to an absolute .svelte path via
|
|
25
|
+
* the file's imports. Returns null when the identifier isn't imported. */
|
|
26
|
+
export declare function resolveComponentImport(componentName: string, imports: string[], docFilePath: string): string | null;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared entity → snippet planning. Both the Vite plugin (URL generation)
|
|
3
|
+
* and the CLI app-gen (pre-computed rollup inputs) derive snippet slugs
|
|
4
|
+
* from this module, so build inputs always byte-match plugin output.
|
|
5
|
+
*/
|
|
6
|
+
import { resolve, dirname } from 'node:path';
|
|
7
|
+
import { slugifyTitle } from '../language/index.js';
|
|
8
|
+
/** URL-safe slug for an example snippet. The 'x-' prefix keeps example
|
|
9
|
+
* slugs disjoint from preview slugs and 'content'. */
|
|
10
|
+
export function exampleSlug(title) {
|
|
11
|
+
return 'x-' + slugifyTitle(title);
|
|
12
|
+
}
|
|
13
|
+
/** URL-safe slug for a preview snippet (from its tab label). */
|
|
14
|
+
export function previewSlug(label) {
|
|
15
|
+
return slugifyTitle(label);
|
|
16
|
+
}
|
|
17
|
+
/** The snippets one entity produces, in order: previews, then examples,
|
|
18
|
+
* or the single 'content' body for PAGE/LAYOUT. */
|
|
19
|
+
export function planEntitySnippets(entity) {
|
|
20
|
+
if (entity.kind === 'DOCS') {
|
|
21
|
+
return [
|
|
22
|
+
...entity.previews.map((p) => ({
|
|
23
|
+
name: p.label,
|
|
24
|
+
slug: previewSlug(p.label),
|
|
25
|
+
role: 'preview',
|
|
26
|
+
body: p.body,
|
|
27
|
+
})),
|
|
28
|
+
...entity.examples.map((e) => ({
|
|
29
|
+
name: e.title,
|
|
30
|
+
slug: exampleSlug(e.title),
|
|
31
|
+
role: 'example',
|
|
32
|
+
body: e.body,
|
|
33
|
+
})),
|
|
34
|
+
];
|
|
35
|
+
}
|
|
36
|
+
return [{ name: 'Content', slug: 'content', role: 'content', body: entity.body }];
|
|
37
|
+
}
|
|
38
|
+
/** Extract import statements from the file-level script content. */
|
|
39
|
+
export function extractImports(scriptContent) {
|
|
40
|
+
const imports = [];
|
|
41
|
+
const regex = /^\s*import\s+.+$/gm;
|
|
42
|
+
let match;
|
|
43
|
+
while ((match = regex.exec(scriptContent)) !== null) {
|
|
44
|
+
imports.push(match[0].trim());
|
|
45
|
+
}
|
|
46
|
+
return imports;
|
|
47
|
+
}
|
|
48
|
+
/** Resolve a preview's component identifier to an absolute .svelte path via
|
|
49
|
+
* the file's imports. Returns null when the identifier isn't imported. */
|
|
50
|
+
export function resolveComponentImport(componentName, imports, docFilePath) {
|
|
51
|
+
for (const imp of imports) {
|
|
52
|
+
const match = imp.match(new RegExp(`import\\s+${componentName}\\s+from\\s+['"](.+?)['"]`));
|
|
53
|
+
if (match) {
|
|
54
|
+
return resolve(dirname(docFilePath), match[1]);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* [PAGE] body transform: markdown-first with Svelte conveniences.
|
|
3
|
+
*
|
|
4
|
+
* The body is CommonMark/GFM; prose passes through untouched so
|
|
5
|
+
* `{expression}` interpolation and `<Component />` islands compile in the
|
|
6
|
+
* generated Svelte preview. Code fences and inline code are INERT: they are
|
|
7
|
+
* highlighted (fences) or wrapped (inline) at transform time with `{` and
|
|
8
|
+
* `}` escaped, so nothing inside code ever interpolates.
|
|
9
|
+
*/
|
|
10
|
+
import type { TocHeading } from '../types.js';
|
|
11
|
+
export interface RenderedPage {
|
|
12
|
+
html: string;
|
|
13
|
+
toc: TocHeading[];
|
|
14
|
+
}
|
|
15
|
+
export declare function renderPageMarkdown(source: string): Promise<RenderedPage>;
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* [PAGE] body transform: markdown-first with Svelte conveniences.
|
|
3
|
+
*
|
|
4
|
+
* The body is CommonMark/GFM; prose passes through untouched so
|
|
5
|
+
* `{expression}` interpolation and `<Component />` islands compile in the
|
|
6
|
+
* generated Svelte preview. Code fences and inline code are INERT: they are
|
|
7
|
+
* highlighted (fences) or wrapped (inline) at transform time with `{` and
|
|
8
|
+
* `}` escaped, so nothing inside code ever interpolates.
|
|
9
|
+
*/
|
|
10
|
+
import { Marked } from 'marked';
|
|
11
|
+
import { highlight } from './highlighter.js';
|
|
12
|
+
function slugify(text) {
|
|
13
|
+
return (text
|
|
14
|
+
.toLowerCase()
|
|
15
|
+
.replace(/[^\w\s-]/g, '')
|
|
16
|
+
.trim()
|
|
17
|
+
.replace(/[\s_]+/g, '-') || 'section');
|
|
18
|
+
}
|
|
19
|
+
/** Make markup Svelte-inert: braces never interpolate. */
|
|
20
|
+
function escapeBraces(html) {
|
|
21
|
+
return html.replace(/\{/g, '{').replace(/\}/g, '}');
|
|
22
|
+
}
|
|
23
|
+
function escapeHtml(text) {
|
|
24
|
+
return text
|
|
25
|
+
.replace(/&/g, '&')
|
|
26
|
+
.replace(/</g, '<')
|
|
27
|
+
.replace(/>/g, '>')
|
|
28
|
+
.replace(/"/g, '"');
|
|
29
|
+
}
|
|
30
|
+
function plainText(tokens) {
|
|
31
|
+
let out = '';
|
|
32
|
+
for (const token of tokens) {
|
|
33
|
+
if ('tokens' in token && token.tokens?.length)
|
|
34
|
+
out += plainText(token.tokens);
|
|
35
|
+
else if ('text' in token)
|
|
36
|
+
out += token.text;
|
|
37
|
+
}
|
|
38
|
+
return out;
|
|
39
|
+
}
|
|
40
|
+
export async function renderPageMarkdown(source) {
|
|
41
|
+
const toc = [];
|
|
42
|
+
const usedIds = new Set();
|
|
43
|
+
const headingIds = new WeakMap();
|
|
44
|
+
const fenceHtml = new WeakMap();
|
|
45
|
+
const marked = new Marked({ gfm: true });
|
|
46
|
+
const tokens = marked.lexer(source);
|
|
47
|
+
const walk = (list) => {
|
|
48
|
+
for (const token of list) {
|
|
49
|
+
if (token.type === 'heading') {
|
|
50
|
+
const text = plainText(token.tokens ?? []);
|
|
51
|
+
const baseId = slugify(text);
|
|
52
|
+
let id = baseId;
|
|
53
|
+
for (let n = 2; usedIds.has(id); n++)
|
|
54
|
+
id = `${baseId}-${n}`;
|
|
55
|
+
usedIds.add(id);
|
|
56
|
+
headingIds.set(token, id);
|
|
57
|
+
if (token.depth >= 2 && token.depth <= 4) {
|
|
58
|
+
toc.push({ text, level: token.depth, id });
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
if ('tokens' in token && token.tokens)
|
|
62
|
+
walk(token.tokens);
|
|
63
|
+
if ('items' in token && token.items)
|
|
64
|
+
walk(token.items);
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
walk(tokens);
|
|
68
|
+
// Highlight fences up front (the renderer hooks below must be sync)
|
|
69
|
+
for (const token of tokens) {
|
|
70
|
+
if (token.type === 'code') {
|
|
71
|
+
const lang = (token.lang ?? '').trim().split(/\s+/)[0] || 'text';
|
|
72
|
+
try {
|
|
73
|
+
fenceHtml.set(token, escapeBraces(await highlight(token.text, lang)));
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
fenceHtml.set(token, `<pre><code>${escapeBraces(escapeHtml(token.text))}</code></pre>`);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
marked.use({
|
|
81
|
+
renderer: {
|
|
82
|
+
heading(token) {
|
|
83
|
+
const html = this.parser.parseInline(token.tokens);
|
|
84
|
+
const id = headingIds.get(token);
|
|
85
|
+
return id
|
|
86
|
+
? `<h${token.depth} id="${id}">${html}</h${token.depth}>\n`
|
|
87
|
+
: `<h${token.depth}>${html}</h${token.depth}>\n`;
|
|
88
|
+
},
|
|
89
|
+
code(token) {
|
|
90
|
+
return (fenceHtml.get(token) ??
|
|
91
|
+
`<pre><code>${escapeBraces(escapeHtml(token.text))}</code></pre>\n`);
|
|
92
|
+
},
|
|
93
|
+
codespan(token) {
|
|
94
|
+
return `<code>${escapeBraces(escapeHtml(token.text))}</code>`;
|
|
95
|
+
},
|
|
96
|
+
// Prose passes through as-is: component islands stay component
|
|
97
|
+
// tags, {expressions} stay expressions.
|
|
98
|
+
html(token) {
|
|
99
|
+
return token.text;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
return { html: marked.parser(tokens), toc };
|
|
104
|
+
}
|
|
@@ -4,8 +4,12 @@ export declare function base64urlEncode(str: string): string;
|
|
|
4
4
|
export declare function base64urlDecode(str: string): string;
|
|
5
5
|
/** Set the root that doc paths are encoded against (the Vite root / staging dir) */
|
|
6
6
|
export declare function setDocPathRoot(root: string): void;
|
|
7
|
-
/** Encode a doc file
|
|
8
|
-
|
|
7
|
+
/** Encode a doc entity (file + entity slug) for URLs and emitted file names.
|
|
8
|
+
* The slug rides inside the token as a '#' fragment so every URL shape keeps
|
|
9
|
+
* exactly one token path segment. */
|
|
10
|
+
export declare function encodeEntityId(filePath: string, entitySlug: string): string;
|
|
11
|
+
/** The docEntries key for one entity of one file */
|
|
12
|
+
export declare function entityKey(filePath: string, entitySlug: string): string;
|
|
9
13
|
/** Resolve relative imports to absolute paths for use in virtual components */
|
|
10
14
|
export declare function resolveImportsToAbsolute(imports: string[], docFilePath: string): string[];
|
|
11
15
|
/** Generate a virtual Svelte iframe wrapper component for a snippet.
|
|
@@ -23,26 +27,22 @@ export interface StaticCssLink {
|
|
|
23
27
|
}
|
|
24
28
|
/** Generate the HTML page for a preview emitted into a host app's build. */
|
|
25
29
|
export declare function generateStaticPreviewHtml(scriptSrc: string, cssLinks: StaticCssLink[]): string;
|
|
30
|
+
export interface ParsedSnippetId {
|
|
31
|
+
docFilePath: string;
|
|
32
|
+
entitySlug: string;
|
|
33
|
+
snippetSlug: string;
|
|
34
|
+
}
|
|
26
35
|
/** Build the virtual module ID for an iframe wrapper component */
|
|
27
|
-
export declare function iframeVirtualId(docFilePath: string,
|
|
36
|
+
export declare function iframeVirtualId(docFilePath: string, entitySlug: string, snippetSlug: string): string;
|
|
28
37
|
/** Build the preview URL for an iframe HTML page (dev mode) */
|
|
29
|
-
export declare function previewUrl(docFilePath: string,
|
|
38
|
+
export declare function previewUrl(docFilePath: string, entitySlug: string, snippetSlug: string): string;
|
|
30
39
|
/** Build the preview URL for static build output */
|
|
31
|
-
export declare function buildPreviewUrl(docFilePath: string,
|
|
40
|
+
export declare function buildPreviewUrl(docFilePath: string, entitySlug: string, snippetSlug: string): string;
|
|
32
41
|
/** Virtual module ID for a preview's mount script (embedded production builds) */
|
|
33
|
-
export declare function mountVirtualId(docFilePath: string,
|
|
42
|
+
export declare function mountVirtualId(docFilePath: string, entitySlug: string, snippetSlug: string): string;
|
|
34
43
|
/** Parse a mount virtual ID back into its parts */
|
|
35
|
-
export declare function parseMountId(id: string):
|
|
36
|
-
docFilePath: string;
|
|
37
|
-
snippetName: string;
|
|
38
|
-
} | null;
|
|
44
|
+
export declare function parseMountId(id: string): ParsedSnippetId | null;
|
|
39
45
|
/** Parse an iframe virtual ID back into its parts */
|
|
40
|
-
export declare function parseIframeId(id: string):
|
|
41
|
-
docFilePath: string;
|
|
42
|
-
snippetName: string;
|
|
43
|
-
} | null;
|
|
46
|
+
export declare function parseIframeId(id: string): ParsedSnippetId | null;
|
|
44
47
|
/** Parse a preview URL back into its parts */
|
|
45
|
-
export declare function parsePreviewUrl(url: string):
|
|
46
|
-
docFilePath: string;
|
|
47
|
-
snippetName: string;
|
|
48
|
-
} | null;
|
|
48
|
+
export declare function parsePreviewUrl(url: string): ParsedSnippetId | null;
|