hd-garden 0.7.0 → 0.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (220) hide show
  1. package/.agents/skills/garden-ui/SKILL.md +26 -19
  2. package/.env.en.example +67 -0
  3. package/README.en.md +57 -0
  4. package/README.md +31 -46
  5. package/app/a2/[slug]/page.tsx +4 -3
  6. package/app/a2/page.tsx +4 -3
  7. package/app/about/page.tsx +8 -9
  8. package/app/admin/page.tsx +7 -6
  9. package/app/admin/preview/[...path]/page.tsx +2 -1
  10. package/app/admin/ui/blocks/page.tsx +2 -1
  11. package/app/admin/ui/catalog/page.tsx +2 -1
  12. package/app/admin/ui/evals/page.tsx +2 -1
  13. package/app/admin/ui/page.tsx +2 -1
  14. package/app/blog/[...path]/page.tsx +14 -5
  15. package/app/blog/page.tsx +4 -2
  16. package/app/columns/[column]/[...path]/page.tsx +17 -18
  17. package/app/columns/page.tsx +12 -8
  18. package/app/components/AdminUiShell.tsx +4 -3
  19. package/app/components/ArticlePager.tsx +32 -0
  20. package/app/components/ArticleReferences.tsx +4 -1
  21. package/app/components/ArticleTags.tsx +3 -2
  22. package/app/components/BlogArchive.tsx +20 -16
  23. package/app/components/Comments.tsx +17 -10
  24. package/app/components/ContentEmptyState.tsx +29 -28
  25. package/app/components/ExternalEmbed.tsx +15 -14
  26. package/app/components/FeedMore.tsx +70 -0
  27. package/app/components/GardenContentGuide.tsx +10 -2
  28. package/app/components/GardenDocsNav.tsx +29 -22
  29. package/app/components/GiscusComments.tsx +2 -1
  30. package/app/components/GithubLiveOverview.tsx +10 -8
  31. package/app/components/GithubProfile.tsx +31 -21
  32. package/app/components/HandbookLangRedirect.tsx +30 -0
  33. package/app/components/HandbookLangSwitch.tsx +53 -0
  34. package/app/components/HandbookView.tsx +270 -0
  35. package/app/components/MarkdownArticle.tsx +39 -9
  36. package/app/components/MarkdownImage.tsx +6 -4
  37. package/app/components/SiteChrome.tsx +11 -2
  38. package/app/components/SiteFooter.tsx +6 -4
  39. package/app/components/SiteHeader.tsx +38 -25
  40. package/app/components/TableOfContents.tsx +15 -9
  41. package/app/components/ThoughtCard.tsx +16 -14
  42. package/app/components/ThoughtFeed.tsx +41 -0
  43. package/app/components/UiBlock.tsx +3 -2
  44. package/app/components/UiLang.tsx +18 -0
  45. package/app/components/Webmentions.tsx +13 -12
  46. package/app/components/WidgetChart.tsx +8 -6
  47. package/app/components/garden/FilePreview.tsx +14 -12
  48. package/app/components/garden/GardenBlock.tsx +10 -1
  49. package/app/components/garden/GardenFile.tsx +10 -8
  50. package/app/components/garden/RefDemo.tsx +62 -0
  51. package/app/components/garden/blocks.tsx +90 -32
  52. package/app/components/garden/chrome.tsx +20 -13
  53. package/app/components/garden/patterns.tsx +497 -0
  54. package/app/garden/[...slug]/page.tsx +11 -130
  55. package/app/garden/en/[...slug]/page.tsx +28 -0
  56. package/app/garden/en/page.tsx +16 -0
  57. package/app/garden/page.tsx +8 -74
  58. package/app/garden-modules.d.ts +3 -0
  59. package/app/globals.css +10 -0
  60. package/app/layout.tsx +2 -4
  61. package/app/lib/a2-meta.ts +26 -3
  62. package/app/lib/content.ts +13 -3
  63. package/app/lib/garden-docs-meta.ts +84 -5
  64. package/app/lib/github-id.mjs +8 -0
  65. package/app/lib/handbook-path.ts +11 -0
  66. package/app/lib/handbook-switch.mjs +12 -0
  67. package/app/lib/i18n.ts +621 -0
  68. package/app/lib/page-alternates.ts +2 -2
  69. package/app/lib/rehype-field-tables.mjs +47 -0
  70. package/app/lib/remark-a2.mjs +18 -8
  71. package/app/lib/remark-link-card.mjs +22 -0
  72. package/app/lib/remark-ref-demos.mjs +42 -0
  73. package/app/lib/tag-filter.ts +9 -2
  74. package/app/not-found.tsx +34 -4
  75. package/app/page.tsx +17 -14
  76. package/app/rss.xml/route.ts +2 -1
  77. package/app/site.config.ts +1 -1
  78. package/app/styles/a2.css +439 -32
  79. package/app/styles/shell.css +21 -2
  80. package/app/tags/page.tsx +7 -5
  81. package/app/thoughts/[slug]/page.tsx +2 -1
  82. package/app/thoughts/page/[n]/page.tsx +44 -0
  83. package/app/thoughts/page.tsx +4 -7
  84. package/app/ui-parity/page.tsx +38 -22
  85. package/bin/garden.mjs +133 -19
  86. package/docs/handbook/ai-writing.md +99 -0
  87. package/docs/handbook/assets/reading-layout-en.svg +26 -0
  88. package/docs/handbook/components/card.md +43 -20
  89. package/docs/handbook/components/chart.md +10 -24
  90. package/docs/handbook/components/choice.md +42 -0
  91. package/docs/handbook/components/compare.md +34 -21
  92. package/docs/handbook/components/figure.md +33 -44
  93. package/docs/handbook/components/file.md +10 -13
  94. package/docs/handbook/components/html.md +10 -28
  95. package/docs/handbook/components/interactive.md +9 -33
  96. package/docs/handbook/components/list.md +65 -0
  97. package/docs/handbook/components/relations.md +95 -0
  98. package/docs/handbook/components/stat.md +10 -26
  99. package/docs/handbook/components/timeline.md +13 -26
  100. package/docs/handbook/components.md +210 -19
  101. package/docs/handbook/config.md +7 -5
  102. package/docs/handbook/deploy.md +2 -2
  103. package/docs/handbook/design.md +23 -15
  104. package/docs/handbook/en/ai-writing.md +99 -0
  105. package/docs/handbook/en/components/card.md +67 -0
  106. package/docs/handbook/en/components/chart.md +38 -0
  107. package/docs/handbook/en/components/choice.md +42 -0
  108. package/docs/handbook/en/components/compare.md +53 -0
  109. package/docs/handbook/en/components/figure.md +59 -0
  110. package/docs/handbook/en/components/file/checklist.md +17 -0
  111. package/docs/handbook/en/components/file.md +29 -0
  112. package/docs/handbook/en/components/html.md +49 -0
  113. package/docs/handbook/en/components/interactive.md +41 -0
  114. package/docs/handbook/en/components/list.md +65 -0
  115. package/docs/handbook/en/components/relations.md +95 -0
  116. package/docs/handbook/en/components/stat.md +39 -0
  117. package/docs/handbook/en/components/timeline.md +37 -0
  118. package/docs/handbook/en/components.md +222 -0
  119. package/docs/handbook/en/config.md +127 -0
  120. package/docs/handbook/en/deploy.md +273 -0
  121. package/docs/handbook/en/design.md +88 -0
  122. package/docs/handbook/en/index.md +192 -0
  123. package/docs/handbook/en/reference/card.md +85 -0
  124. package/docs/handbook/en/reference/chart.md +68 -0
  125. package/docs/handbook/en/reference/choice.md +69 -0
  126. package/docs/handbook/en/reference/compare.md +59 -0
  127. package/docs/handbook/en/reference/figure.md +84 -0
  128. package/docs/handbook/en/reference/file/checklist.md +17 -0
  129. package/docs/handbook/en/reference/file.md +50 -0
  130. package/docs/handbook/en/reference/html.md +62 -0
  131. package/docs/handbook/en/reference/interactive.md +71 -0
  132. package/docs/handbook/en/reference/list.md +72 -0
  133. package/docs/handbook/en/reference/relations.md +113 -0
  134. package/docs/handbook/en/reference/stat.md +60 -0
  135. package/docs/handbook/en/reference/timeline.md +56 -0
  136. package/docs/handbook/en/reference.md +26 -0
  137. package/docs/handbook/en/start.md +92 -0
  138. package/docs/handbook/en/workflow.md +82 -0
  139. package/docs/handbook/en/writing.md +157 -0
  140. package/docs/handbook/index.md +99 -4
  141. package/docs/handbook/reference/card.md +85 -0
  142. package/docs/handbook/reference/chart.md +68 -0
  143. package/docs/handbook/reference/choice.md +69 -0
  144. package/docs/handbook/reference/compare.md +59 -0
  145. package/docs/handbook/reference/figure.md +84 -0
  146. package/docs/handbook/reference/file/checklist.md +17 -0
  147. package/docs/handbook/reference/file.md +50 -0
  148. package/docs/handbook/reference/html.md +62 -0
  149. package/docs/handbook/reference/interactive.md +71 -0
  150. package/docs/handbook/reference/list.md +72 -0
  151. package/docs/handbook/reference/relations.md +113 -0
  152. package/docs/handbook/reference/stat.md +60 -0
  153. package/docs/handbook/reference/timeline.md +56 -0
  154. package/docs/handbook/reference.md +26 -0
  155. package/docs/handbook/start.md +1 -1
  156. package/docs/handbook/workflow.md +6 -5
  157. package/docs/handbook/writing.md +21 -12
  158. package/next.config.ts +6 -0
  159. package/package.json +2 -1
  160. package/public/images/garden/ref-card-book.svg +1 -0
  161. package/public/images/garden/ref-card-lamp.svg +1 -0
  162. package/scripts/build-content.mjs +57 -16
  163. package/scripts/favicon-ico.mjs +9 -6
  164. package/scripts/file-preview.mjs +10 -7
  165. package/scripts/handbook-allowlist.mjs +18 -0
  166. package/scripts/handbook-facts.mjs +339 -11
  167. package/scripts/html-lang.mjs +36 -0
  168. package/scripts/link-assets.mjs +7 -0
  169. package/scripts/new-post.mjs +18 -9
  170. package/scripts/prepare-pages-output.mjs +30 -18
  171. package/scripts/redirects.mjs +17 -6
  172. package/scripts/static-404.mjs +25 -0
  173. package/scripts/ui-cli.mjs +14 -8
  174. package/templates/garden-blocks.md +67 -0
  175. package/templates/garden-snippets.plist +61 -0
  176. package/templates/site/Makefile.en +45 -0
  177. package/templates/site/README.en.md +15 -0
  178. package/templates/site/site.config.en.yaml +55 -0
  179. package/templates/site/site.config.yaml +2 -0
  180. package/templates/skills-en/garden-interactive-chart/SKILL.md +84 -0
  181. package/templates/skills-en/garden-interactive-chart/references/shell.html +49 -0
  182. package/templates/skills-en/garden-interactive-chart/references/style.md +61 -0
  183. package/templates/skills-en/garden-post-review/SKILL.md +63 -0
  184. package/templates/skills-en/garden-ui/SKILL.md +118 -0
  185. package/templates/workflows/pages.en.yml +195 -0
  186. package/templates/workflows/pages.yml +195 -0
  187. package/ui/core/apply.mjs +22 -4
  188. package/ui/core/cli-locale.mjs +67 -0
  189. package/ui/core/fence.mjs +18 -3
  190. package/ui/core/fix-prompt.mjs +18 -15
  191. package/ui/core/garden-fence.mjs +64 -0
  192. package/ui/core/scan.mjs +19 -3
  193. package/ui/garden/adjacent.mjs +21 -0
  194. package/ui/garden/catalog.mjs +205 -4
  195. package/ui/garden/check.mjs +12 -0
  196. package/ui/garden/file-block.mjs +8 -7
  197. package/ui/garden/html-lang.mjs +88 -0
  198. package/ui/garden/markdown-links.mjs +7 -4
  199. package/ui/garden/paging.mjs +30 -0
  200. package/ui/garden/parse.mjs +45 -0
  201. package/ui/garden/relations.mjs +252 -0
  202. package/ui/garden/search-index.mjs +6 -4
  203. package/ui/garden/source-icon.mjs +54 -0
  204. package/ui/parity-fixtures.mjs +77 -0
  205. package/ui/preview/dist/garden-ui.css +9 -9
  206. package/ui/preview/dist/garden-ui.esm.js +206 -169
  207. package/ui/preview/dist/garden-ui.iife.js +206 -169
  208. package/ui/prompt/rules.md +28 -1
  209. package/ui/react/components/chart.tsx +8 -5
  210. package/ui/react/components/display.tsx +7 -4
  211. package/ui/react/components/inputs.tsx +6 -3
  212. package/ui/react/components/layout.tsx +6 -4
  213. package/ui/react/components/media.tsx +7 -4
  214. package/ui/react/components/sandbox.tsx +3 -1
  215. package/ui/react/index.tsx +11 -7
  216. package/ui/react/lang.tsx +89 -0
  217. package/ui/react/parity.tsx +8 -6
  218. package/ui/react/runtime.tsx +3 -1
  219. package/ui/react/ui.css +9 -9
  220. package/worker/index.ts +14 -1
