idcmd 0.0.1 → 0.0.2

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 (89) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +96 -2
  3. package/package.json +52 -6
  4. package/public/_idcmd/live-reload.js +18 -0
  5. package/public/_idcmd/llm-menu.js +153 -0
  6. package/public/_idcmd/nav-prefetch.js +30 -0
  7. package/public/_idcmd/right-rail-scrollspy.js +262 -0
  8. package/public/anthropic-black.svg +16 -0
  9. package/public/anthropic-white.svg +16 -0
  10. package/public/favicon.svg +13 -0
  11. package/public/live-reload.js +18 -0
  12. package/public/llm-menu.js +153 -0
  13. package/public/openai-black.svg +15 -0
  14. package/public/openai-white.svg +15 -0
  15. package/public/right-rail-scrollspy.js +262 -0
  16. package/src/build.ts +230 -0
  17. package/src/cli/args.ts +101 -0
  18. package/src/cli/commands/build.ts +43 -0
  19. package/src/cli/commands/deploy.ts +82 -0
  20. package/src/cli/commands/dev.ts +79 -0
  21. package/src/cli/commands/init.ts +211 -0
  22. package/src/cli/commands/preview.ts +57 -0
  23. package/src/cli/fs.ts +47 -0
  24. package/src/cli/main.ts +120 -0
  25. package/src/cli/normalize.ts +26 -0
  26. package/src/cli/path.ts +30 -0
  27. package/src/cli/prompt.ts +74 -0
  28. package/src/cli/run.ts +17 -0
  29. package/src/cli/version.ts +12 -0
  30. package/src/cli.ts +6 -0
  31. package/src/client/index.ts +7 -0
  32. package/src/content/components/expand.ts +351 -0
  33. package/src/content/components/install-tabs.ts +120 -0
  34. package/src/content/components/registry.ts +12 -0
  35. package/src/content/components/types.ts +21 -0
  36. package/src/content/frontmatter.ts +89 -0
  37. package/src/content/icons.ts +78 -0
  38. package/src/content/llms.ts +94 -0
  39. package/src/content/meta.ts +92 -0
  40. package/src/content/navigation.ts +156 -0
  41. package/src/content/paths.ts +34 -0
  42. package/src/content/store.ts +10 -0
  43. package/src/project/paths.ts +86 -0
  44. package/src/render/layout-loader.ts +46 -0
  45. package/src/render/layout.tsx +340 -0
  46. package/src/render/markdown.ts +14 -0
  47. package/src/render/page-renderer.ts +321 -0
  48. package/src/render/right-rail.tsx +250 -0
  49. package/src/render/toc.ts +66 -0
  50. package/src/search/api.ts +76 -0
  51. package/src/search/contract.ts +44 -0
  52. package/src/search/index.ts +265 -0
  53. package/src/search/page.tsx +96 -0
  54. package/src/search/server-page.ts +99 -0
  55. package/src/seo/files.ts +124 -0
  56. package/src/seo/server.ts +103 -0
  57. package/src/server/headers.ts +10 -0
  58. package/src/server/live-reload.ts +121 -0
  59. package/src/server/static.ts +59 -0
  60. package/src/server/user-routes.ts +209 -0
  61. package/src/server.ts +234 -0
  62. package/src/site/config.ts +244 -0
  63. package/src/site/url-policy.ts +60 -0
  64. package/src/site/urls.ts +46 -0
  65. package/templates/default/README.md +26 -0
  66. package/templates/default/package.json +29 -0
  67. package/templates/default/site/client/layout.tsx +2 -0
  68. package/templates/default/site/client/right-rail.tsx +1 -0
  69. package/templates/default/site/client/search-page.tsx +1 -0
  70. package/templates/default/site/content/404.md +8 -0
  71. package/templates/default/site/content/about.md +10 -0
  72. package/templates/default/site/content/index.md +10 -0
  73. package/templates/default/site/icons/file.svg +1 -0
  74. package/templates/default/site/icons/home.svg +1 -0
  75. package/templates/default/site/icons/info.svg +1 -0
  76. package/templates/default/site/public/_idcmd/live-reload.js +18 -0
  77. package/templates/default/site/public/_idcmd/llm-menu.js +153 -0
  78. package/templates/default/site/public/_idcmd/nav-prefetch.js +30 -0
  79. package/templates/default/site/public/_idcmd/right-rail-scrollspy.js +262 -0
  80. package/templates/default/site/public/anthropic-white.svg +16 -0
  81. package/templates/default/site/public/favicon.svg +13 -0
  82. package/templates/default/site/public/openai-white.svg +15 -0
  83. package/templates/default/site/server/routes/api/hello.ts +2 -0
  84. package/templates/default/site/server/server.ts +4 -0
  85. package/templates/default/site/site.jsonc +21 -0
  86. package/templates/default/site/styles/tailwind.css +452 -0
  87. package/templates/default/tsconfig.json +23 -0
  88. package/templates/default/vercel.json +7 -0
  89. package/index.js +0 -2
