@farming-labs/svelte 0.2.140 → 0.2.142

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.
package/dist/markdown.js CHANGED
@@ -11,11 +11,11 @@
11
11
  import { resolveDocsAudienceMdxContent } from "@farming-labs/docs";
12
12
  import { createDocsMarkdownBlockPlaceholderAllocator, extractDocsMarkdownReferenceDefinitions as extractMarkdownReferenceDefinitions, extractDocsRenderedHeadingElements as extractRenderedHeadingElements, prepareDocsMarkdownHeadings as prepareMarkdownHeadings, replaceDocsMarkdownFencedCodeBlocks as replaceMarkdownFencedCodeBlocks, renderDocsMarkdownBlockContent as renderMarkdownBlockContent, renderDocsMarkdownHeadings as renderMarkdownHeadings, renderDocsMarkdownInline as renderMarkdownInline, restoreDocsMarkdownHeadingOpeningTags as restoreMarkdownHeadingOpeningTags, stripPreparedDocsMarkdownHeadingTokens as stripPreparedMarkdownHeadingTokens, } from "@farming-labs/docs/markdown-rendering";
13
13
  import { extractDocsMarkdownPromptBlocks, parsePromptStringArray, resolvePromptProviderChoices, sanitizePromptText, } from "@farming-labs/docs/server";
14
- import { createHighlighter } from "shiki";
15
14
  let highlighterPromise = null;