@@ -2,6 +2,7 @@
2
2
 
3
3
  import { useCallback, useEffect, useId, useRef, useState, type FocusEvent, type MouseEvent } from "react";
4
4
  import { createPortal } from "react-dom";
5
+ import { siteLang, ui, type UiLang } from "../../lib/i18n";
5
6
  import { previewBody, previewFrameProps } from "../../../ui/garden/file-block.mjs";
6
7
  import { ActionGlyph, FileGlyph } from "./file-icons";
7
8
 
@@ -25,7 +26,8 @@ type Loaded = { state: "idle" | "loading" | "error" } | { state: "ready"; html:
25
26
  * The control is a link: without JavaScript it opens the same preview (or the
26
27
  * file) in a new tab, next to the card's download link.
27
28
  */
28
- export function FilePreview({ file, compact }: { file: PreviewFile; compact: boolean }) {
29
+ export function FilePreview({ file, compact, lang }: { file: PreviewFile; compact: boolean; lang?: UiLang }) {
30
+ const t = ui(lang);
29
31
  const [open, setOpen] = useState(false);
30
32
  const [loaded, setLoaded] = useState<Loaded>({ state: "idle" });
31
33
  const trigger = useRef<HTMLAnchorElement>(null);
@@ -117,7 +119,7 @@ export function FilePreview({ file, compact }: { file: PreviewFile; compact: boo
117
119
  if (file.preview === "md" && loaded.state !== "ready" && loaded.state !== "loading") load();
118
120
  };
119
121
 
120
- const frame = previewFrameProps(file) as { src: string; title: string; referrerPolicy: "no-referrer"; sandbox?: string } | null;
122
+ const frame = previewFrameProps(file, lang ?? siteLang) as { src: string; title: string; referrerPolicy: "no-referrer"; sandbox?: string } | null;
121
123
  const drawer = open
122
124
  ? createPortal(
123
125
  <div className="gf-layer">
@@ -130,13 +132,13 @@ export function FilePreview({ file, compact }: { file: PreviewFile; compact: boo
130
132
  {file.meta ? <div className="gf-meta">{file.meta}</div> : null}
131
133
  </div>
132
134
  <div className="gf-act">
133
- <a className="gf-btn icon" href={file.url} download={file.name} aria-label={`下载 ${file.name}`} title="下载">
135
+ <a className="gf-btn icon" href={file.url} download={file.name} aria-label={t.downloadNamed(file.name)} title={t.download}>
134
136
  <ActionGlyph name="download" />
135
137
  </a>
136
- <a className="gf-btn icon" href={file.url} target="_blank" rel="noopener noreferrer" aria-label={`在新标签页打开 ${file.name}`} title="在新标签页打开">
138
+ <a className="gf-btn icon" href={file.url} target="_blank" rel="noopener noreferrer" aria-label={t.openNamedInTab(file.name)} title={t.openInTab}>
137
139
  <ActionGlyph name="external" />
138
140
  </a>
139
- <button ref={closeButton} type="button" className="gf-btn icon" onClick={close} aria-label="关闭预览" title="关闭">
141
+ <button ref={closeButton} type="button" className="gf-btn icon" onClick={close} aria-label={t.closePreview} title={ui(lang).close.trim()}>
140
142
  <ActionGlyph name="close" />
141
143
  </button>
142
144
  </div>
@@ -147,14 +149,14 @@ export function FilePreview({ file, compact }: { file: PreviewFile; compact: boo
147
149
  <div className="markdown-body prose gf-doc" dangerouslySetInnerHTML={{ __html: loaded.html }} />
148
150
  ) : loaded.state === "error" ? (
149
151
  <div className="gf-state" role="alert">
150
- <p>预览没能载入。</p>
152
+ <p>{t.previewFailed}</p>
151
153
  <div className="gf-state-act">
152
- <button type="button" className="gf-btn" onClick={() => load()}>重试</button>
153
- <a className="gf-btn" href={file.url} download={file.name}>下载文件</a>
154
+ <button type="button" className="gf-btn" onClick={() => load()}>{t.retry}</button>
155
+ <a className="gf-btn" href={file.url} download={file.name}>{t.downloadFile}</a>
154
156
  </div>
155
157
  </div>
156
158
  ) : (
157
- <div className="gf-state" role="status">正在载入预览…</div>
159
+ <div className="gf-state" role="status">{t.previewLoading}</div>
158
160
  )}
159
161
  </div>
160
162
  ) : (
@@ -165,7 +167,7 @@ export function FilePreview({ file, compact }: { file: PreviewFile; compact: boo
165
167
  <span tabIndex={0} className="gf-sentinel" data-gf-sentinel="after" onFocus={fromAfter} />
166
168
  </div>
167
169
  )}
168
- <p className="gf-hint">Esc 关闭</p>
170
+ <p className="gf-hint">{t.escClose}</p>
169
171
  </aside>
170
172
  </div>,
171
173
  document.body,
@@ -183,10 +185,10 @@ export function FilePreview({ file, compact }: { file: PreviewFile; compact: boo
183
185
  data-compact={compact ? "true" : undefined}
184
186
  onClick={openDrawer}
185
187
  aria-haspopup="dialog"
186
- aria-label={`预览 ${file.name}`}
188
+ aria-label={t.previewNamed(file.name)}
187
189
  >
188
190
  <ActionGlyph name="eye" />
189
- <span className="gf-lb">预览</span>
191
+ <span className="gf-lb">{t.preview}</span>
190
192
  </a>
191
193
  {drawer}
192
194
  </>
@@ -1,5 +1,6 @@
1
1
  "use client";
2
2
 
3
+ import { useUiLang } from "../UiLang";
3
4
  import { readGardenPayload } from "../../lib/remark-garden.mjs";
4
5
  import {
5
6
  GardenCard,
@@ -15,8 +16,10 @@ import {
15
16
  GardenUnknown,
16
17
  } from "./blocks";
17
18
  import { GardenFile } from "./GardenFile";
19
+ import { GardenChoice, GardenList, GardenRelations } from "./patterns";
18
20
 
19
21
  export function GardenBlock({ dataPayload }: { dataPayload?: string }) {
22
+ const lang = useUiLang();
20
23
  const block = readGardenPayload(dataPayload);
21
24
  if (!block) return null;
22
25
  if (block.legacy && block.suggestion) return <GardenLegacy suggestion={String(block.suggestion)} />;
@@ -42,7 +45,13 @@ export function GardenBlock({ dataPayload }: { dataPayload?: string }) {
42
45
  return <GardenInteractive block={{ id: block.id, props, children: block.children }} />;
43
46
  case "file":
44
47
  // Post bodies render file cards on the server (MarkdownArticle); this path has no Markdown preview.
45
- return <GardenFile props={props} />;
48
+ return <GardenFile props={props} lang={lang} />;
49
+ case "relations":
50
+ return <GardenRelations props={props} />;
51
+ case "list":
52
+ return <GardenList props={props} />;
53
+ case "choice":
54
+ return <GardenChoice props={props} />;
46
55
  default:
47
56
  return <GardenUnknown name={String(block.name)} />;
48
57
  }
@@ -1,4 +1,5 @@
1
1
  // Server component: the card for `garden file`. Only the 预览 control is a client island.
2
+ import { siteLang, ui, type UiLang } from "../../lib/i18n";
2
3
  import { blockFiles, fileMeta } from "../../../ui/garden/file-block.mjs";
3
4
  import { ActionGlyph, FileGlyph, safeFileHref } from "./file-icons";
4
5
  import { FilePreview, type PreviewFile } from "./FilePreview";
@@ -19,8 +20,9 @@ type FileEntry = {
19
20
 
20
21
  const PREVIEWS = new Set(["md", "html", "pdf"]);
21
22
 
22
- export function GardenFile({ props }: { props: Record<string, unknown> }) {
23
- const block = blockFiles(props) as { layout?: string; title?: string; files: FileEntry[] };
23
+ export function GardenFile({ props, lang }: { props: Record<string, unknown>; lang?: UiLang }) {
24
+ const t = ui(lang);
25
+ const block = blockFiles(props, lang ?? siteLang) as { layout?: string; title?: string; files: FileEntry[] };
24
26
  const files = block.files.filter((file) => safeFileHref(file.url));
25
27
  if (!files.length) return null;
26
28
  const group = block.layout === "group";
@@ -29,7 +31,7 @@ export function GardenFile({ props }: { props: Record<string, unknown> }) {
29
31
  {group && block.title ? <div className="gf-title">{block.title}</div> : null}
30
32
  {files.map((file, index) => {
31
33
  const href = safeFileHref(file.url);
32
- const name = file.name || file.title || "文件";
34
+ const name = file.name || file.title || t.file;
33
35
  const title = file.title || name;
34
36
  const meta = fileMeta(file);
35
37
  const previewSrc = file.preview === "md" ? safeFileHref(file.previewSrc) : "";
@@ -44,20 +46,20 @@ export function GardenFile({ props }: { props: Record<string, unknown> }) {
44
46
  {meta ? <div className="gf-meta">{meta}</div> : null}
45
47
  </div>
46
48
  <div className="gf-act">
47
- {previewFile ? <FilePreview file={previewFile} compact={group} /> : null}
49
+ {previewFile ? <FilePreview file={previewFile} compact={group} lang={lang} /> : null}
48
50
  {file.remote ? (
49
- <a className="gf-btn icon" href={href} target="_blank" rel="noopener noreferrer" aria-label={`在新标签页打开 ${name}`} title="在新标签页打开">
51
+ <a className="gf-btn icon" href={href} target="_blank" rel="noopener noreferrer" aria-label={t.openNamedInTab(name)} title={t.openInTab}>
50
52
  <ActionGlyph name="external" />
51
53
  </a>
52
54
  ) : null}
53
55
  {group ? (
54
- <a className="gf-btn icon" href={href} download={file.remote ? undefined : name} aria-label={`下载 ${name}`} title="下载">
56
+ <a className="gf-btn icon" href={href} download={file.remote ? undefined : name} aria-label={t.downloadNamed(name)} title={t.download}>
55
57
  <ActionGlyph name="download" />
56
58
  </a>
57
59
  ) : (
58
- <a className="gf-btn pri" href={href} download={file.remote ? undefined : name} aria-label={`下载 ${name}`}>
60
+ <a className="gf-btn pri" href={href} download={file.remote ? undefined : name} aria-label={t.downloadNamed(name)}>
59
61
  <ActionGlyph name="download" />
60
- <span className="gf-lb">下载</span>
62
+ <span className="gf-lb">{t.download}</span>
61
63
  </a>
62
64
  )}
63
65
  </div>
@@ -0,0 +1,62 @@
1
+ "use client";
2
+
3
+ import { Children, isValidElement, useEffect, useRef, useState, type ReactNode } from "react";
4
+
5
+ const LABELS = {
6
+ zh: { preview: "效果", code: "代码", tabs: "示例视图" },
7
+ en: { preview: "Preview", code: "Code", tabs: "Example view" },
8
+ } as const;
9
+
10
+ // Examples on screen right now; while any is visible on a narrow screen the
11
+ // floating contents button steps aside so it never covers code.
12
+ const visible = new Set<Element>();
13
+ const sync = () => document.documentElement.classList.toggle("ref-demo-in", visible.size > 0);
14
+
15
+ /** Code on the left and the real block on the right; tabs below 1024px. */
16
+ export function RefDemo({ children, lang = "zh", name }: { children?: ReactNode; lang?: "zh" | "en"; name?: string }) {
17
+ const t = LABELS[lang];
18
+ const [tab, setTab] = useState<"v" | "c">("v");
19
+ const ref = useRef<HTMLDivElement>(null);
20
+ const [code, preview] = Children.toArray(children).filter(isValidElement);
21
+
22
+ useEffect(() => {
23
+ const el = ref.current;
24
+ if (!el || typeof IntersectionObserver === "undefined") return;
25
+ const io = new IntersectionObserver((entries) => {
26
+ for (const entry of entries) {
27
+ if (entry.isIntersecting) visible.add(entry.target);
28
+ else visible.delete(entry.target);
29
+ }
30
+ sync();
31
+ });
32
+ io.observe(el);
33
+ return () => {
34
+ io.disconnect();
35
+ visible.delete(el);
36
+ sync();
37
+ };
38
+ }, []);
39
+
40
+ const tabButton = (id: "v" | "c", label: string) => (
41
+ <button type="button" role="tab" aria-selected={tab === id} data-t={id} onClick={() => setTab(id)}>
42
+ {label}
43
+ </button>
44
+ );
45
+
46
+ return (
47
+ <div className="ref-demo" ref={ref} data-tab={tab} data-name={name}>
48
+ <div className="ref-tabs" role="tablist" aria-label={t.tabs}>
49
+ {tabButton("v", t.preview)}
50
+ {tabButton("c", t.code)}
51
+ </div>
52
+ <div className="ref-pane" data-p="c" data-off={tab !== "c" ? "" : undefined}>
53
+ <p className="ref-pane-h">{t.code}</p>
54
+ {code}
55
+ </div>
56
+ <div className="ref-pane" data-p="v" data-off={tab !== "v" ? "" : undefined}>
57
+ <p className="ref-pane-h">{t.preview}</p>
58
+ {preview}
59
+ </div>
60
+ </div>
61
+ );
62
+ }
@@ -2,12 +2,23 @@
2
2
 
3
3
  import { useEffect, useId, useRef, useState, type ReactNode } from "react";
4
4
  import ReactMarkdown from "react-markdown";
5
- import { stringify } from "yaml";
5
+ import { useUi, useUiLang } from "../UiLang";
6
+ import { langOf, ui } from "../../lib/i18n";
7
+ import { interactiveUiSource } from "../../../ui/core/garden-fence.mjs";
6
8
  import { markdownPlugins } from "../../lib/markdown-plugins";
7
9
  import { compileUiBlock } from "../../../ui/core/compile.mjs";
8
10
  import { splitCompareLine } from "../../../ui/garden/compare-line.mjs";
9
- import { UiErrorCard, UiFigure, type Diagnostic, type UiPayload } from "../../../ui/react";
11
+ import { RuntimeLangProvider, UiErrorCard, UiFigure, type Diagnostic, type UiPayload } from "../../../ui/react";
10
12
  import { FullscreenFrame, SandboxFrame } from "./chrome";
13
+ import { SourceMark, VerdictIcon } from "./patterns";
14
+
15
+ const BASE_PATH = (process.env.NEXT_PUBLIC_BASE_PATH ?? "").replace(/\/+$/u, "");
16
+
17
+ /** A `/public` path (`/images/a.svg`) gets the site's basePath in front. */
18
+ function publicSrc(src: string) {
19
+ if (!BASE_PATH || !src.startsWith("/") || src.startsWith("//") || src.startsWith(`${BASE_PATH}/`)) return src;
20
+ return `${BASE_PATH}${src}`;
21
+ }
11
22
 
12
23
  function Rich({ text }: { text: string }) {
13
24
  return (
@@ -31,15 +42,16 @@ function SourceChip({ source }: { source: unknown }) {
31
42
  if (item && typeof item === "object" && "url" in item) {
32
43
  const url = String((item as { url?: unknown }).url ?? "");
33
44
  const site = String((item as { site?: unknown }).site ?? hostOf(url));
34
- return { url, site };
45
+ const icon = (item as { icon?: unknown }).icon;
46
+ return { url, site, icon: typeof icon === "string" ? icon : undefined };
35
47
  }
36
48
  return null;
37
- }).filter((item): item is { url: string; site: string } => Boolean(item?.url));
49
+ }).filter((item): item is { url: string; site: string; icon?: string } => Boolean(item?.url));
38
50
  if (!list.length) return null;
39
51
  return (
40
- <span className="g-srcs">
52
+ <span className={list.length > 1 ? "g-srcs is-multi" : "g-srcs"}>
41
53
  <a className="g-src ext" href={list[0].url} target="_blank" rel="noopener noreferrer">
42
- <span className="g-src-ic" aria-hidden="true">{[...list[0].site][0] || "·"}</span>
54
+ <SourceMark href={list[0].url} icon={list[0].icon} />
43
55
  <span>{list[0].site}</span>
44
56
  </a>
45
57
  {list.length > 1 ? <span className="g-src-more">+{list.length - 1}</span> : null}
@@ -52,22 +64,36 @@ function itemsOf(props: Record<string, unknown>) {
52
64
  }
53
65
 
54
66
  export function GardenCard({ props }: { props: Record<string, unknown> }) {
67
+ if (Array.isArray(props.cards)) {
68
+ return (
69
+ <div className="g-entities">
70
+ {props.cards.map((card, index) => (
71
+ <GardenCardItem key={index} props={(card ?? {}) as Record<string, unknown>} row />
72
+ ))}
73
+ </div>
74
+ );
75
+ }
76
+ return <GardenCardItem props={props} />;
77
+ }
78
+
79
+ function GardenCardItem({ props, row = false }: { props: Record<string, unknown>; row?: boolean }) {
55
80
  const url = typeof props.url === "string" ? props.url : "";
56
81
  const title = String(props.title ?? url);
57
82
  const description = typeof props.description === "string" ? props.description : typeof props.text === "string" ? props.text : "";
58
83
  const points = Array.isArray(props.points) ? props.points : Array.isArray(props.items) ? props.items : [];
59
84
  return (
60
- <article className="g-entity">
85
+ <article className={`g-entity${row ? " g-entity-row" : ""}`}>
61
86
  {typeof props.image === "string" ? (
62
87
  <div className="g-entity-img">
63
88
  {/* eslint-disable-next-line @next/next/no-img-element */}
64
- <img src={props.image} alt={String(props.alt ?? title)} />
89
+ <img src={publicSrc(props.image)} alt={String(props.alt ?? title)} />
65
90
  </div>
66
91
  ) : null}
67
92
  <div className="g-entity-b">
68
93
  <h4 className="g-entity-t">{url ? <a className="ext" href={url} target="_blank" rel="noopener noreferrer"><Rich text={title} /></a> : <Rich text={title} />}</h4>
69
94
  {typeof props.meta === "string" ? <p className="g-entity-m"><Rich text={props.meta} /></p> : null}
70
95
  {description ? <p><Rich text={description} /> <SourceChip source={props.source} /></p> : null}
96
+ {typeof props.takeaway === "string" ? <p className="g-entity-k"><Rich text={props.takeaway} /></p> : null}
71
97
  </div>
72
98
  {points.length ? (
73
99
  <ol className="g-entity-list">
@@ -117,8 +143,31 @@ function CompareLine({ line }: { line: string }) {
117
143
  );
118
144
  }
119
145
 
146
+ function verdictOf(entry: unknown) {
147
+ if (!entry || typeof entry !== "object") return "";
148
+ const value = String((entry as Record<string, unknown>).verdict ?? "").trim().toLowerCase();
149
+ return value === "good" || value === "bad" ? value : "";
150
+ }
151
+
120
152
  export function GardenCompare({ props }: { props: Record<string, unknown> }) {
121
153
  const items = Array.isArray(props.items) ? props.items as unknown[] : [];
154
+ if (items.some((entry) => verdictOf(entry))) {
155
+ return (
156
+ <div className="g-verdicts" role="group">
157
+ {items.map((entry, index) => {
158
+ const item = (entry && typeof entry === "object" ? entry : { text: String(entry ?? "") }) as Record<string, unknown>;
159
+ const verdict = verdictOf(item);
160
+ const texts = item.text == null ? [] : Array.isArray(item.text) ? item.text : [item.text];
161
+ return (
162
+ <section className={`g-verdict${verdict ? ` is-${verdict}` : ""}`} key={index}>
163
+ <h4><VerdictIcon verdict={verdict} /><Rich text={String(item.title ?? "")} /></h4>
164
+ {texts.map((paragraph, paragraphIndex) => <p key={paragraphIndex}><Rich text={String(paragraph)} /></p>)}
165
+ </section>
166
+ );
167
+ })}
168
+ </div>
169
+ );
170
+ }
122
171
  return (
123
172
  <div className="g-compare" role="group">
124
173
  {items.map((entry, index) => {
@@ -126,7 +175,8 @@ export function GardenCompare({ props }: { props: Record<string, unknown> }) {
126
175
  return <section className="g-cmp" key={index}><CompareLine line={String(entry)} /></section>;
127
176
  }
128
177
  if (!entry || typeof entry !== "object") return null;
129
- const { title, text, ...rest } = entry as Record<string, unknown>;
178
+ const { title, text, verdict: _verdict, ...rest } = entry as Record<string, unknown>;
179
+ void _verdict;
130
180
  const texts = text == null ? [] : Array.isArray(text) ? text : [text];
131
181
  return (
132
182
  <section className="g-cmp" key={index}>
@@ -187,7 +237,8 @@ function chartRows(props: Record<string, unknown>) {
187
237
  export function GardenChart({ props }: { props: Record<string, unknown> }) {
188
238
  const rows = chartRows(props);
189
239
  const max = Math.max(...rows.map((row) => row.y), 1);
190
- const title = String(props.title ?? "图表");
240
+ const t = useUi();
241
+ const title = String(props.title ?? t.chart);
191
242
  return (
192
243
  <figure className="g-chart" role="group" aria-label={title}>
193
244
  {props.title ? <p className="g-chart-t">{title}</p> : null}
@@ -201,9 +252,9 @@ export function GardenChart({ props }: { props: Record<string, unknown> }) {
201
252
  ))}
202
253
  </div>
203
254
  <details className="g-chart-data">
204
- <summary>查看数据表</summary>
255
+ <summary>{t.dataTable}</summary>
205
256
  <table>
206
- <thead><tr><th>{String(props.xLabel ?? (typeof props.x === "string" ? props.x : "项目"))}</th><th>{String(props.yLabel ?? "数值")}</th></tr></thead>
257
+ <thead><tr><th>{String(props.xLabel ?? (typeof props.x === "string" ? props.x : t.item))}</th><th>{String(props.yLabel ?? t.value)}</th></tr></thead>
207
258
  <tbody>
208
259
  {rows.map((row) => <tr key={row.x}><td>{row.x}</td><td>{row.label}</td></tr>)}
209
260
  </tbody>
@@ -216,11 +267,11 @@ export function GardenChart({ props }: { props: Record<string, unknown> }) {
216
267
 
217
268
  type Part = { id: string; label: string; text: string };
218
269
 
219
- function partsOf(props: Record<string, unknown>): Part[] {
270
+ function partsOf(props: Record<string, unknown>, partLabel: (n: number) => string): Part[] {
220
271
  if (!Array.isArray(props.parts)) return [];
221
272
  return props.parts.map((part, index) => {
222
273
  const item = part as Record<string, unknown>;
223
- const label = String(item.label ?? item.name ?? `部分 ${index + 1}`);
274
+ const label = String(item.label ?? item.name ?? partLabel(index + 1));
224
275
  return { id: String(item.id ?? item.name ?? label), label, text: String(item.text ?? "") };
225
276
  });
226
277
  }
@@ -240,7 +291,8 @@ function partOn(selected: string, token: string) {
240
291
  }
241
292
 
242
293
  export function GardenFigure({ props, children }: { props: Record<string, unknown>; children?: string }) {
243
- const parts = partsOf(props);
294
+ const t = useUi();
295
+ const parts = partsOf(props, t.part);
244
296
  const [selected, setSelected] = useState(parts[0]?.id ?? "all");
245
297
  const svgRef = useRef<HTMLDivElement>(null);
246
298
  const base = useId();
@@ -286,7 +338,7 @@ export function GardenFigure({ props, children }: { props: Record<string, unknow
286
338
  const body = (
287
339
  <figure className="g-explore" data-g="explore" data-sel={selected} ref={svgRef}>
288
340
  {parts.length ? (
289
- <div role="tablist" aria-label="选择一个部分">
341
+ <div role="tablist" aria-label={t.pickPart}>
290
342
  {parts.map((part, index) => (
291
343
  <button
292
344
  key={part.id}
@@ -312,7 +364,7 @@ export function GardenFigure({ props, children }: { props: Record<string, unknow
312
364
  {typeof props.image === "string" ? (
313
365
  <div className="fig-card">
314
366
  {/* eslint-disable-next-line @next/next/no-img-element */}
315
- <img src={props.image} alt={String(props.alt ?? props.caption ?? "")} />
367
+ <img src={publicSrc(props.image)} alt={String(props.alt ?? props.caption ?? "")} />
316
368
  </div>
317
369
  ) : null}
318
370
  {typeof props.mermaid === "string" ? <pre className="code"><code>{props.mermaid}</code></pre> : null}
@@ -324,7 +376,7 @@ export function GardenFigure({ props, children }: { props: Record<string, unknow
324
376
  {typeof props.caption === "string" ? <figcaption><Rich text={props.caption} /></figcaption> : null}
325
377
  </figure>
326
378
  );
327
- return parts.length || svg ? <FullscreenFrame label="结构图">{body}</FullscreenFrame> : body;
379
+ return parts.length || svg ? <FullscreenFrame label={t.diagram}>{body}</FullscreenFrame> : body;
328
380
  }
329
381
 
330
382
  function htmlHasScript(source: string) {
@@ -336,6 +388,8 @@ function isLightWidget(source: string) {
336
388
  }
337
389
 
338
390
  function bindGardenWidgets(root: HTMLElement) {
391
+ const attr = root.closest("[lang]")?.getAttribute("lang");
392
+ const t = ui(attr ? langOf(attr) : undefined);
339
393
  root.querySelectorAll<HTMLElement>("[data-g=tabs]").forEach((widget) => {
340
394
  const list = widget.querySelector<HTMLElement>(":scope > [role=tablist]");
341
395
  const tabs = [...(list?.querySelectorAll<HTMLElement>(":scope > [role=tab]") ?? [])];
@@ -373,12 +427,12 @@ function bindGardenWidgets(root: HTMLElement) {
373
427
  const chosen = form.querySelector<HTMLInputElement>("input:checked");
374
428
  if (!chosen) {
375
429
  out.className = "g-quiz-out";
376
- out.textContent = "先选一个答案。";
430
+ out.textContent = t.pickAnswer;
377
431
  return;
378
432
  }
379
433
  const correct = chosen.value === form.dataset.answer;
380
434
  out.className = `g-quiz-out ${correct ? "ok" : "no"}`;
381
- out.textContent = correct ? "✓ 答对了" : "✗ 再想想";
435
+ out.textContent = correct ? t.correct : t.wrong;
382
436
  if (explain) explain.hidden = !correct;
383
437
  });
384
438
  });
@@ -446,7 +500,7 @@ function bindGardenWidgets(root: HTMLElement) {
446
500
  const equation = calc.querySelector("[data-calc]");
447
501
  if (equation && spec.show) {
448
502
  const shown = spec.show.replace(/\{(\w+)\}/g, (_, key: string) => String(values[key]));
449
- equation.textContent = `${shown} = ${spec.format === "GB" ? `${Math.round(result).toLocaleString("en-US")} 字节` : String(Math.round(result * 1e4) / 1e4)}`;
503
+ equation.textContent = `${shown} = ${spec.format === "GB" ? `${Math.round(result).toLocaleString("en-US")} ${t.bytes}` : String(Math.round(result * 1e4) / 1e4)}`;
450
504
  }
451
505
  };
452
506
  calc.addEventListener("input", run);
@@ -484,9 +538,10 @@ function LightGardenHtml({ code }: { code: string }) {
484
538
 
485
539
  export function GardenHtml({ props, children }: { props: Record<string, unknown>; children?: string }) {
486
540
  const code = typeof props.code === "string" ? props.code : children || "";
541
+ const t = useUi();
487
542
  const title = String(props.title ?? "HTML");
488
543
  if (!code && typeof props.src === "string") {
489
- return <p className="g-embed-note">HTML 在 {props.src}。发布时由同名目录读入。</p>;
544
+ return <p className="g-embed-note">{t.htmlAt(String(props.src))}</p>;
490
545
  }
491
546
  if (htmlHasScript(code)) {
492
547
  return <SandboxFrame html={code} title={title} />;
@@ -499,36 +554,38 @@ export function GardenHtml({ props, children }: { props: Record<string, unknown>
499
554
  }
500
555
 
501
556
  export function GardenInteractive({ block }: { block: { id?: string | null; props: Record<string, unknown>; children?: string } }) {
502
- const header = {
503
- id: block.id || block.props.id || "interactive",
504
- caption: block.props.caption ?? "",
505
- intent: block.props.intent ?? "",
506
- ...(block.props.expect ? { expect: block.props.expect } : {}),
507
- };
508
- const source = `---\n${stringify(header)}---\n${block.children ?? ""}`;
557
+ const t = useUi();
558
+ const lang = useUiLang();
559
+ // Same source `garden ui check` validates (ui/core/garden-fence.mjs).
560
+ const { source, header } = interactiveUiSource({ id: block.id ?? null, props: block.props, children: block.children ?? "" });
509
561
  const { payload, check } = compileUiBlock(source);
510
562
  if (!payload) {
511
563
  return (
564
+ <RuntimeLangProvider lang={lang}>
512
565
  <UiErrorCard
513
566
  id={String(header.id)}
514
567
  caption={String(header.caption)}
515
568
  diagnostics={(check.diagnostics ?? []) as Diagnostic[]}
516
569
  />
570
+ </RuntimeLangProvider>
517
571
  );
518
572
  }
519
573
  return (
520
- <FullscreenFrame label={String(header.caption || "交互")}>
574
+ <RuntimeLangProvider lang={lang}>
575
+ <FullscreenFrame label={String(header.caption || t.interactive)}>
521
576
  <div className="g-play">
522
577
  <UiFigure payload={payload as UiPayload} />
523
578
  </div>
524
579
  </FullscreenFrame>
580
+ </RuntimeLangProvider>
525
581
  );
526
582
  }
527
583
 
528
584
  export function GardenIdea({ text }: { text: string }) {
585
+ const t = useUi();
529
586
  return (
530
587
  <aside className="g-idea">
531
- <p className="g-example-label">想法 · 发布前编译</p>
588
+ <p className="g-example-label">{t.ideaLabel}</p>
532
589
  <p>{text}</p>
533
590
  </aside>
534
591
  );
@@ -539,7 +596,8 @@ export function GardenLegacy({ suggestion }: { suggestion: string }) {
539
596
  }
540
597
 
541
598
  export function GardenUnknown({ name }: { name: string }) {
542
- return <p className="g-embed-note">未知 Garden 块:{name}</p>;
599
+ const t = useUi();
600
+ return <p className="g-embed-note">{t.unknownBlock(name)}</p>;
543
601
  }
544
602
 
545
603
  export function GardenMessage({ children }: { children: ReactNode }) {
@@ -1,5 +1,7 @@
1
1
  "use client";
2
2
 
3
+ import { useUi } from "../UiLang";
4
+
3
5
  import {
4
6
  useEffect,
5
7
  useId,
@@ -47,7 +49,9 @@ function writeClipboard(text: string) {
47
49
  });
48
50
  }
49
51
 
50
- export function CopyButton({ getText, label = "复制代码" }: { getText: () => string; label?: string }) {
52
+ export function CopyButton({ getText, label }: { getText: () => string; label?: string }) {
53
+ const t = useUi();
54
+ label = label ?? t.copyCode;
51
55
  const [done, setDone] = useState(false);
52
56
  const timer = useRef<number>(0);
53
57
 
@@ -56,8 +60,8 @@ export function CopyButton({ getText, label = "复制代码" }: { getText: () =>
56
60
  <button
57
61
  type="button"
58
62
  className={`ux-btn ux-copy${done ? " ux-done" : ""}`}
59
- aria-label={done ? "已复制" : label}
60
- title={done ? "已复制" : label}
63
+ aria-label={done ? t.copied : label}
64
+ title={done ? t.copied : label}
61
65
  onClick={() => {
62
66
  const text = getText();
63
67
  writeClipboard(text).then(() => {
@@ -67,9 +71,9 @@ export function CopyButton({ getText, label = "复制代码" }: { getText: () =>
67
71
  }).catch(() => setDone(false));
68
72
  }}
69
73
  >
70
- {done ? "已复制" : COPY_ICON}
74
+ {done ? t.copied : COPY_ICON}
71
75
  </button>
72
- <span className="ux-live" aria-live="polite">{done ? "已复制到剪贴板" : ""}</span>
76
+ <span className="ux-live" aria-live="polite">{done ? t.copiedLive : ""}</span>
73
77
  </>
74
78
  );
75
79
  }
@@ -88,12 +92,12 @@ export function CodeCopy({ children }: { children: ReactNode }) {
88
92
  );
89
93
  }
90
94
 
91
- function diffLines(source: string) {
95
+ function diffLines(source: string, t: ReturnType<typeof useUi>) {
92
96
  return source.replace(/\n$/u, "").split("\n").map((line) => {
93
97
  const kind = line.startsWith("+") ? "add" : line.startsWith("-") ? "del" : "ctx";
94
98
  const body = kind === "ctx" ? line.replace(/^ /u, "") : line.slice(1);
95
99
  const mark = kind === "add" ? "+" : kind === "del" ? "−" : " ";
96
- const spoken = kind === "add" ? "(新增)" : kind === "del" ? "(删除)" : "";
100
+ const spoken = kind === "add" ? t.diffAdded : kind === "del" ? t.diffRemoved : "";
97
101
  return { kind, body, mark, spoken };
98
102
  });
99
103
  }
@@ -109,11 +113,12 @@ export function CodeDiff({
109
113
  }) {
110
114
  const [tab, setTab] = useState(0);
111
115
  const base = useId();
112
- const tabs = ["代码", "对比上一版"];
116
+ const t = useUi();
117
+ const tabs = t.codeTabs;
113
118
 
114
119
  return (
115
120
  <div className="g-tabs g-code-group g-codediff" data-g="tabs">
116
- <div role="tablist" aria-label="代码">
121
+ <div role="tablist" aria-label={t.codeTabsLabel}>
117
122
  {tabs.map((label, index) => (
118
123
  <button
119
124
  key={label}
@@ -146,7 +151,7 @@ export function CodeDiff({
146
151
  <div role="tabpanel" id={`${base}-p1`} aria-labelledby={`${base}-t1`} hidden={tab !== 1}>
147
152
  <pre className="code g-diff">
148
153
  <code>
149
- {diffLines(diff).map((line, index) => (
154
+ {diffLines(diff, t).map((line, index) => (
150
155
  <span className={`d-${line.kind}`} key={index}>
151
156
  <span className="d-m" aria-hidden="true">{line.mark}</span>
152
157
  {line.body}
@@ -164,7 +169,9 @@ function fullscreenElement() {
164
169
  return document.fullscreenElement || (document as Document & { webkitFullscreenElement?: Element }).webkitFullscreenElement || null;
165
170
  }
166
171
 
167
- export function FullscreenFrame({ children, label = "演示" }: { children: ReactNode; label?: string }) {
172
+ export function FullscreenFrame({ children, label }: { children: ReactNode; label?: string }) {
173
+ const t = useUi();
174
+ label = label ?? t.demo;
168
175
  const ref = useRef<HTMLDivElement>(null);
169
176
  const buttonRef = useRef<HTMLButtonElement>(null);
170
177
  const restoreRef = useRef<HTMLElement | null>(null);
@@ -255,8 +262,8 @@ export function FullscreenFrame({ children, label = "演示" }: { children: Reac
255
262
  ref={buttonRef}
256
263
  type="button"
257
264
  className="ux-btn ux-fsb"
258
- aria-label={active ? "退出全屏" : "全屏"}
259
- title={active ? "退出全屏" : "全屏"}
265
+ aria-label={active ? t.exitFullscreen : t.fullscreen}
266
+ title={active ? t.exitFullscreen : t.fullscreen}
260
267
  onClick={() => (active ? close() : open())}
261
268
  >
262
269
  {active ? EXIT_ICON : ENTER_ICON}