import "server-only"; import { readFileSync } from "fs"; import type { Book, LoadedChapter } from "@ebook/lib/book-types"; import { resolveVipDataFile } from "@vip/app/lib/resolveVipDataFile"; function slugify(title: string, index: number): string { const plain = title.replace(/[#*_`]/g, "").trim(); // 与 /book 小册习惯对齐:前言 → 00-preface if (/^前言/.test(plain) || /^preface$/i.test(plain)) return "00-preface"; const base = plain .toLowerCase() .replace(/\s+/g, "-") .replace(/[^\w\u4e00-\u9fff-]+/g, "") .replace(/-+/g, "-") .replace(/^-|-$/g, "") .slice(0, 48) || `section-${index}`; return `${String(index).padStart(2, "0")}-${base}`; } function pickEmoji(title: string, index: number): string { if (/前言|序/.test(title)) return "✈️"; if (/云手机|直播/.test(title)) return "📱"; if (/youtube|yt/i.test(title)) return "📺"; if (/tiktok/i.test(title)) return "🎵"; if (/独立开发|saas|web/i.test(title)) return "🛠️"; if (/vps|服务器|网络|软路由/.test(title)) return "🌐"; if (/赚钱|副业/.test(title)) return "💰"; const pool = ["📖", "✦", "🚀", "💡", "🔥", "🌍", "⚙️"]; return pool[index % pool.length]; } /** * 把 VIP 原 ebook.md 拆成与 /book/read 相同的 Book + LoadedChapter 结构, * 以便直接复用 ScrollReaderShell。 */ export function loadVipEbookAsBook(): { book: Book; chapters: LoadedChapter[] } { const mdPath = resolveVipDataFile("ebook.md"); const source = readFileSync(mdPath, "utf-8"); const parts = source.split(/(?=^# )/m).filter((p) => p.trim()); const total = Math.max(parts.length, 1); const chapters: LoadedChapter[] = (parts.length ? parts : [source]).map((raw, index) => { const lines = raw.trim().split("\n"); const titleLine = lines[0]?.replace(/^#\s+/, "").trim() || `第 ${index + 1} 章`; const body = lines.slice(1).join("\n").trim(); const content = body || raw.trim(); const charCount = content.replace(/\s/g, "").length; const readingMinutes = Math.max(1, Math.round(charCount / 450)); const slug = slugify(titleLine, index); const emoji = pickEmoji(titleLine, index); const hook = body .split("\n") .map((l) => l.replace(/^>\s?/, "").trim()) .find((l) => l && !l.startsWith("#") && !l.startsWith("!") && !l.startsWith("<")) ?.slice(0, 80) || titleLine; return { slug, title: titleLine, emoji, hook, shareQuote: hook, content, index, readingMinutes, total, }; }); const readingMinutes = chapters.reduce((s, c) => s + c.readingMinutes, 0); const book: Book = { title: "数字游民", subtitle: "地理套利与自动化杠杆", author: "nomadro", description: "低成本工作室、云手机、无人直播、出海掘金。", tagline: "在任意城市,过你想要的生活。", coverGradient: "from-violet-950 via-fuchsia-900 to-orange-900", readingMinutes, viralTags: ["数字游民", "云手机", "无人直播", "出海"], stats: [ { emoji: "📖", value: String(chapters.length), label: "章" }, { emoji: "⏱️", value: String(readingMinutes), label: "分钟" }, { emoji: "☁️", value: "云手机", label: "工作室" }, { emoji: "💰", value: "出海", label: "掘金" }, ], hooks: chapters.slice(0, 4).map((c) => ({ emoji: c.emoji, text: c.hook })), chapters: chapters.map(({ slug, title, emoji, hook, shareQuote }) => ({ slug, title, emoji, hook, shareQuote, })), }; return { book, chapters }; }