'多语言'

This commit is contained in:
eric
2026-03-08 04:30:57 -05:00
parent 51eeea3be3
commit def36bf3aa
30 changed files with 6058 additions and 323 deletions

View File

@@ -0,0 +1,244 @@
"use client";
import { useEffect, useState, useRef, useCallback } from "react";
import { Link } from "@/i18n/navigation";
import type { EbookData } from "../lib/ebook";
import "github-markdown-css/github-markdown.css";
import styles from "./ebook.module.css";
function LazyChunk({
html,
estimatedHeight,
forceVisible,
}: {
html: string;
estimatedHeight: number;
forceVisible: boolean;
}) {
const [visible, setVisible] = useState(forceVisible);
const ref = useRef<HTMLDivElement>(null);
const show = forceVisible || visible;
useEffect(() => {
if (forceVisible || visible) return;
const el = ref.current;
if (!el) return;
const observer = new IntersectionObserver(
([entry]) => {
if (entry.isIntersecting) {
setVisible(true);
observer.disconnect();
}
},
{ rootMargin: "1200px 0px" }
);
observer.observe(el);
return () => observer.disconnect();
}, [forceVisible, visible]);
if (show) {
return (
<div
className={styles.chunkEnter}
dangerouslySetInnerHTML={{ __html: html }}
/>
);
}
return (
<div
ref={ref}
className={styles.chunkPlaceholder}
style={{ minHeight: estimatedHeight }}
/>
);
}
export default function EbookClient({ data }: { data: EbookData }) {
const [showDrawer, setShowDrawer] = useState(false);
const [readProgress, setReadProgress] = useState(0);
const [showBackTop, setShowBackTop] = useState(false);
const [forceShowAll, setForceShowAll] = useState(false);
const contentRef = useRef<HTMLDivElement>(null);
const { chunks, headers } = data;
useEffect(() => {
let ticking = false;
const onScroll = () => {
if (ticking) return;
ticking = true;
requestAnimationFrame(() => {
const scrollTop =
window.scrollY || document.documentElement.scrollTop;
const docHeight =
document.documentElement.scrollHeight - window.innerHeight;
setReadProgress(
docHeight > 0 ? Math.min(100, (scrollTop / docHeight) * 100) : 0
);
setShowBackTop(scrollTop > 600);
ticking = false;
});
};
window.addEventListener("scroll", onScroll, { passive: true });
return () => window.removeEventListener("scroll", onScroll);
}, []);
useEffect(() => {
const el = contentRef.current;
if (!el) return;
const onImgLoad = (e: Event) => {
const target = e.target as HTMLElement;
if (target.tagName === "IMG") target.classList.add("loaded");
};
el.addEventListener("load", onImgLoad, true);
const checkCached = () => {
el.querySelectorAll("img").forEach((img) => {
if (img.complete) img.classList.add("loaded");
});
};
const mo = new MutationObserver(checkCached);
mo.observe(el, { childList: true, subtree: true });
checkCached();
return () => {
el.removeEventListener("load", onImgLoad, true);
mo.disconnect();
};
}, []);
const handleJump = useCallback(
(index: number) => {
setForceShowAll(true);
setShowDrawer(false);
const run = () => {
const root = contentRef.current;
if (!root) return;
const h1s = root.querySelectorAll("h1");
const h2s = root.querySelectorAll("h2");
const targets = h1s.length > 0 ? h1s : h2s;
const el = targets[index] as HTMLElement;
if (el) {
try {
el.scrollIntoView({ behavior: "smooth", block: "start" });
} catch {
const top =
el.getBoundingClientRect().top +
(window.pageYOffset || document.documentElement.scrollTop) -
60;
window.scrollTo({ top: Math.max(0, top), behavior: "smooth" });
}
}
};
run();
setTimeout(run, 100);
setTimeout(run, 300);
setTimeout(run, 600);
},
[]
);
return (
<>
<div
className={styles.readingProgress}
style={{ width: `${readProgress}%` }}
/>
<div className={styles.headerSpacer} />
<header className={styles.header}>
<Link href="/" className={styles.headerLeft} aria-label="返回首页">
<svg
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
>
<path d="M15 18l-6-6 6-6" />
</svg>
</Link>
<div className={styles.headerMiddle}></div>
<button
type="button"
className={`${styles.headerRight} ${showDrawer ? styles.headerRightActive : ""}`}
onClick={() => setShowDrawer(!showDrawer)}
aria-label="目录"
>
<svg
width="20"
height="20"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
>
<line x1="3" y1="6" x2="21" y2="6" />
<line x1="3" y1="12" x2="21" y2="12" />
<line x1="3" y1="18" x2="21" y2="18" />
</svg>
<span className="hidden sm:inline sm:ml-1"></span>
</button>
</header>
<div className={styles.book}>
<div ref={contentRef} className={`markdown-body ${styles.markdownBody}`}>
{chunks.map((chunk, i) => (
<LazyChunk
key={i}
html={chunk.html}
estimatedHeight={chunk.estimatedHeight}
forceVisible={forceShowAll || i === 0}
/>
))}
</div>
</div>
{/* 抽屉目录 */}
<div
className={`${styles.drawerMask} ${showDrawer ? styles.drawerMaskOpen : ""}`}
onClick={() => setShowDrawer(false)}
aria-hidden="true"
/>
<aside
className={`${styles.drawer} ${showDrawer ? styles.drawerOpen : ""}`}
>
{headers.map((title, index) => (
<div
key={index}
role="button"
tabIndex={0}
className={styles.drawerItem}
onClick={() => handleJump(index)}
onKeyDown={(e) => {
if (e.key === "Enter" || e.key === " ") handleJump(index);
}}
>
{title}
</div>
))}
</aside>
{showBackTop && (
<button
type="button"
className={styles.backToTop}
onClick={() => window.scrollTo({ top: 0, behavior: "smooth" })}
aria-label="回到顶部"
>
<svg
width="20"
height="20"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
>
<path d="M18 15l-6-6-6 6" />
</svg>
</button>
)}
</>
);
}

