import { useMemo, useState } from 'react' import { View, Text } from '@tarojs/components' import { AUDIT_BOOK_CHAPTERS, AUDIT_BOOK_META } from '../data/auditBook' import { safePx } from '../utils/safeArea' const FALLBACK_BODY = '欢迎阅读《数字游民入门指南》。这是一本面向普通人的生活方式小册:讲如何在合法合规的前提下,把工作做得更灵活,把生活安排得更从容。' const block = (extra?: Record) => ({ display: 'block' as const, ...extra, }) /** 提审小册:包内数据 + View/Text + safePx,无网络 / 无 RichText / 无 scss */ export function AuditBookScreen(_props?: { bookId?: string }) { const chapters = AUDIT_BOOK_CHAPTERS const [idx, setIdx] = useState(0) const safeIdx = Math.min(Math.max(0, idx), Math.max(0, chapters.length - 1)) const chapter = chapters[safeIdx] const lines = useMemo(() => { const plain = markdownToPlain(chapter?.content || '') || FALLBACK_BODY return plain.split('\n') }, [chapter?.content]) return ( {AUDIT_BOOK_META.cover_emoji} {AUDIT_BOOK_META.title} {AUDIT_BOOK_META.subtitle} {chapters.map((c, i) => ( setIdx(i)} style={{ paddingTop: safePx(10), paddingBottom: safePx(10) }} > {`${i + 1}. ${c.title}`} ))} {`${safeIdx + 1}. ${chapter?.title || '正文'}`} {lines.map((line, i) => line ? ( {line} ) : ( ), )} setIdx((v) => Math.max(0, v - 1))} style={{ paddingTop: safePx(12), paddingBottom: safePx(12), paddingRight: safePx(16) }} > 上一章 {`${safeIdx + 1}/${chapters.length}`} setIdx((v) => Math.min(chapters.length - 1, v + 1))} style={{ paddingTop: safePx(12), paddingBottom: safePx(12), paddingLeft: safePx(16) }} > = chapters.length - 1 ? '#334155' : '#94a3b8', })} > 下一章 ) } function stripInline(s: string) { return s .replace(/\*\*(.+?)\*\*/g, '$1') .replace(/\*(.+?)\*/g, '$1') .replace(/`([^`]+)`/g, '$1') .replace(/\[(.+?)\]\((https?:\/\/.+?)\)/g, '$1') } function markdownToPlain(markdown: string): string { const raw = (markdown || '').replace(/\r\n/g, '\n').split('\n') const out: string[] = [] for (const line of raw) { const trimmed = line.trim() if (!trimmed) { if (out.length && out[out.length - 1] !== '') out.push('') continue } if (/^---+$/.test(trimmed)) continue const heading = /^(#{1,3})\s+(.+)$/.exec(trimmed) if (heading) { out.push(stripInline(heading[2])) continue } const quote = /^>\s?(.*)$/.exec(trimmed) if (quote) { out.push(stripInline(quote[1])) continue } const ol = /^\d+\.\s+(.+)$/.exec(trimmed) if (ol) { out.push(`${ol[0].match(/^\d+/)?.[0] || '·'}. ${stripInline(ol[1])}`) continue } const ul = /^[-*]\s+(.+)$/.exec(trimmed) if (ul) { out.push(`· ${stripInline(ul[1])}`) continue } if (/^\|?\s*-{2,}/.test(trimmed)) continue out.push(stripInline(trimmed.replace(/\|/g, ' '))) } return out.join('\n').replace(/\n{3,}/g, '\n\n').trim() }