nomadro/apps/miniprogram/h5/src/components/AuditBookScreen.tsx
Eric 4789785f11 Ship remaining business changes across social API, miniprogram audit/book, VIP track, and deploy.
Includes social realtime entrypoints, weapp audit book surfaces, VIP analytics track routes, MiroTalk/Centrifugo deploy helpers, and related admin/config wiring. Excludes local DB dumps and .tmp probe scripts.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-01 17:10:10 +08:00

202 lines
5.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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<string, string | number>) => ({
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 (
<View
style={{
minHeight: '100vh',
width: '100%',
backgroundColor: '#0c0b12',
paddingTop: safePx(16),
paddingRight: safePx(16),
paddingBottom: safePx(48),
paddingLeft: safePx(16),
boxSizing: 'border-box',
}}
>
<Text style={block({ fontSize: safePx(28), marginBottom: safePx(8), color: '#f8fafc' })}>
{AUDIT_BOOK_META.cover_emoji}
</Text>
<Text
style={block({
fontSize: safePx(22),
fontWeight: '700',
color: '#f8fafc',
lineHeight: '1.35',
})}
>
{AUDIT_BOOK_META.title}
</Text>
<Text
style={block({
fontSize: safePx(13),
color: '#94a3b8',
marginTop: safePx(8),
marginBottom: safePx(16),
lineHeight: '1.45',
})}
>
{AUDIT_BOOK_META.subtitle}
</Text>
<View style={{ marginBottom: safePx(16) }}>
{chapters.map((c, i) => (
<View
key={c.id}
onClick={() => setIdx(i)}
style={{ paddingTop: safePx(10), paddingBottom: safePx(10) }}
>
<Text
style={block({
fontSize: safePx(14),
color: i === safeIdx ? '#f8fafc' : '#64748b',
fontWeight: i === safeIdx ? '700' : '400',
lineHeight: '1.4',
})}
>
{`${i + 1}. ${c.title}`}
</Text>
</View>
))}
</View>
<View style={{ paddingTop: safePx(8) }}>
<Text
style={block({
fontSize: safePx(17),
fontWeight: '700',
color: '#e2e8f0',
marginBottom: safePx(12),
lineHeight: '1.4',
})}
>
{`${safeIdx + 1}. ${chapter?.title || '正文'}`}
</Text>
{lines.map((line, i) =>
line ? (
<Text
key={`b-${i}`}
style={block({
fontSize: safePx(15),
color: '#cbd5e1',
lineHeight: '1.75',
marginBottom: safePx(6),
})}
>
{line}
</Text>
) : (
<View key={`b-${i}`} style={{ height: safePx(8) }} />
),
)}
</View>
<View
style={{
display: 'flex',
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
marginTop: safePx(28),
paddingBottom: safePx(24),
}}
>
<View
onClick={() => setIdx((v) => Math.max(0, v - 1))}
style={{ paddingTop: safePx(12), paddingBottom: safePx(12), paddingRight: safePx(16) }}
>
<Text
style={block({
fontSize: safePx(15),
color: safeIdx <= 0 ? '#334155' : '#94a3b8',
})}
>
上一章
</Text>
</View>
<Text style={block({ fontSize: safePx(13), color: '#64748b' })}>
{`${safeIdx + 1}/${chapters.length}`}
</Text>
<View
onClick={() => setIdx((v) => Math.min(chapters.length - 1, v + 1))}
style={{ paddingTop: safePx(12), paddingBottom: safePx(12), paddingLeft: safePx(16) }}
>
<Text
style={block({
fontSize: safePx(15),
color: safeIdx >= chapters.length - 1 ? '#334155' : '#94a3b8',
})}
>
下一章
</Text>
</View>
</View>
</View>
)
}
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()
}