16
15
  function getHighlighter() {
17
16
  if (!highlighterPromise) {
18
- highlighterPromise = createHighlighter({
17
+ highlighterPromise = import("shiki")
18
+ .then(({ createHighlighter }) => createHighlighter({
19
19
  themes: ["github-light", "github-dark"],
20
20
  langs: [
21
21
  "javascript",
@@ -33,6 +33,10 @@ function getHighlighter() {
33
33
  "python",
34
34
  "dotenv",
35
35
  ],
36
+ }))
37
+ .catch((error) => {
38
+ highlighterPromise = null;
39
+ throw error;
36
40
  });
37
41
  }
38
42
  return highlighterPromise;
@@ -481,7 +485,6 @@ function dedentCode(raw) {
481
485
  export async function renderMarkdown(content, options = {}) {
482
486
  if (!content)
483
487
  return "";
484
- const hl = await getHighlighter();
485
488
  // Prompt children are literal copyable text, not renderable Markdown. Protect
486
489
  // them before audience projection, reference collection, and heading
487
490
  // preparation so none of those document-level passes can consume or interpret
@@ -491,6 +494,14 @@ export async function renderMarkdown(content, options = {}) {
491
494
  let result = extractedPrompts.markdown;
492
495
  result = resolveDocsAudienceMdxContent(result, "human");
493
496
  const allocateBlockPlaceholder = createDocsMarkdownBlockPlaceholderAllocator(result);
497
+ // Queue only blocks that the renderer actually consumes. Prose, literal Prompt
498
+ // children, and agent-only code never import or initialize Shiki.
499
+ const highlights = [];
500
+ function highlightLater(code, lang) {
501
+ const token = allocateBlockPlaceholder("CODEBLOCK");
502
+ highlights.push({ token, code, lang });
503
+ return { html: token, raw: code.replace(/\n$/, "") };
504
+ }
494
505
  const extractedReferences = extractMarkdownReferenceDefinitions(result);
495
506
  const referenceDefinitions = extractedReferences.definitions;
496
507
  const preparedHeadings = prepareMarkdownHeadings(result);
@@ -510,7 +521,7 @@ export async function renderMarkdown(content, options = {}) {
510
521
  const label = title || lang || `Code ${panels.length + 1}`;
511
522
  const value = createCodeGroupTabValue(label, usedValues);
512
523
  const dedented = dedentCode(code);
513
- const { html, raw } = highlightCode(hl, dedented, lang);
524
+ const { html, raw } = highlightLater(dedented, lang);
514
525
  panels.push({
515
526
  label,
516
527
  value,
@@ -553,7 +564,7 @@ export async function renderMarkdown(content, options = {}) {
553
564
  return "";
554
565
  const { lang, title } = parseMeta(info);
555
566
  const dedented = dedentCode(code);
556
- const { html, raw } = highlightCode(hl, dedented, lang);
567
+ const { html, raw } = highlightLater(dedented, lang);
557
568
  renderedCode = wrapCodeWithCopy(html, raw, title, lang);
558
569
  return "";
559
570
  });
@@ -602,7 +613,7 @@ export async function renderMarkdown(content, options = {}) {
602
613
  const codeBlocks = [];
603
614
  result = replaceMarkdownFencedCodeBlocks(result, ({ code, info }) => {
604
615
  const { lang, title } = parseMeta(info);
605
- const { html, raw } = highlightCode(hl, code, lang);
616
+ const { html, raw } = highlightLater(code, lang);
606
617
  const placeholder = allocateBlockPlaceholder("CODEBLOCK");
607
618
  codeBlocks.push({ html: wrapCodeWithCopy(html, raw, title, lang), token: placeholder });
608
619
  return placeholder;
@@ -739,6 +750,12 @@ export async function renderMarkdown(content, options = {}) {
739
750
  result = result.replace(block.token, () => block.html);
740
751
  }
741
752
  result = restoreMarkdownHeadingOpeningTags(stripPreparedMarkdownHeadingTokens(result), headingOpeningTags);
753
+ if (highlights.length > 0) {
754
+ const hl = await getHighlighter();
755
+ for (const { token, code, lang } of highlights) {
756
+ result = result.replace(token, () => highlightCode(hl, code, lang).html);
757
+ }
758
+ }
742
759
  for (const block of promptBlocks) {
743
760
  result = result.replace(block.token, () => renderPrompt(block.attributes, block.children, options));
744
761
  }
package/dist/server.d.ts CHANGED
@@ -1,32 +1,3 @@
1
- /**
2
- * Server-side helpers for SvelteKit docs routes.
3
- *
4
- * `createDocsServer(config)` returns all the load functions and
5
- * handlers needed for a complete docs site. Each route file becomes
6
- * a one-line re-export.
7
- *
8
- * @example
9
- * ```ts
10
- * // src/lib/docs.server.ts
11
- * import { createDocsServer } from "@farming-labs/svelte/server";
12
- * import config from "./docs.config";
13
- *
14
- * // Bundle content at build time for serverless deployments
15
- * const contentFiles = import.meta.glob(["/docs/**\/*.{md,mdx,svx}", "/AGENTS.md", "/skill.md"], {
16
- * query: "?raw", import: "default", eager: true,
17
- * }) as Record<string, string>;
18
- *
19
- * export const { load, GET, HEAD, POST, MCP } = createDocsServer({
20
- * ...config,
21
- * _preloadedContent: contentFiles,
22
- * });
23
- * ```
24
- *
25
- * ```ts
26
- * // routes/docs/+layout.server.js
27
- * export { load } from "$lib/docs.server";
28
- * ```
29
- */
30
1
  import type { DocsMcpHttpHandlers } from "@farming-labs/docs/server";
31
2
  import { loadDocsNavTree } from "./content.js";
32
3
  import type { PageNode } from "./content.js";
package/dist/server.js CHANGED
@@ -1,3 +1,4 @@
1
+ import * as DocsRuntime from "@farming-labs/docs/runtime";
1
2
  /**
2
3
  * Server-side helpers for SvelteKit docs routes.
3
4
  *
@@ -193,6 +194,23 @@ function navTreeFromMap(contentMap, dirPrefix, entry, ordering) {
193
194
  return a.parts.length - b.parts.length;
194
195
  return a.parts.join("/").localeCompare(b.parts.join("/"));
195
196
  });
197
+ // Build per-call indexes so each folder does not scan the entire corpus.
198
+ // Arrays retain duplicate index pages and the existing stable sorted order.
199
+ const childrenByParent = new Map();
200
+ const parentsWithDescendants = new Set();
201
+ for (const dir of dirs) {
202
+ if (dir.parts.length === 0)
203
+ continue;
204
+ const parentKey = JSON.stringify(dir.parts.slice(0, -1));
205
+ const siblings = childrenByParent.get(parentKey);
206
+ if (siblings)
207
+ siblings.push(dir);
208
+ else
209
+ childrenByParent.set(parentKey, [dir]);
210
+ for (let depth = 0; depth < dir.parts.length; depth++) {
211
+ parentsWithDescendants.add(JSON.stringify(dir.parts.slice(0, depth)));
212
+ }
213
+ }
196
214
  const children = [];
197
215
  const rootInfo = dirs.find((d) => d.parts.length === 0);
198
216
  if (rootInfo) {
@@ -203,12 +221,22 @@ function navTreeFromMap(contentMap, dirPrefix, entry, ordering) {
203
221
  icon: rootInfo.icon,
204
222
  });
205
223
  }
224
+ const orderingIndexes = new Map();
206
225
  function findSlugOrder(parentParts) {
207
226
  if (!Array.isArray(ordering))
208
227
  return undefined;
209
228
  let items = ordering;
210
229
  for (const part of parentParts) {
211
- const found = items.find((i) => i.slug === part);
230
+ let index = orderingIndexes.get(items);
231
+ if (!index) {
232
+ index = new Map();
233
+ for (const item of items) {
234
+ if (!index.has(item.slug))
235
+ index.set(item.slug, item);
236
+ }
237
+ orderingIndexes.set(items, index);
238
+ }
239
+ const found = index.get(part);
212
240
  if (!found?.children)
213
241
  return undefined;
214
242
  items = found.children;
@@ -217,21 +245,20 @@ function navTreeFromMap(contentMap, dirPrefix, entry, ordering) {
217
245
  }
218
246
  function buildLevel(parentParts) {
219
247
  const depth = parentParts.length;
220
- const directChildren = dirs.filter((d) => {
221
- if (d.parts.length !== depth + 1)
222
- return false;
223
- for (let i = 0; i < depth; i++) {
224
- if (d.parts[i] !== parentParts[i])
225
- return false;
226
- }
227
- return true;
228
- });
248
+ const directChildren = [...(childrenByParent.get(JSON.stringify(parentParts)) ?? [])];
229
249
  const slugOrder = findSlugOrder(parentParts);
230
250
  if (slugOrder) {
231
251
  const slugMap = new Set(slugOrder.map((i) => i.slug));
232
252
  const ordered = [];
253
+ const childrenBySlug = new Map();
254
+ for (const child of directChildren) {
255
+ const slug = child.parts[depth];
256
+ // Array.find previously selected the first duplicate index page.
257
+ if (!childrenBySlug.has(slug))
258
+ childrenBySlug.set(slug, child);
259
+ }
233
260
  for (const item of slugOrder) {
234
- const match = directChildren.find((d) => d.parts[depth] === item.slug);
261
+ const match = childrenBySlug.get(item.slug);
235
262
  if (match)
236
263
  ordered.push(match);
237
264
  }
@@ -241,11 +268,7 @@ function navTreeFromMap(contentMap, dirPrefix, entry, ordering) {
241
268
  }
242
269
  const nodes = [];
243
270
  for (const child of ordered) {
244
- const hasGrandChildren = dirs.some((d) => {
245
- if (d.parts.length <= child.parts.length)
246
- return false;
247
- return child.parts.every((p, i) => d.parts[i] === p);
248
- });
271
+ const hasGrandChildren = parentsWithDescendants.has(JSON.stringify(child.parts));
249
272
  if (hasGrandChildren) {
250
273
  nodes.push({
251
274
  type: "folder",
@@ -271,11 +294,7 @@ function navTreeFromMap(contentMap, dirPrefix, entry, ordering) {
271
294
  }
272
295
  const nodes = [];
273
296
  for (const child of directChildren) {
274
- const hasGrandChildren = dirs.some((d) => {
275
- if (d.parts.length <= child.parts.length)
276
- return false;
277
- return child.parts.every((p, i) => d.parts[i] === p);
278
- });
297
+ const hasGrandChildren = parentsWithDescendants.has(JSON.stringify(child.parts));
279
298
  if (hasGrandChildren) {
280
299
  nodes.push({
281
300
  type: "folder",
@@ -457,7 +476,16 @@ function createDocsRootServer(config) {
457
476
  const preloaded = rawPreloaded
458
477
  ? Object.fromEntries(Object.entries(rawPreloaded).map(([k, v]) => [k.startsWith("/") ? k : `/${k}`, v]))
459
478
  : undefined;
460
- const preloadedSitemapManifest = readDocsSitemapManifestFromContentMap(preloaded);
479
+ const preparedContent = preloaded
480
+ ? DocsRuntime.createDocsContentMapCache?.(preloaded)
481
+ : undefined;
482
+ function prepareContent(key, prepare) {
483
+ // Older supported core packages retain the original uncached behavior.
484
+ return preparedContent ? preparedContent.get(key, prepare) : prepare();
485
+ }
486
+ function getPreloadedSitemapManifest() {
487
+ return prepareContent("sitemap", () => readDocsSitemapManifestFromContentMap(preloaded));
488
+ }
461
489
  const ordering = config.ordering;
462
490
  const aiConfig = config.ai ?? {};
463
491
  // Allow top-level apiKey as a shorthand
@@ -517,7 +545,7 @@ function createDocsRootServer(config) {
517
545
  const locale = resolveDocsLocale(event.url.searchParams, i18n) ?? i18n?.defaultLocale;
518
546
  const ctx = resolveContextFromPath(event.url.pathname, locale);
519
547
  const tree = applySidebarFolderIndexBehavior(preloaded
520
- ? navTreeFromMap(preloaded, ctx.dirPrefix, entry, ordering)
548
+ ? prepareContent(JSON.stringify(["navigation", ctx.dirPrefix, entry, ordering]), () => navTreeFromMap(preloaded, ctx.dirPrefix, entry, ordering))
521
549
  : loadDocsNavTree(ctx.contentDirAbs, entry, ordering), {
522
550
  sidebar: config.sidebar,
523
551
  defaultBehavior: "toggle",
@@ -539,7 +567,7 @@ function createDocsRootServer(config) {
539
567
  }
540
568
  raw = result.raw;
541
569
  relPath = result.relPath;
542
- const manifestLastmod = resolveDocsSitemapPageLastmod(preloadedSitemapManifest, currentUrl);
570
+ const manifestLastmod = resolveDocsSitemapPageLastmod(getPreloadedSitemapManifest(), currentUrl);
543
571
  lastModified = formatDocsLastModifiedDate(manifestLastmod ?? new Date());
544
572
  lastModifiedIso = manifestLastmod ? `${manifestLastmod}T00:00:00.000Z` : undefined;
545
573
  }
@@ -649,13 +677,16 @@ function createDocsRootServer(config) {
649
677
  const apiOperationPagesByLocale = new Map();
650
678
  function getSearchIndex(ctx) {
651
679
  const key = ctx.locale ?? "__default__";
680
+ if (preloaded) {
681
+ return prepareContent(JSON.stringify(["search", ctx.dirPrefix, entry, ctx.locale]), () => searchIndexFromMap(preloaded, ctx.dirPrefix, entry, ctx.locale, readDocsSitemapManifestFromContentMap(preloaded)));
682
+ }
683
+ if (process.env.NODE_ENV !== "production")
684
+ return withContentRelativeSourcePaths(loadDocsContent(ctx.contentDirAbs, entry), ctx.contentDirAbs);
652
685
  const cached = searchIndexByEntry.get(key);
653
686
  if (cached)
654
- return cached;
655
- const index = preloaded
656
- ? searchIndexFromMap(preloaded, ctx.dirPrefix, entry, ctx.locale, preloadedSitemapManifest)
657
- : withContentRelativeSourcePaths(loadDocsContent(ctx.contentDirAbs, entry), ctx.contentDirAbs);
658
- searchIndexByEntry.set(key, index);
687
+ return structuredClone(cached);
688
+ const index = withContentRelativeSourcePaths(loadDocsContent(ctx.contentDirAbs, entry), ctx.contentDirAbs);
689
+ searchIndexByEntry.set(key, structuredClone(index));
659
690
  return index;
660
691
  }
661
692
  function getApiOperationSearchPages(ctx, baseUrl) {
@@ -990,7 +1021,7 @@ function createDocsRootServer(config) {
990
1021
  siteTitle: llmsTitle,
991
1022
  baseUrl: llmsBaseUrl || event.url.origin,
992
1023
  pages: getSearchIndex(ctx),
993
- manifest: preloadedSitemapManifest ?? readDocsSitemapManifest(rootDir, config.sitemap),
1024
+ manifest: getPreloadedSitemapManifest() ?? readDocsSitemapManifest(rootDir, config.sitemap),
994
1025
  });
995
1026
  if (sitemapResponse)
996
1027
  return sitemapResponse;
@@ -1721,7 +1752,7 @@ function createDocsRootServer(config) {
1721
1752
  getNavigation(locale) {
1722
1753
  const ctx = resolveContextFromPath(`/${entry}`, resolveLocaleForMcp(locale));
1723
1754
  return applySidebarFolderIndexBehavior(preloaded
1724
- ? navTreeFromMap(preloaded, ctx.dirPrefix, entry, ordering)
1755
+ ? prepareContent(JSON.stringify(["navigation", ctx.dirPrefix, entry, ordering]), () => navTreeFromMap(preloaded, ctx.dirPrefix, entry, ordering))
1725
1756
  : loadDocsNavTree(ctx.contentDirAbs, entry, ordering), {
1726
1757
  sidebar: config.sidebar,
1727
1758
  defaultBehavior: "toggle",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@farming-labs/svelte",
3
- "version": "0.2.140",
3
+ "version": "0.2.142",
4
4
  "description": "SvelteKit adapter for @farming-labs/docs — content loading and navigation utilities",
5
5
  "keywords": [
6
6
  "docs",
@@ -57,7 +57,7 @@
57
57
  "@types/node": "^22.10.0",
58
58
  "typescript": "^5.9.3",
59
59
  "vitest": "^4.1.8",
60
- "@farming-labs/docs": "0.2.140"
60
+ "@farming-labs/docs": "0.2.142"
61
61
  },
62
62
  "peerDependencies": {
63
63
  "@farming-labs/docs": ">=0.2.73 || ^0.2.133-perf.0"