返回 CodeWhale
doc-article.tsx
根目录 / web / app / [locale] / docs / _components / doc-article.tsx
1 import Link from "next/link";
2 import { Fragment, type ReactNode } from "react";
3 import { InstallCodeBlock } from "@/components/install-code-block";
4 import { docsCapabilityContext, resolveDocsPage } from "@/lib/docs-release";
5 import { getChrome, getDocsShell, getHome } from "@/lib/i18n/dictionaries";
6 import type { DocsBlock, DocsPageDict } from "@/lib/i18n/dictionaries/types";
7
8 /**
9 * <DocArticle> — the one renderer for every task page under /docs.
10 *
11 * A page answers three questions in order: what you can do here and why
12 * (title + lede), how (sections of prose, commands, tables and steps), and
13 * what to do next (closing links). All copy comes from a `DocsPageDict`; the
14 * page file only picks the dictionary and supplies metadata.
15 *
16 * Inline syntax inside prose strings is deliberately tiny:
17 * `code` → <code class="inline">
18 * [label](/path) → a link; a leading "/" is made locale-relative
19 * Nothing else is interpreted, so dictionary text cannot inject markup.
20 */
21
22 /** Links are underlined so they never rely on color alone (WCAG 1.4.1). */
23 const LINK = "docs-inline-link underline underline-offset-2 hover:text-indigo";
24
25 const INLINE = /(`[^`]+`|\[[^\]]+\]\([^)\s]+\))/g;
26
27 export function renderInline(text: string, locale: string): ReactNode {
28 return text.split(INLINE).map((part, i) => {
29 if (part.startsWith("`") && part.endsWith("`") && part.length > 1) {
30 return (
31 <code key={i} className="inline">
32 {part.slice(1, -1)}
33 </code>
34 );
35 }
36 const link = part.match(/^\[([^\]]+)\]\(([^)\s]+)\)$/);
37 if (link) {
38 const [, label, href] = link;
39 if (href.startsWith("/")) {
40 return (
41 <Link key={i} href={`/${locale}${href}`} className={LINK}>
42 {label}
43 </Link>
44 );
45 }
46 return (
47 <a key={i} href={href} className={LINK} target="_blank" rel="noreferrer">
48 {label}
49 </a>
50 );
51 }
52 return <Fragment key={i}>{part}</Fragment>;
53 });
54 }
55
56 function Block({
57 block,
58 locale,
59 body,
60 noteLabel,
61 }: {
62 block: DocsBlock;
63 locale: string;
64 body: string;
65 noteLabel: string;
66 }) {
67 if ("p" in block) {
68 return <p className={`${body} mt-3`}>{renderInline(block.p, locale)}</p>;
69 }
70 if ("code" in block) {
71 return (
72 <div className="mt-4 space-y-2">
73 <p className="text-xs font-mono text-ink-soft">{block.lang}</p>
74 <InstallCodeBlock cmd={block.code} copyLabel={getHome(locale).copy} copiedLabel={getHome(locale).copied} copyLocale={locale} installIntent={false} />
75 </div>
76 );
77 }
78 if ("rows" in block) {
79 return (
80 <dl className="docs-ref-rows mt-4">
81 {block.rows.map(([term, detail]) => (
82 <div key={term}>
83 <dt>{block.codeTerms ? <code className="inline">{term}</code> : renderInline(term, locale)}</dt>
84 <dd>{renderInline(detail, locale)}</dd>
85 </div>
86 ))}
87 </dl>
88 );
89 }
90 if ("steps" in block) {
91 return (
92 <ol className={`${body} docs-steps mt-3 list-decimal space-y-2 pl-6`}>
93 {block.steps.map((step) => (
94 <li key={step}>{renderInline(step, locale)}</li>
95 ))}
96 </ol>
97 );
98 }
99 if ("list" in block) {
100 return (
101 <ul className={`${body} docs-list mt-3 list-disc space-y-2 pl-6`}>
102 {block.list.map((item) => (
103 <li key={item}>{renderInline(item, locale)}</li>
104 ))}
105 </ul>
106 );
107 }
108 return (
109 <p className={`${body} docs-note hairline-l mt-4 pl-4`}>
110 <strong>{noteLabel}</strong>{" "}
111 {renderInline(block.note, locale)}
112 </p>
113 );
114 }
115
116 export async function DocArticle({
117 t,
118 locale,
119 extra,
120 }: {
121 t: DocsPageDict;
122 locale: string;
123 /** Page-owned content rendered at the end of the section with this id. */
124 extra?: Record<string, ReactNode>;
125 }) {
126 const shell = getDocsShell(locale);
127 const { sections, sourceSections } = resolveDocsPage(t, await docsCapabilityContext());
128 const inSource = getChrome(locale).stateInSource;
129 return (
130 <div className="docs-article space-y-10">
131 <header id="overview" className="scroll-mt-32">
132 <h1 className="font-display text-3xl mb-1">{t.title}</h1>
133 <p className={`${t.bodyClassName} docs-lede mt-3`}>{renderInline(t.lede, locale)}</p>
134 {sections.length > 2 && (
135 <nav className="docs-toc mt-5" aria-label={shell.onThisPage}>
136 <ol className="flex flex-wrap gap-x-5 gap-y-1 text-sm">
137 {sections.map((section) => (
138 <li key={section.id}>
139 <a href={`#${section.id}`} className={LINK}>
140 {section.title}
141 </a>
142 </li>
143 ))}
144 {sourceSections.length > 0 && (
145 <li>
146 <a href="#in-source" className={LINK}>
147 {inSource}
148 </a>
149 </li>
150 )}
151 </ol>
152 </nav>
153 )}
154 </header>
155
156 {sections.map((section) => (
157 <section key={section.id} id={section.id} className="scroll-mt-32">
158 <h2 className="font-display text-2xl mb-1">{section.title}</h2>
159 {section.blocks.map((block, i) => (
160 <Block
161 key={i}
162 block={block}
163 locale={locale}
164 body={t.bodyClassName}
165 noteLabel={shell.noteLabel}
166 />
167 ))}
168 {extra?.[section.id]}
169 </section>
170 ))}
171
172 {sourceSections.length > 0 && (
173 <section id="in-source" className="scroll-mt-32" data-release-scope="source">
174 <h2 className="font-display text-2xl mb-1">{inSource}</h2>
175 <p className={`${t.bodyClassName} mt-3`}>{renderInline(shell.sourceLead, locale)}</p>
176 {sourceSections.map((section) => (
177 <div key={section.id} id={section.id} className="scroll-mt-32 mt-6">
178 <h3 className="font-display text-xl mb-1">{section.title}</h3>
179 {section.blocks.map((block, i) => (
180 <Block
181 key={i}
182 block={block}
183 locale={locale}
184 body={t.bodyClassName}
185 noteLabel={shell.noteLabel}
186 />
187 ))}
188 </div>
189 ))}
190 </section>
191 )}
192
193 {t.next.length > 0 && (
194 <section id="next" className="scroll-mt-32">
195 <h2 className="font-display text-2xl mb-1">{shell.nextHeading}</h2>
196 <div className="hairline-t mt-4">
197 {t.next.map((item) => (
198 <div key={item.href} className="py-4 hairline-b">
199 <h3 className="font-display text-xl">
200 <Link href={`/${locale}${item.href}`} className={LINK}>
201 {item.label}
202 </Link>
203 </h3>
204 <p className={`${t.bodyClassName} mt-1 text-sm`}>{renderInline(item.note, locale)}</p>
205 </div>
206 ))}
207 </div>
208 </section>
209 )}
210
211 {/* Maintainer pointer: kept out of the rendered copy. */}
212 <div hidden data-source-note={t.sourceNote} />
213 </div>
214 );
215 }
216
216 lines Plain Text