@designos/renderer-html 0.1.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { PageDocument } from '@designos/core';
|
|
1
|
+
import { PageDocument, DesignDocument } from '@designos/core';
|
|
2
2
|
import { RendererOptions } from '@designos/renderer-core';
|
|
3
3
|
export { NodeComposition, RenderNodeView, RendererAdapter, RendererOptions, resolveNodeComposition } from '@designos/renderer-core';
|
|
4
4
|
|
|
@@ -16,6 +16,6 @@ interface HtmlRenderOptions extends RendererOptions {
|
|
|
16
16
|
components: HtmlComponentMap;
|
|
17
17
|
}
|
|
18
18
|
/** Serialize a canonical PageDocument to escaped HTML using an explicit component-to-tag map. */
|
|
19
|
-
declare function renderHtmlPage(
|
|
19
|
+
declare function renderHtmlPage(page: PageDocument, design: DesignDocument, options: HtmlRenderOptions): string;
|
|
20
20
|
|
|
21
21
|
export { type HtmlComponentDefinition, type HtmlComponentMap, type HtmlRenderOptions, renderHtmlPage, rendererTarget };
|
package/dist/index.js
CHANGED
|
@@ -45,8 +45,8 @@ function definitionFor(type, components) {
|
|
|
45
45
|
const definition = typeof found === "string" ? { tag: found } : found;
|
|
46
46
|
return definition && validName.test(definition.tag) ? definition : { tag: "div" };
|
|
47
47
|
}
|
|
48
|
-
function renderHtmlPage(
|
|
49
|
-
return traversePageDocument(
|
|
48
|
+
function renderHtmlPage(page, design, options) {
|
|
49
|
+
return traversePageDocument(page, design, {
|
|
50
50
|
renderNode(view) {
|
|
51
51
|
const definition = definitionFor(view.node.type, options.components);
|
|
52
52
|
const tag = definition.tag;
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { ComponentRegistryContract, PageDocument } from \"@designos/core\";\nimport { renderPageDocument as traversePageDocument } from \"@designos/renderer-core\";\nimport type { RendererOptions } from \"@designos/renderer-core\";\n\nexport const rendererTarget = { id: \"html\", kind: \"artifact\" } as const;\n\nexport interface HtmlComponentDefinition {\n tag: string;\n /** Optional prop to emit as escaped text content instead of an attribute. */\n textProp?: string;\n}\nexport type HtmlComponentMap = Record<string, string | HtmlComponentDefinition>;\nexport interface HtmlRenderOptions extends RendererOptions {\n components: HtmlComponentMap;\n}\n\nconst VOID_TAGS = new Set([\"area\", \"base\", \"br\", \"col\", \"embed\", \"hr\", \"img\", \"input\", \"link\", \"meta\", \"param\", \"source\", \"track\", \"wbr\"]);\nconst BOOLEAN_ATTRIBUTES = new Set([\"allowfullscreen\", \"async\", \"autofocus\", \"autoplay\", \"checked\", \"controls\", \"defer\", \"disabled\", \"hidden\", \"loop\", \"multiple\", \"muted\", \"open\", \"readonly\", \"required\", \"selected\"]);\nconst escapeHtml = (value: string) => value.replaceAll(\"&\", \"&\").replaceAll(\"<\", \"<\").replaceAll(\">\", \">\").replaceAll('\"', \""\").replaceAll(\"'\", \"'\");\nconst validName = /^[A-Za-z][A-Za-z0-9:_-]*$/;\n\nfunction serializeStyle(value: unknown): string | undefined {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return typeof value === \"string\" ? value : undefined;\n const declarations = Object.entries(value as Record<string, unknown>).flatMap(([key, item]) => {\n if (item === null || item === undefined || typeof item === \"object\") return [];\n const cssName = key.startsWith(\"--\") ? key : key.replace(/[A-Z]/g, (char) => `-${char.toLowerCase()}`);\n return [`${cssName}:${String(item)}`];\n });\n return declarations.join(\";\");\n}\n\nfunction serializeAttributes(props: Record<string, unknown>, textProp?: string): string {\n const attributes: string[] = [];\n for (const [rawName, value] of Object.entries(props)) {\n if (rawName === textProp || rawName === \"children\" || rawName === \"slots\" || rawName === \"key\" || rawName === \"ref\") continue;\n if (/^on[a-z]/i.test(rawName) || !validName.test(rawName)) continue;\n const name = rawName === \"className\" ? \"class\" : rawName;\n if (name === \"style\") {\n const style = serializeStyle(value);\n if (style) attributes.push(`style=\"${escapeHtml(style)}\"`);\n continue;\n }\n if (value === null || value === undefined || typeof value === \"object\") continue;\n if ([\"href\", \"src\", \"action\", \"formaction\", \"xlink:href\"].includes(name.toLowerCase()) && /^\\s*(?:javascript|vbscript|data):/i.test(String(value))) continue;\n if (typeof value === \"boolean\") {\n if (BOOLEAN_ATTRIBUTES.has(name.toLowerCase())) {\n if (value) attributes.push(`${name}=\"\"`);\n } else if (name.startsWith(\"aria-\") || name.startsWith(\"data-\")) {\n attributes.push(`${name}=\"${String(value)}\"`);\n }\n continue;\n }\n attributes.push(`${name}=\"${escapeHtml(String(value))}\"`);\n }\n return attributes.length ? ` ${attributes.join(\" \")}` : \"\";\n}\n\nfunction definitionFor(type: string, components: HtmlComponentMap): HtmlComponentDefinition {\n const found = components[type];\n const definition = typeof found === \"string\" ? { tag: found } : found;\n return definition && validName.test(definition.tag) ? definition : { tag: \"div\" };\n}\n\n/** Serialize a canonical PageDocument to escaped HTML using an explicit component-to-tag map. */\nexport function renderHtmlPage(
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { ComponentRegistryContract, DesignDocument, PageDocument } from \"@designos/core\";\nimport { renderPageDocument as traversePageDocument } from \"@designos/renderer-core\";\nimport type { RendererOptions } from \"@designos/renderer-core\";\n\nexport const rendererTarget = { id: \"html\", kind: \"artifact\" } as const;\n\nexport interface HtmlComponentDefinition {\n tag: string;\n /** Optional prop to emit as escaped text content instead of an attribute. */\n textProp?: string;\n}\nexport type HtmlComponentMap = Record<string, string | HtmlComponentDefinition>;\nexport interface HtmlRenderOptions extends RendererOptions {\n components: HtmlComponentMap;\n}\n\nconst VOID_TAGS = new Set([\"area\", \"base\", \"br\", \"col\", \"embed\", \"hr\", \"img\", \"input\", \"link\", \"meta\", \"param\", \"source\", \"track\", \"wbr\"]);\nconst BOOLEAN_ATTRIBUTES = new Set([\"allowfullscreen\", \"async\", \"autofocus\", \"autoplay\", \"checked\", \"controls\", \"defer\", \"disabled\", \"hidden\", \"loop\", \"multiple\", \"muted\", \"open\", \"readonly\", \"required\", \"selected\"]);\nconst escapeHtml = (value: string) => value.replaceAll(\"&\", \"&\").replaceAll(\"<\", \"<\").replaceAll(\">\", \">\").replaceAll('\"', \""\").replaceAll(\"'\", \"'\");\nconst validName = /^[A-Za-z][A-Za-z0-9:_-]*$/;\n\nfunction serializeStyle(value: unknown): string | undefined {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return typeof value === \"string\" ? value : undefined;\n const declarations = Object.entries(value as Record<string, unknown>).flatMap(([key, item]) => {\n if (item === null || item === undefined || typeof item === \"object\") return [];\n const cssName = key.startsWith(\"--\") ? key : key.replace(/[A-Z]/g, (char) => `-${char.toLowerCase()}`);\n return [`${cssName}:${String(item)}`];\n });\n return declarations.join(\";\");\n}\n\nfunction serializeAttributes(props: Record<string, unknown>, textProp?: string): string {\n const attributes: string[] = [];\n for (const [rawName, value] of Object.entries(props)) {\n if (rawName === textProp || rawName === \"children\" || rawName === \"slots\" || rawName === \"key\" || rawName === \"ref\") continue;\n if (/^on[a-z]/i.test(rawName) || !validName.test(rawName)) continue;\n const name = rawName === \"className\" ? \"class\" : rawName;\n if (name === \"style\") {\n const style = serializeStyle(value);\n if (style) attributes.push(`style=\"${escapeHtml(style)}\"`);\n continue;\n }\n if (value === null || value === undefined || typeof value === \"object\") continue;\n if ([\"href\", \"src\", \"action\", \"formaction\", \"xlink:href\"].includes(name.toLowerCase()) && /^\\s*(?:javascript|vbscript|data):/i.test(String(value))) continue;\n if (typeof value === \"boolean\") {\n if (BOOLEAN_ATTRIBUTES.has(name.toLowerCase())) {\n if (value) attributes.push(`${name}=\"\"`);\n } else if (name.startsWith(\"aria-\") || name.startsWith(\"data-\")) {\n attributes.push(`${name}=\"${String(value)}\"`);\n }\n continue;\n }\n attributes.push(`${name}=\"${escapeHtml(String(value))}\"`);\n }\n return attributes.length ? ` ${attributes.join(\" \")}` : \"\";\n}\n\nfunction definitionFor(type: string, components: HtmlComponentMap): HtmlComponentDefinition {\n const found = components[type];\n const definition = typeof found === \"string\" ? { tag: found } : found;\n return definition && validName.test(definition.tag) ? definition : { tag: \"div\" };\n}\n\n/** Serialize a canonical PageDocument to escaped HTML using an explicit component-to-tag map. */\nexport function renderHtmlPage(page: PageDocument, design: DesignDocument, options: HtmlRenderOptions): string {\n return traversePageDocument<string>(page, design, {\n renderNode(view) {\n const definition = definitionFor(view.node.type, options.components);\n const tag = definition.tag;\n const text = definition.textProp ? view.node.props[definition.textProp] : undefined;\n const textContent = typeof text === \"string\" || typeof text === \"number\" ? escapeHtml(String(text)) : \"\";\n const descendants = [\n ...view.children,\n ...Object.values(view.slots).flat(),\n ].join(\"\");\n const children = `${textContent}${descendants}`;\n const slotAttribute = view.slotName ? ` slot=\"${escapeHtml(view.slotName)}\"` : \"\";\n const attributes = serializeAttributes(view.node.props, definition.textProp);\n const missingAttribute = options.components[view.node.type] ? \"\" : ` data-designos-missing=\"${escapeHtml(view.node.type)}\"`;\n if (VOID_TAGS.has(tag.toLowerCase())) return `<${tag}${attributes}${slotAttribute}${missingAttribute}>`;\n return `<${tag}${attributes}${slotAttribute}${missingAttribute}>${children}</${tag}>`;\n },\n }, { registry: options.registry });\n}\n\nexport type { RendererAdapter, RendererOptions, RenderNodeView, NodeComposition } from \"@designos/renderer-core\";\nexport { resolveNodeComposition } from \"@designos/renderer-core\";\n"],"mappings":";AACA,SAAS,sBAAsB,4BAA4B;AAqF3D,SAAS,8BAA8B;AAlFhC,IAAM,iBAAiB,EAAE,IAAI,QAAQ,MAAM,WAAW;AAY7D,IAAM,YAAY,oBAAI,IAAI,CAAC,QAAQ,QAAQ,MAAM,OAAO,SAAS,MAAM,OAAO,SAAS,QAAQ,QAAQ,SAAS,UAAU,SAAS,KAAK,CAAC;AACzI,IAAM,qBAAqB,oBAAI,IAAI,CAAC,mBAAmB,SAAS,aAAa,YAAY,WAAW,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,WAAW,GAAG,WAAW,GAAG,WAAW;AAC7C,YAAM,gBAAgB,KAAK,WAAW,UAAU,WAAW,KAAK,QAAQ,CAAC,MAAM;AAC/E,YAAM,aAAa,oBAAoB,KAAK,KAAK,OAAO,WAAW,QAAQ;AAC3E,YAAM,mBAAmB,QAAQ,WAAW,KAAK,KAAK,IAAI,IAAI,KAAK,2BAA2B,WAAW,KAAK,KAAK,IAAI,CAAC;AACxH,UAAI,UAAU,IAAI,IAAI,YAAY,CAAC,EAAG,QAAO,IAAI,GAAG,GAAG,UAAU,GAAG,aAAa,GAAG,gBAAgB;AACpG,aAAO,IAAI,GAAG,GAAG,UAAU,GAAG,aAAa,GAAG,gBAAgB,IAAI,QAAQ,KAAK,GAAG;AAAA,IACpF;AAAA,EACF,GAAG,EAAE,UAAU,QAAQ,SAAS,CAAC;AACnC;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@designos/renderer-html",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -10,8 +10,8 @@
|
|
|
10
10
|
}
|
|
11
11
|
},
|
|
12
12
|
"dependencies": {
|
|
13
|
-
"@designos/core": "0.
|
|
14
|
-
"@designos/renderer-core": "
|
|
13
|
+
"@designos/core": "0.2.0",
|
|
14
|
+
"@designos/renderer-core": "1.0.0"
|
|
15
15
|
},
|
|
16
16
|
"devDependencies": {
|
|
17
17
|
"tsup": "^8.5.0",
|