View File

@@ -0,0 +1,501 @@
/* 复刻 nomadbook 电子书样式 */
.readingProgress {
position: fixed;
top: 0;
left: 0;
height: 4px;
background: linear-gradient(90deg, #4facfe 0%, #00f2fe 100%);
z-index: 201;
transition: width 0.15s linear;
border-radius: 0 4px 4px 0;
}
.headerSpacer {
width: 100%;
height: 88px;
}
.header {
position: fixed;
top: 0;
left: 0;
right: 0;
z-index: 100;
height: 88px;
background: rgba(255, 255, 255, 0.92);
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
border-bottom: 1px solid rgba(0, 0, 0, 0.06);
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-between;
}
.headerLeft {
width: 100px;
display: flex;
align-items: center;
justify-content: center;
color: #1a1a2e;
cursor: pointer;
}
.headerMiddle {
flex: 1;
display: flex;
align-items: center;
justify-content: center;
font-size: 32px;
font-weight: 600;
color: #1a1a2e;
letter-spacing: 2px;
}
.headerRight {
width: 72px;
height: 72px;
padding: 0;
display: flex;
align-items: center;
justify-content: center;
color: #4facfe;
cursor: pointer;
margin-right: 10px;
background: #f0f7ff;
border-radius: 50%;
transition: all 0.2s ease;
}
.headerRight:hover {
background: #dceeff;
}
.headerRightActive {
background: #4facfe !important;
color: #fff !important;
}
.chunkEnter {
animation: chunkAppear 0.5s ease-out both;
}
@keyframes chunkAppear {
from {
opacity: 0;
transform: translateY(20px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.chunkPlaceholder {
background: linear-gradient(90deg, #f6f7f8 25%, #edeef1 37%, #f6f7f8 63%);
background-size: 400% 100%;
animation: shimmer 1.8s ease infinite;
border-radius: 12px;
margin: 24px 0;
}
@keyframes shimmer {
0% { background-position: 200% 0; }
100% { background-position: -200% 0; }
}
.loadingSpinner {
width: 56px;
height: 56px;
border: 5px solid #f0f0f0;
border-top-color: #4facfe;
border-radius: 50%;
animation: spin 0.8s linear infinite;
}
@keyframes spin {
to { transform: rotate(360deg); }
}
.backToTop {
position: fixed;
bottom: 120px;
right: 30px;
width: 80px;
height: 80px;
background: rgba(255, 255, 255, 0.92);
backdrop-filter: blur(10px);
-webkit-backdrop-filter: blur(10px);
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
cursor: pointer;
z-index: 50;
animation: fadeInUp 0.3s ease;
color: #4facfe;
transition: transform 0.2s ease, box-shadow 0.2s ease;
}
.backToTop:hover {
transform: translateY(-2px);
box-shadow: 0 6px 24px rgba(0, 0, 0, 0.12);
}
@keyframes fadeInUp {
from { opacity: 0; transform: translateY(16px); }
to { opacity: 1; transform: translateY(0); }
}
.book {
padding: 30px;
max-width: 100%;
}
.markdownBody {
font-size: 32px !important;
line-height: 1.85 !important;
color: #2c3e50 !important;
word-wrap: break-word;
overflow-wrap: break-word;
}
.markdownBody h1 {
font-size: 48px !important;
font-weight: 800;
color: #1a1a2e;
margin: 56px 0 20px;
padding-bottom: 0;
border-bottom: none;
line-height: 1.3;
}
.markdownBody h1::after {
content: "";
display: block;
width: 80px;
height: 6px;
background: linear-gradient(90deg, #4facfe, #00f2fe);
border-radius: 3px;
margin-top: 16px;
}
.markdownBody h2 {
font-size: 42px !important;
font-weight: 700;
color: #2c3e50;
margin: 44px 0 18px;
padding-bottom: 12px;
border-bottom: 1px solid #f0f0f0;
line-height: 1.3;
}
.markdownBody h3 {
font-size: 36px !important;
font-weight: 600;
color: #34495e;
margin: 36px 0 14px;
}
.markdownBody h4,
.markdownBody h5,
.markdownBody h6 {
font-size: 32px !important;
font-weight: 600;
color: #4a5568;
margin: 28px 0 12px;
}
.markdownBody p {
font-size: 32px !important;
margin-bottom: 20px;
line-height: 1.9;
}
.markdownBody blockquote {
position: relative;
margin: 28px 0;
padding: 20px 24px 20px 30px;
border-left: none;
background: linear-gradient(135deg, #f8fbff 0%, #f0f7ff 100%);
border-radius: 0 12px 12px 0;
color: #4a5568;
}
.markdownBody blockquote::before {
content: "";
position: absolute;
left: 0;
top: 0;
bottom: 0;
width: 6px;
background: linear-gradient(180deg, #4facfe, #00f2fe);
border-radius: 3px;
}
.markdownBody pre {
position: relative;
margin: 28px 0;
padding: 28px 24px 24px;
background: #1e1e2e;
border-radius: 12px;
overflow-x: auto;
}
.markdownBody pre::before {
content: "";
position: absolute;
top: 0;
left: 0;
right: 0;
height: 4px;
background: linear-gradient(90deg, #4facfe, #00f2fe);
border-radius: 12px 12px 0 0;
}
.markdownBody pre code {
font-family: "SF Mono", "Fira Code", Menlo, Monaco, Consolas, monospace;
font-size: 26px !important;
line-height: 1.6;
color: #e2e8f0;
background: none !important;
padding: 0;
}
.markdownBody code {
font-family: "SF Mono", "Fira Code", Menlo, Monaco, Consolas, monospace;
font-size: 28px !important;
padding: 4px 10px;
background: #f1f5f9;
color: #e53e3e;
border-radius: 6px;
}
.markdownBody a {
color: #4facfe;
text-decoration: none;
}
.markdownBody a:hover {
color: #2b8de3;
border-bottom: 1px solid #4facfe;
}
.markdownBody img {
display: block;
margin: 28px auto;
max-width: 100%;
height: auto;
border-radius: 8px;
opacity: 0;
transform: translateY(10px);
transition: opacity 0.6s ease, transform 0.6s ease;
}
.markdownBody img.loaded {
opacity: 1;
transform: translateY(0);
}
.markdownBody ul,
.markdownBody ol {
margin: 16px 0;
padding-left: 40px;
}
.markdownBody table {
width: 100%;
margin: 28px 0;
border-collapse: collapse;
border-radius: 8px;
}
.markdownBody th {
background: linear-gradient(135deg, #f8fafc, #edf2f7);
font-weight: 600;
padding: 16px 20px;
text-align: left;
border: 1px solid #e2e8f0;
}
.markdownBody td {
padding: 14px 20px;
border: 1px solid #e2e8f0;
}
.markdownBody tr:nth-child(even) {
background: #f8fafc;
}
/* 抽屉目录 */
.drawer {
position: fixed;
top: 0;
right: 0;
bottom: 0;
width: 280px;
max-width: 85vw;
background: #fff;
box-shadow: -4px 0 24px rgba(0, 0, 0, 0.1);
z-index: 150;
padding-top: 88px;
overflow-y: auto;
transform: translateX(100%);
transition: transform 0.3s ease;
}
.drawerOpen {
transform: translateX(0);
}
.drawerMask {
position: fixed;
inset: 0;
background: rgba(0, 0, 0, 0.35);
z-index: 140;
opacity: 0;
pointer-events: none;
transition: opacity 0.3s ease;
}
.drawerMaskOpen {
opacity: 1;
pointer-events: auto;
}
.drawerItem {
padding: 16px 20px;
border-bottom: 1px solid #f5f5f5;
color: #2c3e50;
cursor: pointer;
font-size: 15px;
transition: background 0.15s;
}
.drawerItem:hover {
background: #f0f7ff;
}
/* PC 适配 */
@media (min-width: 768px) {
.headerSpacer {
height: 56px;
}
.header {
height: 56px;
}
.headerMiddle {
font-size: 18px;
}
.headerRight {
width: auto;
height: 34px;
padding: 0 14px;
margin-right: 16px;
gap: 5px;
border-radius: 17px;
font-size: 13px;
}
.book {
padding: 30px 60px;
}
.markdownBody {
font-size: 16px !important;
line-height: 1.8 !important;
max-width: 800px;
margin: 0 auto;
}
.markdownBody h1 {
font-size: 28px !important;
margin: 36px 0 14px;
}
.markdownBody h1::after {
width: 50px;
height: 3px;
margin-top: 10px;
}
.markdownBody h2 {
font-size: 23px !important;
margin: 28px 0 12px;
padding-bottom: 8px;
}
.markdownBody h3 {
font-size: 19px !important;
margin: 22px 0 10px;
}
.markdownBody p {
font-size: 16px !important;
margin-bottom: 14px;
}
.markdownBody pre code {
font-size: 14px !important;
}
.markdownBody code {
font-size: 14px !important;
padding: 2px 6px;
}
.backToTop {
width: 44px;
height: 44px;
bottom: 40px;
right: 40px;
}
.drawer {
padding-top: 56px;
width: 300px;
}
.drawerItem {
font-size: 14px;
padding: 14px 18px;
}
}
@media (min-width: 1024px) {
.book {
padding: 30px 80px;
}
.markdownBody {
width: 720px;
font-size: 17px !important;
line-height: 1.85 !important;
}
.markdownBody h1 {
font-size: 30px !important;
}
.markdownBody h2 {
font-size: 25px !important;
}
.markdownBody h3 {
font-size: 20px !important;
}
.markdownBody p {
font-size: 17px !important;
}
.markdownBody img.loaded:hover {
box-shadow: 0 8px 30px rgba(0, 0, 0, 0.12);
transform: translateY(-2px);
}
}

View File

@@ -0,0 +1,17 @@
import { readFileSync } from "fs";
import path from "path";
import { parseEbookMarkdown } from "../../lib/ebook";
import EbookClient from "./EbookClient";
export const metadata = {
title: "电子书 | 数字游民",
description: "数字游民:地理套利与自动化杠杆,低成本工作室、云手机、无人直播、出海掘金。",
};
export default function EbookPage() {
const mdPath = path.join(process.cwd(), "app/data/ebook.md");
const source = readFileSync(mdPath, "utf-8");
const data = parseEbookMarkdown(source);
return <EbookClient data={data} />;
}