-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathbuild-release.mjs
More file actions
1212 lines (1077 loc) · 42.7 KB
/
Copy pathbuild-release.mjs
File metadata and controls
1212 lines (1077 loc) · 42.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env node
import fs from "node:fs/promises";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { transform } from "esbuild";
import { marked } from "marked";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const docsRoot = path.resolve(__dirname, "..", "docs");
const sourceIndexPath = path.join(__dirname, "index.html");
const sourceStylesPath = path.join(__dirname, "styles.css");
const sourceAppJsPath = path.join(__dirname, "app.js");
const sourceNavPath = path.join(__dirname, "content.json");
const sourceRedirectsPath = path.join(__dirname, "redirects.json");
const sourceVendorDir = path.join(__dirname, "vendor");
const screenshotsSourceDir = path.join(docsRoot, "user-guides", "screenshots");
const defaultSiteUrl = "https://docs.paperclip.ing";
const defaultSeoDescription = "Guides, references, and walkthroughs for running Paperclip, an AI company operating system for agent teams, governance, budgets, and workflows.";
function printUsage() {
console.log(`Usage: node site/build-release.mjs [options]
Options:
--base-path <path> Public URL base path for the uploaded docs bundle.
Examples: /, /docs/, /random/paperclip-docs/, auto
Default: auto (explicit paths are recommended for deployment)
--site-url <url> Absolute public origin used for canonical URLs and sitemaps.
Default: ${defaultSiteUrl}
--out-dir <path> Output directory for the release bundle.
Default: site/release
--help Show this help text.`);
}
function parseArgs(argv) {
const options = {
basePath: "auto",
siteUrl: defaultSiteUrl,
outDir: path.join(__dirname, "release"),
};
for (let index = 0; index < argv.length; index += 1) {
const arg = argv[index];
if (arg === "--help") {
printUsage();
process.exit(0);
}
if (arg === "--base-path") {
const value = argv[index + 1];
if (!value) {
throw new Error("--base-path requires a value.");
}
options.basePath = normalizeBasePath(value);
index += 1;
continue;
}
if (arg === "--out-dir") {
const value = argv[index + 1];
if (!value) {
throw new Error("--out-dir requires a value.");
}
options.outDir = path.resolve(process.cwd(), value);
index += 1;
continue;
}
if (arg === "--site-url") {
const value = argv[index + 1];
if (!value) {
throw new Error("--site-url requires a value.");
}
options.siteUrl = normalizeSiteUrl(value);
index += 1;
continue;
}
throw new Error(`Unknown argument: ${arg}`);
}
return options;
}
function normalizeBasePath(value) {
const trimmed = value.trim();
if (!trimmed || trimmed === "auto") return "auto";
if (!trimmed || trimmed === "/") return "/";
const withLeadingSlash = trimmed.startsWith("/") ? trimmed : `/${trimmed}`;
return withLeadingSlash.endsWith("/") ? withLeadingSlash : `${withLeadingSlash}/`;
}
function normalizeSiteUrl(value) {
const trimmed = value.trim().replace(/\/+$/, "");
if (!trimmed) throw new Error("--site-url must not be empty.");
const parsed = new URL(trimmed);
if (parsed.protocol !== "https:" && parsed.protocol !== "http:") {
throw new Error("--site-url must be an http(s) URL.");
}
return parsed.toString().replace(/\/+$/, "");
}
function toPosixPath(value) {
return value.split(path.sep).join("/");
}
function normalizeRouteKey(value) {
return String(value || "").replace(/^\/+/, "").replace(/\/+$/, "");
}
function normalizeDocPath(value) {
const normalized = [];
for (const segment of value.split("/")) {
if (!segment || segment === ".") continue;
if (segment === "..") {
if (normalized.length && normalized[normalized.length - 1] !== "..") {
normalized.pop();
} else {
normalized.push("..");
}
continue;
}
normalized.push(segment);
}
return normalized.join("/");
}
function derivePageSlug(file) {
const normalized = normalizeDocPath(file).replace(/^(\.\.\/)+/, "");
const withoutExtension = normalized.replace(/\.md$/, "");
if (withoutExtension.startsWith("user-guides/guides/")) {
return withoutExtension.slice("user-guides/guides/".length);
}
return withoutExtension;
}
function isNavPage(node) {
return Boolean(node && typeof node === "object" && typeof node.file === "string");
}
function getNavChildren(node) {
return Array.isArray(node?.pages) ? node.pages : [];
}
export function flattenNavPages(nav) {
const pages = [];
for (const section of nav.sections || []) {
const visit = (nodes, groupTrail = []) => {
for (const node of getNavChildren({ pages: nodes })) {
if (isNavPage(node)) {
pages.push({
page: node,
section,
navTrail: [section.title, ...groupTrail, node.title],
});
continue;
}
const children = getNavChildren(node);
if (children.length) visit(children, [...groupTrail, node.title].filter(Boolean));
}
};
visit(section.pages || []);
}
return pages;
}
export function attachSlugs(nav) {
const slugCounts = new Map();
for (const { page, section, navTrail } of flattenNavPages(nav)) {
const baseSlug = normalizeRouteKey(page.slug || derivePageSlug(page.file));
const seenCount = slugCounts.get(baseSlug) || 0;
page.slug = seenCount === 0 ? baseSlug : `${baseSlug}-${seenCount + 1}`;
slugCounts.set(baseSlug, seenCount + 1);
page.sectionTitle = section.title;
page.navTrail = navTrail;
}
return nav;
}
export function isPathInside(parentPath, targetPath) {
const rel = path.relative(parentPath, targetPath);
return rel === "" || (rel && !rel.startsWith("..") && !path.isAbsolute(rel));
}
function releaseTargetPathForDoc(sourcePath, releaseRoot) {
if (!isPathInside(docsRoot, sourcePath)) {
throw new Error(`Refusing to copy a file outside docs/: ${path.relative(process.cwd(), sourcePath)}`);
}
const relativeFromDocsRoot = path.relative(docsRoot, sourcePath);
const targetPath = path.join(releaseRoot, relativeFromDocsRoot);
if (!isPathInside(releaseRoot, targetPath)) {
throw new Error(`Refusing to write outside release directory: ${path.relative(process.cwd(), targetPath)}`);
}
return targetPath;
}
/**
* Parse YAML frontmatter from the head of a markdown string.
*
* Supports only the simple `key: value` shape (one per line). Values may be
* optionally wrapped in single or double quotes; quotes are stripped. The
* frontmatter must start at byte 0 with `---` followed by a newline, and end
* with another `---` on its own line. Malformed or missing frontmatter is
* treated as "no frontmatter" — the original body is returned and the parsed
* object is empty.
*
* Returns `{ body, frontmatter }`.
*/
export function parseFrontmatter(source) {
if (typeof source !== "string") return { body: source, frontmatter: {} };
if (!source.startsWith("---\n") && !source.startsWith("---\r\n")) {
return { body: source, frontmatter: {} };
}
// Find the closing fence: a line containing only `---`.
const closeRegex = /\r?\n---[ \t]*(\r?\n|$)/;
const afterOpen = source.indexOf("\n") + 1;
const rest = source.slice(afterOpen);
const closeMatch = rest.match(closeRegex);
if (!closeMatch) {
return { body: source, frontmatter: {} };
}
const yamlBlock = rest.slice(0, closeMatch.index);
let body = rest.slice(closeMatch.index + closeMatch[0].length);
// Consume a single blank line that authors typically leave between the
// closing fence and the first line of real content. Keeps headings flush.
body = body.replace(/^\r?\n/, "");
const frontmatter = {};
for (const rawLine of yamlBlock.split(/\r?\n/)) {
const line = rawLine.trim();
if (!line || line.startsWith("#")) continue;
const match = line.match(/^([A-Za-z_][\w.-]*)\s*:\s*(.*)$/);
if (!match) continue;
let value = match[2].trim();
if (
(value.startsWith('"') && value.endsWith('"')) ||
(value.startsWith("'") && value.endsWith("'"))
) {
value = value.slice(1, -1);
}
frontmatter[match[1]] = value;
}
return { body, frontmatter };
}
function isLocalDocHref(href) {
return !/^(?:[a-z]+:)?\/\//i.test(href) && !href.startsWith("#");
}
async function pathExists(targetPath) {
try {
await fs.access(targetPath);
return true;
} catch {
return false;
}
}
async function ensureDir(targetPath) {
await fs.mkdir(targetPath, { recursive: true });
}
async function copyFileIntoRelease(sourcePath, releaseRoot) {
const targetPath = releaseTargetPathForDoc(sourcePath, releaseRoot);
await ensureDir(path.dirname(targetPath));
await fs.copyFile(sourcePath, targetPath);
}
// Copy a markdown file into the release bundle while stripping any YAML
// frontmatter. Returns the parsed frontmatter object (empty if none).
async function copyMarkdownIntoRelease(sourcePath, releaseRoot) {
const targetPath = releaseTargetPathForDoc(sourcePath, releaseRoot);
await ensureDir(path.dirname(targetPath));
const source = await fs.readFile(sourcePath, "utf8");
const { body, frontmatter } = parseFrontmatter(source);
await fs.writeFile(targetPath, body);
return frontmatter;
}
async function copyDirRecursive(sourceDir, targetDir) {
await ensureDir(targetDir);
const entries = await fs.readdir(sourceDir, { withFileTypes: true });
for (const entry of entries) {
const sourcePath = path.join(sourceDir, entry.name);
const targetPath = path.join(targetDir, entry.name);
if (entry.isDirectory()) {
await copyDirRecursive(sourcePath, targetPath);
} else if (entry.isFile()) {
await ensureDir(path.dirname(targetPath));
await fs.copyFile(sourcePath, targetPath);
}
}
}
function rewriteAppJs(source, basePath) {
const appBaseBlock = `const APP_DIR_NAME = 'site';
const APP_BASE_PATH = (() => {
const marker = \`/\${APP_DIR_NAME}\`;
const pathname = window.location.pathname;
const markerIndex = pathname.indexOf(marker);
if (markerIndex === -1) return '';
return pathname.slice(0, markerIndex + marker.length);
})();
const APP_BASE_URL = new URL(\`\${APP_BASE_PATH.replace(/\\/$/, '')}/\`, window.location.origin);
const APP_SHELL_URL = new URL('index.html', APP_BASE_URL);`;
const rewrittenBaseBlock = `const RELEASE_BASE_PATH = ${JSON.stringify(basePath)};
let APP_BASE_PATH = "/";
let APP_BASE_URL = new URL("/", window.location.origin);
let APP_SHELL_URL = new URL("index.html", APP_BASE_URL);
let PRELOADED_NAV_DATA = null;
function applyAppBasePath(basePath) {
APP_BASE_PATH = !basePath || basePath === "auto" ? "/" : (basePath.endsWith("/") ? basePath : \`\${basePath}/\`);
APP_BASE_URL = new URL(\`\${APP_BASE_PATH.replace(/\\/$/, "")}/\`, window.location.origin);
APP_SHELL_URL = new URL("index.html", APP_BASE_URL);
}
function isNavPayload(value) {
const isNavPageNode = (node) => Boolean(
node &&
typeof node === "object" &&
typeof node.title === "string" &&
(
typeof node.file === "string" ||
(Array.isArray(node.pages) && node.pages.every(isNavPageNode))
)
);
return Boolean(
value &&
typeof value === "object" &&
Array.isArray(value.sections) &&
value.sections.every((section) =>
section &&
typeof section === "object" &&
typeof section.title === "string" &&
Array.isArray(section.pages) &&
section.pages.every(isNavPageNode)
)
);
}
async function fetchNavForBasePath(basePath) {
const normalizedBasePath = !basePath || basePath === "auto"
? "/"
: (basePath.endsWith("/") ? basePath : \`\${basePath}/\`);
const baseUrl = new URL(\`\${normalizedBasePath.replace(/\\/$/, "")}/\`, window.location.origin);
const response = await fetch(new URL("content.json", baseUrl), {
cache: "no-store",
headers: { Accept: "application/json" },
});
if (!response.ok) return null;
const text = await response.text();
let parsed;
try {
parsed = JSON.parse(text);
} catch {
return null;
}
if (!isNavPayload(parsed)) return null;
return parsed;
}
async function detectAppBasePath() {
if (RELEASE_BASE_PATH !== "auto") {
applyAppBasePath(RELEASE_BASE_PATH);
try {
PRELOADED_NAV_DATA = await fetchNavForBasePath(RELEASE_BASE_PATH);
} catch {
PRELOADED_NAV_DATA = null;
}
return;
}
const cleanPath = window.location.pathname.replace(/\\/index\\.html$/, "").replace(/\\/$/, "");
const segments = cleanPath.split("/").filter(Boolean);
const candidates = [];
for (let index = segments.length; index >= 0; index -= 1) {
const prefix = segments.slice(0, index).join("/");
const candidate = prefix ? \`/\${prefix}/\` : "/";
if (!candidates.includes(candidate)) candidates.push(candidate);
}
for (const candidate of candidates) {
try {
const navData = await fetchNavForBasePath(candidate);
if (navData) {
applyAppBasePath(candidate);
PRELOADED_NAV_DATA = navData;
return;
}
} catch {
// Keep probing parent paths until a valid content.json is found.
}
}
applyAppBasePath("/");
}`;
let output = source.replace(appBaseBlock, rewrittenBaseBlock);
if (output === source) {
throw new Error("Could not rewrite the docs shell base-path block.");
}
output = output.replace(
"async function init() {\n try {",
"async function init() {\n await detectAppBasePath();\n try {",
);
if (!output.includes("await detectAppBasePath();")) {
throw new Error("Could not wire base-path detection into init().");
}
output = output.replace(
` try {
const res = await fetch(resolveContentUrl('content.json'));
if (!res.ok) throw new Error(\`content.json \${res.status}\`);
navData = await res.json();
} catch (e) {`,
` try {
if (PRELOADED_NAV_DATA) {
navData = PRELOADED_NAV_DATA;
} else {
const res = await fetch(resolveContentUrl("content.json"), {
headers: { Accept: "application/json" },
});
if (!res.ok) throw new Error(\`content.json \${res.status}\`);
const text = await res.text();
try {
navData = JSON.parse(text);
} catch {
throw new Error("content.json did not return valid JSON. The server is likely rewriting missing JSON requests to index.html.");
}
}
if (!isNavPayload(navData)) {
throw new Error("content.json did not match the expected Paperclip docs schema.");
}
} catch (e) {`,
);
output = output.replace("../docs/user-guides/screenshots/", "user-guides/screenshots/");
output = output.replace(
"Could not load content.json. Check site hosting and rewrite configuration.",
"Could not load content.json. Check that the release bundle was uploaded intact and the base path is correct.",
);
return output;
}
function getDeploymentBasePath(basePath) {
return basePath === "auto" ? "/paperclip-docs/" : basePath;
}
function escapeHtml(value) {
return String(value)
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """);
}
function escapeAttr(value) {
return escapeHtml(value).replace(/'/g, "'");
}
function escapeXml(value) {
return String(value)
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
const responsiveScreenshotVariants = new Map([
["dashboard/dashboard-overview.png", { width: 2880, height: 1800, variantWidth: 900 }],
]);
function screenshotReleasePath(src, theme = "dark") {
const match = String(src).match(/(?:^|\/)user-guides\/screenshots\/(?:light|dark)\/(.+)$/);
if (match) return `user-guides/screenshots/${theme}/${match[1]}`;
return src;
}
function screenshotVariantConfig(src) {
const match = String(src).match(/(?:^|\/)user-guides\/screenshots\/(?:light|dark)\/(.+)$/);
if (!match) return null;
return responsiveScreenshotVariants.get(match[1]) || null;
}
function releaseMarkdownImage(href, title, text) {
const src = screenshotReleasePath(href);
const attrs = [
`src="${escapeAttr(src)}"`,
`alt="${escapeAttr(text || "")}"`,
];
if (title) attrs.push(`title="${escapeAttr(title)}"`);
const variantConfig = screenshotVariantConfig(src);
if (variantConfig) {
const optimizedSrc = src.replace(/\.png(?:\?.*)?$/i, "-900.webp");
attrs.push(
`class="responsive-screenshot"`,
`data-screenshot="${escapeAttr(href)}"`,
`width="${variantConfig.width}"`,
`height="${variantConfig.height}"`,
`sizes="(max-width: 820px) calc(100vw - 48px), 820px"`,
`srcset="${escapeAttr(`${optimizedSrc} ${variantConfig.variantWidth}w, ${src} ${variantConfig.width}w`)}"`,
`decoding="async"`,
`loading="eager"`,
`fetchpriority="high"`,
`style="aspect-ratio:${variantConfig.width}/${variantConfig.height}"`,
);
}
return `<img ${attrs.join(" ")}>`;
}
function markdownToPlainText(markdown) {
return markdown
.replace(/```[\s\S]*?```/g, " ")
.replace(/`([^`]+)`/g, "$1")
.replace(/!\[([^\]]*)\]\([^)]+\)/g, "$1")
.replace(/\[([^\]]+)\]\([^)]+\)/g, "$1")
.replace(/<[^>]+>/g, " ")
.replace(/[*_>#|-]/g, " ")
.replace(/\s+/g, " ")
.trim();
}
function markdownDescription(markdown) {
const block = markdown
.split(/\n{2,}/)
.map((item) => item.trim())
.find((item) =>
item &&
!item.startsWith("#") &&
!item.startsWith("![") &&
!item.startsWith("|") &&
!item.startsWith("---")
);
const description = block ? markdownToPlainText(block) : defaultSeoDescription;
return description.slice(0, 220);
}
function siteUrlForPath(siteUrl, basePath, routePath = "") {
const publicBasePath = basePath === "auto" ? "/" : basePath;
const base = new URL(publicBasePath, `${siteUrl}/`);
return new URL(routePath.replace(/^\/+/, ""), base).toString();
}
function routeUrlForPage(siteUrl, basePath, page) {
return siteUrlForPath(siteUrl, basePath, `${page.slug}/`);
}
function buildJsonLd(metadata) {
const graph = metadata.page
? {
"@context": "https://schema.org",
"@type": "TechArticle",
headline: metadata.title.replace(/ \| Paperclip Docs$/, ""),
description: metadata.description,
url: metadata.url,
isPartOf: {
"@type": "WebSite",
name: "Paperclip Docs",
url: siteUrlForPath(metadata.siteUrl, metadata.basePath),
},
}
: {
"@context": "https://schema.org",
"@type": "WebSite",
name: "Paperclip Docs",
description: metadata.description,
url: metadata.url,
};
return JSON.stringify(graph);
}
function escapeScriptContent(value) {
return String(value).replace(/<\//g, "<\\/");
}
function injectSeo(html, metadata, { baseHref = null } = {}) {
const title = escapeHtml(metadata.title);
let output = html.replace(/<title>[\s\S]*?<\/title>/, `<title>${title}</title>`);
output = output.replace(/\n\s*<(?:meta|link)\b[^>]*data-seo-managed[^>]*>/g, "");
output = output.replace(/\n\s*<script\b[^>]*data-seo-managed[^>]*>[\s\S]*?<\/script>/g, "");
output = output.replace(/\n\s*<base\b[^>]*data-seo-base[^>]*>/g, "");
const tags = [
...(baseHref ? [`<base data-seo-base href="${escapeHtml(baseHref)}" />`] : []),
`<meta name="description" data-seo-managed content="${escapeHtml(metadata.description)}" />`,
`<meta name="robots" data-seo-managed content="index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1" />`,
`<link rel="canonical" data-seo-managed href="${escapeHtml(metadata.url)}" />`,
`<meta property="og:type" data-seo-managed content="${metadata.page ? "article" : "website"}" />`,
`<meta property="og:site_name" data-seo-managed content="Paperclip Docs" />`,
`<meta property="og:title" data-seo-managed content="${title}" />`,
`<meta property="og:description" data-seo-managed content="${escapeHtml(metadata.description)}" />`,
`<meta property="og:url" data-seo-managed content="${escapeHtml(metadata.url)}" />`,
`<meta name="twitter:card" data-seo-managed content="summary" />`,
`<meta name="twitter:title" data-seo-managed content="${title}" />`,
`<meta name="twitter:description" data-seo-managed content="${escapeHtml(metadata.description)}" />`,
`<script type="application/ld+json" data-seo-managed>${escapeScriptContent(buildJsonLd(metadata))}</script>`,
];
return output.replace(/(<title>[\s\S]*?<\/title>)/, `$1\n ${tags.join("\n ")}`);
}
async function pageMetadataForNav(nav, outDir, siteUrl, basePath) {
const pages = [];
for (const { page, section } of flattenNavPages(nav)) {
const releaseMarkdownPath = path.join(outDir, page.file);
const markdown = await fs.readFile(releaseMarkdownPath, "utf8");
const stats = await fs.stat(releaseMarkdownPath);
const h1 = markdown.match(/^#\s+(.+)$/m)?.[1]?.trim();
const pageTitle = page.title || h1 || "Paperclip Docs";
pages.push({
page,
sectionTitle: section.title,
title: `${pageTitle} | Paperclip Docs`,
description: markdownDescription(markdown),
url: routeUrlForPage(siteUrl, basePath, page),
lastmod: stats.mtime.toISOString().slice(0, 10),
siteUrl,
basePath,
});
}
return pages;
}
function buildSitemap({ siteUrl, basePath, pages }) {
const rootLastmod = pages
.map((page) => page.lastmod)
.sort()
.at(-1) || new Date().toISOString().slice(0, 10);
const entries = [
{ loc: siteUrlForPath(siteUrl, basePath), lastmod: rootLastmod, priority: "1.0" },
...pages.map((page) => ({ loc: page.url, lastmod: page.lastmod, priority: "0.8" })),
];
return `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${entries.map((entry) => ` <url>
<loc>${escapeXml(entry.loc)}</loc>
<lastmod>${escapeXml(entry.lastmod)}</lastmod>
<changefreq>weekly</changefreq>
<priority>${entry.priority}</priority>
</url>`).join("\n")}
</urlset>
`;
}
function buildRobots({ siteUrl, basePath }) {
return `User-agent: *
Allow: /
Sitemap: ${siteUrlForPath(siteUrl, basePath, "sitemap.xml")}
`;
}
// llms.txt (https://llmstxt.org): a plain-text map of the docs for answer
// engines. Title + summary, then every guide/reference grouped by nav
// section with a descriptive line, so an LLM can cite the specific page.
function buildLlmsTxt({ siteUrl, basePath, pages }) {
const rootUrl = siteUrlForPath(siteUrl, basePath);
const groups = [];
const indexBySection = new Map();
for (const page of pages) {
const section = page.sectionTitle || "Docs";
if (!indexBySection.has(section)) {
indexBySection.set(section, groups.length);
groups.push({ section, items: [] });
}
const title = page.title.replace(/ \| Paperclip Docs$/, "");
const description = (page.description || "")
.replace(/\s+/g, " ")
.trim();
groups[indexBySection.get(section)].items.push({ title, url: page.url, description });
}
const lines = [
"# Paperclip Docs",
"",
`> ${defaultSeoDescription}`,
"",
"Paperclip is an open-source AI company operating system: you define a goal, hire AI agents into an org chart with roles, budgets, and approvals, and they pick up ticketed work on a heartbeat. This documentation covers installing Paperclip, running your first company, day-to-day operation, and the full API, CLI, adapter, and deployment reference.",
"",
`- Docs home: ${rootUrl}`,
"- Product site: https://paperclip.ing",
"- Source: https://github.com/paperclipai/paperclip",
"",
];
for (const group of groups) {
lines.push(`## ${group.section}`, "");
for (const item of group.items) {
const suffix = item.description ? `: ${item.description}` : "";
lines.push(`- [${item.title}](${item.url})${suffix}`);
}
lines.push("");
}
return `${lines.join("\n").trimEnd()}\n`;
}
function cloudflarePathForRoute(basePath, routePath, { trailingSlash = false } = {}) {
const baseKey = normalizeRouteKey(getDeploymentBasePath(basePath));
const routeKey = normalizeRouteKey(routePath);
const key = [baseKey, routeKey].filter(Boolean).join("/");
return `/${key}${trailingSlash ? "/" : ""}`;
}
function cloudflareRedirectLine(basePath, sourceRoute, destinationRoute) {
const sourcePath = cloudflarePathForRoute(basePath, sourceRoute);
const sourceSlashPath = cloudflarePathForRoute(basePath, sourceRoute, { trailingSlash: true });
const destinationPath = cloudflarePathForRoute(basePath, destinationRoute, { trailingSlash: true });
if (sourceSlashPath === destinationPath) return [];
return [
`${sourcePath} ${destinationPath} 301`,
`${sourceSlashPath} ${destinationPath} 301`,
];
}
function buildCloudflareRedirects({ basePath, pages, legacyRedirects = {} }) {
const routeRedirects = pages
.map(({ page }) => {
const sourcePath = cloudflarePathForRoute(basePath, page.slug);
const destinationPath = cloudflarePathForRoute(basePath, page.slug, { trailingSlash: true });
return `${sourcePath} ${destinationPath} 301`;
})
.join("\n");
const legacyRouteRedirects = Object.entries(legacyRedirects)
.flatMap(([sourceRoute, destinationRoute]) =>
cloudflareRedirectLine(basePath, sourceRoute, destinationRoute)
)
.join("\n");
return `# Canonical docs URLs include trailing slashes. Keep no-slash requests
# on a normal one-hop 301 instead of Cloudflare Pages' implicit directory 308.
${routeRedirects}
# Legacy docs URLs moved during the information architecture cleanup. Redirect
# them before unknown URLs fall through to 404 so crawlers see one canonical URL per page.
${legacyRouteRedirects}
`;
}
function buildCloudflareHeaders() {
return `/*
Referrer-Policy: strict-origin-when-cross-origin
X-Content-Type-Options: nosniff
Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
Cross-Origin-Opener-Policy: same-origin
Permissions-Policy: camera=(), microphone=(), geolocation=(), payment=()
Content-Security-Policy: default-src 'self'; base-uri 'self'; object-src 'none'; frame-ancestors 'none'; form-action 'none'; img-src 'self' data: https:; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; connect-src 'self' https://api.github.com; upgrade-insecure-requests
/sitemap.xml
Content-Type: application/xml; charset=utf-8
X-Robots-Tag: noindex, nofollow
/robots.txt
Content-Type: text/plain; charset=utf-8
X-Robots-Tag: noindex, nofollow
/llms.txt
Content-Type: text/plain; charset=utf-8
X-Robots-Tag: noindex, nofollow
/*.css
X-Robots-Tag: noindex, nofollow
/*.js
X-Robots-Tag: noindex, nofollow
/*.json
X-Robots-Tag: noindex, nofollow
/*.md
X-Robots-Tag: noindex, nofollow
/*.png
X-Robots-Tag: noindex, nofollow
/*.jpg
X-Robots-Tag: noindex, nofollow
/*.jpeg
X-Robots-Tag: noindex, nofollow
/*.webp
X-Robots-Tag: noindex, nofollow
/*.svg
X-Robots-Tag: noindex, nofollow
/*.txt
X-Robots-Tag: noindex, nofollow
`;
}
function buildNotFoundPage(siteUrl, basePath) {
const metadata = {
title: "Not found | Paperclip Docs",
description: "This Paperclip Docs URL does not exist.",
url: siteUrlForPath(siteUrl, basePath, "404.html"),
siteUrl,
basePath,
};
return `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>${escapeHtml(metadata.title)}</title>
<meta name="robots" content="noindex, nofollow" />
<link rel="canonical" href="${escapeHtml(metadata.url)}" />
</head>
<body>
<main>
<h1>Not found</h1>
<p>This Paperclip Docs URL does not exist.</p>
<p><a href="${escapeAttr(siteUrlForPath(siteUrl, basePath))}">Open the docs home page</a></p>
</main>
</body>
</html>
`;
}
function collectMarkdownLinks(markdown) {
const links = [];
const markdownLinkRegex = /\[[^\]]+\]\(([^)\s]+(?:\s+\"[^\"]*\")?)\)/g;
const htmlImageRegex = /<img\b[^>]*\bsrc=["']([^"']+)["'][^>]*>/gi;
let match;
while ((match = markdownLinkRegex.exec(markdown)) !== null) {
const rawTarget = match[1].trim().replace(/\s+"[^"]*"$/, "");
links.push(rawTarget);
}
while ((match = htmlImageRegex.exec(markdown)) !== null) {
links.push(match[1].trim());
}
return links;
}
async function collectReleaseFiles(nav) {
const markdownFiles = new Set();
const queue = [];
const warnings = [];
for (const { page } of flattenNavPages(nav)) {
const absolutePath = path.resolve(__dirname, page.file);
queue.push(absolutePath);
}
while (queue.length > 0) {
const currentPath = queue.shift();
if (markdownFiles.has(currentPath)) continue;
if (!(await pathExists(currentPath))) {
warnings.push(`Missing markdown file: ${path.relative(process.cwd(), currentPath)}`);
continue;
}
markdownFiles.add(currentPath);
const markdown = await fs.readFile(currentPath, "utf8");
const baseDir = path.dirname(currentPath);
for (const rawHref of collectMarkdownLinks(markdown)) {
const [href] = rawHref.split("#", 1);
if (!href || !isLocalDocHref(href)) continue;
const resolvedPath = path.resolve(baseDir, href);
if (!isPathInside(docsRoot, resolvedPath)) continue;
if (href.endsWith(".md")) {
if (await pathExists(resolvedPath)) {
queue.push(resolvedPath);
} else {
warnings.push(`Missing linked markdown file: ${path.relative(process.cwd(), resolvedPath)}`);
}
}
}
}
return { markdownFiles, warnings };
}
export function rewriteNav(nav) {
const rewriteNodes = (nodes) => getNavChildren({ pages: nodes }).map((node) => {
if (!isNavPage(node)) {
return {
...node,
pages: rewriteNodes(getNavChildren(node)),
};
}
const absolutePath = path.resolve(__dirname, node.file);
const relativeFromDocsRoot = toPosixPath(path.relative(docsRoot, absolutePath));
return {
...node,
file: relativeFromDocsRoot,
};
});
return {
...nav,
sections: nav.sections.map((section) => ({
...section,
pages: rewriteNodes(section.pages),
})),
};
}
function buildHtaccess(basePath) {
const rewriteBaseLine = basePath === "auto" ? "" : `RewriteBase ${basePath}\n\n`;
return `RewriteEngine On
${rewriteBaseLine}RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^ index.html [L]
`;
}
function buildNginxConfig(basePath) {
const deploymentBasePath = getDeploymentBasePath(basePath);
const placeholderComment = basePath === "auto"
? "# Replace /paperclip-docs/ with the public mount path for this bundle before using this snippet.\n"
: "";
return `${placeholderComment}# Paperclip docs static SPA
# Real files must 404 if missing. Only extensionless routes should fall back to index.html.
location ~ ^${deploymentBasePath}.*\\.[A-Za-z0-9]+$ {
try_files $uri =404;
}
location ${deploymentBasePath} {
try_files $uri $uri/ ${deploymentBasePath}index.html;
}
`;
}
function renderTabsBlock(labels, body) {
const names = labels.split(",").map((label) => label.trim());
let output = '<div class="tabs-container">';
output += '<div class="tabs-bar">';
names.forEach((name, index) => {
output += `<button class="tab-btn${index === 0 ? " active" : ""}" data-tab="${escapeAttr(name)}">${escapeHtml(name)}</button>`;
});
output += "</div>";
const tabRegex = /<!-- tab: (.+?) -->([\s\S]*?)(?=<!-- tab:|$)/g;
let match;
let index = 0;
while ((match = tabRegex.exec(body)) !== null) {
output += `<div class="tab-panel${index === 0 ? " active" : ""}" data-panel="${escapeAttr(match[1].trim())}">`;
output += marked.parse(match[2].trim());
output += "</div>";
index += 1;
}
return `${output}</div>`;
}
function preprocessTabs(markdown) {
const openMarker = "<!-- tabs:";
const closeMarker = "<!-- /tabs -->";
const maxIterations = 100;
let output = markdown;
for (let index = 0; index < maxIterations; index += 1) {
const closeIndex = output.indexOf(closeMarker);
if (closeIndex === -1) break;
const openIndex = output.lastIndexOf(openMarker, closeIndex - 1);
if (openIndex === -1) break;
const afterOpen = output.indexOf("-->", openIndex);
if (afterOpen === -1 || afterOpen > closeIndex) break;
const labels = output.slice(openIndex + openMarker.length, afterOpen).trim();
const body = output.slice(afterOpen + 3, closeIndex);
output = output.slice(0, openIndex) + renderTabsBlock(labels, body) + output.slice(closeIndex + closeMarker.length);
}
return output;
}
function renderStaticMarkdown(markdown) {
const renderer = new marked.Renderer();
renderer.image = releaseMarkdownImage;
marked.setOptions({ gfm: true, breaks: false, renderer });
return marked.parse(preprocessTabs(markdown));
}
function inlineReleaseStyles(html, css) {
return html.replace(
'<link rel="stylesheet" href="styles.css" />',
`<style data-inline-release-css>${css}</style>`,
);
}
function buildStaticPageHtml(sourceIndex, metadata, markdown, basePath, releaseStyles) {
const articleHtml = renderStaticMarkdown(markdown);
const routeBaseHref = basePath === "auto" ? "/" : basePath;
return inlineReleaseStyles(injectSeo(sourceIndex, metadata, { baseHref: routeBaseHref }), releaseStyles)
.replace('<section id="landing">', '<section id="landing">')
.replace('<div id="article-view">', '<div id="article-view" class="is-active">')
.replace('<div id="loading">', '<div id="loading" style="display:none">')
.replace('<article id="article" style="display:none"></article>', `<article id="article">${articleHtml}</article>`);
}
async function writeStaticRoutePages({ outDir, sourceIndex, pages, markdownBodiesByFile, basePath, releaseStyles }) {
for (const metadata of pages) {
const { page } = metadata;
const markdown = markdownBodiesByFile.get(page.file);