268 lines
11 KiB
TypeScript
268 lines
11 KiB
TypeScript
import type {
|
|
GetServerSideProps,
|
|
InferGetServerSidePropsType,
|
|
} from "next";
|
|
import Head from "next/head";
|
|
import dynamic from "next/dynamic";
|
|
import Link from "next/link";
|
|
import {
|
|
Children,
|
|
isValidElement,
|
|
useEffect,
|
|
useState,
|
|
type ReactNode,
|
|
} from "react";
|
|
import ReactMarkdown from "react-markdown";
|
|
import remarkGfm from "remark-gfm";
|
|
import { listDocs, readDocument, type Doc, type DocLink } from "@/lib/docs";
|
|
import { Shell, Navigation } from "@/components/Shell";
|
|
import { ArchitectureFigure } from "@/components/ArchitectureFigure";
|
|
import { FigureGallery } from "@/components/FigureGallery";
|
|
import { CodeBlock } from "@/components/CodeBlock";
|
|
import { collectionEntries, collectionLabel } from "@/lib/collections";
|
|
import { Icon } from "@/components/Icon";
|
|
import { headingsFor } from "@/lib/navigation";
|
|
|
|
type Props = { doc: Doc; docs: DocLink[] };
|
|
const AICompanyResearch = dynamic(() => import("@/components/AICompanyResearch"));
|
|
export const getServerSideProps: GetServerSideProps<Props> = async ({ params, res, resolvedUrl }) => {
|
|
res.setHeader("Cache-Control", "private, no-store");
|
|
const slug = Array.isArray(params?.slug) ? params.slug.join("/") : params?.slug;
|
|
if (typeof slug !== "string") return { notFound: true };
|
|
const doc = readDocument(slug);
|
|
if (!doc) return { notFound: true };
|
|
if (slug !== doc.slug) {
|
|
const query = resolvedUrl.includes("?") ? resolvedUrl.slice(resolvedUrl.indexOf("?")) : "";
|
|
return { redirect: { destination: `/docs/${doc.slug}${query}`, permanent: true } };
|
|
}
|
|
return { props: { doc, docs: await listDocs() } };
|
|
};
|
|
function textOf(value: ReactNode): string {
|
|
return Children.toArray(value)
|
|
.map((child) =>
|
|
isValidElement<{ children?: ReactNode }>(child)
|
|
? textOf(child.props.children)
|
|
: String(child),
|
|
)
|
|
.join("");
|
|
}
|
|
export default function DocPage({
|
|
doc,
|
|
docs,
|
|
}: InferGetServerSidePropsType<typeof getServerSideProps>) {
|
|
const headings = headingsFor(doc.content);
|
|
const [active, setActive] = useState("");
|
|
|
|
const siblings = docs.filter((d) => d.collection === doc.collection);
|
|
const collectionEntry = collectionEntries(docs).find((c) => c.name === doc.collection)!.entry;
|
|
const index = siblings.findIndex((d) => d.slug === doc.slug);
|
|
useEffect(() => {
|
|
const observer = new IntersectionObserver(
|
|
(entries) => {
|
|
const visible = entries.filter((e) => e.isIntersecting);
|
|
if (visible[0]) setActive(visible[0].target.id);
|
|
},
|
|
{ rootMargin: "-80px 0px -60% 0px" },
|
|
);
|
|
document
|
|
.querySelectorAll("article h2, article h3")
|
|
.forEach((el) => observer.observe(el));
|
|
return () => observer.disconnect();
|
|
}, [doc.slug]);
|
|
const toc = (
|
|
<nav aria-label="页内目录">
|
|
{headings.map((h) => (
|
|
<a
|
|
key={h.id}
|
|
href={`#${h.id}`}
|
|
className={`${h.level === 3 ? "toc-nested" : ""} ${active === h.id ? "active" : ""}`}
|
|
aria-current={active === h.id ? "location" : undefined}
|
|
>
|
|
{h.text}
|
|
</a>
|
|
))}
|
|
</nav>
|
|
);
|
|
return (
|
|
<>
|
|
<Head>
|
|
<title>{doc.title} · Tabularium</title>
|
|
<meta name="description" content={doc.description} />
|
|
</Head>
|
|
<Shell key={doc.slug} docs={docs} current={doc.slug}>
|
|
<div className={`reading-layout${doc.collection === "AI 公司研究" ? " research-layout" : ""}`}>
|
|
<aside className="sidebar">
|
|
<Navigation docs={docs} current={doc.slug} />
|
|
</aside>
|
|
<main id="content" className="reading-main">
|
|
<div className="breadcrumb">
|
|
<Link href={`/docs/${collectionEntry.slug}`}>{collectionLabel(doc.collection)}</Link>
|
|
<Icon name="chevron" size={12} />
|
|
<span>{doc.slug === collectionEntry.slug ? "概览" : doc.group === doc.collection ? doc.title : doc.group}</span>
|
|
{doc.draft && <span className="draft-badge">草案</span>}
|
|
</div>
|
|
<article className="prose prose-neutral max-w-none">
|
|
<ReactMarkdown
|
|
remarkPlugins={[remarkGfm]}
|
|
components={{
|
|
a: ({ href, children }) => {
|
|
if (!href) return <a>{children}</a>;
|
|
let target = href;
|
|
if (!/^(?:[a-z]+:|\/\/|#)/i.test(href)) {
|
|
const resolved = new URL(href, `https://docs.local/docs/${doc.sourceSlug}`);
|
|
const source = decodeURIComponent(resolved.pathname).replace(/^\/docs\//, "").replace(/\.md$/, "");
|
|
const linked = docs.find((d) => d.sourceSlug === source || d.slug === source);
|
|
if (linked) target = `/docs/${linked.slug}${resolved.search}${resolved.hash}`;
|
|
}
|
|
return <a href={target}>{children}</a>;
|
|
},
|
|
p: ({ children, node }) => {
|
|
const slides = node?.children.flatMap((child) =>
|
|
child.type === "element" && child.tagName === "img" &&
|
|
typeof child.properties.src === "string" &&
|
|
child.properties.src.startsWith("/figures/endor-")
|
|
? [{ source: child.properties.src, alt: String(child.properties.alt ?? "") }]
|
|
: [],
|
|
) ?? [];
|
|
const onlyImages = node?.children.every((child) =>
|
|
(child.type === "element" && child.tagName === "img" &&
|
|
typeof child.properties.src === "string" &&
|
|
child.properties.src.startsWith("/figures/endor-")) ||
|
|
(child.type === "text" && child.value.trim() === ""),
|
|
);
|
|
if (slides.length > 1 && onlyImages) return <FigureGallery slides={slides} />;
|
|
return node?.children.some(
|
|
(child) =>
|
|
child.type === "element" && child.tagName === "img",
|
|
) ? (
|
|
<div>{children}</div>
|
|
) : (
|
|
<p>{children}</p>
|
|
);
|
|
},
|
|
img: ({ src, alt }) =>
|
|
typeof src === "string" && src.startsWith("/figures/endor-") ? (
|
|
<ArchitectureFigure source={src} alt={alt ?? "Endor 架构图"} />
|
|
) : (
|
|
// eslint-disable-next-line @next/next/no-img-element
|
|
<img src={src} alt={alt ?? ""} />
|
|
),
|
|
table: ({ children }) => (
|
|
<div className="table-scroll">
|
|
<table>{children}</table>
|
|
</div>
|
|
),
|
|
h1: ({ children }) => (
|
|
<>
|
|
<h1>{children}</h1>
|
|
{headings.length > 0 && (
|
|
<details className="mobile-toc">
|
|
<summary>本页内容</summary>
|
|
{toc}
|
|
</details>
|
|
)}
|
|
</>
|
|
),
|
|
h2: ({ children, node }) => {
|
|
const h = headings.find(
|
|
(h) => h.line === node?.position?.start.line,
|
|
);
|
|
return (
|
|
<h2 id={h?.id}>
|
|
<a className="heading-anchor" href={`#${h?.id}`}>
|
|
{children}
|
|
<span aria-hidden="true">#</span>
|
|
</a>
|
|
</h2>
|
|
);
|
|
},
|
|
h3: ({ children, node }) => {
|
|
const h = headings.find(
|
|
(h) => h.line === node?.position?.start.line,
|
|
);
|
|
return (
|
|
<h3 id={h?.id}>
|
|
<a className="heading-anchor" href={`#${h?.id}`}>
|
|
{children}
|
|
<span aria-hidden="true">#</span>
|
|
</a>
|
|
</h3>
|
|
);
|
|
},
|
|
pre: ({ children }) => {
|
|
const child = Children.toArray(children)[0];
|
|
if (
|
|
isValidElement<{
|
|
className?: string;
|
|
children?: ReactNode;
|
|
}>(child)
|
|
)
|
|
return (
|
|
<CodeBlock
|
|
code={textOf(child.props.children)}
|
|
language={child.props.className?.replace(
|
|
"language-",
|
|
"",
|
|
)}
|
|
/>
|
|
);
|
|
return <pre>{children}</pre>;
|
|
},
|
|
blockquote: ({ children }) => (
|
|
<blockquote
|
|
className={
|
|
doc.draft
|
|
? "callout callout-draft"
|
|
: "callout"
|
|
}
|
|
>
|
|
<span className="callout-icon" aria-hidden="true">
|
|
ⓘ
|
|
</span>
|
|
<div>{children}</div>
|
|
</blockquote>
|
|
),
|
|
}}
|
|
>
|
|
{doc.content}
|
|
</ReactMarkdown>
|
|
</article>
|
|
{doc.slug === "other-research/ai-company-research/overview" && <AICompanyResearch />}
|
|
<div className="article-end">
|
|
|
|
<a
|
|
href={`/api/documents/source?slug=${encodeURIComponent(doc.slug)}`}
|
|
>
|
|
查看 Markdown 原文 ↗
|
|
</a>
|
|
</div>
|
|
<nav className="pagination" aria-label="相邻文档">
|
|
{index > 0 ? (
|
|
<Link href={`/docs/${siblings[index - 1].slug}`}>
|
|
<small>← 上一篇</small>
|
|
<strong>{siblings[index - 1].title}</strong>
|
|
</Link>
|
|
) : (
|
|
<span />
|
|
)}
|
|
{index < siblings.length - 1 && (
|
|
<Link
|
|
className="next-page"
|
|
href={`/docs/${siblings[index + 1].slug}`}
|
|
>
|
|
<small>下一篇 →</small>
|
|
<strong>{siblings[index + 1].title}</strong>
|
|
</Link>
|
|
)}
|
|
</nav>
|
|
</main>
|
|
<aside className="toc">
|
|
<p className="eyebrow">本页内容</p>
|
|
{toc}
|
|
</aside>
|
|
</div>
|
|
</Shell>
|
|
</>
|
|
);
|
|
}
|