Files
maoxiang d36050cff7 初始化仓库
初始化仓库
2026-09-09 13:19:09 +08:00

89 lines
2.9 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
const fs = require('fs');
const path = require('path');
const ROOT = process.cwd();
const SKILL = '/Users/huanghui/.codebuddy/skills/halo-theme-dev/assets/theme-vite';
// 1. 创建目录
for (const d of ['src', 'src/partials', 'src/css', 'src/js']) {
fs.mkdirSync(path.join(ROOT, d), { recursive: true });
}
// 2. 复制构建配置
fs.copyFileSync(path.join(SKILL, 'package.json'), path.join(ROOT, 'package.json'));
fs.copyFileSync(path.join(SKILL, 'vite.config.ts'), path.join(ROOT, 'vite.config.ts'));
console.log('✓ 已复制 package.json / vite.config.ts');
// 3. 从 templates/index.html 提取 layout 骨架
const idx = fs.readFileSync(path.join(ROOT, 'templates/index.html'), 'utf8');
const header = idx.match(/<header class="site-header">[\s\S]*?<\/header>/)[0];
const footer = idx.match(/<footer class="site-footer">[\s\S]*?<\/footer>/)[0];
const layout = `<!doctype html>
<html
lang="zh-CN"
xmlns:th="https://www.thymeleaf.org"
th:lang="\${#locale.toLanguageTag}"
class="color-scheme-auto"
data-color-scheme="auto"
>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<slot name="head">
<title th:text="\${site.title}">源蝶工作室</title>
</slot>
<script type="module" src="./js/main.ts"></script>
</head>
<body
th:style="'--primary-color:' + (\${theme.config.style?.primary_color} ?: '#2563eb') + ';--radius:' + (\${theme.config.style?.border_radius} ?: '12px')"
th:with="brandName = \${theme.config.brand?.brand_name ?: site.title}"
>
${header}
<main class="site-main">
<slot />
</main>
${footer}
</body>
</html>
`;
fs.writeFileSync(path.join(ROOT, 'src/partials/layout.html'), layout);
console.log('✓ 已生成 src/partials/layout.html');
// 4. 反推各页面(include + template + main 内容)
const pages = [
'index', 'post', 'category', 'tag', 'page',
'archives', 'author', 'categories', 'tags'
];
for (const name of pages) {
const tpl = fs.readFileSync(path.join(ROOT, `templates/${name}.html`), 'utf8');
const titleM = tpl.match(/<title[^>]*>[\s\S]*?<\/title>/);
const mainM = tpl.match(/<main class="site-main">([\s\S]*?)<\/main>/);
if (!mainM) {
console.log('⚠ 跳过(无 main:', name);
continue;
}
const title = titleM ? titleM[0] : `<title th:text="\${site.title}"></title>`;
const content = mainM[1].replace(/\n$/, '');
const out = `<include src="layout.html">
<template name="head">
${title}
</template>
${content}
</include>
`;
fs.writeFileSync(path.join(ROOT, `src/${name}.html`), out);
console.log('✓ 反推 src/' + name + '.html');
}
// 5. 复制 fragment 文件(不含 layout,直接复制)
for (const f of ['header-menu.html', 'user-menu.html', 'sidebar.html']) {
fs.copyFileSync(path.join(ROOT, `templates/${f}`), path.join(ROOT, `src/${f}`));
console.log('✓ 复制 src/' + f);
}
console.log('\n全部 html 反推完成');