@writedocs/generator 0.15.0 → 0.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.
- package/package.json +1 -1
- package/packages/components/src/components/CodeGroup.astro +6 -6
- package/src/cli/build.js +1 -1
- package/src/cli/rewrite-redirect-pages.js +8 -6
- package/src/cli/write-mcp-files.js +4 -1
- package/src/cli/write-redirects-file.js +4 -3
- package/src/content.config.ts +21 -1
- package/src/layout/BaseLayout.astro +38 -49
- package/src/layout/components/MobileMenu.astro +9 -5
- package/src/layout/components/TopBar.astro +8 -4
- package/src/lib/appearance.js +64 -0
- package/src/lib/asset-dimensions.js +2 -1
- package/src/lib/bom.js +20 -0
- package/src/lib/config-file.js +21 -8
- package/src/lib/config-schema.js +14 -7
- package/src/lib/config-schema.ts +19 -12
- package/src/lib/content-check.js +36 -1
- package/src/lib/content-store-prune.js +44 -0
- package/src/lib/json-schema-descriptions.js +4 -1
- package/src/lib/mintlify-convert.js +11 -1
- package/src/lib/pages.js +5 -3
- package/src/lib/wdignore-cases.json +9 -2
- package/src/lib/wdignore.js +5 -0
- package/src/lib/writedocs-legacy-convert.js +1 -1
- package/src/pages/llms-full.txt.ts +6 -3
- package/src/pages/llms.txt.ts +4 -4
- package/writedocs.schema.json +23 -0
package/package.json
CHANGED
|
@@ -124,12 +124,12 @@ const { dropdown = false } = Astro.props as Props;
|
|
|
124
124
|
border-radius: 0;
|
|
125
125
|
background: none;
|
|
126
126
|
}
|
|
127
|
-
/* No-JS / too-small-to-tab fallback only
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
.wd-codegroup > .wd-code-block + .wd-code-block {
|
|
127
|
+
/* No-JS / too-small-to-tab fallback only: without JS every block stays
|
|
128
|
+
visible and stacked, each with its own title bar. Scoped off tabbed
|
|
129
|
+
groups because hiding a block (display: none) doesn't stop `+` from
|
|
130
|
+
matching - every tab after the first would show this gap between the
|
|
131
|
+
tab strip and its code panel. */
|
|
132
|
+
.wd-codegroup:not(.wd-codegroup-tabbed) > .wd-code-block + .wd-code-block {
|
|
133
133
|
margin-top: 0.5rem;
|
|
134
134
|
}
|
|
135
135
|
/* 2rem tall, like a block's title bar, with room on the right for the
|
package/src/cli/build.js
CHANGED
|
@@ -188,7 +188,7 @@ export async function runBuild({ contentDir, packageRoot, verbose = false, draft
|
|
|
188
188
|
writeRedirectsFile(siteDir, contentDir, { outDir: distDir, basePath });
|
|
189
189
|
// Every other host follows Astro's redirect pages - rewritten so the
|
|
190
190
|
// reader never sees one (rewrite-redirect-pages.js).
|
|
191
|
-
rewriteRedirectPages(siteDir, { background: siteConfig.styles?.background?.colors });
|
|
191
|
+
rewriteRedirectPages(siteDir, { background: siteConfig.styles?.background?.colors, appearance: siteConfig.styles?.appearance });
|
|
192
192
|
// The site's MCP server at /mcp: mcp-index.json is already in dist/ (an
|
|
193
193
|
// Astro route); this adds the Cloudflare _worker.js that serves it - see
|
|
194
194
|
// write-mcp-files.js for every host.
|
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
// screen. The meta refresh stays for browsers without JavaScript.
|
|
14
14
|
import fs from 'node:fs';
|
|
15
15
|
import path from 'node:path';
|
|
16
|
+
import { themeScript, serverTheme } from '../lib/appearance.js';
|
|
16
17
|
|
|
17
18
|
// Any delay: Astro waits 2 seconds on a 302's page (the automatic "/"), none on a 301's.
|
|
18
19
|
const REFRESH = /<meta http-equiv="refresh" content="\d+;\s*url=([^"]*)"\s*\/?>/i;
|
|
@@ -34,15 +35,16 @@ function cssColor(value, fallback) {
|
|
|
34
35
|
}
|
|
35
36
|
|
|
36
37
|
/** The page for a redirect to `target`. */
|
|
37
|
-
export function redirectPageHtml({ target, canonical = null, background = {} }) {
|
|
38
|
+
export function redirectPageHtml({ target, canonical = null, background = {}, appearance }) {
|
|
38
39
|
const light = cssColor(background.light, DEFAULT_LIGHT);
|
|
39
40
|
const dark = cssColor(background.dark, DEFAULT_DARK);
|
|
40
41
|
// Inside <script>: a JSON string, with "<" escaped so a target can't end the script.
|
|
41
42
|
const js = JSON.stringify(target).replace(/</g, '\\u003c');
|
|
42
43
|
const attr = encodeAttr(target);
|
|
44
|
+
const fixed = serverTheme(appearance);
|
|
43
45
|
return [
|
|
44
46
|
'<!doctype html>',
|
|
45
|
-
'<html lang="en">',
|
|
47
|
+
fixed ? `<html lang="en" data-theme="${fixed}">` : '<html lang="en">',
|
|
46
48
|
'<head>',
|
|
47
49
|
'<meta charset="utf-8">',
|
|
48
50
|
`<script>location.replace(${js} + location.search + location.hash)</script>`,
|
|
@@ -51,8 +53,8 @@ export function redirectPageHtml({ target, canonical = null, background = {} })
|
|
|
51
53
|
'<meta name="robots" content="noindex">',
|
|
52
54
|
canonical ? `<link rel="canonical" href="${encodeAttr(canonical)}">` : '',
|
|
53
55
|
'<title>Redirecting…</title>',
|
|
54
|
-
//
|
|
55
|
-
|
|
56
|
+
// The same theme choice as every page (BaseLayout.astro) - src/lib/appearance.js.
|
|
57
|
+
`<script>${themeScript(appearance)}</script>`,
|
|
56
58
|
`<style>html{background:${light};color-scheme:light}html[data-theme=dark]{background:${dark};color-scheme:dark}@media (prefers-color-scheme:dark){html:not([data-theme]){background:${dark};color-scheme:dark}}body{margin:0}a{position:absolute;width:1px;height:1px;overflow:hidden;clip-path:inset(50%);white-space:nowrap}</style>`,
|
|
57
59
|
'</head>',
|
|
58
60
|
`<body><a href="${attr}">Continue to ${attr}</a></body>`,
|
|
@@ -62,7 +64,7 @@ export function redirectPageHtml({ target, canonical = null, background = {} })
|
|
|
62
64
|
}
|
|
63
65
|
|
|
64
66
|
/** Rewrites every Astro redirect page under `distDir`. Returns how many. */
|
|
65
|
-
export function rewriteRedirectPages(distDir, { background } = {}) {
|
|
67
|
+
export function rewriteRedirectPages(distDir, { background, appearance } = {}) {
|
|
66
68
|
let count = 0;
|
|
67
69
|
const walk = (dir) => {
|
|
68
70
|
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
@@ -76,7 +78,7 @@ export function rewriteRedirectPages(distDir, { background } = {}) {
|
|
|
76
78
|
const refresh = REFRESH.exec(html);
|
|
77
79
|
if (!refresh || !ASTRO_REDIRECT.test(html)) continue;
|
|
78
80
|
const canonical = CANONICAL.exec(html);
|
|
79
|
-
fs.writeFileSync(full, redirectPageHtml({ target: decodeAttr(refresh[1]), canonical: canonical ? decodeAttr(canonical[1]) : null, background }));
|
|
81
|
+
fs.writeFileSync(full, redirectPageHtml({ target: decodeAttr(refresh[1]), canonical: canonical ? decodeAttr(canonical[1]) : null, background, appearance }));
|
|
80
82
|
count++;
|
|
81
83
|
}
|
|
82
84
|
};
|
|
@@ -26,6 +26,7 @@
|
|
|
26
26
|
import fs from 'node:fs';
|
|
27
27
|
import path from 'node:path';
|
|
28
28
|
import { fileURLToPath } from 'node:url';
|
|
29
|
+
import { readText } from '../lib/config-file.js';
|
|
29
30
|
|
|
30
31
|
const SERVER_SOURCE = path.join(path.dirname(fileURLToPath(import.meta.url)), '..', 'mcp', 'server.js');
|
|
31
32
|
const IGNORED_BY_WORKERS = ['_worker.js', '_routes.json'];
|
|
@@ -92,7 +93,9 @@ export function writeMcpFiles(distDir, contentDir, version, { siteDir = distDir,
|
|
|
92
93
|
fs.writeFileSync(path.join(distDir, '_routes.json'), JSON.stringify({ version: 1, include: [`${basePath}/mcp`, `${basePath}/mcp/`], exclude: [] }, null, 2) + '\n');
|
|
93
94
|
|
|
94
95
|
const ignorePath = path.join(distDir, '.assetsignore');
|
|
95
|
-
|
|
96
|
+
// A project's own public/.assetsignore, without a byte order mark
|
|
97
|
+
// (lib/bom.js).
|
|
98
|
+
const existing = fs.existsSync(ignorePath) ? readText(ignorePath) : '';
|
|
96
99
|
const lines = existing.split(/\r?\n/).map((l) => l.trim());
|
|
97
100
|
const missing = IGNORED_BY_WORKERS.filter((name) => !lines.includes(name));
|
|
98
101
|
if (missing.length) {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import fs from 'node:fs';
|
|
2
2
|
import path from 'node:path';
|
|
3
|
-
import { readConfigText } from '../lib/config-file.js';
|
|
3
|
+
import { readConfigText, readText } from '../lib/config-file.js';
|
|
4
4
|
import { readMovedApiPages } from '../lib/moved-api-pages.js';
|
|
5
5
|
import { withBase } from '../lib/base-path.js';
|
|
6
6
|
|
|
@@ -121,8 +121,9 @@ export function writeRedirectsFile(distDir, contentDir, { outDir = distDir, base
|
|
|
121
121
|
const redirectsPath = path.join(outDir, '_redirects');
|
|
122
122
|
// A hand-written public/_redirects is already copied into dist by Astro
|
|
123
123
|
// by this point - preserve it (and its priority: first match wins) by
|
|
124
|
-
// appending after it rather than overwriting.
|
|
125
|
-
|
|
124
|
+
// appending after it rather than overwriting. Without a byte order mark,
|
|
125
|
+
// which would sit in front of its first rule (lib/bom.js).
|
|
126
|
+
const existing = fs.existsSync(redirectsPath) ? readText(redirectsPath).replace(/\s*$/, '\n\n') : '';
|
|
126
127
|
fs.writeFileSync(redirectsPath, existing + generated);
|
|
127
128
|
}
|
|
128
129
|
|
package/src/content.config.ts
CHANGED
|
@@ -8,6 +8,7 @@ import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
|
8
8
|
import { pageFrontmatterSchema, findAllPages } from './lib/config';
|
|
9
9
|
import { titleFromPath } from './lib/pages.js';
|
|
10
10
|
import { writedocsTempDir } from './lib/writedocs-temp-dir.js';
|
|
11
|
+
import { staleEntryIds } from './lib/content-store-prune.js';
|
|
11
12
|
|
|
12
13
|
const contentDir = process.env.WRITEDOCS_CONTENT_DIR || process.cwd();
|
|
13
14
|
|
|
@@ -63,10 +64,16 @@ function skipIfMissing(inner: Loader): Loader {
|
|
|
63
64
|
// Left empty: the last spec taken out of writedocs.json while
|
|
64
65
|
// `writedocs dev` runs (the folder stays then). glob() would only warn
|
|
65
66
|
// about no files and keep the pages it had.
|
|
66
|
-
|
|
67
|
+
const files = fs
|
|
68
|
+
.readdirSync(generatedDocsBase, { recursive: true })
|
|
69
|
+
.map(String)
|
|
70
|
+
.filter((file) => /\.mdx?$/i.test(file))
|
|
71
|
+
.map((file) => path.join(fileURLToPath(generatedDocsBase), file));
|
|
72
|
+
if (files.length === 0) {
|
|
67
73
|
for (const id of context.store.keys()) context.store.delete(id);
|
|
68
74
|
return;
|
|
69
75
|
}
|
|
76
|
+
forgetMovedFiles(context, files);
|
|
70
77
|
return inner.load({ ...withTitleFallback(context), watcher: undefined });
|
|
71
78
|
},
|
|
72
79
|
};
|
|
@@ -92,6 +99,18 @@ const schemaDigest = createHash('sha256')
|
|
|
92
99
|
.slice(0, 16);
|
|
93
100
|
|
|
94
101
|
type LoaderContext = Parameters<Loader['load']>[0];
|
|
102
|
+
|
|
103
|
+
// A page's stored entry is kept, old `filePath` and all, while its id and
|
|
104
|
+
// text stay the same - and a page with a `slug` keeps both when it moves to
|
|
105
|
+
// another folder. Entries whose file isn't one of the pages about to be
|
|
106
|
+
// synced are deleted first, so glob() reads those pages again from where
|
|
107
|
+
// they are now. Without this, the build failed on the page's old path, and
|
|
108
|
+
// `writedocs dev` on every page - see lib/content-store-prune.js.
|
|
109
|
+
function forgetMovedFiles(context: LoaderContext, files: string[]) {
|
|
110
|
+
const root = fileURLToPath(context.config.root);
|
|
111
|
+
for (const id of staleEntryIds(context.store.entries(), root, files)) context.store.delete(id);
|
|
112
|
+
}
|
|
113
|
+
|
|
95
114
|
function withTitleFallback(context: LoaderContext): LoaderContext {
|
|
96
115
|
return {
|
|
97
116
|
...context,
|
|
@@ -186,6 +205,7 @@ function livePages(dir: string, base: URL): Loader {
|
|
|
186
205
|
for (const id of store.keys()) store.delete(id);
|
|
187
206
|
return;
|
|
188
207
|
}
|
|
208
|
+
forgetMovedFiles(context, files.map((file) => path.join(dir, file)));
|
|
189
209
|
await glob({ pattern: files, base }).load({ ...withTitleFallback(context), watcher: undefined });
|
|
190
210
|
}
|
|
191
211
|
|
|
@@ -88,7 +88,9 @@ import { htmlDirective } from "../lib/agent-directive.js";
|
|
|
88
88
|
import { contextMenuOptions } from "../lib/config";
|
|
89
89
|
import { jsonLd, serializeJsonLd } from "../lib/json-ld.js";
|
|
90
90
|
import { consentOptions } from "../lib/consent.js";
|
|
91
|
+
import { themeScript, serverTheme } from "../lib/appearance.js";
|
|
91
92
|
import { oncePerBuild } from "../lib/build-memo.js";
|
|
93
|
+
import { stripBom } from "../lib/bom.js";
|
|
92
94
|
// writedocs.json's `basePath`: every site address written below goes
|
|
93
95
|
// through siteHref() - see the components package's lib/site-base.ts.
|
|
94
96
|
import { BASE, siteHref } from "../../packages/components/src/lib/site-base";
|
|
@@ -411,11 +413,19 @@ const rootAssets = findRootAssets(contentDir);
|
|
|
411
413
|
// "docs/theme.css") - split and rejoin with nodePath.join rather than
|
|
412
414
|
// handing the raw string straight to it, so a nested asset resolves
|
|
413
415
|
// correctly on every OS this might run on, not just POSIX ones.
|
|
416
|
+
//
|
|
417
|
+
// Without a byte order mark (lib/bom.js): inlined, it would be the first
|
|
418
|
+
// character of the <style>, and the stylesheet's first rule would match
|
|
419
|
+
// nothing. Read right here, with lib/bom.js's dependency-free stripBom():
|
|
420
|
+
// calling a lib/pages.js helper from this frontmatter instead changed how
|
|
421
|
+
// the build splits its chunks - every page then linked BaseLayout.css
|
|
422
|
+
// before its own stylesheet, a different cascade (found comparing dist/
|
|
423
|
+
// with 0.15.0's). scripts/bom.test.js checks both reads go through it.
|
|
414
424
|
const rootCssContents = oncePerBuild("root-css-contents", () => rootAssets.css.map((name) =>
|
|
415
|
-
fs.readFileSync(nodePath.join(contentDir, ...name.split("/")), "utf-8"),
|
|
425
|
+
stripBom(fs.readFileSync(nodePath.join(contentDir, ...name.split("/")), "utf-8")),
|
|
416
426
|
));
|
|
417
427
|
const rootJsContents = oncePerBuild("root-js-contents", () => rootAssets.js.map((name) =>
|
|
418
|
-
fs.readFileSync(nodePath.join(contentDir, ...name.split("/")), "utf-8"),
|
|
428
|
+
stripBom(fs.readFileSync(nodePath.join(contentDir, ...name.split("/")), "utf-8")),
|
|
419
429
|
));
|
|
420
430
|
// public/*.css and public/*.js auto-load too, but as <link>/<script src>
|
|
421
431
|
// pointing at their already-public URL (see findRootAssets()'s own
|
|
@@ -501,7 +511,7 @@ const fontExtraCss = [fontFaceCss, fontFallbackCss, fontWeightCss].filter(Boolea
|
|
|
501
511
|
---
|
|
502
512
|
|
|
503
513
|
<!doctype html>
|
|
504
|
-
<html lang={lang}>
|
|
514
|
+
<html lang={lang} data-theme={serverTheme(config.styles.appearance)}>
|
|
505
515
|
<head>
|
|
506
516
|
<meta charset="UTF-8" />
|
|
507
517
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
@@ -558,52 +568,31 @@ const fontExtraCss = [fontFaceCss, fontFallbackCss, fontWeightCss].filter(Boolea
|
|
|
558
568
|
consent={consent}
|
|
559
569
|
gatedScripts={[...config.scripts.head, ...config.scripts.body].filter(gated)}
|
|
560
570
|
/>
|
|
561
|
-
<
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
function wdApplyStoredPreferences()
|
|
585
|
-
|
|
586
|
-
var stored = localStorage.getItem("wd-theme");
|
|
587
|
-
var theme =
|
|
588
|
-
stored === "light" || stored === "dark"
|
|
589
|
-
? stored
|
|
590
|
-
: window.matchMedia("(prefers-color-scheme: dark)").matches
|
|
591
|
-
? "dark"
|
|
592
|
-
: "light";
|
|
593
|
-
document.documentElement.dataset.theme = theme;
|
|
594
|
-
} catch (e) {
|
|
595
|
-
document.documentElement.dataset.theme = "light";
|
|
596
|
-
}
|
|
597
|
-
try {
|
|
598
|
-
document.documentElement.dataset.bannerDismissed =
|
|
599
|
-
localStorage.getItem("wd-banner-dismissed") === "true" ? "true" : "false";
|
|
600
|
-
} catch (e) {
|
|
601
|
-
document.documentElement.dataset.bannerDismissed = "false";
|
|
602
|
-
}
|
|
603
|
-
}
|
|
604
|
-
wdApplyStoredPreferences();
|
|
605
|
-
document.addEventListener("astro:after-swap", wdApplyStoredPreferences);
|
|
606
|
-
</script>
|
|
571
|
+
{/* Sets [data-theme] and [data-banner-dismissed] on <html> before
|
|
572
|
+
first paint, so the page never flashes the wrong mode or an
|
|
573
|
+
already-dismissed banner. The theme comes from themeScript()
|
|
574
|
+
(src/lib/appearance.js): the reader's toggle choice (unless
|
|
575
|
+
`styles.appearance.strict`), then `styles.appearance.default`, then
|
|
576
|
+
the OS setting. The click handlers that update both live in
|
|
577
|
+
src/scripts/theme-toggle.ts and src/scripts/banner.ts.
|
|
578
|
+
|
|
579
|
+
Also re-run on `astro:after-swap` (ClientRouter, see BaseLayout's
|
|
580
|
+
own import of it below), not just the initial load - Astro's
|
|
581
|
+
client-side navigation swaps <html>'s attributes wholesale to
|
|
582
|
+
match the newly-fetched page's raw server-rendered markup (see
|
|
583
|
+
swapRootAttributes() in astro/dist/transitions/swap-functions.js),
|
|
584
|
+
which has no banner state and at most serverTheme()'s fixed
|
|
585
|
+
default - never the reader's own choice. Without re-running this
|
|
586
|
+
after every swap, a dismissed banner (or a chosen theme) would
|
|
587
|
+
silently come back on the very next navigation, so this has to
|
|
588
|
+
reapply them every time, exactly as it does on first load.
|
|
589
|
+
`astro:after-swap` fires synchronously right after that swap and
|
|
590
|
+
before the browser's next paint, so this still prevents any
|
|
591
|
+
visible flash on client-side navigations too. */}
|
|
592
|
+
<script
|
|
593
|
+
is:inline
|
|
594
|
+
set:html={`function wdApplyStoredPreferences(){${themeScript(config.styles.appearance)}try{document.documentElement.dataset.bannerDismissed=localStorage.getItem("wd-banner-dismissed")==="true"?"true":"false"}catch(e){document.documentElement.dataset.bannerDismissed="false"}}wdApplyStoredPreferences();document.addEventListener("astro:after-swap",wdApplyStoredPreferences);`}
|
|
595
|
+
/>
|
|
607
596
|
<script>
|
|
608
597
|
// Preserves the sidebar's own scroll position across a ClientRouter
|
|
609
598
|
// navigation - see src/scripts/sidebar-scroll.ts for why this is
|
|
@@ -31,6 +31,7 @@
|
|
|
31
31
|
import { isExternalHref, type DocsConfig, type Selector, type GlobalDropdownView } from '../../lib/config';
|
|
32
32
|
import AppIcon from '../../../packages/components/src/components/AppIcon.astro';
|
|
33
33
|
import { logoWidth } from '../../lib/image-size.js';
|
|
34
|
+
import { showsThemeToggle } from '../../lib/appearance.js';
|
|
34
35
|
import { hasMobileMenu, currentOption } from '../../lib/selector-placement.js';
|
|
35
36
|
import '../styles/dropdown.css';
|
|
36
37
|
import '../styles/topbar.css';
|
|
@@ -92,11 +93,14 @@ const showMenu = hasMobileMenu({ sidebar: showSidebarCol, selectors, globalDropd
|
|
|
92
93
|
wires up every `.wd-theme-toggle` it finds (not just the
|
|
93
94
|
first), and both stay in sync for free since [data-theme] on
|
|
94
95
|
<html> - not per-button JS state - is what actually drives
|
|
95
|
-
which icon shows.
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
<
|
|
99
|
-
|
|
96
|
+
which icon shows. Left out with `styles.appearance.strict`,
|
|
97
|
+
like the topbar's (src/lib/appearance.js). */}
|
|
98
|
+
{showsThemeToggle(config.styles.appearance) && (
|
|
99
|
+
<button type="button" class="wd-theme-toggle" aria-label="Toggle color theme">
|
|
100
|
+
<AppIcon icon="sun" class="wd-theme-icon wd-theme-icon-sun" />
|
|
101
|
+
<AppIcon icon="moon" class="wd-theme-icon wd-theme-icon-moon" />
|
|
102
|
+
</button>
|
|
103
|
+
)}
|
|
100
104
|
</div>
|
|
101
105
|
<div class="wd-mobile-menu-scroll">
|
|
102
106
|
{(selectors.some((sel) => !sel.hidden) || globalDropdowns.length > 0) && (
|
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
import { isExternalHref, type DocsConfig, type Selector, type GlobalDropdownView } from '../../lib/config';
|
|
15
15
|
import AppIcon from '../../../packages/components/src/components/AppIcon.astro';
|
|
16
16
|
import { logoWidth } from '../../lib/image-size.js';
|
|
17
|
+
import { showsThemeToggle } from '../../lib/appearance.js';
|
|
17
18
|
import { selectorPlacements, hasMobileMenu, currentOption } from '../../lib/selector-placement.js';
|
|
18
19
|
import '../styles/dropdown.css';
|
|
19
20
|
import '../styles/topbar.css';
|
|
@@ -206,10 +207,13 @@ const tabRows = tabSelectors.length > 0 ? tabSelectors : globalDropdowns.length
|
|
|
206
207
|
<kbd class="wd-search-kbd">⌘K</kbd>
|
|
207
208
|
</button>
|
|
208
209
|
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
<
|
|
212
|
-
|
|
210
|
+
{/* Left out with `styles.appearance.strict` - src/lib/appearance.js. */}
|
|
211
|
+
{showsThemeToggle(config.styles.appearance) && (
|
|
212
|
+
<button type="button" class="wd-theme-toggle" aria-label="Toggle color theme">
|
|
213
|
+
<AppIcon icon="sun" class="wd-theme-icon wd-theme-icon-sun" />
|
|
214
|
+
<AppIcon icon="moon" class="wd-theme-icon wd-theme-icon-moon" />
|
|
215
|
+
</button>
|
|
216
|
+
)}
|
|
213
217
|
</div>
|
|
214
218
|
</div>
|
|
215
219
|
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// `styles.appearance` in writedocs.json: which color mode a page opens in,
|
|
2
|
+
// and whether readers can change it.
|
|
3
|
+
//
|
|
4
|
+
// default: "system" (the reader's OS setting - the default), "light" or "dark"
|
|
5
|
+
// strict: true hides the theme toggle, so every reader gets `default`
|
|
6
|
+
//
|
|
7
|
+
// The theme itself is [data-theme] on <html>, set before the first paint by
|
|
8
|
+
// the script themeScript() writes. Two places run it: every page
|
|
9
|
+
// (BaseLayout.astro's inline script, again after each client-side
|
|
10
|
+
// navigation) and the redirect pages (src/cli/rewrite-redirect-pages.js), so
|
|
11
|
+
// the two can't disagree about a reader's theme.
|
|
12
|
+
//
|
|
13
|
+
// What decides the theme, in order:
|
|
14
|
+
// 1. the reader's own choice from the toggle (localStorage "wd-theme") -
|
|
15
|
+
// skipped when `strict`: there's no toggle, so an old choice saved
|
|
16
|
+
// before the site turned `strict` on mustn't win;
|
|
17
|
+
// 2. `default`, when it's "light" or "dark";
|
|
18
|
+
// 3. the OS setting (prefers-color-scheme), for "system".
|
|
19
|
+
//
|
|
20
|
+
// Plain JavaScript with no imports: the layout imports it, and so does the
|
|
21
|
+
// redirect rewriter, which reads writedocs.json raw (no schema defaults) -
|
|
22
|
+
// resolveAppearance() accepts anything and falls back field by field.
|
|
23
|
+
|
|
24
|
+
export const APPEARANCE_MODES = ['system', 'light', 'dark'];
|
|
25
|
+
|
|
26
|
+
/** `styles.appearance` with its defaults filled in. Accepts a raw, possibly invalid value. */
|
|
27
|
+
export function resolveAppearance(appearance) {
|
|
28
|
+
const value = appearance && typeof appearance === 'object' ? appearance : {};
|
|
29
|
+
return {
|
|
30
|
+
default: APPEARANCE_MODES.includes(value.default) ? value.default : 'system',
|
|
31
|
+
strict: value.strict === true,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Whether the pages show the light/dark toggle. */
|
|
36
|
+
export function showsThemeToggle(appearance) {
|
|
37
|
+
return !resolveAppearance(appearance).strict;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The theme to write on <html data-theme> in the HTML itself, or undefined
|
|
42
|
+
* for "system" (only the reader's browser knows that one). The script sets
|
|
43
|
+
* the theme before the first paint anyway; this is what a reader without
|
|
44
|
+
* JavaScript gets - otherwise every such page is light.
|
|
45
|
+
*/
|
|
46
|
+
export function serverTheme(appearance) {
|
|
47
|
+
const mode = resolveAppearance(appearance).default;
|
|
48
|
+
return mode === 'system' ? undefined : mode;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* The JavaScript that sets <html data-theme> - one self-contained statement
|
|
53
|
+
* (it declares nothing global), safe to run more than once. The settings are
|
|
54
|
+
* written into it as constants, so a page carries only the branch it uses.
|
|
55
|
+
*/
|
|
56
|
+
export function themeScript(appearance) {
|
|
57
|
+
const { default: mode, strict } = resolveAppearance(appearance);
|
|
58
|
+
const stored = strict ? '' : "try{t=localStorage.getItem('wd-theme')}catch(e){}";
|
|
59
|
+
const otherwise =
|
|
60
|
+
mode === 'system'
|
|
61
|
+
? "try{t=matchMedia('(prefers-color-scheme: dark)').matches?'dark':'light'}catch(e){t='light'}"
|
|
62
|
+
: `t='${mode}'`;
|
|
63
|
+
return `(function(){var t=null;${stored}if(t!=='light'&&t!=='dark'){${otherwise}}document.documentElement.dataset.theme=t})();`;
|
|
64
|
+
}
|
|
@@ -26,6 +26,7 @@
|
|
|
26
26
|
// `.writedocs/` is never published (EXCLUDED_TOP_LEVEL_DIRS, lib/pages.js).
|
|
27
27
|
import fs from 'node:fs';
|
|
28
28
|
import path from 'node:path';
|
|
29
|
+
import { stripBom } from './bom.js';
|
|
29
30
|
|
|
30
31
|
/** Where the file is, from the project root. */
|
|
31
32
|
export const ASSET_DIMENSIONS_FILE = '.writedocs/asset-dimensions.json';
|
|
@@ -38,7 +39,7 @@ export function parseAssetDimensions(text) {
|
|
|
38
39
|
const sizes = new Map();
|
|
39
40
|
let data;
|
|
40
41
|
try {
|
|
41
|
-
data = JSON.parse(String(text)
|
|
42
|
+
data = JSON.parse(stripBom(String(text)));
|
|
42
43
|
} catch {
|
|
43
44
|
return sizes;
|
|
44
45
|
}
|
package/src/lib/bom.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// The one place a UTF-8 byte order mark is taken off a file's text.
|
|
2
|
+
//
|
|
3
|
+
// Windows editors - Notepad, PowerShell 5.1's `Out-File`/`Set-Content
|
|
4
|
+
// -Encoding utf8`, Visual Studio - save UTF-8 with a byte order mark (the
|
|
5
|
+
// bytes EF BB BF, read as the character U+FEFF). Read as text, it's an
|
|
6
|
+
// invisible first character: JSON.parse rejects it, a frontmatter `^---`
|
|
7
|
+
// doesn't match after it, and a stylesheet inlined in a <style> tag loses
|
|
8
|
+
// its first rule (the selector starts with U+FEFF). Every reader of a
|
|
9
|
+
// project's files takes it off here - writedocs.json and the other configs
|
|
10
|
+
// (config-file.js, config-schema.ts), pages, stylesheets and scripts, the
|
|
11
|
+
// ignore files, llms.txt - so a file with one reads exactly like the same
|
|
12
|
+
// file without one.
|
|
13
|
+
//
|
|
14
|
+
// No imports, nothing from Node: lib/wdignore.js and config-schema.ts use
|
|
15
|
+
// it, and the WriteDocs platform runs both in a Cloudflare Worker.
|
|
16
|
+
|
|
17
|
+
/** `text` without a leading byte order mark. One in the middle stays. */
|
|
18
|
+
export function stripBom(text) {
|
|
19
|
+
return typeof text === 'string' && text.charCodeAt(0) === 0xfeff ? text.slice(1) : text;
|
|
20
|
+
}
|
package/src/lib/config-file.js
CHANGED
|
@@ -1,25 +1,38 @@
|
|
|
1
|
-
// Reading writedocs.json (and the other JSON
|
|
2
|
-
//
|
|
3
|
-
// with plain Node - see
|
|
1
|
+
// Reading a project's text files: writedocs.json (and the other JSON
|
|
2
|
+
// configs `writedocs convert` reads), pages, stylesheets, llms.txt. Plain
|
|
3
|
+
// JavaScript, not TypeScript, so the CLI can load it with plain Node - see
|
|
4
|
+
// packages/components/src/lib/icons.js's comment.
|
|
4
5
|
//
|
|
5
6
|
// Windows editors - Notepad, PowerShell 5.1's `Out-File`/`Set-Content
|
|
6
7
|
// -Encoding utf8` - save UTF-8 with a byte order mark. JSON.parse rejects
|
|
7
8
|
// it ("Unexpected token U+FEFF"), and the error then blames commas and
|
|
8
|
-
// brackets the file doesn't have. Every reader strips it here instead
|
|
9
|
+
// brackets the file doesn't have. Every reader strips it here instead (the
|
|
10
|
+
// stripping itself is lib/bom.js).
|
|
9
11
|
import fs from 'node:fs';
|
|
10
12
|
import path from 'node:path';
|
|
13
|
+
import { stripBom } from './bom.js';
|
|
11
14
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
+
export { stripBom };
|
|
16
|
+
|
|
17
|
+
/** A project file's text, read as UTF-8, byte order mark removed. */
|
|
18
|
+
export function readText(file) {
|
|
19
|
+
return stripBom(fs.readFileSync(file, 'utf-8'));
|
|
15
20
|
}
|
|
16
21
|
|
|
17
22
|
/** A JSON config file's text, byte order mark removed. */
|
|
18
23
|
export function readJsonText(file) {
|
|
19
|
-
return
|
|
24
|
+
return readText(file);
|
|
20
25
|
}
|
|
21
26
|
|
|
22
27
|
/** `<contentDir>/writedocs.json`'s text, byte order mark removed. */
|
|
23
28
|
export function readConfigText(contentDir) {
|
|
24
29
|
return readJsonText(path.join(contentDir, 'writedocs.json'));
|
|
25
30
|
}
|
|
31
|
+
|
|
32
|
+
/** `<contentDir>/<name>`'s text, byte order mark removed - or null when
|
|
33
|
+
* the project has no such file (a hand-written llms.txt, say). */
|
|
34
|
+
export function readProjectText(contentDir, name) {
|
|
35
|
+
const file = path.join(contentDir, ...name.split('/'));
|
|
36
|
+
if (!fs.existsSync(file)) return null;
|
|
37
|
+
return readText(file);
|
|
38
|
+
}
|
package/src/lib/config-schema.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
import { findNodeAtLocation, parseTree, visit } from "jsonc-parser";
|
|
3
3
|
import { BASE_PATH_FIELD_PATTERN } from "./base-path.js";
|
|
4
|
+
import { stripBom } from "./bom.js";
|
|
4
5
|
const navPageSchema = z.string();
|
|
5
6
|
const navGroupPagesSchema = z.lazy(
|
|
6
7
|
() => z.object({
|
|
@@ -270,6 +271,15 @@ const stylesSchema = z.object({
|
|
|
270
271
|
light: z.string().optional(),
|
|
271
272
|
dark: z.string().optional()
|
|
272
273
|
}).strict().optional()
|
|
274
|
+
}).strict().optional(),
|
|
275
|
+
// Which color mode a page opens in (`default`: the reader's OS setting,
|
|
276
|
+
// or always light/dark) and whether readers can switch (`strict` hides
|
|
277
|
+
// the theme toggle). Same shape as Mintlify's top-level `appearance`.
|
|
278
|
+
// Both fields optional here: resolveAppearance() (src/lib/appearance.js)
|
|
279
|
+
// fills in "system"/false, and is what everything reads it through.
|
|
280
|
+
appearance: z.object({
|
|
281
|
+
default: z.enum(["system", "light", "dark"]).optional(),
|
|
282
|
+
strict: z.boolean().optional()
|
|
273
283
|
}).strict().optional()
|
|
274
284
|
}).default({ colors: { primary: DEFAULT_PRIMARY } });
|
|
275
285
|
const topbarLinkSchema = z.object({
|
|
@@ -1082,7 +1092,7 @@ instalarCorte(
|
|
|
1082
1092
|
/* @__PURE__ */ new Set([navGroupPagesSchema._zod.innerType, navGroupOpenApiSchema, navLinkSchema])
|
|
1083
1093
|
);
|
|
1084
1094
|
function localizarCaminhos(rawText, caminhos) {
|
|
1085
|
-
rawText =
|
|
1095
|
+
rawText = stripBom(rawText);
|
|
1086
1096
|
const raiz = { filhos: /* @__PURE__ */ new Map(), alvo: false };
|
|
1087
1097
|
for (const caminho of caminhos) {
|
|
1088
1098
|
if (caminho.length === 0) continue;
|
|
@@ -1145,7 +1155,7 @@ function createJsonLocator(rawText) {
|
|
|
1145
1155
|
return criarLocalizador(rawText);
|
|
1146
1156
|
}
|
|
1147
1157
|
function criarLocalizador(rawText) {
|
|
1148
|
-
rawText =
|
|
1158
|
+
rawText = stripBom(rawText);
|
|
1149
1159
|
let arvore = null;
|
|
1150
1160
|
let inicios;
|
|
1151
1161
|
return (caminho) => {
|
|
@@ -1286,7 +1296,7 @@ function unknownRootKeyIssues(rawText) {
|
|
|
1286
1296
|
return unknownRootKeyWarnings(rawText).issues;
|
|
1287
1297
|
}
|
|
1288
1298
|
function unknownRootKeyWarnings(rawText, { all = false } = {}) {
|
|
1289
|
-
rawText =
|
|
1299
|
+
rawText = stripBom(rawText);
|
|
1290
1300
|
let raiz;
|
|
1291
1301
|
try {
|
|
1292
1302
|
raiz = JSON.parse(rawText);
|
|
@@ -1318,11 +1328,8 @@ function unknownRootKeyWarnings(rawText, { all = false } = {}) {
|
|
|
1318
1328
|
});
|
|
1319
1329
|
return { issues, issuesTotal: desconhecidas.length };
|
|
1320
1330
|
}
|
|
1321
|
-
function semBom(texto) {
|
|
1322
|
-
return texto.charCodeAt(0) === 65279 ? texto.slice(1) : texto;
|
|
1323
|
-
}
|
|
1324
1331
|
function validateDocsConfig(rawText, { all = false } = {}) {
|
|
1325
|
-
rawText =
|
|
1332
|
+
rawText = stripBom(rawText);
|
|
1326
1333
|
let raw;
|
|
1327
1334
|
try {
|
|
1328
1335
|
raw = JSON.parse(rawText);
|
package/src/lib/config-schema.ts
CHANGED
|
@@ -36,6 +36,9 @@ import { z } from 'zod';
|
|
|
36
36
|
import { findNodeAtLocation, parseTree, visit } from 'jsonc-parser';
|
|
37
37
|
// The form of `basePath`. Plain JS with no `node:` import, like this file.
|
|
38
38
|
import { BASE_PATH_FIELD_PATTERN } from './base-path.js';
|
|
39
|
+
// A leading UTF-8 byte order mark off the text - the one copy, lib/bom.js.
|
|
40
|
+
// Plain JS with no `node:` import too.
|
|
41
|
+
import { stripBom } from './bom.js';
|
|
39
42
|
|
|
40
43
|
// ---------------------------------------------------------------------
|
|
41
44
|
// Pages & groups - the leaf content any container ultimately bottoms out
|
|
@@ -551,6 +554,18 @@ const stylesSchema = z
|
|
|
551
554
|
})
|
|
552
555
|
.strict()
|
|
553
556
|
.optional(),
|
|
557
|
+
// Which color mode a page opens in (`default`: the reader's OS setting,
|
|
558
|
+
// or always light/dark) and whether readers can switch (`strict` hides
|
|
559
|
+
// the theme toggle). Same shape as Mintlify's top-level `appearance`.
|
|
560
|
+
// Both fields optional here: resolveAppearance() (src/lib/appearance.js)
|
|
561
|
+
// fills in "system"/false, and is what everything reads it through.
|
|
562
|
+
appearance: z
|
|
563
|
+
.object({
|
|
564
|
+
default: z.enum(['system', 'light', 'dark']).optional(),
|
|
565
|
+
strict: z.boolean().optional(),
|
|
566
|
+
})
|
|
567
|
+
.strict()
|
|
568
|
+
.optional(),
|
|
554
569
|
})
|
|
555
570
|
.default({ colors: { primary: DEFAULT_PRIMARY } });
|
|
556
571
|
|
|
@@ -2066,7 +2081,7 @@ instalarCorte(
|
|
|
2066
2081
|
* findNodeAtLocation), inclusive com chave repetida num objeto, onde vale a
|
|
2067
2082
|
* primeira; o que nao leva a nenhum caminho pedido e pulado sem callbacks. */
|
|
2068
2083
|
function localizarCaminhos(rawText: string, caminhos: (string | number)[][]): (caminho: (string | number)[]) => { line: number; column: number } | null {
|
|
2069
|
-
rawText =
|
|
2084
|
+
rawText = stripBom(rawText);
|
|
2070
2085
|
type No = { filhos: Map<string | number, No>; alvo: boolean; offset?: number };
|
|
2071
2086
|
const raiz: No = { filhos: new Map(), alvo: false };
|
|
2072
2087
|
for (const caminho of caminhos) {
|
|
@@ -2150,7 +2165,7 @@ export function createJsonLocator(rawText: string): (path: (string | number)[])
|
|
|
2150
2165
|
* '\n' (um `\r\n` e um '\n' com o '\r' no fim da linha anterior), e a coluna e
|
|
2151
2166
|
* em unidades UTF-16, como o offset do jsonc-parser - um emoji conta 2. */
|
|
2152
2167
|
function criarLocalizador(rawText: string): (caminho: (string | number)[]) => { line: number; column: number } | null {
|
|
2153
|
-
rawText =
|
|
2168
|
+
rawText = stripBom(rawText);
|
|
2154
2169
|
let arvore: ReturnType<typeof parseTree> | undefined | null = null;
|
|
2155
2170
|
let inicios: number[] | undefined;
|
|
2156
2171
|
return (caminho) => {
|
|
@@ -2356,7 +2371,7 @@ export function unknownRootKeyWarnings(
|
|
|
2356
2371
|
rawText: string,
|
|
2357
2372
|
{ all = false }: ValidationOptions = {}
|
|
2358
2373
|
): { issues: ValidationIssue[]; issuesTotal: number } {
|
|
2359
|
-
rawText =
|
|
2374
|
+
rawText = stripBom(rawText);
|
|
2360
2375
|
let raiz: unknown;
|
|
2361
2376
|
try {
|
|
2362
2377
|
raiz = JSON.parse(rawText);
|
|
@@ -2390,16 +2405,8 @@ export function unknownRootKeyWarnings(
|
|
|
2390
2405
|
return { issues, issuesTotal: desconhecidas.length };
|
|
2391
2406
|
}
|
|
2392
2407
|
|
|
2393
|
-
/** Texto sem o BOM do UTF-8 no inicio. Editores do Windows (Bloco de Notas, o
|
|
2394
|
-
* `Out-File` do PowerShell 5.1) gravam um, e o JSON.parse o rejeita com uma
|
|
2395
|
-
* mensagem que culpa virgulas e colchetes. Copia local, e nao import de
|
|
2396
|
-
* lib/config-file.js: este modulo so importa zod e jsonc-parser (ver o topo). */
|
|
2397
|
-
function semBom(texto: string): string {
|
|
2398
|
-
return texto.charCodeAt(0) === 0xfeff ? texto.slice(1) : texto;
|
|
2399
|
-
}
|
|
2400
|
-
|
|
2401
2408
|
export function validateDocsConfig(rawText: string, { all = false }: ValidationOptions = {}): ValidationResult {
|
|
2402
|
-
rawText =
|
|
2409
|
+
rawText = stripBom(rawText);
|
|
2403
2410
|
let raw: unknown;
|
|
2404
2411
|
try {
|
|
2405
2412
|
raw = JSON.parse(rawText);
|
package/src/lib/content-check.js
CHANGED
|
@@ -31,11 +31,13 @@ import path from 'node:path';
|
|
|
31
31
|
import matter from 'gray-matter';
|
|
32
32
|
import { visit } from 'unist-util-visit';
|
|
33
33
|
import { findAllPages, fileIdForPath, titleFromPath, navPageId } from './pages.js';
|
|
34
|
+
import { projectIgnore } from './project-ignore.js';
|
|
34
35
|
import { iconExists } from '../../packages/components/src/lib/icons.js';
|
|
35
36
|
import { findUnknownComponents } from './mdx-unknown-components.js';
|
|
36
37
|
import { findInteractiveComponents, simplifiedBuiltinMessage } from './mdx-inline-react.js';
|
|
37
38
|
import { pageFrontmatterSchema, createJsonLocator } from './config-schema.js';
|
|
38
39
|
import { parseOpenApiRef } from './openapi-ref.js';
|
|
40
|
+
import { readText } from './config-file.js';
|
|
39
41
|
import { collectOpenApiGroups, operationKeys } from './openapi-spec.js';
|
|
40
42
|
|
|
41
43
|
/** An issue: { file, line?, message, suggestion? } - `file` relative to the
|
|
@@ -70,7 +72,9 @@ async function parseMdx(text) {
|
|
|
70
72
|
}
|
|
71
73
|
|
|
72
74
|
async function checkPage(contentDir, rel, errors, warnings, openapiRefs) {
|
|
73
|
-
|
|
75
|
+
// Without a byte order mark, so the frontmatter's line numbers below are
|
|
76
|
+
// the same as in the same file without one (lib/bom.js).
|
|
77
|
+
const raw = readText(path.join(contentDir, rel)).replace(/\r\n/g, '\n');
|
|
74
78
|
let parsed;
|
|
75
79
|
try {
|
|
76
80
|
// An options object bypasses gray-matter's cache, which keeps an empty
|
|
@@ -381,6 +385,22 @@ async function checkOpenApi(contentDir, config, locate, openapiRefs, errors, war
|
|
|
381
385
|
* null when it isn't valid JSON (then only the pages themselves are
|
|
382
386
|
* checked). Returns { pages, errors, warnings }.
|
|
383
387
|
*/
|
|
388
|
+
/** The file a navigation id names (`<id>.mdx`, `<id>.md`, or the same as a
|
|
389
|
+
* folder's index), when it exists but the project's ignore rules
|
|
390
|
+
* (projectIgnore() - the default exclusions and `.wdignore`) leave it out.
|
|
391
|
+
* Its project-relative path, or null. */
|
|
392
|
+
function ignoredPageFile(contentDir, id) {
|
|
393
|
+
const rules = projectIgnore(contentDir);
|
|
394
|
+
for (const rel of [`${id}.mdx`, `${id}.md`, `${id}/index.mdx`, `${id}/index.md`]) {
|
|
395
|
+
let isFile = false;
|
|
396
|
+
try {
|
|
397
|
+
isFile = fs.statSync(path.join(contentDir, rel)).isFile();
|
|
398
|
+
} catch {}
|
|
399
|
+
if (isFile && rules.ignores(rel)) return rel;
|
|
400
|
+
}
|
|
401
|
+
return null;
|
|
402
|
+
}
|
|
403
|
+
|
|
384
404
|
export async function checkContent(contentDir, configText) {
|
|
385
405
|
const errors = [];
|
|
386
406
|
const warnings = [];
|
|
@@ -405,6 +425,21 @@ export async function checkContent(contentDir, configText) {
|
|
|
405
425
|
for (const ref of navigationReferences(config.navigation)) {
|
|
406
426
|
// "guides/index" and "guides" both name guides/index.mdx (navPageId()).
|
|
407
427
|
if (ids.has(navPageId(ref.id))) continue;
|
|
428
|
+
// The file is there but an ignore rule leaves it out - README.md is a
|
|
429
|
+
// default exclusion (lib/wdignore.js), and a Mintlify project may list
|
|
430
|
+
// its README. "Create README.mdx" would send the author in circles.
|
|
431
|
+
const ignoredFile = ignoredPageFile(contentDir, ref.id);
|
|
432
|
+
if (ignoredFile) {
|
|
433
|
+
errors.push(
|
|
434
|
+
issue(
|
|
435
|
+
'writedocs.json',
|
|
436
|
+
locate(ref.path)?.line,
|
|
437
|
+
`The navigation lists page "${ref.id}", but ${ignoredFile} is left out of the site by the ignore rules.`,
|
|
438
|
+
`Rename the file, or bring it back with a line "!${ignoredFile}" at the end of .wdignore.`
|
|
439
|
+
)
|
|
440
|
+
);
|
|
441
|
+
continue;
|
|
442
|
+
}
|
|
408
443
|
errors.push(
|
|
409
444
|
issue(
|
|
410
445
|
'writedocs.json',
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// Which entries of Astro's content store point at a file that's no longer
|
|
2
|
+
// one of the project's pages - so the content loaders (src/content.config.ts)
|
|
3
|
+
// can delete them before each sync.
|
|
4
|
+
//
|
|
5
|
+
// Why: Astro's glob() loader keeps a page's stored entry when one with the
|
|
6
|
+
// same id has the same digest - a hash of the file's text, not of where it
|
|
7
|
+
// is (astro/dist/content/loaders/glob.js, syncData()). A page with a `slug`
|
|
8
|
+
// in its frontmatter takes that slug as its id, so a page moved to another
|
|
9
|
+
// folder keeps its id and its text, and the store keeps its old `filePath`.
|
|
10
|
+
// The store is saved between runs (writedocsTempDir()'s cache/), so from
|
|
11
|
+
// then on:
|
|
12
|
+
// - `writedocs build` fails: content-modules.mjs, which Astro writes from
|
|
13
|
+
// the stored entries, imports the page from where it was;
|
|
14
|
+
// - `writedocs dev` fails on every page: writedocs reads a page's file id
|
|
15
|
+
// from its `filePath` (fileIdForEntry() in lib/config.ts), and the
|
|
16
|
+
// navigation names the page where it is now.
|
|
17
|
+
// Deleting the entry makes glob() read the page again, from its new path. A
|
|
18
|
+
// page without `slug` takes its id from its path, so moving it already
|
|
19
|
+
// makes a new entry - deleting is harmless there too.
|
|
20
|
+
//
|
|
21
|
+
// Plain JavaScript, so scripts/content-store-prune.test.js can run it.
|
|
22
|
+
import path from 'node:path';
|
|
23
|
+
|
|
24
|
+
// Windows paths aren't case-sensitive, and the same folder can reach here
|
|
25
|
+
// spelled two ways (lib/canonical-path.js).
|
|
26
|
+
const key = (p) => {
|
|
27
|
+
const resolved = path.resolve(p);
|
|
28
|
+
return process.platform === 'win32' ? resolved.toLowerCase() : resolved;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/** The ids of the stored entries whose file isn't one of `files`.
|
|
32
|
+
* `entries`: the store's [id, entry] pairs - an entry's `filePath` is
|
|
33
|
+
* relative to `root`, Astro's project root. `files`: the absolute paths of
|
|
34
|
+
* the pages the loader is about to sync. An entry with no `filePath` isn't
|
|
35
|
+
* a file's, and is left alone. */
|
|
36
|
+
export function staleEntryIds(entries, root, files) {
|
|
37
|
+
const current = new Set(files.map(key));
|
|
38
|
+
const stale = [];
|
|
39
|
+
for (const [id, entry] of entries) {
|
|
40
|
+
if (typeof entry?.filePath !== 'string' || entry.filePath === '') continue;
|
|
41
|
+
if (!current.has(key(path.resolve(root, entry.filePath)))) stale.push(id);
|
|
42
|
+
}
|
|
43
|
+
return stale;
|
|
44
|
+
}
|
|
@@ -125,7 +125,10 @@ export const DESCRIPTIONS = {
|
|
|
125
125
|
'styles.navbar.dark.accent': 'Color of the underline under the active tab in the topbar, in dark mode.',
|
|
126
126
|
'styles.background': 'Background color of the site, and an optional background image.',
|
|
127
127
|
'styles.background.colors': 'Background colors.',
|
|
128
|
-
'styles.
|
|
128
|
+
'styles.appearance': 'Light and dark mode: which one pages open in, and whether readers can switch.',
|
|
129
|
+
'styles.appearance.default': 'The mode pages open in: "system" follows the reader\'s device setting; "light" or "dark" always starts there. Default "system". A reader who picks a mode with the theme toggle keeps their choice.',
|
|
130
|
+
'styles.appearance.strict': 'Hide the theme toggle, so every reader sees the `default` mode and can\'t switch. Default false.',
|
|
131
|
+
'styles.background.colors.light':'Background color in light mode. Default "#ffffff".',
|
|
129
132
|
'styles.background.colors.dark': 'Background color in dark mode. Default "#0b1120".',
|
|
130
133
|
'styles.background.images': 'Background images, shown behind the content and sidebar.',
|
|
131
134
|
'styles.background.images.light': 'Background image in light mode.',
|
|
@@ -418,6 +418,17 @@ export function convertMintlifyConfig(docs) {
|
|
|
418
418
|
if (Object.keys(background).length) styles.background = background;
|
|
419
419
|
if (bg.decoration) notes.add('background.decoration', ['background', 'decoration'], `\`background.decoration\` ("${bg.decoration}") isn't supported - dropped.`);
|
|
420
420
|
}
|
|
421
|
+
// `appearance` is the same shape in writedocs, under `styles`.
|
|
422
|
+
if (docs.appearance && typeof docs.appearance === 'object') {
|
|
423
|
+
const appearance = {};
|
|
424
|
+
const mode = docs.appearance.default;
|
|
425
|
+
if (['system', 'light', 'dark'].includes(mode)) appearance.default = mode;
|
|
426
|
+
else if (mode !== undefined) {
|
|
427
|
+
notes.add('appearance.default', ['appearance', 'default'], `\`appearance.default: ${JSON.stringify(mode)}\` isn't "system", "light" or "dark" - dropped, so pages follow the reader's device setting.`);
|
|
428
|
+
}
|
|
429
|
+
if (typeof docs.appearance.strict === 'boolean') appearance.strict = docs.appearance.strict;
|
|
430
|
+
if (Object.keys(appearance).length) styles.appearance = appearance;
|
|
431
|
+
}
|
|
421
432
|
if (Object.keys(styles).length) out.styles = styles;
|
|
422
433
|
|
|
423
434
|
// navigation (also collects topbar links from `global`)
|
|
@@ -576,7 +587,6 @@ export function convertMintlifyConfig(docs) {
|
|
|
576
587
|
|
|
577
588
|
// Settings with no equivalent at all
|
|
578
589
|
const unsupported = {
|
|
579
|
-
appearance: 'Light/dark mode defaults (`appearance`) aren\'t configurable - readers choose with the theme toggle.',
|
|
580
590
|
interaction: '`interaction` settings aren\'t supported.',
|
|
581
591
|
thumbnails: 'Social thumbnails (`thumbnails`) aren\'t generated - set `seo.ogImage` for a social preview image.',
|
|
582
592
|
metadata: '`metadata` (last-modified timestamps) isn\'t supported.',
|
package/src/lib/pages.js
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
import fs from 'node:fs';
|
|
6
6
|
import path from 'node:path';
|
|
7
7
|
import matter from 'gray-matter';
|
|
8
|
-
import { readConfigText } from './config-file.js';
|
|
8
|
+
import { readConfigText, readText } from './config-file.js';
|
|
9
9
|
import { oncePerBuild } from './build-memo.js';
|
|
10
10
|
import { projectIgnore } from './project-ignore.js';
|
|
11
11
|
|
|
@@ -41,7 +41,7 @@ export function titleFromPath(filePath) {
|
|
|
41
41
|
function readIgnoreFile(contentDir) {
|
|
42
42
|
let text;
|
|
43
43
|
try {
|
|
44
|
-
text =
|
|
44
|
+
text = readText(path.join(contentDir, '.mintignore'));
|
|
45
45
|
} catch {
|
|
46
46
|
return () => false;
|
|
47
47
|
}
|
|
@@ -184,7 +184,9 @@ function scanAllPages(contentDir) {
|
|
|
184
184
|
if (ignored(rel, false) || rules.ignores(rel)) continue;
|
|
185
185
|
let raw;
|
|
186
186
|
try {
|
|
187
|
-
|
|
187
|
+
// Without its byte order mark: the `^---` below has to see the
|
|
188
|
+
// frontmatter of a page saved by a Windows editor (lib/bom.js).
|
|
189
|
+
raw = readText(abs);
|
|
188
190
|
} catch {
|
|
189
191
|
continue;
|
|
190
192
|
}
|
|
@@ -34,6 +34,11 @@
|
|
|
34
34
|
{ "path": "bun.lock", "ignored": true },
|
|
35
35
|
{ "path": "composer.json", "ignored": true },
|
|
36
36
|
{ "path": "composer.lock", "ignored": true },
|
|
37
|
+
{ "path": "README.md", "ignored": true },
|
|
38
|
+
{ "path": "readme.md", "ignored": true },
|
|
39
|
+
{ "path": "docs/Readme.MD", "ignored": true },
|
|
40
|
+
{ "path": "docs/README.mdx", "ignored": false },
|
|
41
|
+
{ "path": "docs/readme-first.md", "ignored": false },
|
|
37
42
|
{ "path": "node_modules/pkg/index.js", "ignored": true },
|
|
38
43
|
{ "path": "node_modules", "directory": true, "ignored": true },
|
|
39
44
|
{ "path": "examples/app/node_modules/x/a.json", "ignored": true },
|
|
@@ -138,7 +143,7 @@
|
|
|
138
143
|
},
|
|
139
144
|
{
|
|
140
145
|
"name": "default exclusions brought back with !",
|
|
141
|
-
"wdignore": "!.well-known/\n!examples/package.json\n!*.sql\n!examples/dist/\n",
|
|
146
|
+
"wdignore": "!.well-known/\n!examples/package.json\n!*.sql\n!examples/dist/\n!docs/README.md\n",
|
|
142
147
|
"paths": [
|
|
143
148
|
{ "path": ".well-known/security.txt", "ignored": false },
|
|
144
149
|
{ "path": ".well-known", "directory": true, "ignored": false },
|
|
@@ -151,7 +156,9 @@
|
|
|
151
156
|
{ "path": "data/app.db", "ignored": true },
|
|
152
157
|
{ "path": "examples/dist/bundle.zip", "ignored": false },
|
|
153
158
|
{ "path": "dist/bundle.zip", "ignored": true },
|
|
154
|
-
{ "path": "other/dist/bundle.zip", "ignored": true }
|
|
159
|
+
{ "path": "other/dist/bundle.zip", "ignored": true },
|
|
160
|
+
{ "path": "docs/README.md", "ignored": false },
|
|
161
|
+
{ "path": "README.md", "ignored": true }
|
|
155
162
|
]
|
|
156
163
|
},
|
|
157
164
|
{
|
package/src/lib/wdignore.js
CHANGED
|
@@ -66,6 +66,11 @@ export const DEFAULT_PATTERNS = [
|
|
|
66
66
|
'bun.lock',
|
|
67
67
|
'composer.json',
|
|
68
68
|
'composer.lock',
|
|
69
|
+
// The repository's README, at any depth and in any case (README.md,
|
|
70
|
+
// readme.md, docs/Readme.md): it describes the repository, not the
|
|
71
|
+
// site. Without frontmatter it was never a page, but with frontmatter it
|
|
72
|
+
// became one, at /readme/.
|
|
73
|
+
'README.md',
|
|
69
74
|
// Dependencies and build output, at any depth.
|
|
70
75
|
'node_modules/',
|
|
71
76
|
'dist/',
|
|
@@ -490,7 +490,7 @@ export async function convertLegacyConfig(old, contentDir) {
|
|
|
490
490
|
* navbar labels and category/group names. */
|
|
491
491
|
function readTranslations(contentDir) {
|
|
492
492
|
try {
|
|
493
|
-
return JSON.parse(
|
|
493
|
+
return JSON.parse(readJsonText(path.join(contentDir, 'translations.json'))) ?? {};
|
|
494
494
|
} catch {
|
|
495
495
|
return {};
|
|
496
496
|
}
|
|
@@ -6,6 +6,7 @@ import { loadDocsConfig, normalizeEntryId, findAllPages, resolveSiteRoot, fileId
|
|
|
6
6
|
import { writedocsTempDir } from '../lib/writedocs-temp-dir.js';
|
|
7
7
|
import { pageMarkdown } from '../lib/openapi-markdown';
|
|
8
8
|
import { orderByNavigation } from '../lib/llms.js';
|
|
9
|
+
import { readProjectText } from '../lib/config-file.js';
|
|
9
10
|
|
|
10
11
|
// The "everything, concatenated" half of the llms.txt pair - see
|
|
11
12
|
// llms.txt.ts (right next to this file) for the lightweight index half,
|
|
@@ -23,9 +24,11 @@ type DocsEntry = CollectionEntry<'pages'> | CollectionEntry<'generatedDocs'>;
|
|
|
23
24
|
export const GET: APIRoute = async () => {
|
|
24
25
|
const contentDir = process.env.WRITEDOCS_CONTENT_DIR || process.cwd();
|
|
25
26
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
27
|
+
// A hand-written llms-full.txt replaces the generated one - served
|
|
28
|
+
// without a byte order mark (lib/bom.js).
|
|
29
|
+
const custom = readProjectText(contentDir, 'llms-full.txt');
|
|
30
|
+
if (custom !== null) {
|
|
31
|
+
return new Response(custom, {
|
|
29
32
|
headers: { 'Content-Type': 'text/plain; charset=utf-8' },
|
|
30
33
|
});
|
|
31
34
|
}
|
package/src/pages/llms.txt.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import fs from 'node:fs';
|
|
2
|
-
import path from 'node:path';
|
|
3
1
|
import type { APIRoute } from 'astro';
|
|
4
2
|
import { llmsIndexFiles } from '../lib/llms-index';
|
|
3
|
+
import { readProjectText } from '../lib/config-file.js';
|
|
5
4
|
|
|
6
5
|
// A lightweight, LLM-facing index of every page on the site - the
|
|
7
6
|
// llms.txt convention (see https://llmstxt.org), also adopted by Mintlify
|
|
@@ -30,10 +29,11 @@ export const GET: APIRoute = async () => {
|
|
|
30
29
|
const files = await llmsIndexFiles();
|
|
31
30
|
// null: the project has its own llms.txt at its root, next to
|
|
32
31
|
// writedocs.json - it replaces the generated one (and its child files)
|
|
33
|
-
// outright, and a site can fall back by just deleting it.
|
|
32
|
+
// outright, and a site can fall back by just deleting it. Served without
|
|
33
|
+
// a byte order mark (lib/bom.js).
|
|
34
34
|
const text = files
|
|
35
35
|
? files.get('llms.txt')
|
|
36
|
-
:
|
|
36
|
+
: readProjectText(process.env.WRITEDOCS_CONTENT_DIR || process.cwd(), 'llms.txt');
|
|
37
37
|
return new Response(text, {
|
|
38
38
|
headers: { 'Content-Type': 'text/plain; charset=utf-8' },
|
|
39
39
|
});
|
package/writedocs.schema.json
CHANGED
|
@@ -341,6 +341,29 @@
|
|
|
341
341
|
"additionalProperties": false,
|
|
342
342
|
"description": "Background color of the site, and an optional background image.",
|
|
343
343
|
"markdownDescription": "Background color of the site, and an optional background image."
|
|
344
|
+
},
|
|
345
|
+
"appearance": {
|
|
346
|
+
"type": "object",
|
|
347
|
+
"properties": {
|
|
348
|
+
"default": {
|
|
349
|
+
"type": "string",
|
|
350
|
+
"enum": [
|
|
351
|
+
"system",
|
|
352
|
+
"light",
|
|
353
|
+
"dark"
|
|
354
|
+
],
|
|
355
|
+
"description": "The mode pages open in: \"system\" follows the reader's device setting; \"light\" or \"dark\" always starts there. Default \"system\". A reader who picks a mode with the theme toggle keeps their choice.",
|
|
356
|
+
"markdownDescription": "The mode pages open in: \"system\" follows the reader's device setting; \"light\" or \"dark\" always starts there. Default \"system\". A reader who picks a mode with the theme toggle keeps their choice."
|
|
357
|
+
},
|
|
358
|
+
"strict": {
|
|
359
|
+
"type": "boolean",
|
|
360
|
+
"description": "Hide the theme toggle, so every reader sees the `default` mode and can't switch. Default false.",
|
|
361
|
+
"markdownDescription": "Hide the theme toggle, so every reader sees the `default` mode and can't switch. Default false."
|
|
362
|
+
}
|
|
363
|
+
},
|
|
364
|
+
"additionalProperties": false,
|
|
365
|
+
"description": "Light and dark mode: which one pages open in, and whether readers can switch.",
|
|
366
|
+
"markdownDescription": "Light and dark mode: which one pages open in, and whether readers can switch."
|
|
344
367
|
}
|
|
345
368
|
},
|
|
346
369
|
"description": "Colors, logo, favicon, fonts, code block themes, navbar and background.",
|