@gmickel/gno 2.14.0 → 2.16.0

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 (205) hide show
  1. package/README.md +1 -1
  2. package/assets/skill/cli-reference.md +4 -2
  3. package/assets/spa-production.json.gz +0 -0
  4. package/browser-extension/artifacts/{gno-browser-clipper-v2.14.0.zip → gno-browser-clipper-v2.16.0.zip} +0 -0
  5. package/browser-extension/artifacts/gno-browser-clipper-v2.16.0.zip.sha256 +1 -0
  6. package/browser-extension/dist/manifest.json +1 -1
  7. package/package.json +1 -1
  8. package/spec/cli.md +88 -45
  9. package/spec/db/schema.sql +45 -0
  10. package/spec/output-schemas/embed-receipt.schema.json +89 -0
  11. package/src/cli/commands/agents/commands.ts +1 -1
  12. package/src/cli/commands/ask.ts +1 -1
  13. package/src/cli/commands/context-build.ts +1 -1
  14. package/src/cli/commands/embed.ts +341 -20
  15. package/src/cli/commands/links.ts +1 -1
  16. package/src/cli/commands/mcp/install.ts +2 -2
  17. package/src/cli/commands/mcp/status.ts +1 -1
  18. package/src/cli/commands/mcp/uninstall.ts +1 -1
  19. package/src/cli/commands/query.ts +1 -1
  20. package/src/cli/commands/shared.ts +4 -4
  21. package/src/cli/commands/skill/install.ts +1 -1
  22. package/src/cli/commands/skill/paths-cmd.ts +1 -1
  23. package/src/cli/commands/skill/uninstall.ts +1 -1
  24. package/src/cli/commands/status.ts +75 -58
  25. package/src/cli/config-cache.ts +86 -0
  26. package/src/cli/globals.ts +64 -0
  27. package/src/cli/options.ts +23 -0
  28. package/src/cli/program.ts +44 -37
  29. package/src/cli/run.ts +121 -67
  30. package/src/cli/runtime-health.ts +132 -122
  31. package/src/cli/session-binding.ts +11 -2
  32. package/src/config/saver.ts +8 -0
  33. package/src/converters/adapters/browser-export/formats.ts +25 -15
  34. package/src/converters/adapters/email/adapter.ts +37 -53
  35. package/src/converters/adapters/email/html.ts +37 -20
  36. package/src/converters/adapters/email/mime.ts +71 -39
  37. package/src/converters/adapters/ical/adapter.ts +20 -12
  38. package/src/converters/adapters/jsonl/adapter.ts +8 -6
  39. package/src/converters/adapters/shared/html-text.ts +55 -37
  40. package/src/converters/adapters/shared/ooxml-protection.ts +5 -13
  41. package/src/converters/adapters/shared/utf8-lines.ts +69 -31
  42. package/src/converters/adapters/transcript/adapter.ts +12 -7
  43. package/src/converters/adapters/xlsx/adapter.ts +10 -7
  44. package/src/converters/adapters/xlsx/sheet-markdown.ts +93 -50
  45. package/src/converters/canonicalize.ts +17 -3
  46. package/src/converters/native/markdown.ts +24 -17
  47. package/src/converters/native/plaintext.ts +5 -6
  48. package/src/converters/registry.ts +130 -8
  49. package/src/core/activation-status.ts +4 -8
  50. package/src/core/change-diff.ts +38 -11
  51. package/src/core/context-compiler.ts +17 -18
  52. package/src/core/context-evidence.ts +116 -25
  53. package/src/core/context-verifier.ts +70 -31
  54. package/src/core/file-lock.ts +10 -2
  55. package/src/core/graph-analysis.ts +3 -3
  56. package/src/core/link-inventory-markdown.ts +2 -2
  57. package/src/core/links.ts +20 -87
  58. package/src/core/memory-fence.ts +16 -9
  59. package/src/core/memory-recall.ts +168 -34
  60. package/src/core/path-rules.ts +143 -0
  61. package/src/core/section-parse.ts +16 -6
  62. package/src/core/typed-metadata.ts +68 -0
  63. package/src/core/wiki-link-syntax.ts +88 -0
  64. package/src/embed/backlog.ts +173 -60
  65. package/src/embed/batch.ts +83 -12
  66. package/src/embed/resume-cursor.ts +103 -0
  67. package/src/embed/retry.ts +41 -32
  68. package/src/embed/variant-backlog.ts +697 -63
  69. package/src/embed/variant-plan.ts +41 -45
  70. package/src/embed/variant-retry.ts +157 -44
  71. package/src/index.ts +3 -3
  72. package/src/ingestion/budget-error-codes.ts +13 -0
  73. package/src/ingestion/chunker.ts +60 -49
  74. package/src/ingestion/compiled-context.ts +4 -5
  75. package/src/ingestion/convert-file.ts +1 -1
  76. package/src/ingestion/document-metadata.ts +16 -9
  77. package/src/ingestion/excluded-ranges.ts +97 -0
  78. package/src/ingestion/file-processor.ts +122 -37
  79. package/src/ingestion/frontmatter.ts +35 -7
  80. package/src/ingestion/graph-reconciliation.ts +187 -51
  81. package/src/ingestion/language.ts +66 -61
  82. package/src/ingestion/position.ts +5 -5
  83. package/src/ingestion/prepare-file.ts +50 -29
  84. package/src/ingestion/record-adapter-canonical.ts +37 -26
  85. package/src/ingestion/record-adapter.ts +46 -26
  86. package/src/ingestion/record-container.ts +121 -53
  87. package/src/ingestion/record-path.ts +5 -2
  88. package/src/ingestion/source-availability/darwin-io.ts +10 -7
  89. package/src/ingestion/source-availability/darwin-path.ts +21 -3
  90. package/src/ingestion/source-availability/readers.ts +51 -0
  91. package/src/ingestion/strip.ts +39 -105
  92. package/src/ingestion/sync.ts +692 -386
  93. package/src/ingestion/typed-metadata.ts +10 -4
  94. package/src/ingestion/walker.ts +485 -158
  95. package/src/llm/cache.ts +128 -84
  96. package/src/llm/http-inference.ts +112 -35
  97. package/src/llm/http-policy.ts +101 -0
  98. package/src/llm/httpEmbedding.ts +8 -1
  99. package/src/llm/httpGeneration.ts +5 -1
  100. package/src/llm/httpRerank.ts +5 -1
  101. package/src/llm/inference-scope.ts +15 -2
  102. package/src/llm/lazy-ports.ts +33 -3
  103. package/src/llm/lockfile.ts +34 -7
  104. package/src/llm/model-cleanup.ts +24 -0
  105. package/src/llm/native-worker/client.ts +23 -12
  106. package/src/llm/native-worker/dispatcher.ts +19 -9
  107. package/src/llm/native-worker/embedding-identity.ts +42 -16
  108. package/src/llm/native-worker/entry.ts +2 -1
  109. package/src/llm/native-worker/owner.ts +1 -0
  110. package/src/llm/native-worker/ports.ts +86 -23
  111. package/src/llm/native-worker/protocol.ts +386 -55
  112. package/src/llm/nodeLlamaCpp/adapter.ts +46 -29
  113. package/src/llm/nodeLlamaCpp/embedding.ts +93 -40
  114. package/src/llm/nodeLlamaCpp/generation.ts +194 -30
  115. package/src/llm/nodeLlamaCpp/lifecycle.ts +2 -0
  116. package/src/llm/nodeLlamaCpp/rerank-capacity.ts +101 -23
  117. package/src/llm/nodeLlamaCpp/rerank.ts +253 -44
  118. package/src/llm/types.ts +14 -0
  119. package/src/mcp/context.ts +342 -23
  120. package/src/mcp/http-transport.ts +21 -3
  121. package/src/mcp/tool-profile-types.ts +62 -0
  122. package/src/mcp/tool-profile.ts +32 -56
  123. package/src/mcp/tools/context.ts +5 -1
  124. package/src/mcp/tools/index.ts +269 -191
  125. package/src/mcp/tools/links.ts +7 -5
  126. package/src/mcp/tools/query.ts +60 -37
  127. package/src/mcp/tools/vsearch.ts +25 -14
  128. package/src/pipeline/answer.ts +133 -40
  129. package/src/pipeline/claim-verification.ts +293 -189
  130. package/src/pipeline/claim-verifier.ts +6 -7
  131. package/src/pipeline/egress-lineage.ts +37 -26
  132. package/src/pipeline/exclude.ts +34 -11
  133. package/src/pipeline/expansion.ts +233 -19
  134. package/src/pipeline/filters.ts +169 -33
  135. package/src/pipeline/graph-retrieval.ts +117 -61
  136. package/src/pipeline/hybrid.ts +598 -329
  137. package/src/pipeline/hydration.ts +86 -24
  138. package/src/pipeline/intent.ts +15 -7
  139. package/src/pipeline/owner-fusion.ts +61 -19
  140. package/src/pipeline/query-language.ts +15 -2
  141. package/src/pipeline/rerank.ts +15 -22
  142. package/src/pipeline/search.ts +193 -72
  143. package/src/pipeline/snippet.ts +15 -2
  144. package/src/pipeline/temporal.ts +51 -10
  145. package/src/pipeline/vsearch.ts +250 -152
  146. package/src/publish/attachment-raster.ts +2 -16
  147. package/src/publish/encrypted-export.ts +15 -5
  148. package/src/serve/browse-tree.ts +50 -42
  149. package/src/serve/connectors.ts +2 -1
  150. package/src/serve/context.ts +13 -4
  151. package/src/serve/doc-events.ts +3 -0
  152. package/src/serve/public/app.tsx +49 -21
  153. package/src/serve/public/components/BrowseTreeSidebar.tsx +163 -23
  154. package/src/serve/public/components/CaptureModal.tsx +14 -10
  155. package/src/serve/public/components/ai-elements/code-block.tsx +87 -50
  156. package/src/serve/public/components/editor/MarkdownPreview.tsx +99 -67
  157. package/src/serve/public/globals.built.css +1 -1
  158. package/src/serve/public/globals.css +19 -0
  159. package/src/serve/public/hooks/useCaptureModal.tsx +23 -10
  160. package/src/serve/public/lib/metadata-filter.ts +234 -9
  161. package/src/serve/public/pages/Browse.tsx +61 -27
  162. package/src/serve/public/pages/DocView.tsx +44 -25
  163. package/src/serve/public/pages/GraphView.tsx +8 -3
  164. package/src/serve/resident-runtime.ts +199 -48
  165. package/src/serve/routes/api.ts +98 -67
  166. package/src/serve/routes/links.ts +18 -4
  167. package/src/serve/server.ts +1370 -1180
  168. package/src/serve/spa-bundle-source.ts +52 -27
  169. package/src/serve/status.ts +101 -52
  170. package/src/serve/watch-reconciliation-fallback.ts +11 -7
  171. package/src/serve/watch-reconciliation-shared.ts +42 -4
  172. package/src/serve/watch-reconciliation.ts +21 -8
  173. package/src/serve/watch-service-events.ts +7 -1
  174. package/src/serve/watch-service-hosts.ts +3 -1
  175. package/src/serve/watch-service.ts +46 -21
  176. package/src/serve/watch-snapshot-scan.ts +6 -15
  177. package/src/sessions/archive.ts +20 -8
  178. package/src/sessions/sanitize.ts +38 -18
  179. package/src/sessions/service.ts +180 -102
  180. package/src/store/migrations/036-vector-mirror-state.ts +83 -0
  181. package/src/store/migrations/037-performance-indexes.ts +56 -0
  182. package/src/store/migrations/index.ts +4 -0
  183. package/src/store/sqlite/adapter.ts +1217 -651
  184. package/src/store/sqlite/chunking-policy.ts +15 -1
  185. package/src/store/sqlite/eligibility.ts +110 -70
  186. package/src/store/sqlite/graph-neighbors.ts +71 -46
  187. package/src/store/sqlite/graph-reference-state.ts +27 -3
  188. package/src/store/sqlite/graph-similarity.ts +59 -12
  189. package/src/store/sqlite/metadata-predicate.ts +123 -28
  190. package/src/store/sqlite/retrieval-trace-management-store.ts +126 -33
  191. package/src/store/sqlite/retrieval-trace-retention.ts +88 -76
  192. package/src/store/sqlite/retrieval-trace-rows.ts +7 -3
  193. package/src/store/types.ts +32 -0
  194. package/src/store/vector/eligibility.ts +39 -0
  195. package/src/store/vector/lazy.ts +17 -0
  196. package/src/store/vector/partition-index.ts +49 -47
  197. package/src/store/vector/proof-cache.ts +103 -0
  198. package/src/store/vector/sqlite-vec.ts +230 -139
  199. package/src/store/vector/stats.ts +29 -73
  200. package/src/store/vector/status.ts +151 -52
  201. package/src/store/vector/stored-vectors.ts +131 -45
  202. package/src/store/vector/types.ts +10 -0
  203. package/src/store/vector/variant-search.ts +771 -109
  204. package/src/store/vector/variants.ts +785 -144
  205. package/browser-extension/artifacts/gno-browser-clipper-v2.14.0.zip.sha256 +0 -1
