@wkgeo/cli 1.0.0 → 1.1.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/CHANGELOG.md +249 -203
- package/README.md +151 -19
- package/SKILL.md +108 -27
- package/lib/args.mjs +2 -2
- package/lib/asset-role.mjs +68 -0
- package/lib/cms.mjs +2 -2
- package/lib/commands/billing.mjs +56 -10
- package/lib/commands/cms.mjs +49 -13
- package/lib/commands/content.mjs +108 -0
- package/lib/commands/crawl.mjs +219 -0
- package/lib/commands/geo.mjs +1673 -0
- package/lib/commands/index.mjs +640 -528
- package/lib/commands/member.mjs +930 -0
- package/lib/commands/seo.mjs +1386 -0
- package/lib/commands/siteops.mjs +3 -3
- package/lib/commands/sites.mjs +21 -125
- package/lib/commands/template.mjs +1474 -0
- package/lib/config.mjs +16 -13
- package/lib/content-apply.mjs +664 -0
- package/lib/content-check.mjs +887 -0
- package/lib/content-contract.mjs +957 -0
- package/lib/errors.mjs +6 -0
- package/lib/format.mjs +5 -1
- package/lib/manifest-apply.mjs +1499 -0
- package/lib/manifest-schema.json +476 -0
- package/lib/manifest.mjs +1082 -0
- package/lib/oauth-urls.mjs +1 -1
- package/lib/runtime.mjs +5 -1
- package/lib/site-create.mjs +536 -0
- package/lib/tar.mjs +207 -0
- package/lib/template-anim.mjs +1338 -0
- package/lib/template-animation.mjs +232 -0
- package/lib/template-capture.mjs +700 -0
- package/lib/template-gen.mjs +1204 -0
- package/lib/template-pack.mjs +206 -0
- package/lib/template-package.mjs +522 -0
- package/lib/template-publish.mjs +733 -0
- package/lib/template-score.mjs +865 -0
- package/lib/template-validate.mjs +368 -0
- package/lib/template-verify.mjs +434 -0
- package/lib/templates-sync.mjs +616 -0
- package/package.json +2 -2
|
@@ -0,0 +1,1204 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 模板生成:`wkgeo template init --from-url <url>` / `--from-dir <dir>`。
|
|
3
|
+
*
|
|
4
|
+
* ## 思路(为什么不"重写一遍布局")
|
|
5
|
+
* 老板 2026-10-30 定的验收是「生成页与原站截图**自比 ≥95%**」。凭感觉重写布局几乎不可能稳定过线,
|
|
6
|
+
* 所以这里的 from-url 做的是**抓站镜像**:把站点的 HTML 原样存成 `pages/*.html`,
|
|
7
|
+
* 只做两类必要改写——
|
|
8
|
+
* 1. 链接:站内绝对/根相对地址 → 本模板内的相对页面文件(渲染不受影响);
|
|
9
|
+
* 2. 资源:`<link href>` / `<script src>` / `<img src|srcset>` / 内联 `url()` → 下载到 `assets/`。
|
|
10
|
+
* 这样浏览器渲染出来的 DOM 与原站逐节点一致,一致率才有意义。
|
|
11
|
+
*
|
|
12
|
+
* from-dir 是同一套流程换成本地静态目录:读本地 HTML、把被引用的本地资源收进 `assets/`、
|
|
13
|
+
* 反过来从 `<nav>` / `<title>` / 卡片标题推出 v2 `template.json`(栏目 / 菜单 / 字段 / 演示数据)。
|
|
14
|
+
*
|
|
15
|
+
* ## 产出(v2 口径,见 templates/_base/README.md §3 §4)
|
|
16
|
+
* ```
|
|
17
|
+
* <out>/
|
|
18
|
+
* template.json _format=wkgeo-template@2 / key / site / menus / columns / requires / data
|
|
19
|
+
* pages/*.html 入口 index.html(必填)+ 各栏目页
|
|
20
|
+
* partials/ header.html / footer.html(从首页抽出来,便于人改)
|
|
21
|
+
* assets/ 本站资源 + base/(_base 逐字节副本)+ img/placeholder.svg
|
|
22
|
+
* design/ desktop-1440.png / mobile-375.png(+ 各自 -t0 / -t1500 动画帧)
|
|
23
|
+
* ```
|
|
24
|
+
* 截图失败 → 抛 CaptureError(命令层 exit 1),不留"半套"产物。
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
import fs from 'node:fs';
|
|
28
|
+
import fsp from 'node:fs/promises';
|
|
29
|
+
import path from 'node:path';
|
|
30
|
+
import crypto from 'node:crypto';
|
|
31
|
+
import { fileURLToPath } from 'node:url';
|
|
32
|
+
|
|
33
|
+
import { CliError, UsageError } from './errors.mjs';
|
|
34
|
+
import { VIEWPORTS, captureDesign, inspectDesign, resolveChrome } from './template-capture.mjs';
|
|
35
|
+
|
|
36
|
+
export const TEMPLATE_FORMAT = 'wkgeo-template@2';
|
|
37
|
+
export const DESIGN_VIEWPORTS = VIEWPORTS;
|
|
38
|
+
|
|
39
|
+
const HERE = path.dirname(fileURLToPath(import.meta.url));
|
|
40
|
+
|
|
41
|
+
// ---------------------------------------------------------------- 基础文本工具
|
|
42
|
+
|
|
43
|
+
const ENTITIES = {
|
|
44
|
+
amp: '&',
|
|
45
|
+
lt: '<',
|
|
46
|
+
gt: '>',
|
|
47
|
+
quot: '"',
|
|
48
|
+
apos: "'",
|
|
49
|
+
nbsp: ' ',
|
|
50
|
+
ldquo: '“',
|
|
51
|
+
rdquo: '”',
|
|
52
|
+
mdash: '—',
|
|
53
|
+
hellip: '…',
|
|
54
|
+
middot: '·',
|
|
55
|
+
copy: '©',
|
|
56
|
+
reg: '®',
|
|
57
|
+
times: '×',
|
|
58
|
+
laquo: '«',
|
|
59
|
+
raquo: '»',
|
|
60
|
+
ensp: ' ',
|
|
61
|
+
emsp: ' ',
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
export function decodeEntities(s) {
|
|
65
|
+
if (!s) return '';
|
|
66
|
+
return String(s).replace(/&(#x?[0-9a-fA-F]+|[a-zA-Z][a-zA-Z0-9]*);/g, (m, body) => {
|
|
67
|
+
if (body[0] === '#') {
|
|
68
|
+
const code = body[1] === 'x' || body[1] === 'X' ? parseInt(body.slice(2), 16) : parseInt(body.slice(1), 10);
|
|
69
|
+
return Number.isFinite(code) && code > 0 && code < 0x110000 ? String.fromCodePoint(code) : m;
|
|
70
|
+
}
|
|
71
|
+
const k = body.toLowerCase();
|
|
72
|
+
return Object.prototype.hasOwnProperty.call(ENTITIES, k) ? ENTITIES[k] : m;
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** 去标签 + 解实体 + 压空白 */
|
|
77
|
+
export function textOf(html) {
|
|
78
|
+
return decodeEntities(String(html || '').replace(/<[^>]*>/g, ' '))
|
|
79
|
+
.replace(/\s+/g, ' ')
|
|
80
|
+
.trim();
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function escapeHtml(s) {
|
|
84
|
+
return String(s == null ? '' : s)
|
|
85
|
+
.replace(/&/g, '&')
|
|
86
|
+
.replace(/</g, '<')
|
|
87
|
+
.replace(/>/g, '>')
|
|
88
|
+
.replace(/"/g, '"');
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** 从一段"标签原文"里取属性值(不引 DOM 解析器) */
|
|
92
|
+
export function getAttr(tagHtml, name) {
|
|
93
|
+
const re = new RegExp(`(?:^|\\s)${name.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\s*=\\s*("([^"]*)"|'([^']*)'|([^\\s"'>]+))`, 'i');
|
|
94
|
+
const m = String(tagHtml).match(re);
|
|
95
|
+
if (!m) return null;
|
|
96
|
+
return decodeEntities(m[2] !== undefined ? m[2] : m[3] !== undefined ? m[3] : m[4] || '');
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** 用 `<tag ...>` ... `</tag>` 的深度匹配取**第一个**同名元素(含标签本身) */
|
|
100
|
+
export function sliceElement(html, tagName) {
|
|
101
|
+
const s = String(html);
|
|
102
|
+
const open = new RegExp(`<${tagName}\\b`, 'i');
|
|
103
|
+
const any = new RegExp(`<${tagName}\\b[^>]*>|</${tagName}\\s*>`, 'gi');
|
|
104
|
+
const first = s.search(open);
|
|
105
|
+
if (first < 0) return null;
|
|
106
|
+
any.lastIndex = first;
|
|
107
|
+
let depth = 0;
|
|
108
|
+
let m;
|
|
109
|
+
while ((m = any.exec(s))) {
|
|
110
|
+
if (m[0][1] === '/') {
|
|
111
|
+
depth--;
|
|
112
|
+
if (depth === 0) return s.slice(first, m.index + m[0].length);
|
|
113
|
+
} else if (!/\/>$/.test(m[0]) && !['img', 'input', 'br', 'hr', 'meta', 'link', 'source'].includes(tagName.toLowerCase())) {
|
|
114
|
+
depth++;
|
|
115
|
+
} else if (depth === 0) {
|
|
116
|
+
return m[0];
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return null;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** 所有 `name="value"` 形式的属性改写 */
|
|
123
|
+
function replaceAttr(html, attrName, mapper) {
|
|
124
|
+
const re = new RegExp(`(\\s${attrName}\\s*=\\s*)(["'])([^"']*)\\2`, 'gi');
|
|
125
|
+
return String(html).replace(re, (m, pre, q, val) => {
|
|
126
|
+
const next = mapper(val);
|
|
127
|
+
return next == null ? m : `${pre}${q}${next.replace(new RegExp(q, 'g'), q === '"' ? '"' : ''')}${q}`;
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export function slugify(s, fallback = 'page') {
|
|
132
|
+
const out = String(s || '')
|
|
133
|
+
.toLowerCase()
|
|
134
|
+
.replace(/[^a-z0-9\u4e00-\u9fa5]+/g, '-')
|
|
135
|
+
.replace(/^-+|-+$/g, '')
|
|
136
|
+
.replace(/-{2,}/g, '-');
|
|
137
|
+
return out || fallback;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function shortHash(s, n = 6) {
|
|
141
|
+
return crypto.createHash('sha1').update(String(s)).digest('hex').slice(0, n);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
145
|
+
|
|
146
|
+
// ---------------------------------------------------------------- 网络
|
|
147
|
+
|
|
148
|
+
export function normalizeSiteUrl(input) {
|
|
149
|
+
let u;
|
|
150
|
+
try {
|
|
151
|
+
u = new URL(String(input).trim());
|
|
152
|
+
} catch {
|
|
153
|
+
throw new UsageError(`--from-url 不是合法 URL:${input}(要 http:// 或 https:// 开头)`);
|
|
154
|
+
}
|
|
155
|
+
if (!/^https?:$/.test(u.protocol)) throw new UsageError(`--from-url 只支持 http/https:${input}`);
|
|
156
|
+
if (!/\/$/.test(u.pathname) && !path.extname(u.pathname)) u.pathname += '/';
|
|
157
|
+
u.hash = '';
|
|
158
|
+
return u;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const UA = 'wkgeo-cli-template/1.0 (+https://wkgeo.cn)';
|
|
162
|
+
|
|
163
|
+
async function fetchBuffer(url, { fetchImpl, timeoutMs = 30000, referer = null } = {}) {
|
|
164
|
+
const f = fetchImpl || globalThis.fetch;
|
|
165
|
+
const ctrl = new AbortController();
|
|
166
|
+
const timer = setTimeout(() => ctrl.abort(), timeoutMs);
|
|
167
|
+
try {
|
|
168
|
+
const res = await f(url, {
|
|
169
|
+
signal: ctrl.signal,
|
|
170
|
+
redirect: 'follow',
|
|
171
|
+
headers: { 'user-agent': UA, ...(referer ? { referer } : {}) },
|
|
172
|
+
});
|
|
173
|
+
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
|
174
|
+
const buf = Buffer.from(await res.arrayBuffer());
|
|
175
|
+
return { buf, contentType: res.headers.get('content-type') || '' };
|
|
176
|
+
} finally {
|
|
177
|
+
clearTimeout(timer);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
async function fetchText(url, opts = {}) {
|
|
182
|
+
const { buf, contentType } = await fetchBuffer(url, opts);
|
|
183
|
+
let text = buf.toString('utf8');
|
|
184
|
+
if (/charset=(gbk|gb2312|gb18030)/i.test(contentType)) {
|
|
185
|
+
// Node 内置没有 GBK 解码;直接抛出让人知道问题,不静默出乱码
|
|
186
|
+
throw new CliError(`目标页是 GBK 编码,当前实现只支持 UTF-8:${url}`, {
|
|
187
|
+
code: 'EENCODING',
|
|
188
|
+
hint: '先转成 UTF-8 静态目录,再用 --from-dir',
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
return { text, contentType, bytes: buf.length };
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** 抓站失败要给人能照做的原因 */
|
|
195
|
+
function wrapFetchError(url, err, timeoutMs) {
|
|
196
|
+
const msg = String((err && err.message) || err);
|
|
197
|
+
const hint =
|
|
198
|
+
/abort/i.test(msg) || /timeout/i.test(msg)
|
|
199
|
+
? `请求超时(${timeoutMs}ms)——网络慢或站点慢,可加 --http-timeout 调大`
|
|
200
|
+
: /ENOTFOUND|EAI_AGAIN|getaddrinfo/i.test(msg)
|
|
201
|
+
? '域名解析失败——先确认能 ping 通 / 代理是否要设'
|
|
202
|
+
: /HTTP 4\d\d/.test(msg)
|
|
203
|
+
? '站点返回 4xx——地址对不对?'
|
|
204
|
+
: '检查网络与目标站点是否可访问';
|
|
205
|
+
return new CliError(`抓取失败:${url}\n 原因:${msg}\n ${hint}`, { kind: 'fetch', code: 'EFETCH', cause: err });
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// ---------------------------------------------------------------- 定位 _base
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* 找 `templates/_base`(只读引用):显式参数 > 环境变量 > 从 cwd 往上找 > 从本文件往上找。
|
|
212
|
+
* 找不到就返回 null(validate 会降级为"只查 assets/base 存在")。
|
|
213
|
+
*/
|
|
214
|
+
export function resolveBaseDir(explicit, { cwd = process.cwd(), env = process.env } = {}) {
|
|
215
|
+
const ok = (dir) => dir && fs.existsSync(path.join(dir, 'assets', 'base.css'));
|
|
216
|
+
if (ok(explicit)) return path.resolve(explicit);
|
|
217
|
+
if (ok(env.WKGEO_BASE_TEMPLATE)) return path.resolve(env.WKGEO_BASE_TEMPLATE);
|
|
218
|
+
let dir = path.resolve(cwd);
|
|
219
|
+
for (let i = 0; i < 8; i++) {
|
|
220
|
+
const cand = path.join(dir, 'templates', '_base');
|
|
221
|
+
if (ok(cand)) return cand;
|
|
222
|
+
const parent = path.dirname(dir);
|
|
223
|
+
if (parent === dir) break;
|
|
224
|
+
dir = parent;
|
|
225
|
+
}
|
|
226
|
+
dir = HERE;
|
|
227
|
+
for (let i = 0; i < 8; i++) {
|
|
228
|
+
const cand = path.join(dir, 'templates', '_base');
|
|
229
|
+
if (ok(cand)) return cand;
|
|
230
|
+
const parent = path.dirname(dir);
|
|
231
|
+
if (parent === dir) break;
|
|
232
|
+
dir = parent;
|
|
233
|
+
}
|
|
234
|
+
return null;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/** `assets/base/` = `_base/assets/` 的逐字节副本 */
|
|
238
|
+
export async function syncBaseAssets(outDir, baseDir) {
|
|
239
|
+
const dest = path.join(outDir, 'assets', 'base');
|
|
240
|
+
if (!baseDir) return { copied: false, reason: '没找到 templates/_base(跳过 assets/base 同步)' };
|
|
241
|
+
await fsp.mkdir(path.dirname(dest), { recursive: true });
|
|
242
|
+
await fsp.rm(dest, { recursive: true, force: true });
|
|
243
|
+
await fsp.cp(path.join(baseDir, 'assets'), dest, { recursive: true });
|
|
244
|
+
return { copied: true, from: path.join(baseDir, 'assets'), to: dest };
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
const PLACEHOLDER_SVG =
|
|
248
|
+
'<svg xmlns="http://www.w3.org/2000/svg" width="640" height="360" viewBox="0 0 640 360" role="img" aria-label="占位图">' +
|
|
249
|
+
'<rect width="640" height="360" fill="#eef2f7"/><path d="M0 300 200 160l120 90 100-70 220 170H0z" fill="#d5dfeb"/>' +
|
|
250
|
+
'<circle cx="470" cy="90" r="34" fill="#d5dfeb"/></svg>\n';
|
|
251
|
+
|
|
252
|
+
async function ensurePlaceholder(outDir) {
|
|
253
|
+
const file = path.join(outDir, 'assets', 'img', 'placeholder.svg');
|
|
254
|
+
await fsp.mkdir(path.dirname(file), { recursive: true });
|
|
255
|
+
await fsp.writeFile(file, PLACEHOLDER_SVG, 'utf8');
|
|
256
|
+
return file;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
// ---------------------------------------------------------------- v2 template.json 组装
|
|
260
|
+
|
|
261
|
+
/** 一个栏目默认的 4 个字段(12 种类型里只用 text/textarea/image/link,够表达列表卡片) */
|
|
262
|
+
export function defaultFields() {
|
|
263
|
+
return [
|
|
264
|
+
{ key: 'title', label: '标题', type: 'text', required: true, options: [], group: '基础', sort: 1, hint: '列表项标题,≤ 80 字' },
|
|
265
|
+
{ key: 'summary', label: '摘要', type: 'textarea', required: false, options: [], group: '基础', sort: 2, hint: '一两句话说明,≤ 200 字' },
|
|
266
|
+
{ key: 'cover', label: '封面图', type: 'image', required: false, options: [], group: '基础', sort: 3, hint: '建议 16:9,缺省用 assets/img/placeholder.svg' },
|
|
267
|
+
{
|
|
268
|
+
key: 'link',
|
|
269
|
+
label: '链接',
|
|
270
|
+
type: 'link',
|
|
271
|
+
required: false,
|
|
272
|
+
options: [],
|
|
273
|
+
group: '基础',
|
|
274
|
+
sort: 4,
|
|
275
|
+
hint: '站内页面 / 锚点 / 站外 URL 三态',
|
|
276
|
+
},
|
|
277
|
+
];
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/** item 只保留字段声明过的键,多余键丢掉(validate 会查 items 与 fields 对齐) */
|
|
281
|
+
export function alignItems(items, fields) {
|
|
282
|
+
const keys = fields.map((f) => f.key);
|
|
283
|
+
return (items || []).map((it) => {
|
|
284
|
+
const o = {};
|
|
285
|
+
for (const k of keys) o[k] = it && Object.prototype.hasOwnProperty.call(it, k) ? it[k] : '';
|
|
286
|
+
return o;
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
function seoOf({ title, description, keywords }) {
|
|
291
|
+
return {
|
|
292
|
+
title: String(title || '').slice(0, 120),
|
|
293
|
+
keywords: String(keywords || '').slice(0, 200),
|
|
294
|
+
description: String(description || '').slice(0, 300),
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/** 从 HTML 里读 title / description / keywords / 主题色 / 字族 */
|
|
299
|
+
export function analyzeHead(html, fallbackTitle = '') {
|
|
300
|
+
const s = String(html || '');
|
|
301
|
+
const title = textOf((s.match(/<title[^>]*>([\s\S]*?)<\/title>/i) || [])[1] || '') || fallbackTitle;
|
|
302
|
+
const metas = [...s.matchAll(/<meta\b[^>]*>/gi)].map((m) => m[0]);
|
|
303
|
+
const metaOf = (name) => {
|
|
304
|
+
for (const tag of metas) {
|
|
305
|
+
const n = (getAttr(tag, 'name') || getAttr(tag, 'property') || '').toLowerCase();
|
|
306
|
+
if (n === name) return getAttr(tag, 'content') || '';
|
|
307
|
+
}
|
|
308
|
+
return '';
|
|
309
|
+
};
|
|
310
|
+
const css = [...s.matchAll(/<style\b[^>]*>([\s\S]*?)<\/style>/gi)].map((m) => m[1]).join('\n');
|
|
311
|
+
const colorRe = /--c-primary\s*:\s*(#[0-9a-fA-F]{3,8}|rgba?\([^)]+\)|[a-zA-Z]+)/;
|
|
312
|
+
const primary = (css.match(colorRe) || [])[1] || '';
|
|
313
|
+
const fontRe = /--font-sans\s*:\s*([^;]+);/;
|
|
314
|
+
const font = ((css.match(fontRe) || [])[1] || '').replace(/\s+/g, ' ').trim();
|
|
315
|
+
return {
|
|
316
|
+
title,
|
|
317
|
+
description: metaOf('description'),
|
|
318
|
+
keywords: metaOf('keywords'),
|
|
319
|
+
primary: primary || '#1f5eff',
|
|
320
|
+
font: font || 'system-ui, -apple-system, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif',
|
|
321
|
+
hasViewport: /<meta\b[^>]*name=["']viewport["']/i.test(s),
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/** 从页面里抽演示数据:h3(无则 h2)标题 + 紧随其后的 <p> 摘要 */
|
|
326
|
+
export function extractItems(html, { max = 12 } = {}) {
|
|
327
|
+
const s = String(html || '');
|
|
328
|
+
const items = [];
|
|
329
|
+
const seen = new Set();
|
|
330
|
+
const re = /<h([23])\b[^>]*>([\s\S]*?)<\/h\1>/gi;
|
|
331
|
+
let m;
|
|
332
|
+
while ((m = re.exec(s)) && items.length < max) {
|
|
333
|
+
const title = textOf(m[2]);
|
|
334
|
+
if (title.length < 2 || title.length > 80) continue;
|
|
335
|
+
const key = title.toLowerCase();
|
|
336
|
+
if (seen.has(key)) continue;
|
|
337
|
+
const tail = s.slice(m.index + m[0].length, m.index + m[0].length + 1200);
|
|
338
|
+
let summary = '';
|
|
339
|
+
const p = tail.match(/<p\b[^>]*>([\s\S]*?)<\/p>/i);
|
|
340
|
+
if (p) summary = textOf(p[1]).slice(0, 160);
|
|
341
|
+
const a = tail.match(/<a\b[^>]*>/i);
|
|
342
|
+
const href = a ? getAttr(a[0], 'href') : null;
|
|
343
|
+
seen.add(key);
|
|
344
|
+
items.push({
|
|
345
|
+
title,
|
|
346
|
+
summary,
|
|
347
|
+
cover: '',
|
|
348
|
+
link: href ? { menu_type: 'custom', link_type: /^(https?:)?\/\//i.test(href) ? 'external' : 'internal', href } : '',
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
return items;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/** 抽链接(只要 label 非空的),`scope` 是 HTML 片段 */
|
|
355
|
+
export function extractLinks(scope, baseHref = '/') {
|
|
356
|
+
const out = [];
|
|
357
|
+
const re = /<a\b([^>]*)>([\s\S]*?)<\/a>/gi;
|
|
358
|
+
let m;
|
|
359
|
+
while ((m = re.exec(String(scope || '')))) {
|
|
360
|
+
const attrs = m[1];
|
|
361
|
+
const href = getAttr(`<a${attrs}>`, 'href');
|
|
362
|
+
const label = textOf(m[2]);
|
|
363
|
+
if (!href || !label) continue;
|
|
364
|
+
if (/^(javascript:|tel:|mailto:)/i.test(href)) {
|
|
365
|
+
out.push({ href, label, external: false, special: true });
|
|
366
|
+
continue;
|
|
367
|
+
}
|
|
368
|
+
out.push({
|
|
369
|
+
href,
|
|
370
|
+
label,
|
|
371
|
+
external: /^(https?:)?\/\//i.test(href) && baseHref ? new URL(href, baseHref).origin !== new URL(baseHref).origin : /^(https?:)?\/\//i.test(href),
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
return out;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/** 把 href 变成"站内路径 + 锚点" */
|
|
378
|
+
export function splitHref(href, baseUrl) {
|
|
379
|
+
try {
|
|
380
|
+
const u = new URL(href, baseUrl);
|
|
381
|
+
return { path: u.pathname, hash: u.hash.replace(/^#/, ''), origin: u.origin, search: u.search, isHttp: /^https?:$/.test(u.protocol) };
|
|
382
|
+
} catch {
|
|
383
|
+
return { path: href, hash: '', origin: '', search: '', isHttp: false };
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/** 路径 → 页面主名(不带 .html) */
|
|
388
|
+
export function pageNameFor(pathname) {
|
|
389
|
+
const segs = String(pathname || '/')
|
|
390
|
+
.split('/')
|
|
391
|
+
.filter(Boolean)
|
|
392
|
+
.map((s) => slugify(s.replace(/\.html?$/i, ''), 'page'));
|
|
393
|
+
if (!segs.length) return 'index';
|
|
394
|
+
return segs.slice(0, 3).join('-');
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
// ---------------------------------------------------------------- 资源本地化
|
|
398
|
+
|
|
399
|
+
function extOf(urlPath, contentType) {
|
|
400
|
+
const byPath = path.extname(String(urlPath || '').split('?')[0]).toLowerCase();
|
|
401
|
+
if (byPath && byPath.length <= 6) return byPath;
|
|
402
|
+
const map = {
|
|
403
|
+
'text/css': '.css',
|
|
404
|
+
'text/javascript': '.js',
|
|
405
|
+
'application/javascript': '.js',
|
|
406
|
+
'image/png': '.png',
|
|
407
|
+
'image/jpeg': '.jpg',
|
|
408
|
+
'image/svg+xml': '.svg',
|
|
409
|
+
'image/webp': '.webp',
|
|
410
|
+
'image/gif': '.gif',
|
|
411
|
+
'image/x-icon': '.ico',
|
|
412
|
+
'font/woff2': '.woff2',
|
|
413
|
+
'font/woff': '.woff',
|
|
414
|
+
};
|
|
415
|
+
return map[String(contentType || '').split(';')[0].trim()] || '.bin';
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
function safeAssetName(urlPath, contentType) {
|
|
419
|
+
const base = path.basename(String(urlPath || '').split('?')[0]) || 'asset';
|
|
420
|
+
const clean = base.replace(/[^A-Za-z0-9._-]/g, '-').replace(/^-+/, '') || 'asset';
|
|
421
|
+
const ext = extOf(urlPath, contentType);
|
|
422
|
+
const stem = clean.toLowerCase().endsWith(ext.toLowerCase()) ? clean.slice(0, -ext.length) : clean;
|
|
423
|
+
return `${slugify(stem, 'asset')}${ext}`;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
/**
|
|
427
|
+
* 下载清单里的资源并改写引用。
|
|
428
|
+
* @param items [{attr, value, kind:'css'|'js'|'img'|'url'|'font'}]
|
|
429
|
+
* @returns {rewrite: Map(原值 → ../assets/xxx), files:[{url,name,bytes}], errors:[]}
|
|
430
|
+
*/
|
|
431
|
+
async function localizeAssets(remoteItems, { assetsDir, fetchImpl, baseUrl, referer, timeoutMs, log }) {
|
|
432
|
+
const rewrite = new Map();
|
|
433
|
+
const files = [];
|
|
434
|
+
const errors = [];
|
|
435
|
+
const used = new Set();
|
|
436
|
+
for (const it of remoteItems) {
|
|
437
|
+
const raw = String(it.value || '').trim();
|
|
438
|
+
if (!raw || /^(data:|#|mailto:|tel:|javascript:)/i.test(raw)) continue;
|
|
439
|
+
if (rewrite.has(raw)) continue;
|
|
440
|
+
let abs;
|
|
441
|
+
try {
|
|
442
|
+
abs = new URL(raw, baseUrl);
|
|
443
|
+
} catch {
|
|
444
|
+
continue;
|
|
445
|
+
}
|
|
446
|
+
if (!/^https?:$/.test(abs.protocol)) continue;
|
|
447
|
+
let name = safeAssetName(abs.pathname, '');
|
|
448
|
+
if (used.has(name)) name = `${path.basename(name, path.extname(name))}-${shortHash(abs.href)}${path.extname(name)}`;
|
|
449
|
+
used.add(name);
|
|
450
|
+
try {
|
|
451
|
+
const { buf, contentType } = await fetchBuffer(abs.href, { fetchImpl, timeoutMs, referer });
|
|
452
|
+
if (/text\/css/i.test(contentType)) {
|
|
453
|
+
// CSS 里的 url() 再下一层(只一层,避免递归失控)
|
|
454
|
+
const css = buf.toString('utf8');
|
|
455
|
+
const subItems = [...css.matchAll(/url\(\s*(['"]?)([^'")]+)\1\s*\)/gi)].map((m) => ({ value: m[2] }));
|
|
456
|
+
const sub = await localizeAssets(subItems, {
|
|
457
|
+
assetsDir,
|
|
458
|
+
fetchImpl,
|
|
459
|
+
baseUrl: abs.href,
|
|
460
|
+
referer: abs.href,
|
|
461
|
+
timeoutMs,
|
|
462
|
+
log,
|
|
463
|
+
});
|
|
464
|
+
const patched = css.replace(/url\(\s*(['"]?)([^'")]+)\1\s*\)/gi, (m, q, v) => {
|
|
465
|
+
const to = sub.rewrite.get(v);
|
|
466
|
+
return to ? `url(${to.replace(/^\.\.\//, '')})` : m;
|
|
467
|
+
});
|
|
468
|
+
await fsp.writeFile(path.join(assetsDir, name), patched, 'utf8');
|
|
469
|
+
files.push({ url: abs.href, name, bytes: Buffer.byteLength(patched) });
|
|
470
|
+
} else {
|
|
471
|
+
await fsp.writeFile(path.join(assetsDir, name), buf);
|
|
472
|
+
files.push({ url: abs.href, name, bytes: buf.length });
|
|
473
|
+
}
|
|
474
|
+
rewrite.set(raw, `../assets/${name}`);
|
|
475
|
+
} catch (e) {
|
|
476
|
+
errors.push({ url: abs.href, reason: String((e && e.message) || e) });
|
|
477
|
+
if (log) log(` ! 资源下载失败(保留原地址):${abs.href} — ${(e && e.message) || e}`);
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
return { rewrite, files, errors };
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
/** 收集 HTML 里所有"外部/相对资源"引用 */
|
|
484
|
+
function collectResourceRefs(html, baseUrl) {
|
|
485
|
+
const s = String(html || '');
|
|
486
|
+
const items = [];
|
|
487
|
+
const push = (value) => {
|
|
488
|
+
if (value) items.push({ value });
|
|
489
|
+
};
|
|
490
|
+
for (const m of s.matchAll(/<(?:link|script|img|source|video|audio|iframe|embed)\b[^>]*>/gi)) {
|
|
491
|
+
const tag = m[0];
|
|
492
|
+
const name = (tag.match(/^<([a-z]+)/i) || [])[1].toLowerCase();
|
|
493
|
+
if (name === 'link') push(getAttr(tag, 'href'));
|
|
494
|
+
if (name === 'script') push(getAttr(tag, 'src'));
|
|
495
|
+
if (['img', 'source', 'video', 'audio', 'iframe', 'embed'].includes(name)) {
|
|
496
|
+
push(getAttr(tag, 'src'));
|
|
497
|
+
push(getAttr(tag, 'poster'));
|
|
498
|
+
const srcset = getAttr(tag, 'srcset');
|
|
499
|
+
if (srcset) for (const part of srcset.split(',')) push(part.trim().split(/\s+/)[0]);
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
for (const m of s.matchAll(/<style\b[^>]*>([\s\S]*?)<\/style>/gi)) {
|
|
503
|
+
for (const u of m[1].matchAll(/url\(\s*(['"]?)([^'")]+)\1\s*\)/gi)) push(u[2]);
|
|
504
|
+
}
|
|
505
|
+
for (const m of s.matchAll(/\sstyle\s*=\s*(["'])([\s\S]*?)\1/gi)) {
|
|
506
|
+
for (const u of m[2].matchAll(/url\(\s*(['"]?)([^'")]+)\1\s*\)/gi)) push(u[2]);
|
|
507
|
+
}
|
|
508
|
+
// 去掉 data:/纯锚点再按 URL 归一
|
|
509
|
+
const out = [];
|
|
510
|
+
const seen = new Set();
|
|
511
|
+
for (const it of items) {
|
|
512
|
+
const raw = String(it.value || '').trim();
|
|
513
|
+
if (!raw || /^(data:|#|mailto:|tel:|javascript:)/i.test(raw)) continue;
|
|
514
|
+
let abs;
|
|
515
|
+
try {
|
|
516
|
+
abs = new URL(raw, baseUrl);
|
|
517
|
+
} catch {
|
|
518
|
+
continue;
|
|
519
|
+
}
|
|
520
|
+
if (!/^https?:$/.test(abs.protocol)) continue;
|
|
521
|
+
if (seen.has(abs.href)) continue;
|
|
522
|
+
seen.add(abs.href);
|
|
523
|
+
out.push({ raw, href: abs.href });
|
|
524
|
+
}
|
|
525
|
+
return out;
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
/**
|
|
529
|
+
* 用 rewrite 表改写一段 HTML:href/src/srcset 属性 + <style>/style="" 里的 url()。
|
|
530
|
+
*/
|
|
531
|
+
function applyRewrites(html, { linkMap, assetMap }) {
|
|
532
|
+
let s = String(html || '');
|
|
533
|
+
|
|
534
|
+
// 1) <a href> → 本地页面
|
|
535
|
+
s = s.replace(/<a\b[^>]*>/gi, (tag) => {
|
|
536
|
+
const href = getAttr(tag, 'href');
|
|
537
|
+
if (href == null) return tag;
|
|
538
|
+
const mapped = linkMap.get(href);
|
|
539
|
+
return mapped == null ? tag : tag.replace(/(\shref\s*=\s*)(["'])[^"']*\2/i, `$1$2${mapped}$2`);
|
|
540
|
+
});
|
|
541
|
+
|
|
542
|
+
// 2) 资源属性
|
|
543
|
+
for (const attrName of ['href', 'src', 'poster']) {
|
|
544
|
+
s = replaceAttr(s, attrName, (v) => {
|
|
545
|
+
const to = assetMap.get(v);
|
|
546
|
+
if (to) return to;
|
|
547
|
+
// 相对/绝对地址也可能是同一资源(按 URL 归一后再试)
|
|
548
|
+
return null;
|
|
549
|
+
});
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
// 3) srcset(逗号分隔的多项)
|
|
553
|
+
s = s.replace(/(\ssrcset\s*=\s*)(["'])([^"']*)\2/gi, (m, pre, q, val) => {
|
|
554
|
+
const parts = val.split(',').map((p) => {
|
|
555
|
+
const [url, ...rest] = p.trim().split(/\s+/);
|
|
556
|
+
const to = assetMap.get(url);
|
|
557
|
+
return [to || url, ...rest].join(' ');
|
|
558
|
+
});
|
|
559
|
+
return `${pre}${q}${parts.join(', ')}${q}`;
|
|
560
|
+
});
|
|
561
|
+
|
|
562
|
+
// 4) <style> 与 style="" 里的 url()
|
|
563
|
+
const subUrl = (css) =>
|
|
564
|
+
css.replace(/url\(\s*(['"]?)([^'")]+)\1\s*\)/gi, (m, q, v) => {
|
|
565
|
+
const to = assetMap.get(v);
|
|
566
|
+
return to ? `url(${to.replace(/^\.\.\//, '')})` : m;
|
|
567
|
+
});
|
|
568
|
+
s = s.replace(/(<style\b[^>]*>)([\s\S]*?)(<\/style>)/gi, (m, a, body, b) => a + subUrl(body) + b);
|
|
569
|
+
s = s.replace(/(\sstyle\s*=\s*)(["'])([\s\S]*?)\2/gi, (m, pre, q, body) => `${pre}${q}${subUrl(body)}${q}`);
|
|
570
|
+
return s;
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
// ---------------------------------------------------------------- from-url
|
|
574
|
+
|
|
575
|
+
/**
|
|
576
|
+
* 抓站 → 生成模板。
|
|
577
|
+
* @returns {Promise<{dir,key,template,stats,pages,design}>}
|
|
578
|
+
*/
|
|
579
|
+
export async function generateFromUrl({
|
|
580
|
+
url,
|
|
581
|
+
outDir,
|
|
582
|
+
key,
|
|
583
|
+
fetchImpl,
|
|
584
|
+
timeoutMs = 30000,
|
|
585
|
+
maxPages = 8,
|
|
586
|
+
capture = true,
|
|
587
|
+
designDir = null,
|
|
588
|
+
chromeExe,
|
|
589
|
+
log = null,
|
|
590
|
+
logLine = null,
|
|
591
|
+
}) {
|
|
592
|
+
const site = normalizeSiteUrl(url);
|
|
593
|
+
const say = (s) => logLine && logLine(s);
|
|
594
|
+
|
|
595
|
+
say(`抓首页 ${site.href}`);
|
|
596
|
+
let home;
|
|
597
|
+
try {
|
|
598
|
+
home = await fetchText(site.href, { fetchImpl, timeoutMs });
|
|
599
|
+
} catch (e) {
|
|
600
|
+
throw wrapFetchError(site.href, e, timeoutMs);
|
|
601
|
+
}
|
|
602
|
+
const head = analyzeHead(home.text, site.hostname);
|
|
603
|
+
|
|
604
|
+
// ---- 1) 栏目:nav / header 里的链接(去重、去掉纯外链) ----
|
|
605
|
+
const headerHtml = sliceElement(home.text, 'header') || '';
|
|
606
|
+
const navHtml = sliceElement(home.text, 'nav') || '';
|
|
607
|
+
const navLinks = extractLinks(navHtml || headerHtml, site.href).filter((l) => !l.special);
|
|
608
|
+
const topCandidates = [];
|
|
609
|
+
const seenPath = new Set();
|
|
610
|
+
for (const l of navLinks) {
|
|
611
|
+
const s = splitHref(l.href, site.href);
|
|
612
|
+
if (!s.isHttp || s.origin !== site.origin) continue;
|
|
613
|
+
const id = `${s.path}#${s.hash}`;
|
|
614
|
+
if (seenPath.has(id)) continue;
|
|
615
|
+
seenPath.add(id);
|
|
616
|
+
topCandidates.push({ ...l, ...s });
|
|
617
|
+
}
|
|
618
|
+
say(`抽出导航链接 ${topCandidates.length} 条`);
|
|
619
|
+
|
|
620
|
+
// ---- 2) 页面集合:首页 + 导航里的站内页面(去重、限量) ----
|
|
621
|
+
const pagesWanted = [{ path: site.pathname || '/', hash: '', name: 'index' }];
|
|
622
|
+
const byPath = new Map([[(site.pathname || '/').replace(/\/?$/, '/'), 'index']]);
|
|
623
|
+
for (const c of topCandidates) {
|
|
624
|
+
if (c.hash) continue;
|
|
625
|
+
const p = c.path.replace(/\/?$/, '/');
|
|
626
|
+
if (byPath.has(p)) continue;
|
|
627
|
+
if (pagesWanted.length >= maxPages) break;
|
|
628
|
+
const name = pageNameFor(c.path);
|
|
629
|
+
byPath.set(p, name);
|
|
630
|
+
pagesWanted.push({ path: p, hash: '', name });
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
const pageHtml = new Map();
|
|
634
|
+
pageHtml.set('index', home.text);
|
|
635
|
+
for (const p of pagesWanted.slice(1)) {
|
|
636
|
+
const abs = new URL(p.path, site.origin).href;
|
|
637
|
+
try {
|
|
638
|
+
const got = await fetchText(abs, { fetchImpl, timeoutMs, referer: site.href });
|
|
639
|
+
pageHtml.set(p.name, got.text);
|
|
640
|
+
say(`抓栏目页 ${p.path} → pages/${p.name}.html(${got.bytes}B)`);
|
|
641
|
+
} catch (e) {
|
|
642
|
+
say(` ! 栏目页抓取失败(跳过该页):${p.path} — ${(e && e.message) || e}`);
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
// ---- 3) 资源本地化(首页 + 各页出现过的全部资源) ----
|
|
647
|
+
const assetsDir = path.join(outDir, 'assets');
|
|
648
|
+
await fsp.mkdir(assetsDir, { recursive: true });
|
|
649
|
+
const allRefs = new Map();
|
|
650
|
+
for (const [, html] of pageHtml) {
|
|
651
|
+
for (const r of collectResourceRefs(html, site.href)) allRefs.set(r.raw, r);
|
|
652
|
+
}
|
|
653
|
+
const localized = await localizeAssets([...allRefs.values()].map((r) => ({ value: r.raw })), {
|
|
654
|
+
assetsDir,
|
|
655
|
+
fetchImpl,
|
|
656
|
+
baseUrl: site.href,
|
|
657
|
+
referer: site.href,
|
|
658
|
+
timeoutMs,
|
|
659
|
+
log: say,
|
|
660
|
+
});
|
|
661
|
+
if (localized.files.length) say(`本地化资源 ${localized.files.length} 个(失败 ${localized.errors.length})`);
|
|
662
|
+
|
|
663
|
+
// ---- 4) 链接映射:站内路径 → 页面文件 ----
|
|
664
|
+
const linkMap = new Map();
|
|
665
|
+
const unmapped = [];
|
|
666
|
+
const localPageOf = (pathname, hash) => {
|
|
667
|
+
const p = pathname.replace(/\/?$/, '/');
|
|
668
|
+
if (byPath.has(p)) return `${byPath.get(p)}.html${hash ? `#${hash}` : ''}`;
|
|
669
|
+
// 就近落:找已抓页面的前缀(/services/kaiban/x/ → services.html)
|
|
670
|
+
for (const [pp, name] of byPath) {
|
|
671
|
+
if (pp !== '/' && p.startsWith(pp)) return `${name}.html${hash ? `#${hash}` : ''}`;
|
|
672
|
+
}
|
|
673
|
+
unmapped.push(pathname);
|
|
674
|
+
return `index.html${hash ? `#${hash}` : ''}`;
|
|
675
|
+
};
|
|
676
|
+
for (const [, html] of pageHtml) {
|
|
677
|
+
for (const m of String(html).matchAll(/<a\b[^>]*>/gi)) {
|
|
678
|
+
const href = getAttr(m[0], 'href');
|
|
679
|
+
if (!href || linkMap.has(href)) continue;
|
|
680
|
+
const s = splitHref(href, site.href);
|
|
681
|
+
if (!s.isHttp) continue;
|
|
682
|
+
if (s.origin !== site.origin) continue;
|
|
683
|
+
linkMap.set(href, localPageOf(s.path, s.hash));
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
// ---- 5) 写 pages/ ----
|
|
688
|
+
const pagesDir = path.join(outDir, 'pages');
|
|
689
|
+
await fsp.mkdir(pagesDir, { recursive: true });
|
|
690
|
+
const pagesOut = [];
|
|
691
|
+
for (const [name, html] of pageHtml) {
|
|
692
|
+
const rewritten = applyRewrites(html, { linkMap, assetMap: localized.rewrite });
|
|
693
|
+
const file = path.join(pagesDir, `${name}.html`);
|
|
694
|
+
await fsp.writeFile(file, rewritten, 'utf8');
|
|
695
|
+
pagesOut.push({ name, file, bytes: Buffer.byteLength(rewritten), source: name === 'index' ? site.href : new URL(`/${name}`, site.origin).href });
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
// ---- 6) partials/(首页 header / footer 原文,便于人改) ----
|
|
699
|
+
const partialsDir = path.join(outDir, 'partials');
|
|
700
|
+
await fsp.mkdir(partialsDir, { recursive: true });
|
|
701
|
+
const partHeader = sliceElement(home.text, 'header');
|
|
702
|
+
const partNav = sliceElement(home.text, 'nav');
|
|
703
|
+
const partFooter = sliceElement(home.text, 'footer');
|
|
704
|
+
if (partHeader) await fsp.writeFile(path.join(partialsDir, 'header.html'), applyRewrites(partHeader, { linkMap, assetMap: localized.rewrite }), 'utf8');
|
|
705
|
+
if (partNav) await fsp.writeFile(path.join(partialsDir, 'nav.html'), applyRewrites(partNav, { linkMap, assetMap: localized.rewrite }), 'utf8');
|
|
706
|
+
if (partFooter) await fsp.writeFile(path.join(partialsDir, 'footer.html'), applyRewrites(partFooter, { linkMap, assetMap: localized.rewrite }), 'utf8');
|
|
707
|
+
|
|
708
|
+
// ---- 7) 栏目 / 菜单 / 演示数据 ----
|
|
709
|
+
const columns = [];
|
|
710
|
+
const menusTop = [];
|
|
711
|
+
const usedKeys = new Set();
|
|
712
|
+
const addColumn = (spec) => {
|
|
713
|
+
// key 由栏目名派生(中文名 slugify 为空 → 退到页面名),同名再 -2 去重
|
|
714
|
+
let base = spec.key || slugify(spec.name) || slugify(spec.page) || "column";
|
|
715
|
+
if (!/^[a-z0-9][a-z0-9-]*$/.test(base)) base = slugify(spec.page) || "column";
|
|
716
|
+
let k = base;
|
|
717
|
+
let i = 2;
|
|
718
|
+
while (usedKeys.has(k)) k = `${base}-${i++}`;
|
|
719
|
+
usedKeys.add(k);
|
|
720
|
+
columns.push({ ...spec, key: k, slug: k, sort: columns.length + 1 });
|
|
721
|
+
return k;
|
|
722
|
+
};
|
|
723
|
+
for (const c of topCandidates) {
|
|
724
|
+
const pageName = c.hash ? 'index' : byPath.get(c.path.replace(/\/?$/, '/')) || pageNameFor(c.path);
|
|
725
|
+
const pageHead = analyzeHead(pageHtml.get(pageName) || home.text, c.label);
|
|
726
|
+
const fields = defaultFields();
|
|
727
|
+
const items = alignItems(extractItems(pageHtml.get(pageName) || '', { max: 12 }), fields);
|
|
728
|
+
const k = addColumn({
|
|
729
|
+
name: c.label,
|
|
730
|
+
page: pageName,
|
|
731
|
+
in_menu: true,
|
|
732
|
+
menu_type: c.hash ? 'page' : 'page',
|
|
733
|
+
link_type: c.hash ? 'anchor' : 'internal',
|
|
734
|
+
anchor: c.hash || '',
|
|
735
|
+
parent: '',
|
|
736
|
+
seo: seoOf({ title: pageHead.title, description: pageHead.description, keywords: pageHead.keywords }),
|
|
737
|
+
fields,
|
|
738
|
+
items,
|
|
739
|
+
});
|
|
740
|
+
menusTop.push({
|
|
741
|
+
key: k,
|
|
742
|
+
label: c.label,
|
|
743
|
+
href: c.hash ? `/${c.path.replace(/^\//, '')}#${c.hash}` : `/${c.path.replace(/^\//, '')}`,
|
|
744
|
+
menu_type: 'page',
|
|
745
|
+
link_type: c.hash ? 'anchor' : 'internal',
|
|
746
|
+
anchor: c.hash || '',
|
|
747
|
+
target: '_self',
|
|
748
|
+
children: [],
|
|
749
|
+
});
|
|
750
|
+
}
|
|
751
|
+
// 导航里没写但抓到的页面也补成栏目(in_menu=false),保证每个页面都能在后台编辑
|
|
752
|
+
for (const p of pagesWanted) {
|
|
753
|
+
if (p.name === 'index') continue;
|
|
754
|
+
if (columns.some((c) => c.page === p.name)) continue;
|
|
755
|
+
const pageHead = analyzeHead(pageHtml.get(p.name) || '', p.name);
|
|
756
|
+
const fields = defaultFields();
|
|
757
|
+
addColumn({
|
|
758
|
+
name: pageHead.title ? pageHead.title.split(/[-_|·]/)[0].trim() : p.name,
|
|
759
|
+
page: p.name,
|
|
760
|
+
in_menu: false,
|
|
761
|
+
menu_type: 'page',
|
|
762
|
+
link_type: 'internal',
|
|
763
|
+
anchor: '',
|
|
764
|
+
parent: '',
|
|
765
|
+
seo: seoOf({ title: pageHead.title, description: pageHead.description, keywords: pageHead.keywords }),
|
|
766
|
+
fields,
|
|
767
|
+
items: alignItems(extractItems(pageHtml.get(p.name) || ''), fields),
|
|
768
|
+
});
|
|
769
|
+
}
|
|
770
|
+
if (!columns.length) {
|
|
771
|
+
const fields = defaultFields();
|
|
772
|
+
addColumn({
|
|
773
|
+
name: head.title || '首页',
|
|
774
|
+
page: 'index',
|
|
775
|
+
in_menu: true,
|
|
776
|
+
menu_type: 'page',
|
|
777
|
+
link_type: 'internal',
|
|
778
|
+
anchor: '',
|
|
779
|
+
parent: '',
|
|
780
|
+
seo: seoOf(head),
|
|
781
|
+
fields,
|
|
782
|
+
items: alignItems(extractItems(home.text), fields),
|
|
783
|
+
});
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
const footerLinks = extractLinks(partFooter || '', site.href)
|
|
787
|
+
.filter((l) => l.label && !l.special && l.label.length <= 24)
|
|
788
|
+
.slice(0, 12)
|
|
789
|
+
.map((l, i) => {
|
|
790
|
+
const s = splitHref(l.href, site.href);
|
|
791
|
+
const internal = s.isHttp && s.origin === site.origin;
|
|
792
|
+
return {
|
|
793
|
+
key: `footer-${i + 1}`,
|
|
794
|
+
label: l.label,
|
|
795
|
+
href: internal ? localPageOf(s.path, s.hash).replace(/^/, '/') : l.href,
|
|
796
|
+
menu_type: internal ? 'page' : 'custom',
|
|
797
|
+
link_type: internal ? (s.hash ? 'anchor' : 'internal') : 'external',
|
|
798
|
+
anchor: internal ? s.hash : '',
|
|
799
|
+
target: internal ? '_self' : '_blank',
|
|
800
|
+
children: [],
|
|
801
|
+
};
|
|
802
|
+
});
|
|
803
|
+
|
|
804
|
+
const demo = columns.some((c) => c.items.length > 0);
|
|
805
|
+
const template = {
|
|
806
|
+
_format: TEMPLATE_FORMAT,
|
|
807
|
+
key,
|
|
808
|
+
name: head.title ? toTemplateName(head.title, site.hostname) : site.hostname,
|
|
809
|
+
summary: head.description || `${site.hostname} 镜像生成(${columns.length} 个栏目 / ${pagesOut.length} 个页面)`,
|
|
810
|
+
version: '0.0.1',
|
|
811
|
+
source: { kind: 'url', url: site.href, capturedAt: new Date().toISOString(), pages: pagesOut.length },
|
|
812
|
+
demo,
|
|
813
|
+
site: {
|
|
814
|
+
title: head.title || site.hostname,
|
|
815
|
+
logo: 'assets/img/placeholder.svg',
|
|
816
|
+
theme: { primary: head.primary, font: head.font },
|
|
817
|
+
seo: seoOf(head),
|
|
818
|
+
},
|
|
819
|
+
menus: { top: menusTop, footer: footerLinks },
|
|
820
|
+
columns,
|
|
821
|
+
requires: [],
|
|
822
|
+
data: {},
|
|
823
|
+
files: pagesOut.map((p) => `pages/${p.name}.html`).concat(['partials/header.html', 'partials/nav.html', 'partials/footer.html', 'assets/img/placeholder.svg']),
|
|
824
|
+
};
|
|
825
|
+
|
|
826
|
+
// ---- 8) assets/base + 占位图 + template.json ----
|
|
827
|
+
const baseDir = resolveBaseDir(null, {});
|
|
828
|
+
const baseCopy = await syncBaseAssets(outDir, baseDir);
|
|
829
|
+
await ensurePlaceholder(outDir);
|
|
830
|
+
await fsp.writeFile(path.join(outDir, 'template.json'), JSON.stringify(template, null, 2) + '\n', 'utf8');
|
|
831
|
+
|
|
832
|
+
// ---- 9) 载图(design/) ----
|
|
833
|
+
let design = null;
|
|
834
|
+
if (capture) {
|
|
835
|
+
design = await captureDesign({
|
|
836
|
+
url: site.href,
|
|
837
|
+
dir: designDir || path.join(outDir, 'design'),
|
|
838
|
+
viewports: DESIGN_VIEWPORTS,
|
|
839
|
+
frames: true,
|
|
840
|
+
chromeExe,
|
|
841
|
+
// 清单从**生成出来的模板**源码抽(不是原站);几何证据不写 —— 这里拍的是原站,
|
|
842
|
+
// 元素坐标属于原站,对账要的是模板渲染页上的坐标(verify 会自己在渲染页重量一次)。
|
|
843
|
+
templateDir: outDir,
|
|
844
|
+
writeGeom: false,
|
|
845
|
+
onProgress: say,
|
|
846
|
+
});
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
return {
|
|
850
|
+
dir: outDir,
|
|
851
|
+
key,
|
|
852
|
+
template,
|
|
853
|
+
design,
|
|
854
|
+
baseCopy,
|
|
855
|
+
stats: {
|
|
856
|
+
columns: columns.length,
|
|
857
|
+
menuTop: menusTop.length,
|
|
858
|
+
pages: pagesOut.length,
|
|
859
|
+
items: columns.reduce((n, c) => n + c.items.length, 0),
|
|
860
|
+
assets: localized.files.length,
|
|
861
|
+
assetErrors: localized.errors.length,
|
|
862
|
+
unmappedLinks: [...new Set(unmapped)].length,
|
|
863
|
+
partials: [partHeader, partNav, partFooter].filter(Boolean).length,
|
|
864
|
+
},
|
|
865
|
+
pages: pagesOut,
|
|
866
|
+
};
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
function toTemplateName(title, host) {
|
|
870
|
+
const cleaned = String(title || '')
|
|
871
|
+
.split(/[-_|·]/)[0]
|
|
872
|
+
.trim();
|
|
873
|
+
return (cleaned || host).slice(0, 40);
|
|
874
|
+
}
|
|
875
|
+
|
|
876
|
+
// ---------------------------------------------------------------- from-dir
|
|
877
|
+
|
|
878
|
+
const WALK_SKIP = new Set(['node_modules', '.git', 'design', 'pages', 'partials']);
|
|
879
|
+
|
|
880
|
+
async function walkHtml(root, { max = 40 } = {}) {
|
|
881
|
+
const out = [];
|
|
882
|
+
const walk = async (dir, depth) => {
|
|
883
|
+
if (depth > 4 || out.length >= max) return;
|
|
884
|
+
const entries = await fsp.readdir(dir, { withFileTypes: true }).catch(() => []);
|
|
885
|
+
for (const e of entries) {
|
|
886
|
+
if (out.length >= max) return;
|
|
887
|
+
if (e.name.startsWith('.') || WALK_SKIP.has(e.name)) continue;
|
|
888
|
+
const full = path.join(dir, e.name);
|
|
889
|
+
if (e.isDirectory()) await walk(full, depth + 1);
|
|
890
|
+
else if (/\.html?$/i.test(e.name)) out.push(full);
|
|
891
|
+
}
|
|
892
|
+
};
|
|
893
|
+
await walk(root, 0);
|
|
894
|
+
return out;
|
|
895
|
+
}
|
|
896
|
+
|
|
897
|
+
/**
|
|
898
|
+
* 本地静态站 → 模板(同一套 v2 口径)。
|
|
899
|
+
* 会把被引用的本地资源复制进 `assets/`,并把 pages/ 里的引用改成 `../assets/...`。
|
|
900
|
+
*/
|
|
901
|
+
export async function generateFromDir({ srcDir, outDir, key, capture = true, designDir = null, chromeExe, logLine = null }) {
|
|
902
|
+
const src = path.resolve(srcDir);
|
|
903
|
+
const say = (s) => logLine && logLine(s);
|
|
904
|
+
const st = await fsp.stat(src).catch(() => null);
|
|
905
|
+
if (!st || !st.isDirectory()) throw new UsageError(`--from-dir 不是目录:${src}`);
|
|
906
|
+
|
|
907
|
+
const htmlFiles = await walkHtml(src);
|
|
908
|
+
if (!htmlFiles.length) throw new UsageError(`--from-dir 目录里没有 .html 文件:${src}`);
|
|
909
|
+
say(`扫到 ${htmlFiles.length} 个 HTML 页面`);
|
|
910
|
+
|
|
911
|
+
const assetsDir = path.join(outDir, 'assets');
|
|
912
|
+
const pagesDir = path.join(outDir, 'pages');
|
|
913
|
+
const partialsDir = path.join(outDir, 'partials');
|
|
914
|
+
await fsp.mkdir(assetsDir, { recursive: true });
|
|
915
|
+
await fsp.mkdir(pagesDir, { recursive: true });
|
|
916
|
+
await fsp.mkdir(partialsDir, { recursive: true });
|
|
917
|
+
|
|
918
|
+
const pageByFile = new Map();
|
|
919
|
+
const usedNames = new Set();
|
|
920
|
+
for (const file of htmlFiles) {
|
|
921
|
+
const rel = path.relative(src, file).replace(/\\/g, '/');
|
|
922
|
+
let name = /(^|\/)index\.html?$/i.test(rel) && rel.split('/').length === 1 ? 'index' : pageNameFor(rel.replace(/\.html?$/i, ''));
|
|
923
|
+
if (usedNames.has(name)) name = `${name}-${shortHash(rel, 4)}`;
|
|
924
|
+
usedNames.add(name);
|
|
925
|
+
pageByFile.set(file, name);
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
const assetMap = new Map();
|
|
929
|
+
const copied = [];
|
|
930
|
+
const copyAsset = async (absFile, hintRel) => {
|
|
931
|
+
if (!fs.existsSync(absFile) || !fs.statSync(absFile).isFile()) return null;
|
|
932
|
+
if (assetMap.has(absFile)) return assetMap.get(absFile);
|
|
933
|
+
const relFromRoot = path.relative(src, absFile).replace(/\\/g, '/');
|
|
934
|
+
let rel = relFromRoot;
|
|
935
|
+
if (!/^assets\//.test(rel)) rel = `assets/${slugify(path.basename(absFile, path.extname(absFile)), 'asset')}${path.extname(absFile)}`;
|
|
936
|
+
let target = path.join(outDir, rel);
|
|
937
|
+
if (fs.existsSync(target) && path.resolve(target) !== path.resolve(absFile)) {
|
|
938
|
+
const p = path.parse(rel);
|
|
939
|
+
rel = path.join(p.dir, `${p.name}-${shortHash(absFile, 4)}${p.ext}`).replace(/\\/g, '/');
|
|
940
|
+
target = path.join(outDir, rel);
|
|
941
|
+
}
|
|
942
|
+
await fsp.mkdir(path.dirname(target), { recursive: true });
|
|
943
|
+
await fsp.copyFile(absFile, target);
|
|
944
|
+
copied.push({ from: absFile, to: rel, bytes: fs.statSync(target).size });
|
|
945
|
+
const local = `../${rel}`;
|
|
946
|
+
assetMap.set(absFile, local);
|
|
947
|
+
return local;
|
|
948
|
+
};
|
|
949
|
+
|
|
950
|
+
const pagesOut = [];
|
|
951
|
+
const homeHtml = fs.readFileSync(htmlFiles.find((f) => pageByFile.get(f) === 'index') || htmlFiles[0], 'utf8');
|
|
952
|
+
const homeFile = htmlFiles.find((f) => pageByFile.get(f) === 'index') || htmlFiles[0];
|
|
953
|
+
const head = analyzeHead(homeHtml, path.basename(src));
|
|
954
|
+
|
|
955
|
+
for (const file of htmlFiles) {
|
|
956
|
+
const name = pageByFile.get(file);
|
|
957
|
+
const dir = path.dirname(file);
|
|
958
|
+
const html = fs.readFileSync(file, 'utf8');
|
|
959
|
+
// 收集本地资源引用(相对路径、非 data:)
|
|
960
|
+
const refs = new Map();
|
|
961
|
+
for (const m of html.matchAll(/<(?:link|script|img|source|video|audio|iframe)\b[^>]*>/gi)) {
|
|
962
|
+
const tag = m[0];
|
|
963
|
+
const n = (tag.match(/^<([a-z]+)/i) || [])[1].toLowerCase();
|
|
964
|
+
const vals = [];
|
|
965
|
+
if (n === 'link') vals.push(getAttr(tag, 'href'));
|
|
966
|
+
if (n === 'script') vals.push(getAttr(tag, 'src'));
|
|
967
|
+
if (['img', 'source', 'video', 'audio', 'iframe'].includes(n)) {
|
|
968
|
+
vals.push(getAttr(tag, 'src'), getAttr(tag, 'poster'));
|
|
969
|
+
const ss = getAttr(tag, 'srcset');
|
|
970
|
+
if (ss) for (const part of ss.split(',')) vals.push(part.trim().split(/\s+/)[0]);
|
|
971
|
+
}
|
|
972
|
+
for (const v of vals) if (v) refs.set(v, (refs.get(v) || 0) + 1);
|
|
973
|
+
}
|
|
974
|
+
const rewrittenRefs = new Map();
|
|
975
|
+
for (const raw of refs.keys()) {
|
|
976
|
+
if (!raw || /^(data:|#|mailto:|tel:|javascript:|https?:)?/i.test(raw) === false) continue;
|
|
977
|
+
if (/^(data:|#|mailto:|tel:|javascript:|https?:\/\/)/i.test(raw)) continue;
|
|
978
|
+
const abs = path.resolve(dir, raw.split(/[?#]/)[0]);
|
|
979
|
+
const local = await copyAsset(abs, raw);
|
|
980
|
+
if (local) rewrittenRefs.set(raw, local);
|
|
981
|
+
}
|
|
982
|
+
const mapAttr = (v) => rewrittenRefs.get(v) || null;
|
|
983
|
+
let out = html;
|
|
984
|
+
for (const attrName of ['href', 'src', 'poster']) out = replaceAttr(out, attrName, mapAttr);
|
|
985
|
+
out = out.replace(/(\ssrcset\s*=\s*)(["'])([^"']*)\2/gi, (m, pre, q, val) => {
|
|
986
|
+
const parts = val.split(',').map((p) => {
|
|
987
|
+
const [u, ...rest] = p.trim().split(/\s+/);
|
|
988
|
+
return [rewrittenRefs.get(u) || u, ...rest].join(' ');
|
|
989
|
+
});
|
|
990
|
+
return `${pre}${q}${parts.join(', ')}${q}`;
|
|
991
|
+
});
|
|
992
|
+
// style 里的 url()
|
|
993
|
+
const subUrl = (css) =>
|
|
994
|
+
css.replace(/url\(\s*(['"]?)([^'")]+)\1\s*\)/gi, (m, q, v) => {
|
|
995
|
+
const to = rewrittenRefs.get(v);
|
|
996
|
+
return to ? `url(${to.replace(/^\.\.\//, '')})` : m;
|
|
997
|
+
});
|
|
998
|
+
out = out.replace(/(<style\b[^>]*>)([\s\S]*?)(<\/style>)/gi, (m, a, body, b) => a + subUrl(body) + b);
|
|
999
|
+
out = out.replace(/(\sstyle\s*=\s*)(["'])([\s\S]*?)\2/gi, (m, pre, q, body) => `${pre}${q}${subUrl(body)}${q}`);
|
|
1000
|
+
|
|
1001
|
+
const target = path.join(pagesDir, `${name}.html`);
|
|
1002
|
+
await fsp.writeFile(target, out, 'utf8');
|
|
1003
|
+
pagesOut.push({ name, file: target, bytes: Buffer.byteLength(out), source: file });
|
|
1004
|
+
}
|
|
1005
|
+
|
|
1006
|
+
// 页面间链接重写(a.html → a.html 不变;index 引用保持相对,都在 pages/ 下同名即可)
|
|
1007
|
+
say(`写出 ${pagesOut.length} 个页面,复制资源 ${copied.length} 个`);
|
|
1008
|
+
|
|
1009
|
+
// partials
|
|
1010
|
+
const pH = sliceElement(homeHtml, 'header');
|
|
1011
|
+
const pN = sliceElement(homeHtml, 'nav');
|
|
1012
|
+
const pF = sliceElement(homeHtml, 'footer');
|
|
1013
|
+
if (pH) await fsp.writeFile(path.join(partialsDir, 'header.html'), pH, 'utf8');
|
|
1014
|
+
if (pN) await fsp.writeFile(path.join(partialsDir, 'nav.html'), pN, 'utf8');
|
|
1015
|
+
if (pF) await fsp.writeFile(path.join(partialsDir, 'footer.html'), pF, 'utf8');
|
|
1016
|
+
|
|
1017
|
+
// 栏目:优先 nav 链接,其次页面清单
|
|
1018
|
+
const navLinks = extractLinks(pN || pH || '', `http://local.invalid/`).filter((l) => !l.special);
|
|
1019
|
+
const columns = [];
|
|
1020
|
+
const menusTop = [];
|
|
1021
|
+
const usedKeys = new Set();
|
|
1022
|
+
const addColumn = (spec) => {
|
|
1023
|
+
// key 由栏目名派生(中文名 slugify 为空 → 退到页面名),同名再 -2 去重
|
|
1024
|
+
let base = spec.key || slugify(spec.name) || slugify(spec.page) || "column";
|
|
1025
|
+
if (!/^[a-z0-9][a-z0-9-]*$/.test(base)) base = slugify(spec.page) || "column";
|
|
1026
|
+
let k = base;
|
|
1027
|
+
let i = 2;
|
|
1028
|
+
while (usedKeys.has(k)) k = `${base}-${i++}`;
|
|
1029
|
+
usedKeys.add(k);
|
|
1030
|
+
columns.push({ ...spec, key: k, slug: k, sort: columns.length + 1 });
|
|
1031
|
+
return k;
|
|
1032
|
+
};
|
|
1033
|
+
const pageNameSet = new Set(pagesOut.map((p) => p.name));
|
|
1034
|
+
const pageNameOfRel = (href) => {
|
|
1035
|
+
const clean = String(href || '').split(/[?#]/)[0].replace(/^\.?\//, '').replace(/\.html?$/i, '');
|
|
1036
|
+
const cand = pageNameFor(clean);
|
|
1037
|
+
if (pageNameSet.has(cand)) return cand;
|
|
1038
|
+
if (!clean || clean === '.') return 'index';
|
|
1039
|
+
if (pageNameSet.has('index') && /^index$/i.test(clean)) return 'index';
|
|
1040
|
+
return null;
|
|
1041
|
+
};
|
|
1042
|
+
for (const l of navLinks) {
|
|
1043
|
+
const page = pageNameOfRel(l.href);
|
|
1044
|
+
if (!page) continue;
|
|
1045
|
+
const html = fs.readFileSync(pagesOut.find((p) => p.name === page).file, 'utf8');
|
|
1046
|
+
const ph = analyzeHead(html, l.label);
|
|
1047
|
+
const fields = defaultFields();
|
|
1048
|
+
const hash = (String(l.href).split('#')[1] || '').trim();
|
|
1049
|
+
const k = addColumn({
|
|
1050
|
+
name: l.label,
|
|
1051
|
+
page,
|
|
1052
|
+
in_menu: true,
|
|
1053
|
+
menu_type: 'page',
|
|
1054
|
+
link_type: hash ? 'anchor' : 'internal',
|
|
1055
|
+
anchor: hash,
|
|
1056
|
+
parent: '',
|
|
1057
|
+
seo: seoOf(ph),
|
|
1058
|
+
fields,
|
|
1059
|
+
items: alignItems(extractItems(html), fields),
|
|
1060
|
+
});
|
|
1061
|
+
menusTop.push({
|
|
1062
|
+
key: k,
|
|
1063
|
+
label: l.label,
|
|
1064
|
+
href: hash ? `/${page}#${hash}` : `/${page}`,
|
|
1065
|
+
menu_type: 'page',
|
|
1066
|
+
link_type: hash ? 'anchor' : 'internal',
|
|
1067
|
+
anchor: hash,
|
|
1068
|
+
target: '_self',
|
|
1069
|
+
children: [],
|
|
1070
|
+
});
|
|
1071
|
+
}
|
|
1072
|
+
for (const p of pagesOut) {
|
|
1073
|
+
if (columns.some((c) => c.page === p.name)) continue;
|
|
1074
|
+
const h = analyzeHead(fs.readFileSync(p.file, 'utf8'), p.name);
|
|
1075
|
+
const fields = defaultFields();
|
|
1076
|
+
addColumn({
|
|
1077
|
+
name: (h.title || p.name).split(/[-_|·]/)[0].trim() || p.name,
|
|
1078
|
+
page: p.name,
|
|
1079
|
+
in_menu: p.name === 'index',
|
|
1080
|
+
menu_type: 'page',
|
|
1081
|
+
link_type: 'internal',
|
|
1082
|
+
anchor: '',
|
|
1083
|
+
parent: '',
|
|
1084
|
+
seo: seoOf(h),
|
|
1085
|
+
fields,
|
|
1086
|
+
items: alignItems(extractItems(fs.readFileSync(p.file, 'utf8')), fields),
|
|
1087
|
+
});
|
|
1088
|
+
}
|
|
1089
|
+
if (!columns.length) {
|
|
1090
|
+
const fields = defaultFields();
|
|
1091
|
+
addColumn({
|
|
1092
|
+
name: head.title || '首页',
|
|
1093
|
+
page: pagesOut[0].name,
|
|
1094
|
+
in_menu: true,
|
|
1095
|
+
menu_type: 'page',
|
|
1096
|
+
link_type: 'internal',
|
|
1097
|
+
anchor: '',
|
|
1098
|
+
parent: '',
|
|
1099
|
+
seo: seoOf(head),
|
|
1100
|
+
fields,
|
|
1101
|
+
items: alignItems(extractItems(homeHtml), fields),
|
|
1102
|
+
});
|
|
1103
|
+
}
|
|
1104
|
+
|
|
1105
|
+
const footerLinks = extractLinks(pF || '', 'http://local.invalid/')
|
|
1106
|
+
.filter((l) => l.label && !l.special && l.label.length <= 24)
|
|
1107
|
+
.slice(0, 12)
|
|
1108
|
+
.map((l, i) => {
|
|
1109
|
+
const page = pageNameOfRel(l.href);
|
|
1110
|
+
return {
|
|
1111
|
+
key: `footer-${i + 1}`,
|
|
1112
|
+
label: l.label,
|
|
1113
|
+
href: page ? `/${page}` : l.href,
|
|
1114
|
+
menu_type: page ? 'page' : 'custom',
|
|
1115
|
+
link_type: page ? 'internal' : 'external',
|
|
1116
|
+
anchor: '',
|
|
1117
|
+
target: page ? '_self' : '_blank',
|
|
1118
|
+
children: [],
|
|
1119
|
+
};
|
|
1120
|
+
});
|
|
1121
|
+
|
|
1122
|
+
const template = {
|
|
1123
|
+
_format: TEMPLATE_FORMAT,
|
|
1124
|
+
key,
|
|
1125
|
+
name: head.title ? toTemplateName(head.title, path.basename(src)) : path.basename(src),
|
|
1126
|
+
summary: head.description || `本地静态站反推(${columns.length} 个栏目 / ${pagesOut.length} 个页面)`,
|
|
1127
|
+
version: '0.0.1',
|
|
1128
|
+
source: { kind: 'dir', dir: src, capturedAt: new Date().toISOString(), pages: pagesOut.length },
|
|
1129
|
+
demo: columns.some((c) => c.items.length > 0),
|
|
1130
|
+
site: {
|
|
1131
|
+
title: head.title || path.basename(src),
|
|
1132
|
+
logo: 'assets/img/placeholder.svg',
|
|
1133
|
+
theme: { primary: head.primary, font: head.font },
|
|
1134
|
+
seo: seoOf(head),
|
|
1135
|
+
},
|
|
1136
|
+
menus: { top: menusTop, footer: footerLinks },
|
|
1137
|
+
columns,
|
|
1138
|
+
requires: [],
|
|
1139
|
+
data: {},
|
|
1140
|
+
files: pagesOut.map((p) => `pages/${p.name}.html`).concat(['partials/header.html', 'partials/nav.html', 'partials/footer.html', 'assets/img/placeholder.svg']),
|
|
1141
|
+
};
|
|
1142
|
+
|
|
1143
|
+
const baseDir = resolveBaseDir(null, {});
|
|
1144
|
+
const baseCopy = await syncBaseAssets(outDir, baseDir);
|
|
1145
|
+
await ensurePlaceholder(outDir);
|
|
1146
|
+
await fsp.writeFile(path.join(outDir, 'template.json'), JSON.stringify(template, null, 2) + '\n', 'utf8');
|
|
1147
|
+
|
|
1148
|
+
// 载图:本地起静态服务,对 pages/index.html 截图(与 from-url 同一套 design 命名)
|
|
1149
|
+
let design = null;
|
|
1150
|
+
if (capture) {
|
|
1151
|
+
const { startStaticServer } = await import('./template-capture.mjs');
|
|
1152
|
+
const server = await startStaticServer(outDir);
|
|
1153
|
+
try {
|
|
1154
|
+
design = await captureDesign({
|
|
1155
|
+
url: `${server.url}pages/index.html`,
|
|
1156
|
+
dir: designDir || path.join(outDir, 'design'),
|
|
1157
|
+
viewports: DESIGN_VIEWPORTS,
|
|
1158
|
+
frames: true,
|
|
1159
|
+
chromeExe,
|
|
1160
|
+
// 这里拍的就是模板自己(临时静态服务指到 outDir),所以清单 + 几何证据都能落到 design/
|
|
1161
|
+
templateDir: outDir,
|
|
1162
|
+
page: '/pages/index.html',
|
|
1163
|
+
onProgress: say,
|
|
1164
|
+
});
|
|
1165
|
+
} finally {
|
|
1166
|
+
await server.close();
|
|
1167
|
+
}
|
|
1168
|
+
}
|
|
1169
|
+
|
|
1170
|
+
return {
|
|
1171
|
+
dir: outDir,
|
|
1172
|
+
key,
|
|
1173
|
+
template,
|
|
1174
|
+
design,
|
|
1175
|
+
baseCopy,
|
|
1176
|
+
stats: {
|
|
1177
|
+
columns: columns.length,
|
|
1178
|
+
menuTop: menusTop.length,
|
|
1179
|
+
pages: pagesOut.length,
|
|
1180
|
+
items: columns.reduce((n, c) => n + c.items.length, 0),
|
|
1181
|
+
assets: copied.length,
|
|
1182
|
+
partials: [pH, pN, pF].filter(Boolean).length,
|
|
1183
|
+
},
|
|
1184
|
+
pages: pagesOut,
|
|
1185
|
+
};
|
|
1186
|
+
}
|
|
1187
|
+
|
|
1188
|
+
/** 生成前把输出目录准备好:存在且非空 → 用法错误(exit 2),不覆盖别人的东西 */
|
|
1189
|
+
export async function prepareOutDir(outDir, { force = false } = {}) {
|
|
1190
|
+
const abs = path.resolve(outDir);
|
|
1191
|
+
const st = await fsp.stat(abs).catch(() => null);
|
|
1192
|
+
if (st) {
|
|
1193
|
+
if (!st.isDirectory()) throw new UsageError(`--out 已存在且不是目录:${abs}`);
|
|
1194
|
+
const entries = await fsp.readdir(abs);
|
|
1195
|
+
if (entries.length && !force) {
|
|
1196
|
+
throw new UsageError(`--out 目录已存在且非空:${abs}\n (要覆盖请加 --force,或换一个 --out)`);
|
|
1197
|
+
}
|
|
1198
|
+
if (entries.length && force) await fsp.rm(abs, { recursive: true, force: true });
|
|
1199
|
+
}
|
|
1200
|
+
await fsp.mkdir(abs, { recursive: true });
|
|
1201
|
+
return abs;
|
|
1202
|
+
}
|
|
1203
|
+
|
|
1204
|
+
export { inspectDesign };
|