mirror of
https://github.com/aculix/huashu-design.git
synced 2026-10-27 07:15:54 +00:00
幻灯片交付升级为 HTML-first:每页独立 HTML + index.html 聚合永远是 默认基础产物,PDF/PPTX 是从 HTML 一行命令导出的衍生物。 核心变化: - export_deck_pptx.mjs 删掉 image 模式(168 行→85 行),只保留 editable 路径 - editable-pptx.md 末尾新增 Fallback 章节:已有视觉稿又必须出 editable PPTX 时,AI 以视觉稿为蓝本重写合规 HTML(不是硬转截图) - slide-decks.md 新增 3 节: · 批量制作前先做 2 页 grammar showcase 的流程规则(≥5 页 deck 强制) · 出版物 grammar 模板(masthead + kicker + h1 + italic 副 + footer,moxt 实测) · 常见踩坑(emoji 不渲染 / playwright 路径 / 字体加载 / 信息密度) - SKILL.md 同步更新 checkpoint 和 Starter Components 表格 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
108 lines
3.7 KiB
JavaScript
108 lines
3.7 KiB
JavaScript
#!/usr/bin/env node
|
||
/**
|
||
* export_deck_pptx.mjs — 把多文件 slide deck 导出为可编辑 PPTX
|
||
*
|
||
* 用法:
|
||
* node export_deck_pptx.mjs --slides <dir> --out <file.pptx>
|
||
*
|
||
* 行为:
|
||
* - 调用 scripts/html2pptx.js 把 HTML DOM 逐元素翻译成 PowerPoint 原生对象
|
||
* - 文字是真文本框,PPT 里直接双击能编辑
|
||
* - body 尺寸 960pt × 540pt(LAYOUT_WIDE,13.333″ × 7.5″)
|
||
*
|
||
* ⚠️ HTML 必须符合 4 条硬约束(见 references/editable-pptx.md):
|
||
* 1. 文字包在 <p>/<h1>-<h6> 里(div 不能直接放文字)
|
||
* 2. 不用 CSS 渐变
|
||
* 3. <p>/<h*> 不能有 background/border/shadow(放外层 div)
|
||
* 4. div 不能 background-image(用 <img>)
|
||
*
|
||
* 视觉驱动的 HTML 几乎无法 pass —— 必须从写 HTML 的第一行就按约束写。
|
||
* 视觉自由度优先的场景(动画、web component、CSS 渐变、复杂 SVG)
|
||
* 应改用 export_deck_pdf.mjs / export_deck_stage_pdf.mjs 导出 PDF。
|
||
*
|
||
* 依赖:npm install playwright pptxgenjs sharp
|
||
*
|
||
* 按文件名排序(01-xxx.html → 02-xxx.html → ...)。
|
||
*/
|
||
|
||
import pptxgen from 'pptxgenjs';
|
||
import fs from 'fs/promises';
|
||
import path from 'path';
|
||
import { fileURLToPath } from 'url';
|
||
|
||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||
|
||
function parseArgs() {
|
||
const args = {};
|
||
const a = process.argv.slice(2);
|
||
for (let i = 0; i < a.length; i += 2) {
|
||
const k = a[i].replace(/^--/, '');
|
||
args[k] = a[i + 1];
|
||
}
|
||
if (!args.slides || !args.out) {
|
||
console.error('用法: node export_deck_pptx.mjs --slides <dir> --out <file.pptx>');
|
||
console.error('');
|
||
console.error('⚠️ HTML 必须符合 4 条硬约束(见 references/editable-pptx.md)。');
|
||
console.error(' 视觉自由度优先的场景请改用 export_deck_pdf.mjs 导出 PDF。');
|
||
process.exit(1);
|
||
}
|
||
return args;
|
||
}
|
||
|
||
async function main() {
|
||
const { slides, out } = parseArgs();
|
||
const slidesDir = path.resolve(slides);
|
||
const outFile = path.resolve(out);
|
||
|
||
const files = (await fs.readdir(slidesDir))
|
||
.filter(f => f.endsWith('.html'))
|
||
.sort();
|
||
if (!files.length) {
|
||
console.error(`No .html files found in ${slidesDir}`);
|
||
process.exit(1);
|
||
}
|
||
|
||
console.log(`Converting ${files.length} slides via html2pptx...`);
|
||
|
||
const { createRequire } = await import('module');
|
||
const require = createRequire(import.meta.url);
|
||
let html2pptx;
|
||
try {
|
||
html2pptx = require(path.join(__dirname, 'html2pptx.js'));
|
||
} catch (e) {
|
||
console.error(`✗ 加载 html2pptx.js 失败:${e.message}`);
|
||
console.error(` 依赖缺失时请跑:npm install playwright pptxgenjs sharp`);
|
||
process.exit(1);
|
||
}
|
||
|
||
const pres = new pptxgen();
|
||
pres.layout = 'LAYOUT_WIDE'; // 13.333 × 7.5 inch,对应 HTML body 960 × 540 pt
|
||
|
||
const errors = [];
|
||
for (let i = 0; i < files.length; i++) {
|
||
const f = files[i];
|
||
const fullPath = path.join(slidesDir, f);
|
||
try {
|
||
await html2pptx(fullPath, pres);
|
||
console.log(` [${i + 1}/${files.length}] ${f} ✓`);
|
||
} catch (e) {
|
||
console.error(` [${i + 1}/${files.length}] ${f} ✗ ${e.message}`);
|
||
errors.push({ file: f, error: e.message });
|
||
}
|
||
}
|
||
|
||
if (errors.length) {
|
||
console.error(`\n⚠️ ${errors.length} 张 slide 转换失败。常见原因:HTML 不符合 4 条硬约束。`);
|
||
console.error(` 详见 references/editable-pptx.md 的「常见错误速查」。`);
|
||
if (errors.length === files.length) {
|
||
console.error(`✗ 全部失败,不生成 PPTX。`);
|
||
process.exit(1);
|
||
}
|
||
}
|
||
|
||
await pres.writeFile({ fileName: outFile });
|
||
console.log(`\n✓ Wrote ${outFile} (${files.length - errors.length}/${files.length} slides, 可编辑 PPTX)`);
|
||
}
|
||
|
||
main().catch(e => { console.error(e); process.exit(1); });
|