designmockdown 0.0.0-stage → 0.2.1
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 +130 -2
- package/README.zh-CN.md +129 -0
- package/dist/app/assets/index-CmugGtLS.js +15 -0
- package/dist/app/assets/index-CwhgxL4e.css +1 -0
- package/dist/app/index.html +13 -0
- package/dist/lib/cli.d.ts +11 -0
- package/dist/lib/cli.d.ts.map +1 -0
- package/dist/lib/cli.js +968 -0
- package/dist/lib/cli.js.map +1 -0
- package/dist/lib/collab/client.d.ts +20 -0
- package/dist/lib/collab/client.d.ts.map +1 -0
- package/dist/lib/collab/client.js +126 -0
- package/dist/lib/collab/client.js.map +1 -0
- package/dist/lib/collab/index.d.ts +40 -0
- package/dist/lib/collab/index.d.ts.map +1 -0
- package/dist/lib/collab/index.js +154 -0
- package/dist/lib/collab/index.js.map +1 -0
- package/dist/lib/collab/room.d.ts +111 -0
- package/dist/lib/collab/room.d.ts.map +1 -0
- package/dist/lib/collab/room.js +120 -0
- package/dist/lib/collab/room.js.map +1 -0
- package/dist/lib/collab/ws.d.ts +33 -0
- package/dist/lib/collab/ws.d.ts.map +1 -0
- package/dist/lib/collab/ws.js +191 -0
- package/dist/lib/collab/ws.js.map +1 -0
- package/dist/lib/components/basic.d.ts +8 -0
- package/dist/lib/components/basic.d.ts.map +1 -0
- package/dist/lib/components/basic.js +181 -0
- package/dist/lib/components/basic.js.map +1 -0
- package/dist/lib/components/config.d.ts +26 -0
- package/dist/lib/components/config.d.ts.map +1 -0
- package/dist/lib/components/config.js +94 -0
- package/dist/lib/components/config.js.map +1 -0
- package/dist/lib/components/containers.d.ts +9 -0
- package/dist/lib/components/containers.d.ts.map +1 -0
- package/dist/lib/components/containers.js +102 -0
- package/dist/lib/components/containers.js.map +1 -0
- package/dist/lib/components/controls.d.ts +8 -0
- package/dist/lib/components/controls.d.ts.map +1 -0
- package/dist/lib/components/controls.js +376 -0
- package/dist/lib/components/controls.js.map +1 -0
- package/dist/lib/components/data.d.ts +8 -0
- package/dist/lib/components/data.d.ts.map +1 -0
- package/dist/lib/components/data.js +494 -0
- package/dist/lib/components/data.js.map +1 -0
- package/dist/lib/components/feedback.d.ts +8 -0
- package/dist/lib/components/feedback.d.ts.map +1 -0
- package/dist/lib/components/feedback.js +151 -0
- package/dist/lib/components/feedback.js.map +1 -0
- package/dist/lib/components/icons.d.ts +28 -0
- package/dist/lib/components/icons.d.ts.map +1 -0
- package/dist/lib/components/icons.js +85 -0
- package/dist/lib/components/icons.js.map +1 -0
- package/dist/lib/components/layout.d.ts +10 -0
- package/dist/lib/components/layout.d.ts.map +1 -0
- package/dist/lib/components/layout.js +82 -0
- package/dist/lib/components/layout.js.map +1 -0
- package/dist/lib/components/nav.d.ts +8 -0
- package/dist/lib/components/nav.d.ts.map +1 -0
- package/dist/lib/components/nav.js +216 -0
- package/dist/lib/components/nav.js.map +1 -0
- package/dist/lib/components/plugin.d.ts +27 -0
- package/dist/lib/components/plugin.d.ts.map +1 -0
- package/dist/lib/components/plugin.js +152 -0
- package/dist/lib/components/plugin.js.map +1 -0
- package/dist/lib/components/registry.d.ts +111 -0
- package/dist/lib/components/registry.d.ts.map +1 -0
- package/dist/lib/components/registry.js +375 -0
- package/dist/lib/components/registry.js.map +1 -0
- package/dist/lib/components/types.d.ts +111 -0
- package/dist/lib/components/types.d.ts.map +1 -0
- package/dist/lib/components/types.js +6 -0
- package/dist/lib/components/types.js.map +1 -0
- package/dist/lib/core/anim.d.ts +31 -0
- package/dist/lib/core/anim.d.ts.map +1 -0
- package/dist/lib/core/anim.js +95 -0
- package/dist/lib/core/anim.js.map +1 -0
- package/dist/lib/core/ast.d.ts +174 -0
- package/dist/lib/core/ast.d.ts.map +1 -0
- package/dist/lib/core/ast.js +42 -0
- package/dist/lib/core/ast.js.map +1 -0
- package/dist/lib/core/chart-source.d.ts +37 -0
- package/dist/lib/core/chart-source.d.ts.map +1 -0
- package/dist/lib/core/chart-source.js +125 -0
- package/dist/lib/core/chart-source.js.map +1 -0
- package/dist/lib/core/data.d.ts +32 -0
- package/dist/lib/core/data.d.ts.map +1 -0
- package/dist/lib/core/data.js +150 -0
- package/dist/lib/core/data.js.map +1 -0
- package/dist/lib/core/define.d.ts +16 -0
- package/dist/lib/core/define.d.ts.map +1 -0
- package/dist/lib/core/define.js +232 -0
- package/dist/lib/core/define.js.map +1 -0
- package/dist/lib/core/diagnostics.d.ts +64 -0
- package/dist/lib/core/diagnostics.d.ts.map +1 -0
- package/dist/lib/core/diagnostics.js +62 -0
- package/dist/lib/core/diagnostics.js.map +1 -0
- package/dist/lib/core/errors.d.ts +27 -0
- package/dist/lib/core/errors.d.ts.map +1 -0
- package/dist/lib/core/errors.js +70 -0
- package/dist/lib/core/errors.js.map +1 -0
- package/dist/lib/core/expand.d.ts +21 -0
- package/dist/lib/core/expand.d.ts.map +1 -0
- package/dist/lib/core/expand.js +226 -0
- package/dist/lib/core/expand.js.map +1 -0
- package/dist/lib/core/expr.d.ts +29 -0
- package/dist/lib/core/expr.d.ts.map +1 -0
- package/dist/lib/core/expr.js +313 -0
- package/dist/lib/core/expr.js.map +1 -0
- package/dist/lib/core/format.d.ts +30 -0
- package/dist/lib/core/format.d.ts.map +1 -0
- package/dist/lib/core/format.js +106 -0
- package/dist/lib/core/format.js.map +1 -0
- package/dist/lib/core/lexer.d.ts +54 -0
- package/dist/lib/core/lexer.d.ts.map +1 -0
- package/dist/lib/core/lexer.js +311 -0
- package/dist/lib/core/lexer.js.map +1 -0
- package/dist/lib/core/parser.d.ts +29 -0
- package/dist/lib/core/parser.d.ts.map +1 -0
- package/dist/lib/core/parser.js +674 -0
- package/dist/lib/core/parser.js.map +1 -0
- package/dist/lib/core/resolver.d.ts +37 -0
- package/dist/lib/core/resolver.d.ts.map +1 -0
- package/dist/lib/core/resolver.js +240 -0
- package/dist/lib/core/resolver.js.map +1 -0
- package/dist/lib/core/spec.d.ts +87 -0
- package/dist/lib/core/spec.d.ts.map +1 -0
- package/dist/lib/core/spec.js +207 -0
- package/dist/lib/core/spec.js.map +1 -0
- package/dist/lib/core/state.d.ts +19 -0
- package/dist/lib/core/state.d.ts.map +1 -0
- package/dist/lib/core/state.js +64 -0
- package/dist/lib/core/state.js.map +1 -0
- package/dist/lib/core/tokens.d.ts +36 -0
- package/dist/lib/core/tokens.d.ts.map +1 -0
- package/dist/lib/core/tokens.js +198 -0
- package/dist/lib/core/tokens.js.map +1 -0
- package/dist/lib/export/pages.d.ts +39 -0
- package/dist/lib/export/pages.d.ts.map +1 -0
- package/dist/lib/export/pages.js +95 -0
- package/dist/lib/export/pages.js.map +1 -0
- package/dist/lib/export/raster.d.ts +39 -0
- package/dist/lib/export/raster.d.ts.map +1 -0
- package/dist/lib/export/raster.js +152 -0
- package/dist/lib/export/raster.js.map +1 -0
- package/dist/lib/index.d.ts +33 -0
- package/dist/lib/index.d.ts.map +1 -0
- package/dist/lib/index.js +30 -0
- package/dist/lib/index.js.map +1 -0
- package/dist/lib/layout/engine.d.ts +32 -0
- package/dist/lib/layout/engine.d.ts.map +1 -0
- package/dist/lib/layout/engine.js +1063 -0
- package/dist/lib/layout/engine.js.map +1 -0
- package/dist/lib/layout/measure.d.ts +30 -0
- package/dist/lib/layout/measure.d.ts.map +1 -0
- package/dist/lib/layout/measure.js +148 -0
- package/dist/lib/layout/measure.js.map +1 -0
- package/dist/lib/layout/types.d.ts +94 -0
- package/dist/lib/layout/types.d.ts.map +1 -0
- package/dist/lib/layout/types.js +16 -0
- package/dist/lib/layout/types.js.map +1 -0
- package/dist/lib/lsp/server.d.ts +76 -0
- package/dist/lib/lsp/server.d.ts.map +1 -0
- package/dist/lib/lsp/server.js +264 -0
- package/dist/lib/lsp/server.js.map +1 -0
- package/dist/lib/output/docs.d.ts +26 -0
- package/dist/lib/output/docs.d.ts.map +1 -0
- package/dist/lib/output/docs.js +231 -0
- package/dist/lib/output/docs.js.map +1 -0
- package/dist/lib/output/ir.d.ts +105 -0
- package/dist/lib/output/ir.d.ts.map +1 -0
- package/dist/lib/output/ir.js +89 -0
- package/dist/lib/output/ir.js.map +1 -0
- package/dist/lib/output/schema.d.ts +69 -0
- package/dist/lib/output/schema.d.ts.map +1 -0
- package/dist/lib/output/schema.js +84 -0
- package/dist/lib/output/schema.js.map +1 -0
- package/dist/lib/paths.d.ts +12 -0
- package/dist/lib/paths.d.ts.map +1 -0
- package/dist/lib/paths.js +27 -0
- package/dist/lib/paths.js.map +1 -0
- package/dist/lib/playground/examples.d.ts +16 -0
- package/dist/lib/playground/examples.d.ts.map +1 -0
- package/dist/lib/playground/examples.js +515 -0
- package/dist/lib/playground/examples.js.map +1 -0
- package/dist/lib/playground/index.d.ts +51 -0
- package/dist/lib/playground/index.d.ts.map +1 -0
- package/dist/lib/playground/index.js +598 -0
- package/dist/lib/playground/index.js.map +1 -0
- package/dist/lib/render/assets.d.ts +35 -0
- package/dist/lib/render/assets.d.ts.map +1 -0
- package/dist/lib/render/assets.js +114 -0
- package/dist/lib/render/assets.js.map +1 -0
- package/dist/lib/render/backends.d.ts +31 -0
- package/dist/lib/render/backends.d.ts.map +1 -0
- package/dist/lib/render/backends.js +57 -0
- package/dist/lib/render/backends.js.map +1 -0
- package/dist/lib/render/css.d.ts +24 -0
- package/dist/lib/render/css.d.ts.map +1 -0
- package/dist/lib/render/css.js +148 -0
- package/dist/lib/render/css.js.map +1 -0
- package/dist/lib/render/document-html.d.ts +28 -0
- package/dist/lib/render/document-html.d.ts.map +1 -0
- package/dist/lib/render/document-html.js +214 -0
- package/dist/lib/render/document-html.js.map +1 -0
- package/dist/lib/render/dom.d.ts +32 -0
- package/dist/lib/render/dom.d.ts.map +1 -0
- package/dist/lib/render/dom.js +531 -0
- package/dist/lib/render/dom.js.map +1 -0
- package/dist/lib/render/primitives.d.ts +81 -0
- package/dist/lib/render/primitives.d.ts.map +1 -0
- package/dist/lib/render/primitives.js +172 -0
- package/dist/lib/render/primitives.js.map +1 -0
- package/dist/lib/render/react.d.ts +23 -0
- package/dist/lib/render/react.d.ts.map +1 -0
- package/dist/lib/render/react.js +447 -0
- package/dist/lib/render/react.js.map +1 -0
- package/dist/lib/render/svg.d.ts +45 -0
- package/dist/lib/render/svg.d.ts.map +1 -0
- package/dist/lib/render/svg.js +179 -0
- package/dist/lib/render/svg.js.map +1 -0
- package/dist/lib/runtime/proto.d.ts +19 -0
- package/dist/lib/runtime/proto.d.ts.map +1 -0
- package/dist/lib/runtime/proto.js +81 -0
- package/dist/lib/runtime/proto.js.map +1 -0
- package/dist/lib/skill/install.d.ts +28 -0
- package/dist/lib/skill/install.d.ts.map +1 -0
- package/dist/lib/skill/install.js +89 -0
- package/dist/lib/skill/install.js.map +1 -0
- package/dist/lib/workbench/app.d.ts +24 -0
- package/dist/lib/workbench/app.d.ts.map +1 -0
- package/dist/lib/workbench/app.js +45 -0
- package/dist/lib/workbench/app.js.map +1 -0
- package/dist/lib/workbench/assets.d.ts +21 -0
- package/dist/lib/workbench/assets.d.ts.map +1 -0
- package/dist/lib/workbench/assets.js +44 -0
- package/dist/lib/workbench/assets.js.map +1 -0
- package/dist/lib/workbench/client.d.ts +18 -0
- package/dist/lib/workbench/client.d.ts.map +1 -0
- package/dist/lib/workbench/client.js +736 -0
- package/dist/lib/workbench/client.js.map +1 -0
- package/dist/lib/workbench/drag.d.ts +90 -0
- package/dist/lib/workbench/drag.d.ts.map +1 -0
- package/dist/lib/workbench/drag.js +109 -0
- package/dist/lib/workbench/drag.js.map +1 -0
- package/dist/lib/workbench/geometry.d.ts +28 -0
- package/dist/lib/workbench/geometry.d.ts.map +1 -0
- package/dist/lib/workbench/geometry.js +28 -0
- package/dist/lib/workbench/geometry.js.map +1 -0
- package/dist/lib/workbench/select.d.ts +56 -0
- package/dist/lib/workbench/select.d.ts.map +1 -0
- package/dist/lib/workbench/select.js +111 -0
- package/dist/lib/workbench/select.js.map +1 -0
- package/dist/lib/workbench/server.d.ts +223 -0
- package/dist/lib/workbench/server.d.ts.map +1 -0
- package/dist/lib/workbench/server.js +576 -0
- package/dist/lib/workbench/server.js.map +1 -0
- package/dist/lib/workbench/source.d.ts +124 -0
- package/dist/lib/workbench/source.d.ts.map +1 -0
- package/dist/lib/workbench/source.js +405 -0
- package/dist/lib/workbench/source.js.map +1 -0
- package/docs/README.md +49 -0
- package/docs/cheatsheet.md +243 -0
- package/docs/cli.md +95 -0
- package/docs/data.md +112 -0
- package/docs/export.md +106 -0
- package/docs/getting-started.md +105 -0
- package/docs/language.md +136 -0
- package/docs/layout.md +159 -0
- package/docs/plugins.md +82 -0
- package/docs/reference.md +1156 -0
- package/docs/roadmap.md +54 -0
- package/docs/syntax.md +312 -0
- package/docs/workbench.md +125 -0
- package/examples/app.dm +76 -0
- package/examples/assets/avatar.png +0 -0
- package/examples/assets/cover.png +0 -0
- package/examples/assets/logo.svg +4 -0
- package/examples/assets.dm +49 -0
- package/examples/dashboard.dm +64 -0
- package/examples/data.dm +107 -0
- package/examples/layout.dm +93 -0
- package/examples/login.dm +43 -0
- package/examples/login.html +61 -0
- package/examples/plugins/rating/README.md +67 -0
- package/examples/plugins/rating/example.dm +29 -0
- package/examples/plugins/rating/index.ts +122 -0
- package/examples/plugins/rating/package.json +22 -0
- package/examples/recipe/assets/broccoli.jpg +0 -0
- package/examples/recipe/assets/cheesecake.jpg +0 -0
- package/examples/recipe/assets/chicken-pot.jpg +0 -0
- package/examples/recipe/assets/noodles.jpg +0 -0
- package/examples/recipe/assets/shrimp.jpg +0 -0
- package/examples/recipe/assets/tomato-beef.jpg +0 -0
- package/examples/recipe/detail.dm +105 -0
- package/examples/recipe/discover.dm +116 -0
- package/examples/recipe/favorites.dm +115 -0
- package/examples/recipe/index.dm +124 -0
- package/examples/recipe/profile.dm +134 -0
- package/examples/recipe/steps.dm +89 -0
- package/examples/settings.dm +108 -0
- package/package.json +85 -4
- package/skills/designmockdown/REFERENCE.md +373 -0
- package/skills/designmockdown/SKILL.md +179 -0
|
@@ -0,0 +1,674 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DesignMockdown Parser: turns the lexer's line stream into an AST.
|
|
3
|
+
*
|
|
4
|
+
* Rules:
|
|
5
|
+
* - one `.dm` file holds one or more pages, each started by `@page <name> {attrs}`;
|
|
6
|
+
* - a single `#` line is not a page title any more: it errors with the @page equivalent;
|
|
7
|
+
* - front-matter supports flat `key: value` only (L2 data: arrives in M2);
|
|
8
|
+
* - indentation expresses parent/child; consecutive `|` lines auto-group into an implicit @table;
|
|
9
|
+
* - `key: value` lines inside `@theme` become tokens.
|
|
10
|
+
*/
|
|
11
|
+
import { DesignMockdownError } from "./errors.js";
|
|
12
|
+
import { CODES } from "./diagnostics.js";
|
|
13
|
+
import { lex } from "./lexer.js";
|
|
14
|
+
import { isContainer } from "./ast.js";
|
|
15
|
+
import { parseDataBlock } from "./data.js";
|
|
16
|
+
/** Directives that only arrive in L3/L4; M1/M2 give a clear pointer */
|
|
17
|
+
const FUTURE_DIRECTIVES = {
|
|
18
|
+
define: 'custom components (L3), arrives in M3',
|
|
19
|
+
state: 'state machines (L4), arrives in M4',
|
|
20
|
+
anim: 'animation (L4), arrives in M4',
|
|
21
|
+
};
|
|
22
|
+
const META_KEYS = new Set(['title', 'canvas', 'theme', 'width', 'height', 'bg', 'data']);
|
|
23
|
+
/** These prefixes inside front-matter mean the closing --- was forgotten */
|
|
24
|
+
const BODY_LIKE_RE = /^(#|@|\||>)/;
|
|
25
|
+
/**
|
|
26
|
+
* Parse a single-page file. Multi-page files must go through parseDocument: a PageNode alone
|
|
27
|
+
* cannot represent them, and silently returning the first page would hide the rest.
|
|
28
|
+
*/
|
|
29
|
+
export function parse(source, file = 'input.dm') {
|
|
30
|
+
const doc = parseDocument(source, file);
|
|
31
|
+
if (doc.pages.length > 1) {
|
|
32
|
+
throw new DesignMockdownError(`"${file}" has ${doc.pages.length} pages`, { line: doc.pages[1].startLine ?? 1, col: 1 }, 'use parseDocument() when a file declares more than one @page');
|
|
33
|
+
}
|
|
34
|
+
return doc.pages[0];
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Parse a whole .dm file into a document of pages.
|
|
38
|
+
*
|
|
39
|
+
* Content before the first `@page` may only be `@theme` / `@define`: those are document
|
|
40
|
+
* metadata. A stray component there has no page to belong to, so it errors instead of vanishing.
|
|
41
|
+
*/
|
|
42
|
+
export function parseDocument(source, file = 'input.dm') {
|
|
43
|
+
const lines = lex(source);
|
|
44
|
+
// Pre-page container: holds document metadata, then must be empty before returning
|
|
45
|
+
const docRoot = {
|
|
46
|
+
type: 'page',
|
|
47
|
+
id: 'doc-root',
|
|
48
|
+
pos: { line: 1, col: 1 },
|
|
49
|
+
title: '',
|
|
50
|
+
meta: {},
|
|
51
|
+
children: [],
|
|
52
|
+
slots: {},
|
|
53
|
+
defs: {},
|
|
54
|
+
};
|
|
55
|
+
const doc = {
|
|
56
|
+
type: 'document',
|
|
57
|
+
id: 'document',
|
|
58
|
+
pos: { line: 1, col: 1 },
|
|
59
|
+
pages: [],
|
|
60
|
+
meta: docRoot.meta,
|
|
61
|
+
tokens: {},
|
|
62
|
+
defs: {},
|
|
63
|
+
};
|
|
64
|
+
const page = docRoot;
|
|
65
|
+
const pages = doc.pages;
|
|
66
|
+
let idCounter = 0;
|
|
67
|
+
const nextId = (name) => `${name}-${++idCounter}`;
|
|
68
|
+
const seenIds = new Set();
|
|
69
|
+
let bodyStarted = false;
|
|
70
|
+
let fmOpened = false;
|
|
71
|
+
let inFrontMatter = false;
|
|
72
|
+
let fmStartLine = 0;
|
|
73
|
+
/** Which front-matter key the current indented lines belong to (only `data` takes a block) */
|
|
74
|
+
let fmKey;
|
|
75
|
+
let dataLines = [];
|
|
76
|
+
const flushData = () => {
|
|
77
|
+
if (dataLines.length === 0)
|
|
78
|
+
return;
|
|
79
|
+
page.meta.data = parseDataBlock(dataLines);
|
|
80
|
+
dataLines = [];
|
|
81
|
+
};
|
|
82
|
+
// The stack starts at the document root: lines before the first @page land there and are
|
|
83
|
+
// rejected at the end, which is how "content outside @page" is reported instead of dropped
|
|
84
|
+
const stack = [{ indent: -1, node: page }];
|
|
85
|
+
let prevIndent = -1;
|
|
86
|
+
let prevWasLeaf = false;
|
|
87
|
+
// Last line that belonged to the page body. A node's endLine is stamped when it is closed, i.e.
|
|
88
|
+
// when a later line proves it has no more children - the parser is the only place that knows.
|
|
89
|
+
let lastBodyLine = 0;
|
|
90
|
+
/**
|
|
91
|
+
* Close the innermost open node, remembering where it ended.
|
|
92
|
+
*
|
|
93
|
+
* Table rows are why this matters: they live in `rows`, never in `children`, so a walk of the
|
|
94
|
+
* tree cannot see them and a `@table` would look like a single line - inserting anything after
|
|
95
|
+
* it would land between the header and its own rows.
|
|
96
|
+
*/
|
|
97
|
+
const closeTop = () => {
|
|
98
|
+
const top = stack.pop();
|
|
99
|
+
if (top && top.node !== page && top.node.endLine === undefined)
|
|
100
|
+
top.node.endLine = lastBodyLine;
|
|
101
|
+
};
|
|
102
|
+
const seenPageKeys = new Set();
|
|
103
|
+
/**
|
|
104
|
+
* Begin a new page. `@page` is a file-level boundary: every following line belongs to it until
|
|
105
|
+
* the next `@page`, regardless of indentation (the same ownership rule as `##` sections).
|
|
106
|
+
*/
|
|
107
|
+
const startPage = (line) => {
|
|
108
|
+
if (line.indent > 0) {
|
|
109
|
+
throw new DesignMockdownError('@page must start at column 1', { line: line.lineNo, col: line.indent + 1 }, 'remove the indentation before "@page"');
|
|
110
|
+
}
|
|
111
|
+
const name = (line.content ?? '').trim();
|
|
112
|
+
if (name === '') {
|
|
113
|
+
throw new DesignMockdownError('@page needs a name', { line: line.lineNo, col: 1 }, 'write "@page 首页 {canvas=phone}"');
|
|
114
|
+
}
|
|
115
|
+
const key = line.id ?? name;
|
|
116
|
+
if (seenPageKeys.has(key)) {
|
|
117
|
+
throw new DesignMockdownError(`duplicate page "${key}"`, { line: line.lineNo, col: 1 }, 'page names and #ids must be unique: goto: and the canvas both address pages by them');
|
|
118
|
+
}
|
|
119
|
+
seenPageKeys.add(key);
|
|
120
|
+
const node = {
|
|
121
|
+
type: 'page',
|
|
122
|
+
// The first page keeps the id `page` so single-page SVG output (clip ids, edit hooks)
|
|
123
|
+
// stays byte-identical to the pre-@page era; later pages get page-2, page-3…
|
|
124
|
+
id: pages.length === 0 ? 'page' : `page-${pages.length + 1}`,
|
|
125
|
+
pos: { line: line.lineNo, col: 1 },
|
|
126
|
+
title: name,
|
|
127
|
+
meta: { ...docRoot.meta },
|
|
128
|
+
children: [],
|
|
129
|
+
slots: {},
|
|
130
|
+
defs: {},
|
|
131
|
+
key,
|
|
132
|
+
startLine: line.lineNo,
|
|
133
|
+
};
|
|
134
|
+
for (const a of line.attrs ?? [])
|
|
135
|
+
applyPageAttr(node, a, line);
|
|
136
|
+
pages.push(node);
|
|
137
|
+
// A new page starts a fresh indentation scope; a previous page's open containers must not
|
|
138
|
+
// swallow the new page's body
|
|
139
|
+
stack.length = 0;
|
|
140
|
+
stack.push({ indent: -1, node });
|
|
141
|
+
prevIndent = -1;
|
|
142
|
+
prevWasLeaf = false;
|
|
143
|
+
bodyStarted = true;
|
|
144
|
+
};
|
|
145
|
+
const registerId = (id, line) => {
|
|
146
|
+
if (seenIds.has(id)) {
|
|
147
|
+
throw new DesignMockdownError(`duplicate id "#${id}"`, { line: line.lineNo, col: 1 }, 'ids must be unique within a page', CODES.duplicateId);
|
|
148
|
+
}
|
|
149
|
+
seenIds.add(id);
|
|
150
|
+
};
|
|
151
|
+
for (const line of lines) {
|
|
152
|
+
if (line.kind === 'blank' || line.kind === 'comment')
|
|
153
|
+
continue;
|
|
154
|
+
// front-matter starts when the first non-blank line is ---
|
|
155
|
+
// it may sit at the very top of the file or after the page title (but not after the body)
|
|
156
|
+
if (!bodyStarted && !fmOpened && line.kind === 'divider') {
|
|
157
|
+
inFrontMatter = true;
|
|
158
|
+
fmOpened = true;
|
|
159
|
+
fmStartLine = line.lineNo;
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
if (inFrontMatter) {
|
|
163
|
+
if (line.kind === 'divider') {
|
|
164
|
+
flushData();
|
|
165
|
+
inFrontMatter = false;
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
if (line.indent > 0) {
|
|
169
|
+
// Indented lines only belong to a `data:` block; anything else is a forgotten closing ---
|
|
170
|
+
if (fmKey !== 'data') {
|
|
171
|
+
throw new DesignMockdownError('nested front-matter is not supported', { line: line.lineNo, col: 1 }, 'only "data:" takes an indented block; keep the other keys flat');
|
|
172
|
+
}
|
|
173
|
+
dataLines.push({ lineNo: line.lineNo, indent: line.indent, text: line.raw.trim() });
|
|
174
|
+
continue;
|
|
175
|
+
}
|
|
176
|
+
const trimmedRaw = line.raw.trim();
|
|
177
|
+
if (BODY_LIKE_RE.test(trimmedRaw)) {
|
|
178
|
+
throw new DesignMockdownError('unclosed front-matter block', { line: fmStartLine, col: 1 }, 'add a closing "---" before the page body');
|
|
179
|
+
}
|
|
180
|
+
const m = /^([A-Za-z_][\w-]*)\s*:\s*(.*)$/.exec(trimmedRaw);
|
|
181
|
+
if (!m) {
|
|
182
|
+
throw new DesignMockdownError(`invalid front-matter line: "${trimmedRaw}"`, { line: line.lineNo, col: 1 }, 'expected "key: value"');
|
|
183
|
+
}
|
|
184
|
+
// A new top-level key ends the previous data block
|
|
185
|
+
flushData();
|
|
186
|
+
if (m[1] === 'data') {
|
|
187
|
+
fmKey = 'data';
|
|
188
|
+
if (m[2].trim() !== '') {
|
|
189
|
+
throw new DesignMockdownError('data: takes an indented block, not an inline value', { line: line.lineNo, col: 1 }, 'write "data:" alone, then indent the keys below it');
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
else {
|
|
193
|
+
fmKey = undefined;
|
|
194
|
+
applyMeta(page.meta, m[1], m[2].trim(), line);
|
|
195
|
+
}
|
|
196
|
+
continue;
|
|
197
|
+
}
|
|
198
|
+
// `@page` is a file-level boundary, intercepted before the indentation rules: it is not a
|
|
199
|
+
// node in the current parent's flow
|
|
200
|
+
if (line.kind === 'component' && line.name === 'page') {
|
|
201
|
+
startPage(line);
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
bodyStarted = true;
|
|
205
|
+
// A leaf line cannot be followed by deeper indentation
|
|
206
|
+
if (prevWasLeaf && line.indent > prevIndent) {
|
|
207
|
+
throw new DesignMockdownError('unexpected indentation', { line: line.lineNo, col: line.indent + 1 }, 'the previous line is a leaf (text/divider/annotation) and cannot have children');
|
|
208
|
+
}
|
|
209
|
+
// Indentation rule: a section owns the following lines at its own indent
|
|
210
|
+
// (so you do not have to indent everything after @section)
|
|
211
|
+
if (line.kind === 'section') {
|
|
212
|
+
while (stack.length > 1 && line.indent <= stack[stack.length - 1].indent)
|
|
213
|
+
closeTop();
|
|
214
|
+
}
|
|
215
|
+
else {
|
|
216
|
+
while (stack.length > 1 && line.indent < stack[stack.length - 1].indent)
|
|
217
|
+
closeTop();
|
|
218
|
+
const top = stack[stack.length - 1];
|
|
219
|
+
if (stack.length > 1 && line.indent === top.indent && top.node.type !== 'section')
|
|
220
|
+
closeTop();
|
|
221
|
+
}
|
|
222
|
+
const parent = stack[stack.length - 1].node;
|
|
223
|
+
switch (line.kind) {
|
|
224
|
+
case 'section': {
|
|
225
|
+
const node = {
|
|
226
|
+
type: 'section',
|
|
227
|
+
id: nextId('section'),
|
|
228
|
+
pos: { line: line.lineNo, col: 1 },
|
|
229
|
+
title: line.title ?? '',
|
|
230
|
+
level: line.level ?? 2,
|
|
231
|
+
children: [],
|
|
232
|
+
slots: {},
|
|
233
|
+
};
|
|
234
|
+
parent.children.push(node);
|
|
235
|
+
stack.push({ indent: line.indent, node });
|
|
236
|
+
prevWasLeaf = false;
|
|
237
|
+
prevIndent = line.indent;
|
|
238
|
+
break;
|
|
239
|
+
}
|
|
240
|
+
case 'component': {
|
|
241
|
+
const name = line.name;
|
|
242
|
+
// @if / @for are control flow, not components: they build their own node types and are
|
|
243
|
+
// expanded away by the resolver before layout ever sees them
|
|
244
|
+
if (name === 'if' || name === 'for') {
|
|
245
|
+
const node = createControlFlow(name, line, nextId(name));
|
|
246
|
+
parent.children.push(node);
|
|
247
|
+
stack.push({ indent: line.indent, node });
|
|
248
|
+
prevWasLeaf = false;
|
|
249
|
+
prevIndent = line.indent;
|
|
250
|
+
break;
|
|
251
|
+
}
|
|
252
|
+
if (FUTURE_DIRECTIVES[name]) {
|
|
253
|
+
throw new DesignMockdownError(`"@${name}" is not available yet`, { line: line.lineNo, col: 1 }, FUTURE_DIRECTIVES[name]);
|
|
254
|
+
}
|
|
255
|
+
const id = line.id ?? nextId(name);
|
|
256
|
+
registerId(id, line);
|
|
257
|
+
const node = createComponent(line, id);
|
|
258
|
+
parent.children.push(node);
|
|
259
|
+
stack.push({ indent: line.indent, node });
|
|
260
|
+
prevWasLeaf = false;
|
|
261
|
+
prevIndent = line.indent;
|
|
262
|
+
break;
|
|
263
|
+
}
|
|
264
|
+
case 'define': {
|
|
265
|
+
// @define collects its indented block as a template. The block is parsed by the same
|
|
266
|
+
// switch, so a template may contain any component, section or control flow.
|
|
267
|
+
const defName = line.name;
|
|
268
|
+
if (page.defs[defName]) {
|
|
269
|
+
throw new DesignMockdownError(`"@${defName}" is already defined`, { line: line.lineNo, col: 1 }, 'pick another name');
|
|
270
|
+
}
|
|
271
|
+
const holder = {
|
|
272
|
+
type: 'component',
|
|
273
|
+
id: `def-${defName}`,
|
|
274
|
+
pos: { line: line.lineNo, col: 1 },
|
|
275
|
+
name: defName,
|
|
276
|
+
content: '',
|
|
277
|
+
attrs: {},
|
|
278
|
+
children: [],
|
|
279
|
+
slots: {},
|
|
280
|
+
};
|
|
281
|
+
page.defs[defName] = holder;
|
|
282
|
+
stack.push({ indent: line.indent, node: holder });
|
|
283
|
+
prevWasLeaf = false;
|
|
284
|
+
prevIndent = line.indent;
|
|
285
|
+
break;
|
|
286
|
+
}
|
|
287
|
+
case 'state': {
|
|
288
|
+
// @state collects its indented block as attribute overrides for the enclosing component.
|
|
289
|
+
// The block is `key: value` lines, the same shape @theme uses.
|
|
290
|
+
const stateName = line.name;
|
|
291
|
+
const parentComp = stack[stack.length - 1]?.node;
|
|
292
|
+
if (!parentComp || parentComp.type !== 'component') {
|
|
293
|
+
throw new DesignMockdownError(`"@state ${stateName}" must be nested inside a component`, { line: line.lineNo, col: 1 }, 'indent it under the component it varies');
|
|
294
|
+
}
|
|
295
|
+
if (parentComp.states?.[stateName]) {
|
|
296
|
+
throw new DesignMockdownError(`state "${stateName}" is already defined`, { line: line.lineNo, col: 1 }, 'pick another name');
|
|
297
|
+
}
|
|
298
|
+
parentComp.states = { ...parentComp.states, [stateName]: {} };
|
|
299
|
+
const holder = {
|
|
300
|
+
type: 'component',
|
|
301
|
+
id: `state-${stateName}`,
|
|
302
|
+
pos: { line: line.lineNo, col: 1 },
|
|
303
|
+
name: stateName,
|
|
304
|
+
content: '',
|
|
305
|
+
attrs: {},
|
|
306
|
+
children: [],
|
|
307
|
+
slots: {},
|
|
308
|
+
};
|
|
309
|
+
// The holder is a scratch node: it is attached so the indentation stack can parent the
|
|
310
|
+
// body lines under it, then harvested into parentComp.states by finalize and dropped
|
|
311
|
+
parentComp.children.push(holder);
|
|
312
|
+
stack.push({ indent: line.indent, node: holder });
|
|
313
|
+
prevWasLeaf = false;
|
|
314
|
+
prevIndent = line.indent;
|
|
315
|
+
break;
|
|
316
|
+
}
|
|
317
|
+
case 'anim': {
|
|
318
|
+
// @anim collects its indented block as timing overrides for the enclosing component.
|
|
319
|
+
// Same shape as @state: `key: value` lines harvested by finalize.
|
|
320
|
+
const animName = line.name;
|
|
321
|
+
const parentComp = stack[stack.length - 1]?.node;
|
|
322
|
+
if (!parentComp || parentComp.type !== 'component') {
|
|
323
|
+
throw new DesignMockdownError(`"@anim ${animName}" must be nested inside a component`, { line: line.lineNo, col: 1 }, 'indent it under the component it animates');
|
|
324
|
+
}
|
|
325
|
+
if (parentComp.anims?.[animName]) {
|
|
326
|
+
throw new DesignMockdownError(`animation "${animName}" is already defined`, { line: line.lineNo, col: 1 }, 'pick another name');
|
|
327
|
+
}
|
|
328
|
+
parentComp.anims = { ...parentComp.anims, [animName]: {} };
|
|
329
|
+
const holder = {
|
|
330
|
+
type: 'component',
|
|
331
|
+
id: `anim-${animName}`,
|
|
332
|
+
pos: { line: line.lineNo, col: 1 },
|
|
333
|
+
name: animName,
|
|
334
|
+
content: '',
|
|
335
|
+
attrs: {},
|
|
336
|
+
children: [],
|
|
337
|
+
slots: {},
|
|
338
|
+
};
|
|
339
|
+
// Attached so the indentation stack can parent the body lines under it, then harvested
|
|
340
|
+
// into parentComp.anims by finalize and dropped
|
|
341
|
+
parentComp.children.push(holder);
|
|
342
|
+
stack.push({ indent: line.indent, node: holder });
|
|
343
|
+
prevWasLeaf = false;
|
|
344
|
+
prevIndent = line.indent;
|
|
345
|
+
break;
|
|
346
|
+
}
|
|
347
|
+
case 'text': {
|
|
348
|
+
parent.children.push({
|
|
349
|
+
type: 'text',
|
|
350
|
+
id: nextId('text'),
|
|
351
|
+
pos: { line: line.lineNo, col: 1 },
|
|
352
|
+
content: line.content ?? '',
|
|
353
|
+
});
|
|
354
|
+
prevWasLeaf = true;
|
|
355
|
+
prevIndent = line.indent;
|
|
356
|
+
break;
|
|
357
|
+
}
|
|
358
|
+
case 'divider': {
|
|
359
|
+
parent.children.push({
|
|
360
|
+
type: 'divider',
|
|
361
|
+
id: nextId('divider'),
|
|
362
|
+
pos: { line: line.lineNo, col: 1 },
|
|
363
|
+
content: '',
|
|
364
|
+
attrs: {},
|
|
365
|
+
});
|
|
366
|
+
prevWasLeaf = true;
|
|
367
|
+
prevIndent = line.indent;
|
|
368
|
+
break;
|
|
369
|
+
}
|
|
370
|
+
case 'annotation': {
|
|
371
|
+
parent.children.push({
|
|
372
|
+
type: 'annotation',
|
|
373
|
+
id: nextId('note'),
|
|
374
|
+
pos: { line: line.lineNo, col: 1 },
|
|
375
|
+
content: line.content ?? '',
|
|
376
|
+
});
|
|
377
|
+
prevWasLeaf = true;
|
|
378
|
+
prevIndent = line.indent;
|
|
379
|
+
break;
|
|
380
|
+
}
|
|
381
|
+
case 'table-row': {
|
|
382
|
+
const cells = line.cells ?? [];
|
|
383
|
+
// Parent is @table, or the previous sibling is @table -> append a row;
|
|
384
|
+
// otherwise create an implicit @table
|
|
385
|
+
const target = parent.type === 'component' && parent.name === 'table'
|
|
386
|
+
? parent
|
|
387
|
+
: parent.children[parent.children.length - 1];
|
|
388
|
+
if (target && target.type === 'component' && target.name === 'table') {
|
|
389
|
+
target.rows = target.rows ?? [];
|
|
390
|
+
target.rows.push(cells);
|
|
391
|
+
// Rows are not children, so the table's extent has to be extended by hand (see closeTop)
|
|
392
|
+
target.endLine = Math.max(target.endLine ?? target.pos.line, line.lineNo);
|
|
393
|
+
}
|
|
394
|
+
else {
|
|
395
|
+
parent.children.push({
|
|
396
|
+
type: 'component',
|
|
397
|
+
id: nextId('table'),
|
|
398
|
+
pos: { line: line.lineNo, col: 1 },
|
|
399
|
+
name: 'table',
|
|
400
|
+
content: '',
|
|
401
|
+
attrs: {},
|
|
402
|
+
children: [],
|
|
403
|
+
slots: {},
|
|
404
|
+
rows: [cells],
|
|
405
|
+
endLine: line.lineNo,
|
|
406
|
+
});
|
|
407
|
+
}
|
|
408
|
+
prevWasLeaf = true;
|
|
409
|
+
prevIndent = line.indent;
|
|
410
|
+
break;
|
|
411
|
+
}
|
|
412
|
+
default:
|
|
413
|
+
break;
|
|
414
|
+
}
|
|
415
|
+
// Every body line counts, including one that only appended a table row
|
|
416
|
+
lastBodyLine = line.lineNo;
|
|
417
|
+
}
|
|
418
|
+
// Nodes still open at the end of the file end at the last body line
|
|
419
|
+
while (stack.length > 1)
|
|
420
|
+
closeTop();
|
|
421
|
+
if (inFrontMatter) {
|
|
422
|
+
throw new DesignMockdownError('unclosed front-matter block', { line: fmStartLine, col: 1 }, 'add a closing "---"');
|
|
423
|
+
}
|
|
424
|
+
if (pages.length === 0) {
|
|
425
|
+
throw new DesignMockdownError('missing @page', { line: 1, col: 1 }, `add "@page ${file.replace(/\.dm$/, '')}" above the first component`);
|
|
426
|
+
}
|
|
427
|
+
// Document metadata lives outside pages: @theme tokens and @define templates. Anything else
|
|
428
|
+
// before the first @page has no page to belong to.
|
|
429
|
+
finalize(docRoot);
|
|
430
|
+
for (const child of docRoot.children) {
|
|
431
|
+
if (child.type === 'component' && child.name === 'theme') {
|
|
432
|
+
Object.assign(doc.tokens, child.tokens ?? {});
|
|
433
|
+
continue;
|
|
434
|
+
}
|
|
435
|
+
throw new DesignMockdownError('content must be inside a @page', child.pos, 'add a "@page 名称" line above it');
|
|
436
|
+
}
|
|
437
|
+
docRoot.children = [];
|
|
438
|
+
doc.defs = docRoot.defs ?? {};
|
|
439
|
+
// A template declared inside one page is still a document-level template: pages share
|
|
440
|
+
// templates, and a duplicate name must be reported rather than silently shadowed
|
|
441
|
+
for (const p of pages) {
|
|
442
|
+
for (const [name, def] of Object.entries(p.defs ?? {})) {
|
|
443
|
+
if (doc.defs[name]) {
|
|
444
|
+
throw new DesignMockdownError(`"@${name}" is already defined`, def.pos, 'component templates are shared by every page');
|
|
445
|
+
}
|
|
446
|
+
doc.defs[name] = def;
|
|
447
|
+
}
|
|
448
|
+
p.defs = {};
|
|
449
|
+
}
|
|
450
|
+
const totalLines = source.split(/\r?\n/).length;
|
|
451
|
+
pages.forEach((p, i) => {
|
|
452
|
+
// Each page gets its own clone of the templates: one page's @state must not leak into
|
|
453
|
+
// another (same reason @define clones are deep copies - pitfall 33)
|
|
454
|
+
p.defs = structuredClone(doc.defs);
|
|
455
|
+
p.meta.tokens = doc.tokens;
|
|
456
|
+
finalize(p);
|
|
457
|
+
p.endLine = (pages[i + 1]?.startLine ?? totalLines + 1) - 1;
|
|
458
|
+
if (p.defs && Object.keys(p.defs).length === 0)
|
|
459
|
+
delete p.defs;
|
|
460
|
+
});
|
|
461
|
+
doc.meta = docRoot.meta;
|
|
462
|
+
return doc;
|
|
463
|
+
}
|
|
464
|
+
/** Build an `@if` / `@for` node from its raw expression text */
|
|
465
|
+
function createControlFlow(name, line, id) {
|
|
466
|
+
const expr = (line.content ?? '').trim();
|
|
467
|
+
const pos = { line: line.lineNo, col: 1 };
|
|
468
|
+
if (name === 'if') {
|
|
469
|
+
// The braces are part of the expression syntax, not an attribute block
|
|
470
|
+
const cond = expr.replace(/^\{/, '').replace(/\}$/, '').trim();
|
|
471
|
+
if (cond === '') {
|
|
472
|
+
throw new DesignMockdownError('@if needs a condition', pos, 'write "@if {user.active}"');
|
|
473
|
+
}
|
|
474
|
+
return { type: 'if', id, pos, cond, children: [], slots: {} };
|
|
475
|
+
}
|
|
476
|
+
const m = /^([A-Za-z_][\w-]*)\s+in\s+(.+)$/.exec(expr);
|
|
477
|
+
if (!m) {
|
|
478
|
+
throw new DesignMockdownError('@for needs "variable in collection"', pos, 'write "@for item in items"');
|
|
479
|
+
}
|
|
480
|
+
return { type: 'for', id, pos, varName: m[1], over: m[2].trim(), children: [], slots: {} };
|
|
481
|
+
}
|
|
482
|
+
function createComponent(line, id) {
|
|
483
|
+
const attrs = {};
|
|
484
|
+
for (const a of line.attrs ?? [])
|
|
485
|
+
attrs[a.key] = a.value;
|
|
486
|
+
return {
|
|
487
|
+
type: 'component',
|
|
488
|
+
id,
|
|
489
|
+
pos: { line: line.lineNo, col: 1 },
|
|
490
|
+
name: line.name,
|
|
491
|
+
content: line.content ?? '',
|
|
492
|
+
attrs,
|
|
493
|
+
children: [],
|
|
494
|
+
slots: {},
|
|
495
|
+
rawTail: line.rawTail,
|
|
496
|
+
};
|
|
497
|
+
}
|
|
498
|
+
/**
|
|
499
|
+
* Apply one `@page {k=v}` attribute.
|
|
500
|
+
*
|
|
501
|
+
* x / y are canvas coordinates and live on the node; everything else is ordinary page
|
|
502
|
+
* metadata, so it goes through applyMeta and inherits its validation and messages.
|
|
503
|
+
*/
|
|
504
|
+
function applyPageAttr(node, attr, line) {
|
|
505
|
+
if (attr.key === 'x' || attr.key === 'y' || attr.key === 'width' || attr.key === 'height') {
|
|
506
|
+
const n = typeof attr.value === 'number' ? attr.value : Number(String(attr.value));
|
|
507
|
+
if (!Number.isFinite(n)) {
|
|
508
|
+
throw new DesignMockdownError(`@page ${attr.key} must be a number`, { line: line.lineNo, col: 1 }, `got "${String(attr.value)}"`);
|
|
509
|
+
}
|
|
510
|
+
if (attr.key === 'x')
|
|
511
|
+
node.x = n;
|
|
512
|
+
else if (attr.key === 'y')
|
|
513
|
+
node.y = n;
|
|
514
|
+
else
|
|
515
|
+
applyMeta(node.meta, attr.key, String(n), line);
|
|
516
|
+
return;
|
|
517
|
+
}
|
|
518
|
+
if (attr.key === 'canvas' || attr.key === 'theme' || attr.key === 'bg') {
|
|
519
|
+
applyMeta(node.meta, attr.key, String(attr.value), line);
|
|
520
|
+
return;
|
|
521
|
+
}
|
|
522
|
+
// `id` is already extracted from the name by the lexer, so it needs no work here
|
|
523
|
+
if (attr.key === 'id')
|
|
524
|
+
return;
|
|
525
|
+
throw new DesignMockdownError(`unknown @page attribute "${attr.key}"`, { line: line.lineNo, col: 1 }, 'supported: canvas, theme, bg, width, height, x, y, id');
|
|
526
|
+
}
|
|
527
|
+
function applyMeta(meta, key, value, line) {
|
|
528
|
+
if (!META_KEYS.has(key)) {
|
|
529
|
+
throw new DesignMockdownError(`unknown front-matter key "${key}"`, { line: line.lineNo, col: 1 }, `supported keys: ${[...META_KEYS].join(', ')}`);
|
|
530
|
+
}
|
|
531
|
+
if (key === 'width' || key === 'height') {
|
|
532
|
+
const n = Number(value);
|
|
533
|
+
if (!Number.isFinite(n) || n <= 0) {
|
|
534
|
+
throw new DesignMockdownError(`"${key}" must be a positive number`, { line: line.lineNo, col: 1 });
|
|
535
|
+
}
|
|
536
|
+
meta[key] = n;
|
|
537
|
+
return;
|
|
538
|
+
}
|
|
539
|
+
if (key === 'title')
|
|
540
|
+
meta.title = value;
|
|
541
|
+
else if (key === 'canvas')
|
|
542
|
+
meta.canvas = value;
|
|
543
|
+
else if (key === 'theme')
|
|
544
|
+
meta.theme = value;
|
|
545
|
+
else if (key === 'bg')
|
|
546
|
+
meta.bg = value;
|
|
547
|
+
}
|
|
548
|
+
/**
|
|
549
|
+
* Coerce a `key: value` scalar the same way the attribute block does, so a state override and an
|
|
550
|
+
* inline attribute end up with the same type (`opacity: 0.5` must be a number, not a string).
|
|
551
|
+
*/
|
|
552
|
+
function coerceScalar(raw) {
|
|
553
|
+
if (raw === 'true')
|
|
554
|
+
return true;
|
|
555
|
+
if (raw === 'false')
|
|
556
|
+
return false;
|
|
557
|
+
if (/^-?\d+(\.\d+)?$/.test(raw))
|
|
558
|
+
return Number(raw);
|
|
559
|
+
return raw;
|
|
560
|
+
}
|
|
561
|
+
/** Post-pass: @theme tokenization and @state harvesting.
|
|
562
|
+
*
|
|
563
|
+
* Slot grouping deliberately does NOT happen here: it runs in the resolver after L2 expansion,
|
|
564
|
+
* because a `@for` inside a component is what produces the slotted children.
|
|
565
|
+
*/
|
|
566
|
+
function finalize(page) {
|
|
567
|
+
const visit = (node) => {
|
|
568
|
+
if (!isContainer(node))
|
|
569
|
+
return;
|
|
570
|
+
// @state bodies were parsed into scratch holder nodes. Harvest their `key: value` lines into
|
|
571
|
+
// the enclosing component's `states` map and drop the holders, so layout never sees them.
|
|
572
|
+
if (node.type === 'component' && node.states) {
|
|
573
|
+
const kept = [];
|
|
574
|
+
for (const child of node.children) {
|
|
575
|
+
if (child.type === 'component' && child.id.startsWith('state-') && node.states[child.name]) {
|
|
576
|
+
const overrides = {};
|
|
577
|
+
for (const line of child.children) {
|
|
578
|
+
if (line.type !== 'text') {
|
|
579
|
+
throw new DesignMockdownError(`invalid line inside @state ${child.name}: "${line.type}"`, line.pos, '@state only accepts "key: value" lines');
|
|
580
|
+
}
|
|
581
|
+
const m = /^([A-Za-z_][\w-]*)\s*:\s*(.+)$/.exec(line.content);
|
|
582
|
+
if (!m) {
|
|
583
|
+
throw new DesignMockdownError(`invalid state override: "${line.content}"`, line.pos, 'expected "key: value"');
|
|
584
|
+
}
|
|
585
|
+
overrides[m[1]] = coerceScalar(m[2].trim());
|
|
586
|
+
}
|
|
587
|
+
node.states[child.name] = overrides;
|
|
588
|
+
continue;
|
|
589
|
+
}
|
|
590
|
+
kept.push(child);
|
|
591
|
+
}
|
|
592
|
+
node.children = kept;
|
|
593
|
+
}
|
|
594
|
+
// @anim bodies were parsed into scratch holder nodes too. Harvest their `key: value` lines
|
|
595
|
+
// into the enclosing component's `anims` map and drop the holders.
|
|
596
|
+
if (node.type === 'component' && node.anims) {
|
|
597
|
+
const kept = [];
|
|
598
|
+
for (const child of node.children) {
|
|
599
|
+
if (child.type === 'component' && child.id.startsWith('anim-') && node.anims[child.name]) {
|
|
600
|
+
const overrides = {};
|
|
601
|
+
for (const line of child.children) {
|
|
602
|
+
if (line.type !== 'text') {
|
|
603
|
+
throw new DesignMockdownError(`invalid line inside @anim ${child.name}: "${line.type}"`, line.pos, '@anim only accepts "key: value" lines');
|
|
604
|
+
}
|
|
605
|
+
const m = /^([A-Za-z_][\w-]*)\s*:\s*(.+)$/.exec(line.content);
|
|
606
|
+
if (!m) {
|
|
607
|
+
throw new DesignMockdownError(`invalid animation override: "${line.content}"`, line.pos, 'expected "key: value"');
|
|
608
|
+
}
|
|
609
|
+
overrides[m[1]] = coerceScalar(m[2].trim());
|
|
610
|
+
}
|
|
611
|
+
node.anims[child.name] = overrides;
|
|
612
|
+
continue;
|
|
613
|
+
}
|
|
614
|
+
kept.push(child);
|
|
615
|
+
}
|
|
616
|
+
node.children = kept;
|
|
617
|
+
}
|
|
618
|
+
if (node.type === 'component' && node.name === 'theme') {
|
|
619
|
+
const tokens = {};
|
|
620
|
+
for (const child of node.children) {
|
|
621
|
+
if (child.type !== 'text') {
|
|
622
|
+
throw new DesignMockdownError(`invalid line inside @theme: "${child.type}"`, child.pos, '@theme only accepts "key: value" lines');
|
|
623
|
+
}
|
|
624
|
+
const m = /^([A-Za-z_][\w-]*)\s*:\s*(.+)$/.exec(child.content);
|
|
625
|
+
if (!m) {
|
|
626
|
+
throw new DesignMockdownError(`invalid theme token: "${child.content}"`, child.pos, 'expected "key: value", e.g. "brand: #4F6EF7"');
|
|
627
|
+
}
|
|
628
|
+
tokens[m[1]] = m[2].trim();
|
|
629
|
+
}
|
|
630
|
+
node.tokens = tokens;
|
|
631
|
+
node.children = [];
|
|
632
|
+
}
|
|
633
|
+
for (const child of node.children)
|
|
634
|
+
visit(child);
|
|
635
|
+
for (const list of Object.values(node.slots))
|
|
636
|
+
for (const c of list)
|
|
637
|
+
visit(c);
|
|
638
|
+
};
|
|
639
|
+
for (const child of page.children)
|
|
640
|
+
visit(child);
|
|
641
|
+
// @define templates live in page.defs, not in the body, so they need their own pass: a template
|
|
642
|
+
// may contain @theme-style @state blocks that must be harvested before the template is cloned
|
|
643
|
+
for (const def of Object.values(page.defs ?? {}))
|
|
644
|
+
visit(def);
|
|
645
|
+
}
|
|
646
|
+
/**
|
|
647
|
+
* Move children carrying `slot=xxx` into their parent's slot map.
|
|
648
|
+
* Exported for the resolver, which runs it after L2 expansion.
|
|
649
|
+
*/
|
|
650
|
+
export function groupSlots(page) {
|
|
651
|
+
const visit = (node) => {
|
|
652
|
+
if (!isContainer(node))
|
|
653
|
+
return;
|
|
654
|
+
const kept = [];
|
|
655
|
+
for (const child of node.children) {
|
|
656
|
+
if (child.type === 'component' && child.attrs.slot !== undefined) {
|
|
657
|
+
const slotName = String(child.attrs.slot);
|
|
658
|
+
delete child.attrs.slot;
|
|
659
|
+
(node.slots[slotName] ??= []).push(child);
|
|
660
|
+
}
|
|
661
|
+
else {
|
|
662
|
+
kept.push(child);
|
|
663
|
+
}
|
|
664
|
+
visit(child);
|
|
665
|
+
}
|
|
666
|
+
node.children = kept;
|
|
667
|
+
for (const list of Object.values(node.slots))
|
|
668
|
+
for (const c of list)
|
|
669
|
+
visit(c);
|
|
670
|
+
};
|
|
671
|
+
for (const child of page.children)
|
|
672
|
+
visit(child);
|
|
673
|
+
}
|
|
674
|
+
//# sourceMappingURL=parser.js.map
|