/** * Pack src/modules/ebook-content → Taro H5 offline (cover + reader). * SoT: src/modules/ebook-content/book.json + chapters/*.md */ import fs from 'fs' import path from 'path' import { fileURLToPath } from 'url' const __dirname = path.dirname(fileURLToPath(import.meta.url)) const contentDir = path.resolve(__dirname, '../../../../src/modules/ebook-content') const chaptersDir = path.join(contentDir, 'chapters') const outDir = path.resolve(__dirname, '../../h5/src/data') const book = JSON.parse(fs.readFileSync(path.join(contentDir, 'book.json'), 'utf8')) const chapters = book.chapters.map((ch, i) => { const mdPath = path.join(chaptersDir, `${ch.slug}.md`) const md = fs.existsSync(mdPath) ? fs.readFileSync(mdPath, 'utf8') : '' return { id: ch.slug, title: ch.title, order: i + 1, emoji: ch.emoji || '', hook: ch.hook || '', shareQuote: ch.shareQuote || '', content: md, } }) const bookDetail = { id: 'default', title: book.title, subtitle: book.subtitle, author: book.author, cover_emoji: '📖', description: book.description, tags: book.viralTags || [], price_fen: 0, product_id: 'ebook_default', unlock_percent: 100, is_free: true, chapter_count: chapters.length, word_count: chapters.reduce((s, c) => s + c.content.length, 0), rating: 0, chapters: chapters.map((c) => ({ id: c.id, title: c.title, order: c.order })), } const payload = { book: bookDetail, chapters, unlocked: true, unlock_percent: 100, preview_ratio: 1, is_free: true, } const coverBookTs = `/** * Auto-packed from src/modules/ebook-content/book.json — keep in sync with Next /book. * Run: pnpm pack:nomad-code */ export type CoverStat = { emoji: string; value: string; label: string } export type CoverHook = { emoji: string; text: string } export type CoverChapter = { slug: string title: string emoji: string hook?: string shareQuote?: string } export const COVER_BOOK = ${JSON.stringify( { title: book.title, subtitle: book.subtitle, author: book.author, description: book.description, tagline: book.tagline, viralTags: book.viralTags || [], readingMinutes: book.readingMinutes || 45, coverGradient: book.coverGradient || '', stats: book.stats || [], hooks: book.hooks || [], chapters: (book.chapters || []).map((ch) => ({ slug: ch.slug, title: ch.title, emoji: ch.emoji, hook: ch.hook || '', shareQuote: ch.shareQuote || '', })), }, null, 2 )} as const ` const nomadCodeTs = `/** Auto-packed from src/modules/ebook-content — match Next /book/read */ import type { BookDetail, ReadPayload } from '../services/ebookApi' export const NOMAD_CODE_BOOK: BookDetail = ${JSON.stringify(bookDetail, null, 2)} export function nomadCodeReadPayload(): ReadPayload { return ${JSON.stringify(payload, null, 2)} } ` fs.writeFileSync(path.join(outDir, 'coverBook.ts'), coverBookTs, 'utf8') fs.writeFileSync(path.join(outDir, 'nomadCodeBook.ts'), nomadCodeTs, 'utf8') console.log('packed coverBook.ts + nomadCodeBook.ts from', contentDir) console.log('chapters', chapters.length, 'words', bookDetail.word_count)