@@ -0,0 +1,18 @@
1
+ (() => {
2
+ const protocol = location.protocol === "https:" ? "wss:" : "ws:";
3
+ const socket = new WebSocket(
4
+ `${protocol}//${location.host}/_idcmd/live-reload`
5
+ );
6
+
7
+ socket.addEventListener("message", (event) => {
8
+ if (event.data === "reload") {
9
+ location.reload();
10
+ }
11
+ });
12
+
13
+ socket.addEventListener("close", () => {
14
+ setTimeout(() => {
15
+ location.reload();
16
+ }, 1000);
17
+ });
18
+ })();
@@ -0,0 +1,153 @@
1
+ const COPY_SELECTOR = 'a[data-copy-markdown="1"]';
2
+ const LABEL_SELECTOR = '[data-copy-markdown-label="1"]';
3
+ const RESET_DELAY_MS = 2000;
4
+
5
+ const setLinkDisabled = (link, disabled) => {
6
+ if (disabled) {
7
+ link.setAttribute("aria-disabled", "true");
8
+ link.style.pointerEvents = "none";
9
+ link.style.opacity = "0.8";
10
+ return;
11
+ }
12
+
13
+ link.removeAttribute("aria-disabled");
14
+ link.style.pointerEvents = "";
15
+ link.style.opacity = "";
16
+ };
17
+
18
+ const setLinkLabel = (link, next) => {
19
+ const label = link.querySelector(LABEL_SELECTOR);
20
+ if (label) {
21
+ label.textContent = next;
22
+ }
23
+ };
24
+
25
+ const toAbsoluteUrl = (href) => {
26
+ try {
27
+ return new URL(href, window.location.href).toString();
28
+ } catch {
29
+ return href;
30
+ }
31
+ };
32
+
33
+ const createHiddenTextarea = (text) => {
34
+ const textarea = document.createElement("textarea");
35
+ textarea.value = text;
36
+ textarea.setAttribute("readonly", "true");
37
+ textarea.style.position = "fixed";
38
+ textarea.style.left = "-9999px";
39
+ textarea.style.top = "0";
40
+ return textarea;
41
+ };
42
+
43
+ const safeExecCommandCopy = () => {
44
+ try {
45
+ return document.execCommand("copy");
46
+ } catch {
47
+ return false;
48
+ }
49
+ };
50
+
51
+ const copyViaExecCommand = (text) => {
52
+ const textarea = createHiddenTextarea(text);
53
+ document.body.append(textarea);
54
+ textarea.focus();
55
+ textarea.select();
56
+ const ok = safeExecCommandCopy();
57
+ textarea.remove();
58
+ return ok;
59
+ };
60
+
61
+ const copyText = async (text) => {
62
+ const { clipboard } = navigator;
63
+ if (clipboard?.writeText) {
64
+ try {
65
+ await clipboard.writeText(text);
66
+ return true;
67
+ } catch {
68
+ // Fall back below.
69
+ }
70
+ }
71
+
72
+ return copyViaExecCommand(text);
73
+ };
74
+
75
+ const closeMenuIfPresent = (link) => {
76
+ const details = link.closest("details");
77
+ if (details instanceof HTMLDetailsElement) {
78
+ details.open = false;
79
+ }
80
+ };
81
+
82
+ const getOriginalLabel = (link) =>
83
+ link.querySelector(LABEL_SELECTOR)?.textContent ??
84
+ "Copy Markdown to Clipboard";
85
+
86
+ const fetchMarkdownText = async (href) => {
87
+ const res = await fetch(toAbsoluteUrl(href), { credentials: "same-origin" });
88
+ if (!res.ok) {
89
+ return null;
90
+ }
91
+ return res.text();
92
+ };
93
+
94
+ const copyMarkdownFromHref = async (href) => {
95
+ try {
96
+ const text = await fetchMarkdownText(href);
97
+ if (!text) {
98
+ return false;
99
+ }
100
+ return copyText(text);
101
+ } catch {
102
+ return false;
103
+ }
104
+ };
105
+
106
+ const startCopyOperation = (link) => {
107
+ setLinkDisabled(link, true);
108
+ setLinkLabel(link, "Copying...");
109
+ };
110
+
111
+ const finishCopyOperation = (link, ok) => {
112
+ setLinkLabel(link, ok ? "Copied" : "Copy failed");
113
+ closeMenuIfPresent(link);
114
+ };
115
+
116
+ const scheduleResetOperation = (link, originalLabel) => {
117
+ window.setTimeout(() => {
118
+ setLinkLabel(link, originalLabel);
119
+ setLinkDisabled(link, false);
120
+ }, RESET_DELAY_MS);
121
+ };
122
+
123
+ const handleCopyClick = async (link, originalLabel) => {
124
+ const href = link.getAttribute("href");
125
+ if (!href) {
126
+ return;
127
+ }
128
+
129
+ startCopyOperation(link);
130
+ const ok = await copyMarkdownFromHref(href);
131
+ finishCopyOperation(link, ok);
132
+ scheduleResetOperation(link, originalLabel);
133
+ };
134
+
135
+ const attachCopyHandler = (link) => {
136
+ const originalLabel = getOriginalLabel(link);
137
+ link.addEventListener("click", async (event) => {
138
+ event.preventDefault();
139
+ if (link.getAttribute("aria-disabled") === "true") {
140
+ return;
141
+ }
142
+ await handleCopyClick(link, originalLabel);
143
+ });
144
+ };
145
+
146
+ const initCopyMarkdownButtons = () => {
147
+ const links = [...document.querySelectorAll(COPY_SELECTOR)];
148
+ for (const link of links) {
149
+ attachCopyHandler(link);
150
+ }
151
+ };
152
+
153
+ initCopyMarkdownButtons();
@@ -0,0 +1,15 @@
1
+ <svg width="721" height="721" viewBox="0 0 721 721" fill="none" xmlns="http://www.w3.org/2000/svg">
2
+ <g clip-path="url(#clip0_1637_2934)">
3
+ <g clip-path="url(#clip1_1637_2934)">
4
+ <path d="M304.246 294.611V249.028C304.246 245.189 305.687 242.309 309.044 240.392L400.692 187.612C413.167 180.415 428.042 177.058 443.394 177.058C500.971 177.058 537.44 221.682 537.44 269.182C537.44 272.54 537.44 276.379 536.959 280.218L441.954 224.558C436.197 221.201 430.437 221.201 424.68 224.558L304.246 294.611ZM518.245 472.145V363.224C518.245 356.505 515.364 351.707 509.608 348.349L389.174 278.296L428.519 255.743C431.877 253.826 434.757 253.826 438.115 255.743L529.762 308.523C556.154 323.879 573.905 356.505 573.905 388.171C573.905 424.636 552.315 458.225 518.245 472.141V472.145ZM275.937 376.182L236.592 353.152C233.235 351.235 231.794 348.354 231.794 344.515V238.956C231.794 187.617 271.139 148.749 324.4 148.749C344.555 148.749 363.264 155.468 379.102 167.463L284.578 222.164C278.822 225.521 275.942 230.319 275.942 237.039V376.186L275.937 376.182ZM360.626 425.122L304.246 393.455V326.283L360.626 294.616L417.002 326.283V393.455L360.626 425.122ZM396.852 570.989C376.698 570.989 357.989 564.27 342.151 552.276L436.674 497.574C442.431 494.217 445.311 489.419 445.311 482.699V343.552L485.138 366.582C488.495 368.499 489.936 371.379 489.936 375.219V480.778C489.936 532.117 450.109 570.985 396.852 570.985V570.989ZM283.134 463.99L191.486 411.211C165.094 395.854 147.343 363.229 147.343 331.562C147.343 294.616 169.415 261.509 203.48 247.593V356.991C203.48 363.71 206.361 368.508 212.117 371.866L332.074 441.437L292.729 463.99C289.372 465.907 286.491 465.907 283.134 463.99ZM277.859 542.68C223.639 542.68 183.813 501.895 183.813 451.514C183.813 447.675 184.294 443.836 184.771 439.997L279.295 494.698C285.051 498.056 290.812 498.056 296.568 494.698L417.002 425.127V470.71C417.002 474.549 415.562 477.429 412.204 479.346L320.557 532.126C308.081 539.323 293.206 542.68 277.854 542.68H277.859ZM396.852 599.776C454.911 599.776 503.37 558.513 514.41 503.812C568.149 489.896 602.696 439.515 602.696 388.176C602.696 354.587 588.303 321.962 562.392 298.45C564.791 288.373 566.231 278.296 566.231 268.224C566.231 199.611 510.571 148.267 446.274 148.267C433.322 148.267 420.846 150.184 408.37 154.505C386.775 133.392 357.026 119.958 324.4 119.958C266.342 119.958 217.883 161.22 206.843 215.921C153.104 229.837 118.557 280.218 118.557 331.557C118.557 365.146 132.95 397.771 158.861 421.283C156.462 431.36 155.022 441.437 155.022 451.51C155.022 520.123 210.682 571.466 274.978 571.466C287.931 571.466 300.407 569.549 312.883 565.228C334.473 586.341 364.222 599.776 396.852 599.776Z" fill="black"/>
5
+ </g>
6
+ </g>
7
+ <defs>
8
+ <clipPath id="clip0_1637_2934">
9
+ <rect width="720" height="720" fill="white" transform="translate(0.606934 0.0999756)"/>
10
+ </clipPath>
11
+ <clipPath id="clip1_1637_2934">
12
+ <rect width="484.139" height="479.818" fill="white" transform="translate(118.557 119.958)"/>
13
+ </clipPath>
14
+ </defs>
15
+ </svg>
@@ -0,0 +1,15 @@
1
+ <svg width="721" height="721" viewBox="0 0 721 721" fill="none" xmlns="http://www.w3.org/2000/svg">
2
+ <g clip-path="url(#clip0_1637_2935)">
3
+ <g clip-path="url(#clip1_1637_2935)">
4
+ <path d="M304.246 295.411V249.828C304.246 245.989 305.687 243.109 309.044 241.191L400.692 188.412C413.167 181.215 428.042 177.858 443.394 177.858C500.971 177.858 537.44 222.482 537.44 269.982C537.44 273.34 537.44 277.179 536.959 281.018L441.954 225.358C436.197 222 430.437 222 424.68 225.358L304.246 295.411ZM518.245 472.945V364.024C518.245 357.304 515.364 352.507 509.608 349.149L389.174 279.096L428.519 256.543C431.877 254.626 434.757 254.626 438.115 256.543L529.762 309.323C556.154 324.679 573.905 357.304 573.905 388.971C573.905 425.436 552.315 459.024 518.245 472.941V472.945ZM275.937 376.982L236.592 353.952C233.235 352.034 231.794 349.154 231.794 345.315V239.756C231.794 188.416 271.139 149.548 324.4 149.548C344.555 149.548 363.264 156.268 379.102 168.262L284.578 222.964C278.822 226.321 275.942 231.119 275.942 237.838V376.986L275.937 376.982ZM360.626 425.922L304.246 394.255V327.083L360.626 295.416L417.002 327.083V394.255L360.626 425.922ZM396.852 571.789C376.698 571.789 357.989 565.07 342.151 553.075L436.674 498.374C442.431 495.017 445.311 490.219 445.311 483.499V344.352L485.138 367.382C488.495 369.299 489.936 372.179 489.936 376.018V481.577C489.936 532.917 450.109 571.785 396.852 571.785V571.789ZM283.134 464.79L191.486 412.01C165.094 396.654 147.343 364.029 147.343 332.362C147.343 295.416 169.415 262.309 203.48 248.393V357.791C203.48 364.51 206.361 369.308 212.117 372.665L332.074 442.237L292.729 464.79C289.372 466.707 286.491 466.707 283.134 464.79ZM277.859 543.48C223.639 543.48 183.813 502.695 183.813 452.314C183.813 448.475 184.294 444.636 184.771 440.797L279.295 495.498C285.051 498.856 290.812 498.856 296.568 495.498L417.002 425.927V471.509C417.002 475.349 415.562 478.229 412.204 480.146L320.557 532.926C308.081 540.122 293.206 543.48 277.854 543.48H277.859ZM396.852 600.576C454.911 600.576 503.37 559.313 514.41 504.612C568.149 490.696 602.696 440.315 602.696 388.976C602.696 355.387 588.303 322.762 562.392 299.25C564.791 289.173 566.231 279.096 566.231 269.024C566.231 200.411 510.571 149.067 446.274 149.067C433.322 149.067 420.846 150.984 408.37 155.305C386.775 134.192 357.026 120.758 324.4 120.758C266.342 120.758 217.883 162.02 206.843 216.721C153.104 230.637 118.557 281.018 118.557 332.357C118.557 365.946 132.95 398.571 158.861 422.083C156.462 432.16 155.022 442.237 155.022 452.309C155.022 520.922 210.682 572.266 274.978 572.266C287.931 572.266 300.407 570.349 312.883 566.028C334.473 587.141 364.222 600.576 396.852 600.576Z" fill="white"/>
5
+ </g>
6
+ </g>
7
+ <defs>
8
+ <clipPath id="clip0_1637_2935">
9
+ <rect width="720" height="720" fill="white" transform="translate(0.606934 0.899902)"/>
10
+ </clipPath>
11
+ <clipPath id="clip1_1637_2935">
12
+ <rect width="484.139" height="479.818" fill="white" transform="translate(118.557 120.758)"/>
13
+ </clipPath>
14
+ </defs>
15
+ </svg>
@@ -0,0 +1,262 @@
1
+ const TOC_ROOT_SELECTOR = '[data-toc-root="1"]';
2
+ const TOC_LINK_SELECTOR = 'a[data-toc-link="1"][href^="#"]';
3
+ const TOC_SCROLL_CONTAINER_SELECTOR = '[data-toc-scroll-container="1"]';
4
+
5
+ const NAVBAR_GAP_PX = 16;
6
+ const clamp = (value, min, max) => Math.min(Math.max(value, min), max);
7
+
8
+ const getTopOffset = () => {
9
+ const header = document.querySelector("header");
10
+ const headerHeight = header?.getBoundingClientRect().height ?? 0;
11
+ return Math.ceil(headerHeight + NAVBAR_GAP_PX);
12
+ };
13
+
14
+ const decodeAnchorId = (href) => {
15
+ if (!href?.startsWith("#")) {
16
+ return null;
17
+ }
18
+
19
+ const raw = href.slice(1);
20
+ if (!raw) {
21
+ return null;
22
+ }
23
+
24
+ try {
25
+ return decodeURIComponent(raw);
26
+ } catch {
27
+ return raw;
28
+ }
29
+ };
30
+
31
+ const toEntry = (link) => {
32
+ const id = decodeAnchorId(link.getAttribute("href"));
33
+ if (!id) {
34
+ return null;
35
+ }
36
+
37
+ // ids like "11-overview" are valid HTML ids but invalid CSS selectors unless escaped.
38
+ // eslint-disable-next-line unicorn/prefer-query-selector
39
+ const heading = document.getElementById(id);
40
+ if (!heading) {
41
+ return null;
42
+ }
43
+
44
+ return { heading, link, y: 0 };
45
+ };
46
+
47
+ const buildEntries = (tocRoot) =>
48
+ [...tocRoot.querySelectorAll(TOC_LINK_SELECTOR)]
49
+ .map(toEntry)
50
+ .filter((entry) => entry !== null);
51
+
52
+ const measureEntries = (entries) => {
53
+ for (const entry of entries) {
54
+ entry.y = entry.heading.getBoundingClientRect().top + window.scrollY;
55
+ }
56
+ };
57
+
58
+ const setScrollMarginTop = (topOffset) => {
59
+ document.documentElement.style.setProperty(
60
+ "--scroll-margin-top",
61
+ `${topOffset}px`
62
+ );
63
+ };
64
+
65
+ const binarySearchLastAtOrAbove = (entries, anchorLine) => {
66
+ let lo = 0;
67
+ let hi = entries.length;
68
+
69
+ // Find the first entry with y > anchorLine, then step back one.
70
+ while (lo < hi) {
71
+ const mid = Math.floor((lo + hi) / 2);
72
+ if (entries[mid].y <= anchorLine) {
73
+ lo = mid + 1;
74
+ } else {
75
+ hi = mid;
76
+ }
77
+ }
78
+
79
+ return lo - 1;
80
+ };
81
+
82
+ const findActiveIndex = (entries, topOffset) => {
83
+ const anchorLine = window.scrollY + topOffset + 1;
84
+ const best = binarySearchLastAtOrAbove(entries, anchorLine);
85
+ return Math.max(0, best);
86
+ };
87
+
88
+ const parseTransformValues = (transform, prefix) =>
89
+ transform
90
+ .slice(prefix.length, -1)
91
+ .split(",")
92
+ .map((value) => Number.parseFloat(value.trim()));
93
+
94
+ const getNumberAtIndex = (values, index) => {
95
+ const [value] = values.slice(index, index + 1);
96
+ return Number.isFinite(value) ? value : 0;
97
+ };
98
+
99
+ const getMatrix3dTranslateY = (values) => getNumberAtIndex(values, 13);
100
+
101
+ const getMatrixTranslateY = (values) => getNumberAtIndex(values, 5);
102
+
103
+ const getComputedTranslateY = (element) => {
104
+ const { transform } = window.getComputedStyle(element);
105
+ if (!transform || transform === "none") {
106
+ return 0;
107
+ }
108
+
109
+ if (transform.startsWith("matrix3d(")) {
110
+ const values = parseTransformValues(transform, "matrix3d(");
111
+ return getMatrix3dTranslateY(values);
112
+ }
113
+
114
+ if (transform.startsWith("matrix(")) {
115
+ const values = parseTransformValues(transform, "matrix(");
116
+ return getMatrixTranslateY(values);
117
+ }
118
+
119
+ return 0;
120
+ };
121
+
122
+ const setTranslateY = (element, next) => {
123
+ element.style.transform = `translate3d(0, ${next}px, 0)`;
124
+ };
125
+
126
+ const getElementCenterY = (element) => {
127
+ const rect = element.getBoundingClientRect();
128
+ return rect.top + rect.height / 2;
129
+ };
130
+
131
+ const getCenteredTranslateY = (scrollContainer, list, link) => {
132
+ const containerHeight = scrollContainer.clientHeight;
133
+ const listHeight = list.scrollHeight;
134
+
135
+ if (listHeight <= containerHeight + 1) {
136
+ return 0;
137
+ }
138
+
139
+ const delta = getElementCenterY(scrollContainer) - getElementCenterY(link);
140
+ const currentTranslate = getComputedTranslateY(list);
141
+ const minTranslate = containerHeight - listHeight;
142
+ return clamp(currentTranslate + delta, minTranslate, 0);
143
+ };
144
+
145
+ const centerLinkIfNeeded = (state, link) => {
146
+ const { scrollContainer, tocList } = state;
147
+ if (!scrollContainer || !tocList) {
148
+ return;
149
+ }
150
+
151
+ const next = getCenteredTranslateY(scrollContainer, tocList, link);
152
+ const current = getComputedTranslateY(tocList);
153
+ if (Math.abs(current - next) < 0.5) {
154
+ return;
155
+ }
156
+
157
+ setTranslateY(tocList, next);
158
+ };
159
+
160
+ const setActiveLink = (state, index) => {
161
+ if (index === state.activeIndex) {
162
+ return;
163
+ }
164
+
165
+ const previous = state.entries[state.activeIndex];
166
+ previous?.link.removeAttribute("aria-current");
167
+
168
+ state.activeIndex = index;
169
+ const current = state.entries[state.activeIndex];
170
+ current.link.setAttribute("aria-current", "location");
171
+
172
+ if (state.centerActiveItem) {
173
+ centerLinkIfNeeded(state, current.link);
174
+ }
175
+ };
176
+
177
+ const updateActive = (state) => {
178
+ setActiveLink(state, findActiveIndex(state.entries, state.topOffset));
179
+ };
180
+
181
+ const scheduleUpdate = (state) => {
182
+ if (state.isTicking) {
183
+ return;
184
+ }
185
+
186
+ state.isTicking = true;
187
+ requestAnimationFrame(() => {
188
+ state.isTicking = false;
189
+ updateActive(state);
190
+ });
191
+ };
192
+
193
+ const refreshLayout = (state) => {
194
+ state.topOffset = getTopOffset();
195
+ setScrollMarginTop(state.topOffset);
196
+ measureEntries(state.entries);
197
+ updateActive(state);
198
+
199
+ if (state.centerActiveItem) {
200
+ const current = state.entries[state.activeIndex];
201
+ if (current) {
202
+ centerLinkIfNeeded(state, current.link);
203
+ }
204
+ }
205
+ };
206
+
207
+ const createState = () => {
208
+ const { body } = document;
209
+ const tocRoot = document.querySelector(TOC_ROOT_SELECTOR);
210
+ const entries = tocRoot ? buildEntries(tocRoot) : [];
211
+ if (
212
+ !body ||
213
+ body.dataset.scrollspy !== "1" ||
214
+ !tocRoot ||
215
+ entries.length === 0
216
+ ) {
217
+ return null;
218
+ }
219
+
220
+ const centerActiveItem = body.dataset.scrollspyCenter === "1";
221
+ const scrollContainer = tocRoot.querySelector(TOC_SCROLL_CONTAINER_SELECTOR);
222
+ const tocList = scrollContainer?.querySelector("ul") ?? null;
223
+
224
+ return {
225
+ activeIndex: -1,
226
+ centerActiveItem,
227
+ entries,
228
+ isTicking: false,
229
+ scrollContainer,
230
+ tocList,
231
+ topOffset: getTopOffset(),
232
+ };
233
+ };
234
+
235
+ const start = (state) => {
236
+ // Disable independent TOC scrolling whenever scrollspy is active.
237
+ // The TOC list position is controlled by JS (either centered or left as-is).
238
+ document.body.dataset.tocFollow = "1";
239
+
240
+ window.addEventListener("scroll", () => scheduleUpdate(state), {
241
+ passive: true,
242
+ });
243
+ window.addEventListener("resize", () => refreshLayout(state));
244
+ window.addEventListener("load", () => {
245
+ refreshLayout(state);
246
+ setTimeout(() => refreshLayout(state), 250);
247
+ setTimeout(() => refreshLayout(state), 1000);
248
+ });
249
+
250
+ refreshLayout(state);
251
+ };
252
+
253
+ const init = () => {
254
+ const state = createState();
255
+ if (!state) {
256
+ return;
257
+ }
258
+
259
+ start(state);
260
+ };
261
+
262
+ init();
package/src/build.ts ADDED
@@ -0,0 +1,230 @@
1
+ import { expandMarkdownForAgent } from "./content/components/expand";
2
+ import { generateLlmsTxt } from "./content/llms";
3
+ import { discoverNavigation } from "./content/navigation";
4
+ import { scanContentFiles, slugFromContentFile } from "./content/paths";
5
+ import { getProjectPaths } from "./project/paths";
6
+ import { renderDocument, renderMarkdownPage } from "./render/page-renderer";
7
+ import { generateSearchIndexFromContent } from "./search/index";
8
+ import { renderSearchPageContent } from "./search/page";
9
+ import {
10
+ collectSitemapPagesFromContent,
11
+ generateRobotsTxt,
12
+ generateSitemapXml,
13
+ } from "./seo/files";
14
+ import { loadSiteConfig, resolveRightRailConfig } from "./site/config";
15
+ import { resolveCanonicalUrl } from "./site/urls";
16
+
17
+ const MAX_INDEX_BYTES = 5 * 1024 * 1024;
18
+ const MAX_BUILD_SECONDS = 60;
19
+ const MIN_SEARCH_QUERY_LENGTH = 2;
20
+
21
+ const project = await getProjectPaths();
22
+
23
+ // Find all content files in `content/` (`content/<slug>.md`).
24
+ const contentFiles: string[] = [];
25
+
26
+ const buildStart = performance.now();
27
+
28
+ for await (const file of scanContentFiles()) {
29
+ contentFiles.push(file);
30
+ }
31
+
32
+ console.log(`Found ${contentFiles.length} content pages`);
33
+
34
+ const siteConfig = await loadSiteConfig();
35
+
36
+ // Discover navigation once for all pages
37
+ console.log("Discovering navigation...");
38
+ const navigation = await discoverNavigation();
39
+ console.log(
40
+ `Found ${navigation.length} groups with ${navigation.reduce((acc, g) => acc + g.items.length, 0)} total pages`
41
+ );
42
+
43
+ // Ensure dist directory exists
44
+ await Bun.write(`${project.distDir}/.gitkeep`, "");
45
+
46
+ const shouldCopyPublicPath = (relativePath: string): boolean =>
47
+ // Do not overwrite the minified `dist/styles.css` produced by `build:css`.
48
+ relativePath !== "styles.css";
49
+
50
+ const copyPublicFileToDist = async (relativePath: string): Promise<void> => {
51
+ const file = Bun.file(`${project.publicDir}/${relativePath}`);
52
+ if (!(await file.exists())) {
53
+ return;
54
+ }
55
+ await Bun.write(`${project.distDir}/${relativePath}`, file);
56
+ };
57
+
58
+ const copyPublicToDist = async (): Promise<void> => {
59
+ const publicFiles = new Bun.Glob("**/*").scan(project.publicDir);
60
+ for await (const relativePath of publicFiles) {
61
+ if (shouldCopyPublicPath(relativePath)) {
62
+ await copyPublicFileToDist(relativePath);
63
+ }
64
+ }
65
+ };
66
+
67
+ const writeMarkdownOutputs = async (
68
+ slug: string,
69
+ markdown: string
70
+ ): Promise<void> => {
71
+ const flatMarkdownPath =
72
+ slug === "index"
73
+ ? `${project.distDir}/index.md`
74
+ : `${project.distDir}/${slug}.md`;
75
+
76
+ const expanded = await expandMarkdownForAgent(markdown, {
77
+ currentPath: slug === "index" ? "/" : `/${slug}/`,
78
+ instanceId: `${slug}:build-md`,
79
+ isDev: false,
80
+ slug,
81
+ });
82
+
83
+ await Bun.write(flatMarkdownPath, expanded);
84
+ console.log(` markdown -> ${flatMarkdownPath}`);
85
+ };
86
+
87
+ await copyPublicToDist();
88
+
89
+ const resolveCssSource = async (): Promise<string | null> => {
90
+ if (await Bun.file(`${project.distDir}/styles.css`).exists()) {
91
+ return `${project.distDir}/styles.css`;
92
+ }
93
+ if (await Bun.file(`${project.publicDir}/styles.css`).exists()) {
94
+ return `${project.publicDir}/styles.css`;
95
+ }
96
+ return null;
97
+ };
98
+
99
+ const cssSource = await resolveCssSource();
100
+ const inlineCss = cssSource ? await Bun.file(cssSource).text() : undefined;
101
+ const cssPath = inlineCss ? undefined : "/styles.css";
102
+
103
+ const renderStaticSearchPage = (): Promise<string> => {
104
+ const topPages = navigation
105
+ .flatMap((group) => group.items)
106
+ .slice(0, 8)
107
+ .map((item) => ({ href: item.href, title: item.title }));
108
+
109
+ const contentHtml = renderSearchPageContent({
110
+ minQueryLength: MIN_SEARCH_QUERY_LENGTH,
111
+ query: "",
112
+ results: [],
113
+ topPages,
114
+ });
115
+
116
+ const rightRail = resolveRightRailConfig(siteConfig.rightRail);
117
+ const canonicalUrl = resolveCanonicalUrl(
118
+ { configuredBaseUrl: siteConfig.baseUrl, isDev: false },
119
+ "/search/"
120
+ );
121
+
122
+ return renderDocument({
123
+ canonicalUrl,
124
+ contentHtml,
125
+ cssPath,
126
+ currentPath: "/search/",
127
+ description: siteConfig.description,
128
+ inlineCss,
129
+ navigation,
130
+ rightRail,
131
+ searchQuery: "",
132
+ showRightRail: false,
133
+ siteName: siteConfig.name,
134
+ title: `Search - ${siteConfig.name}`,
135
+ tocItems: [],
136
+ });
137
+ };
138
+
139
+ await Bun.write(
140
+ `${project.distDir}/search/index.html`,
141
+ await renderStaticSearchPage()
142
+ );
143
+ console.log(` generated ${project.distDir}/search/index.html`);
144
+
145
+ for (const file of contentFiles) {
146
+ const filePath = `${project.contentDir}/${file}`;
147
+ const markdown = await Bun.file(filePath).text();
148
+
149
+ const slug = slugFromContentFile(file);
150
+ const currentPath = slug === "index" ? "/" : `/${slug}/`;
151
+
152
+ const html = await renderMarkdownPage(markdown, {
153
+ cssPath,
154
+ currentPath,
155
+ inlineCss,
156
+ navigation,
157
+ siteConfig,
158
+ });
159
+
160
+ let outPath: string;
161
+ if (slug === "index") {
162
+ outPath = `${project.distDir}/index.html`;
163
+ } else if (slug === "404") {
164
+ outPath = `${project.distDir}/404.html`;
165
+ } else {
166
+ outPath = `${project.distDir}/${slug}/index.html`;
167
+ }
168
+
169
+ await Bun.write(outPath, html);
170
+ console.log(` ${filePath} -> ${outPath}`);
171
+ await writeMarkdownOutputs(slug, markdown);
172
+
173
+ if (slug === "404") {
174
+ const nested404Path = `${project.distDir}/404/index.html`;
175
+ await Bun.write(nested404Path, html);
176
+ console.log(` ${filePath} -> ${nested404Path}`);
177
+ }
178
+ }
179
+
180
+ const llmsTxt = await generateLlmsTxt();
181
+ await Bun.write(`${project.distDir}/llms.txt`, llmsTxt);
182
+ console.log(` generated ${project.distDir}/llms.txt`);
183
+
184
+ const searchIndex = await generateSearchIndexFromContent({ siteConfig });
185
+ await Bun.write(
186
+ `${project.distDir}/search-index.json`,
187
+ JSON.stringify(searchIndex)
188
+ );
189
+ const searchIndexBytes = Bun.file(`${project.distDir}/search-index.json`).size;
190
+ console.log(
191
+ ` generated dist/search-index.json (${(searchIndexBytes / (1024 * 1024)).toFixed(2)} MB)`
192
+ );
193
+ if (searchIndexBytes > MAX_INDEX_BYTES) {
194
+ console.warn(
195
+ `Warning: search index exceeds target of 5 MB (${searchIndexBytes} bytes)`
196
+ );
197
+ }
198
+
199
+ if (siteConfig.baseUrl) {
200
+ const sitemapPages = await collectSitemapPagesFromContent();
201
+ await Bun.write(
202
+ `${project.distDir}/sitemap.xml`,
203
+ generateSitemapXml(sitemapPages, siteConfig.baseUrl)
204
+ );
205
+ console.log(` generated ${project.distDir}/sitemap.xml`);
206
+
207
+ await Bun.write(
208
+ `${project.distDir}/robots.txt`,
209
+ generateRobotsTxt(siteConfig.baseUrl)
210
+ );
211
+ console.log(` generated ${project.distDir}/robots.txt`);
212
+ } else {
213
+ console.log(
214
+ "Warning: site.jsonc missing baseUrl; skipping sitemap.xml and robots.txt."
215
+ );
216
+ }
217
+
218
+ if (cssSource) {
219
+ console.log(`Using CSS from ${cssSource}`);
220
+ } else {
221
+ console.log("Warning: No styles.css found. Run build:css first.");
222
+ }
223
+
224
+ const buildSeconds = (performance.now() - buildStart) / 1000;
225
+ console.log(`Build duration: ${buildSeconds.toFixed(2)}s`);
226
+ if (buildSeconds > MAX_BUILD_SECONDS) {
227
+ console.warn(`Warning: build exceeds target of ${MAX_BUILD_SECONDS}s`);
228
+ }
229
+
230
+ console.log("\nBuild complete!");