@astryxdesign/cli 0.7.4 → 0.7.5-canary.1093026
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -18
- package/api/discover/_adapter.d.mts +1 -1
- package/api/experimental.d.mts +5 -0
- package/api/experimental.mjs +15 -0
- package/api/hook/list/list.d.mts +1 -1
- package/api/icon/_adapter.d.mts +3 -2
- package/api/icon/_adapter.mjs +16 -3
- package/api/icon/_authoring.d.mts +5 -1
- package/api/icon/_authoring.mjs +478 -63
- package/api/icon/_options.d.mts +11 -0
- package/api/icon/_options.mjs +39 -4
- package/api/icon/_records.d.mts +19 -0
- package/api/icon/_records.mjs +25 -0
- package/api/icon/add/add.d.mts +3 -3
- package/api/icon/add/add.mjs +227 -14
- package/api/icon/all-or-nothing.test.mjs +283 -0
- package/api/icon/detail/detail.mjs +2 -13
- package/api/icon/error-codes.test.mjs +14 -7
- package/api/icon/icon.d.mts +2 -2
- package/api/icon/icon.doc.mjs +30 -2
- package/api/icon/icon.mjs +57 -8
- package/api/icon/icon.test.mjs +133 -1
- package/api/icon/icon.type.d.mts +126 -1
- package/api/icon/icon.type.mjs +67 -3
- package/api/icon/iconAdd.doc.mjs +22 -6
- package/api/icon/iconMap.doc.mjs +24 -8
- package/api/icon/iconNewDefinition.doc.mjs +2 -2
- package/api/icon/iconNewSet.doc.mjs +30 -2
- package/api/icon/iconUse.doc.mjs +4 -1
- package/api/icon/map/map.d.mts +4 -4
- package/api/icon/map/map.mjs +134 -9
- package/api/icon/new-definition/new-definition.mjs +6 -2
- package/api/icon/new-set/new-set.mjs +2 -2
- package/api/icon/new-set/new-set.test.mjs +60 -10
- package/api/icon/option-pairs.test.mjs +368 -0
- package/api/icon/search/search.d.mts +10 -0
- package/api/icon/search/search.mjs +55 -14
- package/api/icon/use/use.mjs +2 -13
- package/api/integration/add-helpers.d.mts +15 -1
- package/api/integration/add-helpers.mjs +1 -1
- package/api/integration/validate-integration.type.d.mts +3 -2
- package/api/integration/validate-integration.type.mjs +3 -2
- package/api/render/_adapter.d.mts +28 -0
- package/api/render/_adapter.mjs +540 -0
- package/api/render/render.d.mts +15 -0
- package/api/render/render.doc.d.mts +9 -0
- package/api/render/render.doc.mjs +112 -0
- package/api/render/render.mjs +113 -0
- package/api/render/render.test.mjs +1155 -0
- package/api/render/render.type.d.mts +74 -0
- package/api/render/render.type.mjs +64 -0
- package/api/search/_result.d.mts +5 -5
- package/api/search/search.d.mts +1 -1
- package/api/template/template.type.d.mts +2 -1
- package/api/template/template.type.mjs +1 -1
- package/assets/discover/official-packages.json +4 -0
- package/assets/docs/tree/icon-definitions.doc.mjs +2 -2
- package/assets/docs/tree/icon-set-files.doc.mjs +1 -1
- package/assets/docs/tree/icon-sets.doc.mjs +50 -20
- package/assets/docs/tree/render-pages.doc.mjs +94 -0
- package/assets/icons/definitions/activity.doc.mjs +1 -1
- package/assets/icons/definitions/alignCenter.doc.mjs +9 -0
- package/assets/icons/definitions/alignEnd.doc.mjs +10 -0
- package/assets/icons/definitions/alignStart.doc.mjs +10 -0
- package/assets/icons/definitions/archive.doc.mjs +1 -1
- package/assets/icons/definitions/arrowsUpDown.doc.mjs +1 -1
- package/assets/icons/definitions/attach.doc.mjs +1 -1
- package/assets/icons/definitions/audio.doc.mjs +1 -1
- package/assets/icons/definitions/cart.doc.mjs +1 -1
- package/assets/icons/definitions/checkDouble.doc.mjs +1 -1
- package/assets/icons/definitions/chevronsLeft.doc.mjs +1 -1
- package/assets/icons/definitions/chevronsRight.doc.mjs +1 -1
- package/assets/icons/definitions/close.doc.mjs +1 -1
- package/assets/icons/definitions/collapse.doc.mjs +1 -1
- package/assets/icons/definitions/compose.doc.mjs +1 -1
- package/assets/icons/definitions/conversation.doc.mjs +1 -1
- package/assets/icons/definitions/copy.doc.mjs +1 -1
- package/assets/icons/definitions/cpu.doc.mjs +9 -0
- package/assets/icons/definitions/database.doc.mjs +1 -1
- package/assets/icons/definitions/desktop.doc.mjs +1 -1
- package/assets/icons/definitions/discount.doc.mjs +1 -1
- package/assets/icons/definitions/download.doc.mjs +1 -1
- package/assets/icons/definitions/emoji.doc.mjs +1 -1
- package/assets/icons/definitions/error.doc.mjs +1 -1
- package/assets/icons/definitions/expand.doc.mjs +1 -1
- package/assets/icons/definitions/experiment.doc.mjs +1 -1
- package/assets/icons/definitions/externalLink.doc.mjs +7 -1
- package/assets/icons/definitions/eyeSlash.doc.mjs +1 -1
- package/assets/icons/definitions/flipHorizontal.doc.mjs +9 -0
- package/assets/icons/definitions/flipVertical.doc.mjs +9 -0
- package/assets/icons/definitions/folderAdd.doc.mjs +1 -1
- package/assets/icons/definitions/gitBranch.doc.mjs +9 -0
- package/assets/icons/definitions/gitFork.doc.mjs +9 -0
- package/assets/icons/definitions/info.doc.mjs +1 -1
- package/assets/icons/definitions/location.doc.mjs +1 -1
- package/assets/icons/definitions/mention.doc.mjs +1 -1
- package/assets/icons/definitions/menu.doc.mjs +1 -1
- package/assets/icons/definitions/microphone.doc.mjs +1 -1
- package/assets/icons/definitions/mobile.doc.mjs +1 -1
- package/assets/icons/definitions/money.doc.mjs +1 -1
- package/assets/icons/definitions/moreHorizontal.doc.mjs +1 -1
- package/assets/icons/definitions/notificationActive.doc.mjs +1 -1
- package/assets/icons/definitions/notificationOff.doc.mjs +1 -1
- package/assets/icons/definitions/panelLeft.doc.mjs +10 -0
- package/assets/icons/definitions/panelRight.doc.mjs +10 -0
- package/assets/icons/definitions/raiseHand.doc.mjs +1 -1
- package/assets/icons/definitions/redo.doc.mjs +1 -1
- package/assets/icons/definitions/refresh.doc.mjs +1 -1
- package/assets/icons/definitions/rotate.doc.mjs +9 -0
- package/assets/icons/definitions/scale.doc.mjs +10 -0
- package/assets/icons/definitions/search.doc.mjs +1 -1
- package/assets/icons/definitions/send.doc.mjs +1 -1
- package/assets/icons/definitions/settings.doc.mjs +1 -1
- package/assets/icons/definitions/star.doc.mjs +1 -1
- package/assets/icons/definitions/stop.doc.mjs +1 -1
- package/assets/icons/definitions/success.doc.mjs +1 -1
- package/assets/icons/definitions/textCase.doc.mjs +9 -0
- package/assets/icons/definitions/thumbsDown.doc.mjs +1 -1
- package/assets/icons/definitions/thumbsUp.doc.mjs +1 -1
- package/assets/icons/definitions/trendDown.doc.mjs +7 -1
- package/assets/icons/definitions/trendUp.doc.mjs +1 -1
- package/assets/icons/definitions/undo.doc.mjs +1 -1
- package/assets/icons/definitions/upload.doc.mjs +1 -1
- package/assets/icons/definitions/userAdd.doc.mjs +1 -1
- package/assets/icons/definitions/warning.doc.mjs +1 -1
- package/assets/templates/blocks/components/Tokenizer/TokenizerClear.tsx +15 -14
- package/assets/templates/blocks/components/Tokenizer/TokenizerCreatable.tsx +30 -30
- package/assets/templates/blocks/components/Tokenizer/TokenizerEndContent.tsx +15 -14
- package/assets/templates/blocks/components/Tokenizer/TokenizerIcon.tsx +15 -14
- package/assets/templates/blocks/components/Tokenizer/TokenizerMaxEntries.tsx +17 -16
- package/assets/templates/blocks/components/Tokenizer/TokenizerOverflow.tsx +29 -29
- package/assets/templates/blocks/components/Tokenizer/TokenizerShowcase.tsx +9 -8
- package/assets/templates/blocks/components/Tokenizer/TokenizerStates.tsx +39 -41
- package/assets/templates/pages/canvas-editor/template.doc.mjs +9 -0
- package/assets/templates/pages/form-wizard-inline/template.doc.mjs +6 -1
- package/assets/templates/pages/table-comparison/template.doc.mjs +1 -0
- package/assets/templates/pages/table-tree/template.doc.mjs +3 -0
- package/authoring/debug/parse.d.mts +2 -2
- package/authoring/doctypes/icon-definition/icon-definition.doc.mjs +4 -4
- package/authoring/doctypes/icon-set/icon-set-data.doc.mjs +6 -0
- package/authoring/doctypes/icon-set/parse.d.mts +1 -0
- package/authoring/doctypes/icon-set/parse.mjs +1 -0
- package/authoring/doctypes/icon-set/type.ts +5 -0
- package/authoring/doctypes/template/template.doc.mjs +1 -1
- package/authoring/doctypes/template/type.ts +2 -1
- package/clients/cli/commands/doctor.mjs +16 -17
- package/clients/cli/commands/experimental/icon.mjs +167 -35
- package/clients/cli/commands/experimental/index.mjs +2 -0
- package/clients/cli/commands/experimental/render.mjs +113 -0
- package/clients/cli/commands/experimental-icon-add.doc.mjs +14 -4
- package/clients/cli/commands/experimental-icon-map.doc.mjs +12 -2
- package/clients/cli/commands/experimental-icon-new-definition.doc.mjs +1 -1
- package/clients/cli/commands/experimental-icon-new-set.doc.mjs +28 -1
- package/clients/cli/commands/experimental-icon.doc.mjs +18 -2
- package/clients/cli/commands/experimental-render.doc.mjs +60 -0
- package/clients/cli/commands/experimental.doc.mjs +4 -2
- package/clients/cli/commands/icon-authoring.test.mjs +1823 -13
- package/clients/cli/commands/icon-options-wiring.test.mjs +158 -0
- package/clients/cli/commands/icon.test.mjs +1 -1
- package/clients/cli/commands/template.mjs +4 -1
- package/clients/cli/commands/text-json-parity.test.mjs +46 -0
- package/clients/cli/index.mjs +1 -0
- package/foundation/discovery/component-discovery.d.mts +2 -1
- package/foundation/discovery/component-discovery.export-names.test.mjs +41 -0
- package/foundation/discovery/component-discovery.mjs +13 -7
- package/foundation/discovery/icon-set-checks.d.mts +43 -9
- package/foundation/discovery/icon-set-checks.mjs +296 -21
- package/foundation/discovery/icon-set-checks.test.mjs +125 -12
- package/foundation/discovery/icon-set-writer.d.mts +192 -3
- package/foundation/discovery/icon-set-writer.mjs +608 -34
- package/foundation/discovery/icon-set-writer.test.mjs +26 -0
- package/foundation/discovery/module-exports.mjs +82 -25
- package/foundation/discovery/module-exports.test.mjs +38 -1
- package/foundation/discovery/package-exports.d.mts +13 -0
- package/foundation/discovery/package-exports.mjs +40 -1
- package/foundation/discovery/template-icons.d.mts +6 -0
- package/foundation/discovery/template-icons.mjs +98 -10
- package/foundation/discovery/template-icons.test.mjs +103 -0
- package/foundation/doc-compiler/doc-loads.test.mjs +15 -0
- package/foundation/doc-compiler/tree.mjs +3 -1
- package/foundation/render/assets.d.mts +31 -0
- package/foundation/render/assets.mjs +102 -0
- package/foundation/render/component-map.d.mts +52 -0
- package/foundation/render/component-map.mjs +141 -0
- package/foundation/render/css-cut.d.mts +29 -0
- package/foundation/render/css-cut.mjs +144 -0
- package/foundation/render/embedded-files.d.mts +28 -0
- package/foundation/render/embedded-files.mjs +108 -0
- package/foundation/render/embedded-imports.d.mts +61 -0
- package/foundation/render/embedded-imports.mjs +315 -0
- package/foundation/render/html-reader.d.mts +37 -0
- package/foundation/render/html-reader.mjs +121 -0
- package/foundation/render/page-worker.d.mts +7 -0
- package/foundation/render/page-worker.mjs +519 -0
- package/foundation/render/rebuild.d.mts +19 -0
- package/foundation/render/rebuild.mjs +269 -0
- package/foundation/render/render-page.d.mts +85 -0
- package/foundation/render/render-page.mjs +612 -0
- package/foundation/render/render.test.mjs +117 -0
- package/foundation/render/source-block.d.mts +52 -0
- package/foundation/render/source-block.mjs +106 -0
- package/foundation/response/error-codes.d.mts +10 -1
- package/foundation/response/error-codes.doc.mjs +56 -0
- package/foundation/response/error-codes.mjs +61 -0
- package/foundation/response/response-types.doc.mjs +38 -1
- package/foundation/text/string-utils.mjs +33 -0
- package/foundation/text/string-utils.test.mjs +17 -0
- package/package.json +14 -9
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @file `astryx experimental render --reverse` without an embedded source:
|
|
5
|
+
* rebuild a page module from Astryx markup. Each element whose class list
|
|
6
|
+
* names an Astryx component (`astryx-<name>`) becomes that component, with
|
|
7
|
+
* the props its `data-*` attributes record and its text. This keeps text and
|
|
8
|
+
* layout; styling, state and behavior are not in the markup, so the result is
|
|
9
|
+
* a starting point, not the original.
|
|
10
|
+
*
|
|
11
|
+
* @position foundation/render — used by the render subject
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import {astryxClass, decode, find, FORM_TAGS, hasTag, isEl, parseHtml, textOf, VOID} from './html-reader.mjs';
|
|
15
|
+
import {componentMap, pascal} from './component-map.mjs';
|
|
16
|
+
|
|
17
|
+
/** @typedef {import('./html-reader.mjs').ElementNode} ElementNode */
|
|
18
|
+
/** @typedef {import('./html-reader.mjs').Node} Node */
|
|
19
|
+
/** @typedef {import('./component-map.mjs').ComponentEntry} ComponentEntry */
|
|
20
|
+
|
|
21
|
+
/** @param {string} s */
|
|
22
|
+
const camel = s => s.replace(/-([a-z0-9])/g, (_, c) => c.toUpperCase());
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Pick the component for one element.
|
|
26
|
+
* @param {ComponentEntry[]} list
|
|
27
|
+
* @param {string} target
|
|
28
|
+
* @param {ElementNode} el
|
|
29
|
+
*/
|
|
30
|
+
function pick(list, target, el) {
|
|
31
|
+
const exact = list.find(e => e.name === pascal(target));
|
|
32
|
+
const dir = el.attrs['data-direction'];
|
|
33
|
+
if (dir) {
|
|
34
|
+
const prefix = dir === 'horizontal' ? 'H' : dir === 'vertical' ? 'V' : '';
|
|
35
|
+
const hit = prefix ? list.find(e => e.name === `${prefix}${pascal(target)}`) : undefined;
|
|
36
|
+
if (hit) return hit;
|
|
37
|
+
}
|
|
38
|
+
return exact ?? list.find(e => e.name.endsWith(pascal(target))) ?? list[0];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Theme targets whose content the markup cannot give back (an icon's
|
|
42
|
+
* drawing is markup, not the component it came from). */
|
|
43
|
+
const OMIT = new Set(['icon']);
|
|
44
|
+
|
|
45
|
+
/** Attributes a component writes about itself that are not props. */
|
|
46
|
+
const NOT_PROPS = new Set(['astryx-pressable', 'state']);
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* A stand-in value for a required prop the markup does not record, or null
|
|
50
|
+
* when its type allows none (a list, an object, a named type).
|
|
51
|
+
* @param {string} prop
|
|
52
|
+
* @param {string} type
|
|
53
|
+
* @param {ElementNode} el
|
|
54
|
+
* @param {string} text
|
|
55
|
+
* @returns {string | null}
|
|
56
|
+
*/
|
|
57
|
+
function standIn(prop, type, el, text) {
|
|
58
|
+
const ty = type.replace(/\/\*[\s\S]*?\*\//g, '').trim();
|
|
59
|
+
if (/=>/.test(ty)) return `${prop}={() => {}}`;
|
|
60
|
+
if (/\[\]|Array<|Record<|\{/.test(ty)) return null;
|
|
61
|
+
if (/^number\b/.test(ty)) {
|
|
62
|
+
const v = Number(el.attrs['aria-valuenow'] ?? el.attrs[`data-${prop}`] ?? 0);
|
|
63
|
+
return `${prop}={${Number.isFinite(v) ? v : 0}}`;
|
|
64
|
+
}
|
|
65
|
+
if (/^boolean\b/.test(ty)) return `${prop}={false}`;
|
|
66
|
+
if (/^(React\.)?(ReactNode|ReactElement)/.test(ty)) return `${prop}={null}`;
|
|
67
|
+
if (/^string\b/.test(ty)) {
|
|
68
|
+
const v = el.attrs['aria-label'] ?? el.attrs.title ?? (/(label|title|name|text)$/i.test(prop) ? text : '');
|
|
69
|
+
return `${prop}=${jsxAttr(v)}`;
|
|
70
|
+
}
|
|
71
|
+
const literal = /^['"]([^'"]*)['"]/.exec(ty);
|
|
72
|
+
if (literal) return `${prop}=${jsxAttr(literal[1])}`;
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** @param {string} s */
|
|
77
|
+
const jsxText = s => s.replace(/[{}<>]/g, c => `{'${c}'}`);
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* A JSX attribute value: a plain quoted string when it can be one, else an
|
|
81
|
+
* expression (`{"say \\"hi\\""}`), since JSX strings take no escapes.
|
|
82
|
+
* @param {string} s
|
|
83
|
+
*/
|
|
84
|
+
const jsxAttr = s => (/["\\\n\r{}]/.test(s) ? `{${JSON.stringify(s)}}` : `"${s}"`);
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Text safe inside a `//` comment: every control character and line or
|
|
88
|
+
* paragraph separator becomes a space, so nothing after it can become code.
|
|
89
|
+
* @param {string} s
|
|
90
|
+
*/
|
|
91
|
+
const oneLine = s =>
|
|
92
|
+
[...s]
|
|
93
|
+
.map(c => {
|
|
94
|
+
const n = /** @type {number} */ (c.codePointAt(0));
|
|
95
|
+
return n < 32 || (n >= 127 && n <= 159) || n === 0x2028 || n === 0x2029 ? ' ' : c;
|
|
96
|
+
})
|
|
97
|
+
.join('');
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Rebuild a page module from Astryx markup.
|
|
101
|
+
* @param {string} html
|
|
102
|
+
* @param {string} fromDir where to find the installed packages
|
|
103
|
+
* @returns {{source: string, components: string[], elements: number, unknown: string[], omitted: string[]}}
|
|
104
|
+
*/
|
|
105
|
+
export function rebuildPage(html, fromDir) {
|
|
106
|
+
const doc = parseHtml(html);
|
|
107
|
+
const body = find(doc, 'body') ?? doc;
|
|
108
|
+
const maps = ['@astryxdesign/core', '@astryxdesign/lab', '@astryxdesign/charts'].map(p => componentMap(p, fromDir));
|
|
109
|
+
/** @param {string} target */
|
|
110
|
+
const lookup = target => {
|
|
111
|
+
for (const m of maps) {
|
|
112
|
+
const list = m.get(target);
|
|
113
|
+
if (list?.length) return list;
|
|
114
|
+
}
|
|
115
|
+
return undefined;
|
|
116
|
+
};
|
|
117
|
+
/** @type {Map<string, Set<string>>} */
|
|
118
|
+
const imports = new Map();
|
|
119
|
+
/** @type {Set<string>} */
|
|
120
|
+
const unknown = new Set();
|
|
121
|
+
/** @type {Set<string>} */
|
|
122
|
+
const omitted = new Set();
|
|
123
|
+
/** @type {Set<string>} */
|
|
124
|
+
const degraded = new Set();
|
|
125
|
+
let elements = 0;
|
|
126
|
+
|
|
127
|
+
/** @param {Node} n @returns {boolean} */
|
|
128
|
+
const hasAstryx = n =>
|
|
129
|
+
isEl(n) && (Boolean(astryxClass(n)) || n.children.some(hasAstryx));
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* An element's content as plain text elements, components left out.
|
|
133
|
+
* @param {Node} n
|
|
134
|
+
* @param {string} indent
|
|
135
|
+
* @returns {string[]}
|
|
136
|
+
*/
|
|
137
|
+
const plain = (n, indent) => {
|
|
138
|
+
if (!isEl(n)) {
|
|
139
|
+
const t = n.text.replace(/\s+/g, ' ').trim();
|
|
140
|
+
return t ? [`${indent}<span>${jsxText(t)}</span>`] : [];
|
|
141
|
+
}
|
|
142
|
+
if (n.tag === 'script' || n.tag === 'style' || n.tag === 'template') return [];
|
|
143
|
+
const block = n.children.some(c => isEl(c) && !/^(span|strong|em|code|a|small|b|i)$/.test(c.tag));
|
|
144
|
+
if (!block) {
|
|
145
|
+
const t = textOf(n).replace(/\s+/g, ' ').trim();
|
|
146
|
+
return t ? [`${indent}<span>${jsxText(t)}</span>`] : [];
|
|
147
|
+
}
|
|
148
|
+
return n.children.flatMap(c => plain(c, indent));
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* @param {Node} n
|
|
153
|
+
* @param {string} indent
|
|
154
|
+
* @returns {string[]}
|
|
155
|
+
*/
|
|
156
|
+
const emit = (n, indent) => {
|
|
157
|
+
if (!isEl(n)) {
|
|
158
|
+
const t = n.text.replace(/\s+/g, ' ').trim();
|
|
159
|
+
return t ? [`${indent}${jsxText(t)}`] : [];
|
|
160
|
+
}
|
|
161
|
+
if (n.tag === 'script' || n.tag === 'style' || n.tag === 'template') return [];
|
|
162
|
+
const target = astryxClass(n);
|
|
163
|
+
if (target && OMIT.has(target)) {
|
|
164
|
+
omitted.add(target);
|
|
165
|
+
return [];
|
|
166
|
+
}
|
|
167
|
+
const list = target ? lookup(target) : undefined;
|
|
168
|
+
if (target && !list) unknown.add(target);
|
|
169
|
+
if (!list || !target) {
|
|
170
|
+
if (n.children.some(hasAstryx)) return n.children.flatMap(c => emit(c, indent));
|
|
171
|
+
const t = textOf(n).replace(/\s+/g, ' ').trim();
|
|
172
|
+
if (!t) {
|
|
173
|
+
if (n.tag === 'img' && n.attrs.src) {
|
|
174
|
+
return [`${indent}<img src=${jsxAttr(n.attrs.src)} alt=${jsxAttr(n.attrs.alt ?? '')} />`];
|
|
175
|
+
}
|
|
176
|
+
return [];
|
|
177
|
+
}
|
|
178
|
+
const tag = /^(h[1-6]|p|span|strong|em|code|pre|li|a|label|small)$/.test(n.tag) ? n.tag : 'span';
|
|
179
|
+
const href = tag === 'a' && n.attrs.href ? ` href=${jsxAttr(n.attrs.href)}` : '';
|
|
180
|
+
return [`${indent}<${tag}${href}>${jsxText(t)}</${tag}>`];
|
|
181
|
+
}
|
|
182
|
+
const entry = pick(list, target, n);
|
|
183
|
+
// The props the markup records, as JSX.
|
|
184
|
+
/** @type {Map<string, string>} */
|
|
185
|
+
const given = new Map();
|
|
186
|
+
for (const [k, v] of Object.entries(n.attrs)) {
|
|
187
|
+
if (!k.startsWith('data-')) continue;
|
|
188
|
+
const prop = camel(k.slice(5));
|
|
189
|
+
if (NOT_PROPS.has(k.slice(5)) || !entry.props.has(prop)) continue;
|
|
190
|
+
if (v === '' || v === 'true') given.set(prop, prop);
|
|
191
|
+
else if (v === 'false') given.set(prop, `${prop}={false}`);
|
|
192
|
+
else if (/^-?\d+(\.\d+)?$/.test(v)) given.set(prop, `${prop}={${v}}`);
|
|
193
|
+
else given.set(prop, `${prop}=${jsxAttr(v)}`);
|
|
194
|
+
}
|
|
195
|
+
for (const a of ['href', 'role']) {
|
|
196
|
+
if (n.attrs[a] && entry.props.has(a)) given.set(a, `${a}=${jsxAttr(n.attrs[a])}`);
|
|
197
|
+
}
|
|
198
|
+
const t = textOf(n).replace(/\s+/g, ' ').trim();
|
|
199
|
+
// A component that draws a form control takes its text as a label, never
|
|
200
|
+
// as children (they would land on the control itself).
|
|
201
|
+
const formControl = hasTag(n, FORM_TAGS);
|
|
202
|
+
// Astryx parts inside a form control are its own drawing, not content:
|
|
203
|
+
// it is rebuilt as a leaf.
|
|
204
|
+
const nested = n.children.some(hasAstryx) && !formControl;
|
|
205
|
+
const textProp =
|
|
206
|
+
!nested && t && entry.label && (formControl || !entry.props.has('children')) ? 'label' : null;
|
|
207
|
+
// Required props the markup does not record get a stand-in when their
|
|
208
|
+
// type allows one; a component needing data it cannot hold (items,
|
|
209
|
+
// options) is not rebuilt, and its content is kept as plain text.
|
|
210
|
+
let lacking = false;
|
|
211
|
+
for (const [r, type] of entry.required) {
|
|
212
|
+
if (r === 'children' || r === textProp || given.has(r)) continue;
|
|
213
|
+
const fill = standIn(r, type, n, t);
|
|
214
|
+
if (fill == null) {
|
|
215
|
+
lacking = true;
|
|
216
|
+
break;
|
|
217
|
+
}
|
|
218
|
+
given.set(r, fill);
|
|
219
|
+
}
|
|
220
|
+
if (lacking) {
|
|
221
|
+
degraded.add(entry.name);
|
|
222
|
+
return plain(n, indent);
|
|
223
|
+
}
|
|
224
|
+
elements++;
|
|
225
|
+
const mod = imports.get(entry.module) ?? new Set();
|
|
226
|
+
mod.add(entry.name);
|
|
227
|
+
imports.set(entry.module, mod);
|
|
228
|
+
const props = [...given.values()];
|
|
229
|
+
const open = `${indent}<${entry.name}${props.length ? ` ${props.join(' ')}` : ''}`;
|
|
230
|
+
if (nested) {
|
|
231
|
+
const inner = n.children.flatMap(c => emit(c, `${indent} `));
|
|
232
|
+
return inner.length ? [`${open}>`, ...inner, `${indent}</${entry.name}>`] : [`${open} />`];
|
|
233
|
+
}
|
|
234
|
+
if (!t || VOID.has(n.tag) || (formControl && !textProp)) return [`${open} />`];
|
|
235
|
+
if (textProp) {
|
|
236
|
+
return [`${open.replace(`<${entry.name}`, `<${entry.name} label=${jsxAttr(t)}`)} />`];
|
|
237
|
+
}
|
|
238
|
+
return [`${open}>${jsxText(t)}</${entry.name}>`];
|
|
239
|
+
};
|
|
240
|
+
|
|
241
|
+
const lines = body.children.flatMap(c => emit(c, ' '));
|
|
242
|
+
const importLines = [...imports.entries()]
|
|
243
|
+
.sort(([a], [b]) => a.localeCompare(b))
|
|
244
|
+
.map(([mod, names]) => `import {${[...names].sort().join(', ')}} from '${mod}';`);
|
|
245
|
+
const title = decode(textOf(find(doc, 'title') ?? {text: ''})).trim();
|
|
246
|
+
const source = [
|
|
247
|
+
`// Rebuilt by astryx experimental render --reverse from rendered markup${title ? `: ${oneLine(title)}` : ''}.`,
|
|
248
|
+
'// Text and layout come from the markup; styling, state and behavior do not,',
|
|
249
|
+
'// so review it before use.',
|
|
250
|
+
'',
|
|
251
|
+
...importLines,
|
|
252
|
+
'',
|
|
253
|
+
'export default function Page() {',
|
|
254
|
+
' return (',
|
|
255
|
+
' <>',
|
|
256
|
+
...lines.map(l => ` ${l}`),
|
|
257
|
+
' </>',
|
|
258
|
+
' );',
|
|
259
|
+
'}',
|
|
260
|
+
'',
|
|
261
|
+
].join('\n');
|
|
262
|
+
return {
|
|
263
|
+
source,
|
|
264
|
+
components: [...imports.values()].flatMap(s => [...s]).sort(),
|
|
265
|
+
elements,
|
|
266
|
+
unknown: [...unknown].sort(),
|
|
267
|
+
omitted: [...omitted, ...degraded].sort(),
|
|
268
|
+
};
|
|
269
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// @generated by scripts/sync-api-types.mjs from the JSDoc in foundation/**/*.mjs.
|
|
2
|
+
// DO NOT EDIT — run `pnpm sync:api-types` to regenerate.
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Render a page module to one self-contained HTML file.
|
|
6
|
+
* @param {string} entry absolute path of the page module
|
|
7
|
+
* @param {{title?: string, timeoutMs?: number, version?: string}} [options]
|
|
8
|
+
* @returns {Promise<RenderedPage>}
|
|
9
|
+
*/
|
|
10
|
+
export function renderPage(entry: string, options?: {
|
|
11
|
+
title?: string;
|
|
12
|
+
timeoutMs?: number;
|
|
13
|
+
version?: string;
|
|
14
|
+
}): Promise<RenderedPage>;
|
|
15
|
+
/** How long one page may take to render before it is stopped. */
|
|
16
|
+
export const RENDER_TIMEOUT_MS: 60000;
|
|
17
|
+
export { PAGE_EXTENSIONS } from "./embedded-files.mjs";
|
|
18
|
+
/**
|
|
19
|
+
* @typedef {object} RenderWarning
|
|
20
|
+
* @property {'dropped-children' | 'remote-asset' | 'missing-asset' | 'page-output' | 'page-script' | 'no-astryx-styles' | 'chart-size' | 'not-embedded'} code - What kind of problem.
|
|
21
|
+
* @property {string} message - What happened and what to do, in one sentence.
|
|
22
|
+
*/
|
|
23
|
+
/**
|
|
24
|
+
* @typedef {object} RenderedPage
|
|
25
|
+
* @property {string} html - The whole file.
|
|
26
|
+
* @property {string[]} files - Local files the page loaded, relative to the folder that holds them all (the page's folder unless it imports from above it); the page first. Each is embedded.
|
|
27
|
+
* @property {string[]} stylesheets - The packages' stylesheets that were cut and inlined.
|
|
28
|
+
* @property {number} classes - Distinct class names in the markup.
|
|
29
|
+
* @property {number} cssBytes - Bytes of inlined CSS.
|
|
30
|
+
* @property {number} markupBytes - Bytes of rendered markup.
|
|
31
|
+
* @property {number} images - Local files inlined as data: URIs.
|
|
32
|
+
* @property {RenderWarning[]} warnings
|
|
33
|
+
*/
|
|
34
|
+
/** An error carrying a render error code. */
|
|
35
|
+
export class RenderError extends Error {
|
|
36
|
+
/**
|
|
37
|
+
* @param {string} code
|
|
38
|
+
* @param {string} message
|
|
39
|
+
* @param {Record<string, unknown>} [detail]
|
|
40
|
+
*/
|
|
41
|
+
constructor(code: string, message: string, detail?: Record<string, unknown>);
|
|
42
|
+
code: string;
|
|
43
|
+
detail: Record<string, unknown> | undefined;
|
|
44
|
+
}
|
|
45
|
+
export type RenderWarning = {
|
|
46
|
+
/**
|
|
47
|
+
* - What kind of problem.
|
|
48
|
+
*/
|
|
49
|
+
code: "dropped-children" | "remote-asset" | "missing-asset" | "page-output" | "page-script" | "no-astryx-styles" | "chart-size" | "not-embedded";
|
|
50
|
+
/**
|
|
51
|
+
* - What happened and what to do, in one sentence.
|
|
52
|
+
*/
|
|
53
|
+
message: string;
|
|
54
|
+
};
|
|
55
|
+
export type RenderedPage = {
|
|
56
|
+
/**
|
|
57
|
+
* - The whole file.
|
|
58
|
+
*/
|
|
59
|
+
html: string;
|
|
60
|
+
/**
|
|
61
|
+
* - Local files the page loaded, relative to the folder that holds them all (the page's folder unless it imports from above it); the page first. Each is embedded.
|
|
62
|
+
*/
|
|
63
|
+
files: string[];
|
|
64
|
+
/**
|
|
65
|
+
* - The packages' stylesheets that were cut and inlined.
|
|
66
|
+
*/
|
|
67
|
+
stylesheets: string[];
|
|
68
|
+
/**
|
|
69
|
+
* - Distinct class names in the markup.
|
|
70
|
+
*/
|
|
71
|
+
classes: number;
|
|
72
|
+
/**
|
|
73
|
+
* - Bytes of inlined CSS.
|
|
74
|
+
*/
|
|
75
|
+
cssBytes: number;
|
|
76
|
+
/**
|
|
77
|
+
* - Bytes of rendered markup.
|
|
78
|
+
*/
|
|
79
|
+
markupBytes: number;
|
|
80
|
+
/**
|
|
81
|
+
* - Local files inlined as data: URIs.
|
|
82
|
+
*/
|
|
83
|
+
images: number;
|
|
84
|
+
warnings: RenderWarning[];
|
|
85
|
+
};
|