@@ -1,4 +1,5 @@
1
- import type { ShikiTransformer } from "shiki/types";
1
+ import type { BundledLanguage } from "shiki";
2
+ import type { HighlighterCore, ShikiTransformer } from "shiki/types";
2
3
 
3
4
  import { CheckIcon, CopyIcon } from "lucide-react";
4
5
  import {
@@ -10,8 +11,6 @@ import {
10
11
  useRef,
11
12
  useState,
12
13
  } from "react";
13
- import { type BundledLanguage, createHighlighter } from "shiki";
14
- import { createJavaScriptRegexEngine } from "shiki/engine/javascript";
15
14
 
16
15
  import { resolveCodeLanguage } from "../../lib/code-language";
17
16
  import { cn } from "../../lib/utils";
@@ -33,22 +32,65 @@ const CodeBlockContext = createContext<CodeBlockContextType>({
33
32
  code: "",
34
33
  });
35
34
 
36
- let highlighterPromise: ReturnType<typeof createHighlighter> | null = null;
35
+ let highlighterPromise: Promise<HighlighterCore> | null = null;
36
+ let bundledLanguagesPromise: Promise<
37
+ typeof import("shiki/langs").bundledLanguages
38
+ > | null = null;
39
+ const loadingLanguages = new Map<string, Promise<void>>();
37
40
 
38
- function getHighlighterRuntime(): ReturnType<typeof createHighlighter> {
39
- highlighterPromise ??= createHighlighter({
40
- engine: createJavaScriptRegexEngine(),
41
- langs: ["text"],
42
- themes: ["one-light", "one-dark-pro"],
43
- });
41
+ /**
42
+ * Lazily load shiki/core with only the two themes GNO uses (`one-light` and
43
+ * `one-dark-pro`), keeping Shiki completely off pages without code blocks and
44
+ * dropping ~58 unused bundled themes from the SPA bundle.
45
+ */
46
+ function getHighlighterRuntime(): Promise<HighlighterCore> {
47
+ highlighterPromise ??= Promise.all([
48
+ import("shiki/core"),
49
+ import("shiki/engine/javascript"),
50
+ import("shiki/themes/one-light.mjs"),
51
+ import("shiki/themes/one-dark-pro.mjs"),
52
+ ]).then(([shikiCore, jsEngine, oneLight, oneDarkPro]) =>
53
+ shikiCore.createHighlighterCore({
54
+ engine: jsEngine.createJavaScriptRegexEngine(),
55
+ langs: [],
56
+ themes: [oneLight.default, oneDarkPro.default],
57
+ })
58
+ );
44
59
  return highlighterPromise;
45
60
  }
46
61
 
47
- async function getHighlighter(language: BundledLanguage | "text") {
62
+ function getBundledLanguages() {
63
+ bundledLanguagesPromise ??= import("shiki/langs").then(
64
+ (mod) => mod.bundledLanguages
65
+ );
66
+ return bundledLanguagesPromise;
67
+ }
68
+
69
+ async function getHighlighter(
70
+ language: BundledLanguage | "text"
71
+ ): Promise<HighlighterCore> {
48
72
  const highlighter = await getHighlighterRuntime();
49
- if (!highlighter.getLoadedLanguages().includes(language)) {
50
- await highlighter.loadLanguage(language);
73
+ if (
74
+ language === "text" ||
75
+ highlighter.getLoadedLanguages().includes(language)
76
+ ) {
77
+ return highlighter;
78
+ }
79
+ let inflight = loadingLanguages.get(language);
80
+ if (!inflight) {
81
+ inflight = (async () => {
82
+ const langs = await getBundledLanguages();
83
+ const loader = langs[language];
84
+ if (loader) {
85
+ const grammar = await loader();
86
+ await highlighter.loadLanguage(grammar);
87
+ }
88
+ })().finally(() => {
89
+ loadingLanguages.delete(language);
90
+ });
91
+ loadingLanguages.set(language, inflight);
51
92
  }
93
+ await inflight;
52
94
  return highlighter;
53
95
  }
54
96
 
@@ -99,12 +141,26 @@ function createLineTransformer(
99
141
  };
100
142
  }
101
143
 
102
- export async function highlightCode(
144
+ const DUAL_THEMES = {
145
+ light: "one-light",
146
+ dark: "one-dark-pro",
147
+ } as const;
148
+
149
+ /**
150
+ * Highlight code in a single Shiki pass using dual-theme CSS variables so
151
+ * light and dark themes share one tokenized HTML tree.
152
+ */
153
+ /**
154
+ * Highlight code with dual-theme CSS variables (`one-light` + `one-dark-pro`),
155
+ * allowing CodeBlock to mount a single highlighted DOM tree instead of
156
+ * rendering separate light and dark subtrees.
157
+ */
158
+ export async function highlightCodeDualTheme(
103
159
  code: string,
104
160
  language: string,
105
161
  showLineNumbers = false,
106
162
  highlightedLines: number[] = []
107
- ): Promise<[string, string]> {
163
+ ): Promise<string> {
108
164
  const resolvedLanguage = resolveCodeLanguage(language);
109
165
  const transformers: ShikiTransformer[] =
110
166
  showLineNumbers || highlightedLines.length > 0
@@ -113,31 +169,19 @@ export async function highlightCode(
113
169
  const highlighter = await getHighlighter(resolvedLanguage);
114
170
 
115
171
  try {
116
- return [
117
- highlighter.codeToHtml(code, {
118
- lang: resolvedLanguage,
119
- theme: "one-light",
120
- transformers,
121
- }),
122
- highlighter.codeToHtml(code, {
123
- lang: resolvedLanguage,
124
- theme: "one-dark-pro",
125
- transformers,
126
- }),
127
- ];
172
+ return highlighter.codeToHtml(code, {
173
+ lang: resolvedLanguage,
174
+ themes: DUAL_THEMES,
175
+ defaultColor: "light",
176
+ transformers,
177
+ });
128
178
  } catch {
129
- return [
130
- highlighter.codeToHtml(code, {
131
- lang: "text",
132
- theme: "one-light",
133
- transformers,
134
- }),
135
- highlighter.codeToHtml(code, {
136
- lang: "text",
137
- theme: "one-dark-pro",
138
- transformers,
139
- }),
140
- ];
179
+ return highlighter.codeToHtml(code, {
180
+ lang: "text",
181
+ themes: DUAL_THEMES,
182
+ defaultColor: "light",
183
+ transformers,
184
+ });
141
185
  }
142
186
  }
143
187
 
@@ -152,7 +196,6 @@ export const CodeBlock = ({
152
196
  ...props
153
197
  }: CodeBlockProps) => {
154
198
  const [html, setHtml] = useState<string>("");
155
- const [darkHtml, setDarkHtml] = useState<string>("");
156
199
  const requestIdRef = useRef(0);
157
200
  const containerRef = useRef<HTMLDivElement>(null);
158
201
 
@@ -161,7 +204,7 @@ export const CodeBlock = ({
161
204
  const requestId = ++requestIdRef.current;
162
205
 
163
206
  async function highlight() {
164
- const [light, dark] = await highlightCode(
207
+ const rendered = await highlightCodeDualTheme(
165
208
  code,
166
209
  language,
167
210
  showLineNumbers,
@@ -169,8 +212,7 @@ export const CodeBlock = ({
169
212
  );
170
213
  // Only apply if this is still the latest request AND not cancelled
171
214
  if (!cancelled && requestId === requestIdRef.current) {
172
- setHtml(light);
173
- setDarkHtml(dark);
215
+ setHtml(rendered);
174
216
  }
175
217
  }
176
218
 
@@ -191,7 +233,7 @@ export const CodeBlock = ({
191
233
  });
192
234
 
193
235
  return () => cancelAnimationFrame(frame);
194
- }, [darkHtml, html, scrollToLine]);
236
+ }, [html, scrollToLine]);
195
237
 
196
238
  return (
197
239
  <CodeBlockContext.Provider value={{ code }}>
@@ -205,15 +247,10 @@ export const CodeBlock = ({
205
247
  >
206
248
  <div className="relative">
207
249
  <div
208
- className="overflow-auto dark:hidden [&>pre]:m-0 [&>pre]:bg-background! [&>pre]:p-4 [&>pre]:text-foreground! [&>pre]:text-sm [&_code]:font-mono [&_code]:text-sm"
250
+ className="overflow-auto [&>pre]:m-0 [&>pre]:bg-background! [&>pre]:p-4 [&>pre]:text-foreground! [&>pre]:text-sm [&_code]:font-mono [&_code]:text-sm"
209
251
  // oxlint-disable-next-line react/no-danger -- syntax highlighting requires innerHTML
210
252
  dangerouslySetInnerHTML={{ __html: html }}
211
253
  />
212
- <div
213
- className="hidden overflow-auto dark:block [&>pre]:m-0 [&>pre]:bg-background! [&>pre]:p-4 [&>pre]:text-foreground! [&>pre]:text-sm [&_code]:font-mono [&_code]:text-sm"
214
- // oxlint-disable-next-line react/no-danger -- syntax highlighting requires innerHTML
215
- dangerouslySetInnerHTML={{ __html: darkHtml }}
216
- />
217
254
  {children && (
218
255
  <div className="absolute top-2 right-2 flex items-center gap-2">
219
256
  {children}
@@ -8,18 +8,18 @@
8
8
  import type { ComponentProps, FC, ReactNode } from "react";
9
9
 
10
10
  import { ExternalLinkIcon } from "lucide-react";
11
- import { memo } from "react";
11
+ import { createContext, memo, useContext, useMemo } from "react";
12
12
  import ReactMarkdown from "react-markdown";
13
13
  import rehypeSanitize from "rehype-sanitize";
14
14
  import remarkGfm from "remark-gfm";
15
15
 
16
+ import { slugifySectionTitle } from "../../../../core/sections";
16
17
  import {
17
18
  normalizeWikiName,
18
19
  parseTargetParts,
19
20
  splitWikiLinkContent,
20
21
  stripWikiMdExt,
21
- } from "../../../../core/links";
22
- import { slugifySectionTitle } from "../../../../core/sections";
22
+ } from "../../../../core/wiki-link-syntax";
23
23
  import {
24
24
  extractMarkdownCodeLanguage,
25
25
  resolveCodeLanguage,
@@ -188,35 +188,55 @@ function flattenNodeText(node: ReactNode): string {
188
188
  return "";
189
189
  }
190
190
 
191
- // Heading styles with proper hierarchy
192
- const createHeading =
193
- (
194
- level: 1 | 2 | 3 | 4 | 5 | 6,
195
- anchorCounts: Map<string, number>
196
- ): FC<{ children?: ReactNode }> =>
197
- ({ children }) => {
198
- const Tag = `h${level}` as const;
199
- const sizes = {
200
- 1: "text-3xl mt-8 mb-4 pb-2 border-b border-border/50",
201
- 2: "text-2xl mt-6 mb-3 pb-1.5 border-b border-border/30",
202
- 3: "text-xl mt-5 mb-2",
203
- 4: "text-lg mt-4 mb-2",
204
- 5: "text-base mt-3 mb-1 font-semibold",
205
- 6: "text-sm mt-3 mb-1 font-semibold text-muted-foreground",
206
- };
191
+ interface MarkdownRenderContextValue {
192
+ docUri?: string;
193
+ anchorCounts: Map<string, number>;
194
+ }
195
+
196
+ const MarkdownRenderContext = createContext<MarkdownRenderContextValue>({
197
+ anchorCounts: new Map(),
198
+ });
199
+
200
+ const HEADING_SIZES: Record<1 | 2 | 3 | 4 | 5 | 6, string> = {
201
+ 1: "text-3xl mt-8 mb-4 pb-2 border-b border-border/50",
202
+ 2: "text-2xl mt-6 mb-3 pb-1.5 border-b border-border/30",
203
+ 3: "text-xl mt-5 mb-2",
204
+ 4: "text-lg mt-4 mb-2",
205
+ 5: "text-base mt-3 mb-1 font-semibold",
206
+ 6: "text-sm mt-3 mb-1 font-semibold text-muted-foreground",
207
+ };
208
+
209
+ // Stable heading components prevent React from unmounting/remounting headings on every render.
210
+ const createStableHeading = (
211
+ level: 1 | 2 | 3 | 4 | 5 | 6
212
+ ): FC<{ children?: ReactNode }> => {
213
+ const Tag = `h${level}` as const;
214
+ const className = cn(
215
+ "scroll-mt-24 font-serif tracking-tight",
216
+ HEADING_SIZES[level]
217
+ );
218
+ const HeadingComponent: FC<{ children?: ReactNode }> = ({ children }) => {
219
+ const { anchorCounts } = useContext(MarkdownRenderContext);
207
220
  const baseAnchor = slugifySectionTitle(flattenNodeText(children));
208
221
  const seen = (anchorCounts.get(baseAnchor) ?? 0) + 1;
209
222
  anchorCounts.set(baseAnchor, seen);
210
223
  const anchor = seen === 1 ? baseAnchor : `${baseAnchor}-${seen}`;
211
224
  return (
212
- <Tag
213
- className={cn("scroll-mt-24 font-serif tracking-tight", sizes[level])}
214
- id={anchor}
215
- >
225
+ <Tag className={className} id={anchor}>
216
226
  {children}
217
227
  </Tag>
218
228
  );
219
229
  };
230
+ HeadingComponent.displayName = `MarkdownH${level}`;
231
+ return HeadingComponent;
232
+ };
233
+
234
+ const H1 = createStableHeading(1);
235
+ const H2 = createStableHeading(2);
236
+ const H3 = createStableHeading(3);
237
+ const H4 = createStableHeading(4);
238
+ const H5 = createStableHeading(5);
239
+ const H6 = createStableHeading(6);
220
240
 
221
241
  // Code block with syntax highlighting
222
242
  const Pre: FC<ComponentProps<"pre">> = ({ children, ...props }) => {
@@ -417,6 +437,42 @@ const Image: FC<ComponentProps<"img"> & { node?: unknown }> = ({
417
437
  />
418
438
  );
419
439
 
440
+ const MarkdownImage: FC<ComponentProps<"img"> & { node?: unknown }> = ({
441
+ node,
442
+ src,
443
+ ...props
444
+ }) => {
445
+ const { docUri } = useContext(MarkdownRenderContext);
446
+ return (
447
+ <Image {...props} node={node} src={resolveMarkdownAssetSrc(src, docUri)} />
448
+ );
449
+ };
450
+
451
+ const REMARK_PLUGINS = [remarkGfm];
452
+ const REHYPE_PLUGINS = [rehypeSanitize];
453
+ const MARKDOWN_COMPONENTS = {
454
+ h1: H1,
455
+ h2: H2,
456
+ h3: H3,
457
+ h4: H4,
458
+ h5: H5,
459
+ h6: H6,
460
+ p: Paragraph,
461
+ a: Link,
462
+ code: InlineCode,
463
+ pre: Pre,
464
+ blockquote: Blockquote,
465
+ ul: UnorderedList,
466
+ ol: OrderedList,
467
+ table: Table,
468
+ thead: TableHead,
469
+ tr: TableRow,
470
+ td: TableCell,
471
+ th: TableHeaderCell,
472
+ hr: Hr,
473
+ img: MarkdownImage,
474
+ };
475
+
420
476
  /**
421
477
  * Renders markdown content with syntax highlighting and proper styling.
422
478
  * Sanitizes HTML to prevent XSS attacks.
@@ -429,6 +485,14 @@ export const MarkdownPreview = memo(
429
485
  wikiLinks,
430
486
  docUri,
431
487
  }: MarkdownPreviewProps) => {
488
+ const renderedContent = useMemo(
489
+ () =>
490
+ content
491
+ ? renderMarkdownWithWikiLinks(content, collection, wikiLinks)
492
+ : "",
493
+ [content, collection, wikiLinks]
494
+ );
495
+
432
496
  if (!content) {
433
497
  return (
434
498
  <div className={cn("text-muted-foreground italic", className)}>
@@ -437,43 +501,9 @@ export const MarkdownPreview = memo(
437
501
  );
438
502
  }
439
503
 
440
- const renderedContent = renderMarkdownWithWikiLinks(
441
- content,
442
- collection,
443
- wikiLinks
444
- );
445
- const anchorCounts = new Map<string, number>();
446
- const components = {
447
- h1: createHeading(1, anchorCounts),
448
- h2: createHeading(2, anchorCounts),
449
- h3: createHeading(3, anchorCounts),
450
- h4: createHeading(4, anchorCounts),
451
- h5: createHeading(5, anchorCounts),
452
- h6: createHeading(6, anchorCounts),
453
- p: Paragraph,
454
- a: Link,
455
- code: InlineCode,
456
- pre: Pre,
457
- blockquote: Blockquote,
458
- ul: UnorderedList,
459
- ol: OrderedList,
460
- table: Table,
461
- thead: TableHead,
462
- tr: TableRow,
463
- td: TableCell,
464
- th: TableHeaderCell,
465
- hr: Hr,
466
- img: ({
467
- node,
468
- src,
469
- ...props
470
- }: ComponentProps<"img"> & { node?: unknown }) => (
471
- <Image
472
- {...props}
473
- node={node}
474
- src={resolveMarkdownAssetSrc(src, docUri)}
475
- />
476
- ),
504
+ const renderContextValue: MarkdownRenderContextValue = {
505
+ docUri,
506
+ anchorCounts: new Map<string, number>(),
477
507
  };
478
508
 
479
509
  return (
@@ -485,13 +515,15 @@ export const MarkdownPreview = memo(
485
515
  className
486
516
  )}
487
517
  >
488
- <ReactMarkdown
489
- components={components}
490
- rehypePlugins={[rehypeSanitize]}
491
- remarkPlugins={[remarkGfm]}
492
- >
493
- {renderedContent}
494
- </ReactMarkdown>
518
+ <MarkdownRenderContext.Provider value={renderContextValue}>
519
+ <ReactMarkdown
520
+ components={MARKDOWN_COMPONENTS}
521
+ rehypePlugins={REHYPE_PLUGINS}
522
+ remarkPlugins={REMARK_PLUGINS}
523
+ >
524
+ {renderedContent}
525
+ </ReactMarkdown>
526
+ </MarkdownRenderContext.Provider>
495
527
  </div>
496
528
  );
497
529
  }