feat: 搭建设计生成工作台骨架

This commit is contained in:
que01 committed 2026-10-15 15:54:21 +08:00
1 parent bf4bbf81c7
commit 083a323c1f
20 files changed
+587 -1

No files matched your search

+11
View File
@@ -0,0 +1,11 @@
# Design Spec
DesignSpec 是 Design Lab 的唯一设计源头。它描述品牌信息、设计 token 和受限 section 结构,不包含业务数据库访问或小程序实现细节。
当前能力:
- `DesignSpec` TypeScript 契约
- `validateDesignSpec` 结构校验
- `renderDesignSpec` 生成带 `data-template-section` / `data-field` / `data-action` 标注的移动端 HTML
输出 HTML 只作为设计稿和转换依据,不直接复制进线上项目。
+14
View File
@@ -0,0 +1,14 @@
{
"name": "@personal-brand/design-spec",
"version": "0.1.0",
"private": true,
"type": "module",
"exports": {
".": "./src/index.ts"
},
"scripts": {
"typecheck": "tsc -p tsconfig.json --noEmit",
"lint": "eslint .",
"test": "node --experimental-strip-types --test test/*.test.mjs"
}
}
+103
View File
@@ -0,0 +1,103 @@
export const DESIGN_SPEC_VERSION = "0.1" as const;
export type DesignSpecSectionType =
| "hero"
| "positioning"
| "biography"
| "credentials"
| "portfolio"
| "services"
| "process"
| "expectations"
| "availability"
| "contact";
export type DesignSpecSection = {
type: DesignSpecSectionType;
title: string;
content?: string[];
action?: string;
};
export type DesignSpec = {
version: typeof DESIGN_SPEC_VERSION;
templateKey: string;
displayName: string;
tone: string;
theme: {
primaryColor: string;
backgroundColor: string;
surfaceColor: string;
textColor: string;
mutedTextColor: string;
borderRadius: number;
};
sections: DesignSpecSection[];
};
const SECTION_TYPES: DesignSpecSectionType[] = [
"hero",
"positioning",
"biography",
"credentials",
"portfolio",
"services",
"process",
"expectations",
"availability",
"contact",
];
const HEX_COLOR_PATTERN = /^#[0-9a-f]{6}$/i;
export function validateDesignSpec(value: unknown): string[] {
const errors: string[] = [];
if (!value || typeof value !== "object") return ["DesignSpec must be an object"];
const spec = value as Partial<DesignSpec>;
if (spec.version !== DESIGN_SPEC_VERSION) errors.push(`version must be ${DESIGN_SPEC_VERSION}`);
if (!spec.templateKey?.trim()) errors.push("templateKey is required");
if (!spec.displayName?.trim()) errors.push("displayName is required");
if (!spec.tone?.trim()) errors.push("tone is required");
if (!spec.theme || typeof spec.theme !== "object") {
errors.push("theme is required");
} else {
const theme = spec.theme;
for (const key of [
"primaryColor",
"backgroundColor",
"surfaceColor",
"textColor",
"mutedTextColor",
] as const) {
if (!HEX_COLOR_PATTERN.test(theme[key] ?? ""))
errors.push(`theme.${key} must be a six-digit hex color`);
}
if (!Number.isFinite(theme.borderRadius) || theme.borderRadius < 0 || theme.borderRadius > 32)
errors.push("theme.borderRadius must be between 0 and 32");
}
if (!Array.isArray(spec.sections) || !spec.sections.length)
errors.push("sections must not be empty");
else {
const sectionTypes = new Set<string>();
spec.sections.forEach((section, index) => {
const prefix = `sections.${index}`;
if (!section || typeof section !== "object") {
errors.push(`${prefix} must be an object`);
return;
}
if (!SECTION_TYPES.includes(section.type)) errors.push(`${prefix}.type is invalid`);
if (!section.title?.trim()) errors.push(`${prefix}.title is required`);
if (section.content !== undefined && !Array.isArray(section.content))
errors.push(`${prefix}.content must be an array`);
if (section.type === "contact") sectionTypes.add(section.type);
});
if (!sectionTypes.has("contact")) errors.push("sections must include a contact section");
}
return errors;
}
export { renderDesignSpec } from "./render.ts";
+62
View File
@@ -0,0 +1,62 @@
import type { DesignSpec } from "./index.ts";
function escapeHtml(value: string): string {
return value
.replaceAll("&", "&amp;")
.replaceAll("<", "&lt;")
.replaceAll(">", "&gt;")
.replaceAll('"', "&quot;");
}
export function renderDesignSpec(spec: DesignSpec): string {
const sections = spec.sections
.map((section) => {
const content = (section.content ?? [])
.map((item) => `<p data-field="content">${escapeHtml(item)}</p>`)
.join("");
const action = section.action
? `<button type="button" data-action="${escapeHtml(section.type)}">${escapeHtml(section.action)}</button>`
: "";
return `<section data-template-section="${escapeHtml(section.type)}"><h2 data-field="title">${escapeHtml(section.title)}</h2>${content}${action}</section>`;
})
.join("");
return `<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>${escapeHtml(spec.displayName)}</title>
<style>
:root {
--color-primary: ${spec.theme.primaryColor};
--color-bg: ${spec.theme.backgroundColor};
--color-surface: ${spec.theme.surfaceColor};
--color-text: ${spec.theme.textColor};
--color-muted: ${spec.theme.mutedTextColor};
--radius: ${spec.theme.borderRadius}px;
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--color-bg); color: var(--color-text); font-family: system-ui, sans-serif; }
main { max-width: 375px; margin: 0 auto; padding: 24px 20px 48px; }
header { padding: 12px 0 28px; }
h1 { font-size: 28px; margin: 0; }
header p { color: var(--color-muted); margin: 8px 0 0; }
section { background: var(--color-surface); border-radius: var(--radius); padding: 20px; margin-bottom: 16px; }
h2 { font-size: 17px; margin: 0 0 12px; }
p { color: var(--color-muted); font-size: 14px; line-height: 1.7; margin: 0 0 10px; }
p:last-of-type { margin-bottom: 0; }
button { background: var(--color-primary); color: #fff; border: 0; border-radius: var(--radius); padding: 10px 16px; font-size: 14px; margin-top: 12px; }
</style>
</head>
<body>
<main>
<header>
<h1 data-field="displayName">${escapeHtml(spec.displayName)}</h1>
<p data-field="tone">${escapeHtml(spec.tone)}</p>
</header>
${sections}
</main>
</body>
</html>`;
}
@@ -0,0 +1,41 @@
import assert from "node:assert/strict";
import test from "node:test";
import { DESIGN_SPEC_VERSION, validateDesignSpec } from "../src/index.ts";
import { renderDesignSpec } from "../src/render.ts";
const validSpec = {
version: DESIGN_SPEC_VERSION,
templateKey: "acheng-campus-food",
displayName: "阿成校园餐",
tone: "干净、亲切、可信",
theme: {
primaryColor: "#f2683c",
backgroundColor: "#f7f7f4",
surfaceColor: "#ffffff",
textColor: "#1f2328",
mutedTextColor: "#6b7280",
borderRadius: 16,
},
sections: [
{ type: "hero", title: "每天现做的校园餐", content: ["周三到周日供应"], action: "查看菜单" },
{ type: "services", title: "供应方式", content: ["单餐预订", "周餐预订"] },
{ type: "contact", title: "联系阿成", content: ["微信预约"] },
],
};
test("accepts a valid DesignSpec", () => {
assert.deepEqual(validateDesignSpec(validSpec), []);
});
test("requires a contact section", () => {
const errors = validateDesignSpec({ ...validSpec, sections: validSpec.sections.slice(0, 2) });
assert.equal(errors.includes("sections must include a contact section"), true);
});
test("renders machine-readable HTML", () => {
const html = renderDesignSpec(validSpec);
assert.match(html, /data-template-section="hero"/);
assert.match(html, /data-field="displayName"/);
assert.match(html, /data-action="hero"/);
});
+9
View File
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"allowJs": true,
"allowImportingTsExtensions": true,
"noEmit": true
},
"include": ["src/**/*.ts", "test/**/*.mjs"]
}