#!/usr/bin/env node /** * Build a self-contained offline copy of the Web Blender user manual. * * node tools/web/build-offline-manual.mjs [outputDir] * * The manual's search is client-side (`data-search` attributes) so no server * is needed: rewriting the absolute paths to relative ones makes the page work * from file://. Step images that the manual embeds from /tutorial/full/steps * are copied alongside; links into the running app or the online course are * neutralised because they cannot resolve offline. */ import fs from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../.."); const source = path.join(repoRoot, "web/dist/manuals"); const output = path.resolve(process.argv[2] ?? path.join(repoRoot, "web/manual-offline")); if (!fs.existsSync(path.join(source, "index.html"))) { throw new Error(`manual source missing: ${source} (run the web build first)`); } if (output === source || output.startsWith(`${source}${path.sep}`)) throw new Error("refusing to build into the source directory"); fs.rmSync(output, { recursive: true, force: true }); fs.mkdirSync(output, { recursive: true }); fs.cpSync(source, output, { recursive: true }); let html = fs.readFileSync(path.join(output, "index.html"), "utf8"); const copied = new Set(); /* Local manual assets and step images referenced from the tutorial tree. */ /* Attributes are preceded by whitespace, not a quote: match the name and the * opening quote only. */ html = html.replace(/\b(src|href)="\/manuals\//g, '$1="./'); html = html.replace(/\b(src|href)="\/tutorial\/full\/steps\/([^"]+)"/g, (match, name, file) => { const from = path.join(repoRoot, "web/dist/tutorial/full/steps", file); const to = path.join(output, "tutorial/full/steps", file); if (fs.existsSync(from)) { fs.mkdirSync(path.dirname(to), { recursive: true }); if (!copied.has(file)) { fs.copyFileSync(from, to); copied.add(file); } } return `${name}="./tutorial/full/steps/${file}"`; }); /* Offline mode has no web app or online course to open. */ const before = html; html = html.replace(/href="\/(?:\?ui=web|tutorial\/[^"]*)"/g, 'href="#" data-offline-link="unavailable" title="离线手册不含该在线页面"'); html = html.replace(/href="\/"/g, 'href="#" data-offline-link="unavailable" title="离线手册不含在线应用"'); if (html !== before) html = html.replace(" { for (const entry of fs.readdirSync(dir, { withFileTypes: true })) { const full = path.join(dir, entry.name); if (entry.isDirectory()) walk(full); else files.push(full); } }; walk(output); const bytes = files.reduce((sum, file) => sum + fs.statSync(file).size, 0); fs.writeFileSync(path.join(output, "OFFLINE.txt"), `Web Blender 用户手册(离线版)\n\n双击 index.html 即可使用,无需联网或启动服务器。\n` + `搜索框在页面顶部,支持章节全文过滤。\nPDF 手册位于本目录根下。\n\n构建来源:web/dist/manuals\n`); console.log(`offline-manual-ok dir=${output} files=${files.length} bytes=${bytes}`);