@writedocs/generator 0.14.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.
Files changed (120) hide show
  1. package/astro.config.mjs +31 -69
  2. package/package.json +4 -2
  3. package/packages/components/package.json +53 -0
  4. package/{src → packages/components/src}/components/Accordion.astro +2 -2
  5. package/{src → packages/components/src}/components/AppIcon.astro +3 -3
  6. package/{src → packages/components/src}/components/Callout.astro +3 -3
  7. package/{src → packages/components/src}/components/CodeBlock.astro +8 -27
  8. package/{src → packages/components/src}/components/CodeGroup.astro +9 -9
  9. package/{src → packages/components/src}/components/Image.astro +7 -9
  10. package/{src → packages/components/src}/components/compound.ts +1 -1
  11. package/packages/components/src/host.js +24 -0
  12. package/packages/components/src/index.ts +59 -0
  13. package/packages/components/src/integration.js +151 -0
  14. package/packages/components/src/lib/base-path.js +39 -0
  15. package/{src → packages/components/src}/lib/code-line-numbers.js +1 -1
  16. package/packages/components/src/lib/code-transformers.js +45 -0
  17. package/{src → packages/components/src}/lib/icons.js +13 -7
  18. package/{src → packages/components/src}/lib/shiki-code-block.js +9 -9
  19. package/{src → packages/components/src}/lib/site-base.ts +9 -6
  20. package/packages/components/src/scripts/code-block.ts +62 -0
  21. package/packages/components/src/styles/components.css +373 -0
  22. package/packages/components/src/styles/theme.css +63 -0
  23. package/src/cli/build.js +1 -1
  24. package/src/cli/rewrite-redirect-pages.js +8 -6
  25. package/src/cli/run-astro.js +1 -1
  26. package/src/cli/write-mcp-files.js +4 -1
  27. package/src/cli/write-redirects-file.js +4 -3
  28. package/src/components/ApiPlayground.astro +2 -2
  29. package/src/components/ApiSchemaField.astro +1 -1
  30. package/src/components/CopyPageMenu.astro +2 -2
  31. package/src/content.config.ts +21 -1
  32. package/src/icons/README.md +1 -1
  33. package/src/layout/BaseLayout.astro +45 -51
  34. package/src/layout/components/AnalyticsScripts.astro +2 -2
  35. package/src/layout/components/Breadcrumbs.astro +1 -1
  36. package/src/layout/components/ConsentBanner.astro +1 -1
  37. package/src/layout/components/MobileMenu.astro +11 -7
  38. package/src/layout/components/NavTree.astro +2 -2
  39. package/src/layout/components/Sidebar.astro +2 -2
  40. package/src/layout/components/SiteFooter.astro +2 -2
  41. package/src/layout/components/TopBar.astro +10 -6
  42. package/src/layout/styles/base.css +4 -37
  43. package/src/lib/a11y-check.js +1 -1
  44. package/src/lib/appearance.js +64 -0
  45. package/src/lib/asset-dimensions.js +2 -1
  46. package/src/lib/base-path.js +4 -25
  47. package/src/lib/bom.js +20 -0
  48. package/src/lib/color.js +1 -1
  49. package/src/lib/components-host.ts +21 -0
  50. package/src/lib/config-file.js +21 -8
  51. package/src/lib/config-schema.js +14 -7
  52. package/src/lib/config-schema.ts +19 -12
  53. package/src/lib/config.ts +4 -3
  54. package/src/lib/content-check.js +38 -3
  55. package/src/lib/content-store-prune.js +44 -0
  56. package/src/lib/json-schema-descriptions.js +4 -1
  57. package/src/lib/link-check.js +2 -2
  58. package/src/lib/mdx-inject-builtins.js +2 -2
  59. package/src/lib/mdx-inline-react.js +1 -1
  60. package/src/lib/mdx-title-anchor-ids.js +2 -2
  61. package/src/lib/mdx-unknown-components.js +1 -1
  62. package/src/lib/mintlify-convert.js +13 -3
  63. package/src/lib/openapi-reload-integration.js +1 -1
  64. package/src/lib/pages.js +6 -4
  65. package/src/lib/project-files-integration.js +1 -1
  66. package/src/lib/rehype-base-path.js +1 -1
  67. package/src/lib/wdignore-cases.json +9 -2
  68. package/src/lib/wdignore.js +5 -0
  69. package/src/lib/writedocs-legacy-convert.js +2 -2
  70. package/src/pages/404.astro +1 -1
  71. package/src/pages/[...slug].astro +53 -416
  72. package/src/pages/llms-full.txt.ts +6 -3
  73. package/src/pages/llms.txt.ts +4 -4
  74. package/writedocs.schema.json +23 -0
  75. package/src/components/index.ts +0 -62
  76. /package/{src → packages/components/src}/components/AccordionGroup.astro +0 -0
  77. /package/{src → packages/components/src}/components/Badge.astro +0 -0
  78. /package/{src → packages/components/src}/components/Card.astro +0 -0
  79. /package/{src → packages/components/src}/components/CardGroup.astro +0 -0
  80. /package/{src → packages/components/src}/components/Check.astro +0 -0
  81. /package/{src → packages/components/src}/components/Color.astro +0 -0
  82. /package/{src → packages/components/src}/components/ColorItem.astro +0 -0
  83. /package/{src → packages/components/src}/components/ColorRow.astro +0 -0
  84. /package/{src → packages/components/src}/components/Column.astro +0 -0
  85. /package/{src → packages/components/src}/components/Columns.astro +0 -0
  86. /package/{src → packages/components/src}/components/Danger.astro +0 -0
  87. /package/{src → packages/components/src}/components/Expandable.astro +0 -0
  88. /package/{src → packages/components/src}/components/Frame.astro +0 -0
  89. /package/{src → packages/components/src}/components/GitHubRepo.astro +0 -0
  90. /package/{src → packages/components/src}/components/Hint.astro +0 -0
  91. /package/{src → packages/components/src}/components/Icon.astro +0 -0
  92. /package/{src → packages/components/src}/components/Info.astro +0 -0
  93. /package/{src → packages/components/src}/components/Note.astro +0 -0
  94. /package/{src → packages/components/src}/components/Panel.astro +0 -0
  95. /package/{src → packages/components/src}/components/ParamField.astro +0 -0
  96. /package/{src → packages/components/src}/components/Parameter.astro +0 -0
  97. /package/{src → packages/components/src}/components/Prompt.astro +0 -0
  98. /package/{src → packages/components/src}/components/RequestExample.astro +0 -0
  99. /package/{src → packages/components/src}/components/ResponseExample.astro +0 -0
  100. /package/{src → packages/components/src}/components/ResponseField.astro +0 -0
  101. /package/{src → packages/components/src}/components/Searchbar.astro +0 -0
  102. /package/{src → packages/components/src}/components/Step.astro +0 -0
  103. /package/{src → packages/components/src}/components/Steps.astro +0 -0
  104. /package/{src → packages/components/src}/components/Tab.astro +0 -0
  105. /package/{src → packages/components/src}/components/Tabs.astro +0 -0
  106. /package/{src → packages/components/src}/components/Tile.astro +0 -0
  107. /package/{src → packages/components/src}/components/Tip.astro +0 -0
  108. /package/{src → packages/components/src}/components/Tooltip.astro +0 -0
  109. /package/{src → packages/components/src}/components/Tree.astro +0 -0
  110. /package/{src → packages/components/src}/components/TreeFile.astro +0 -0
  111. /package/{src → packages/components/src}/components/TreeFolder.astro +0 -0
  112. /package/{src → packages/components/src}/components/Update.astro +0 -0
  113. /package/{src → packages/components/src}/components/Video.astro +0 -0
  114. /package/{src → packages/components/src}/components/View.astro +0 -0
  115. /package/{src → packages/components/src}/components/Visibility.astro +0 -0
  116. /package/{src → packages/components/src}/components/Warning.astro +0 -0
  117. /package/{src → packages/components/src}/components/class-names.ts +0 -0
  118. /package/{src → packages/components/src}/lib/cli-report.js +0 -0
  119. /package/{src → packages/components/src}/lib/inline-markdown.js +0 -0
  120. /package/{src → packages/components/src}/lib/plain-text.js +0 -0
@@ -55,6 +55,11 @@ import "../styles/global.css";
55
55
  // theme with no extra CSS needed here.
56
56
  import "katex/dist/katex.min.css";
57
57
  import "./styles/base.css";
58
+ // What the components package's components share: code blocks (every ```
59
+ // fence goes through the package's transformers - astro.config.mjs), Shiki's
60
+ // dark theme, icons given as an image. The --wd-* variables it uses are this
61
+ // layout's own (the define:vars <style> below).
62
+ import "../../packages/components/src/styles/components.css";
58
63
  import "./styles/banner.css";
59
64
  // Astro's client-side router - swaps <body> content between same-origin
60
65
  // navigations via the native View Transition API instead of a hard
@@ -83,10 +88,12 @@ import { htmlDirective } from "../lib/agent-directive.js";
83
88
  import { contextMenuOptions } from "../lib/config";
84
89
  import { jsonLd, serializeJsonLd } from "../lib/json-ld.js";
85
90
  import { consentOptions } from "../lib/consent.js";
91
+ import { themeScript, serverTheme } from "../lib/appearance.js";
86
92
  import { oncePerBuild } from "../lib/build-memo.js";
93
+ import { stripBom } from "../lib/bom.js";
87
94
  // writedocs.json's `basePath`: every site address written below goes
88
- // through siteHref() - see lib/site-base.ts.
89
- import { BASE, siteHref } from "../lib/site-base";
95
+ // through siteHref() - see the components package's lib/site-base.ts.
96
+ import { BASE, siteHref } from "../../packages/components/src/lib/site-base";
90
97
 
91
98
  interface Props {
92
99
  config: DocsConfig;
@@ -406,11 +413,19 @@ const rootAssets = findRootAssets(contentDir);
406
413
  // "docs/theme.css") - split and rejoin with nodePath.join rather than
407
414
  // handing the raw string straight to it, so a nested asset resolves
408
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.
409
424
  const rootCssContents = oncePerBuild("root-css-contents", () => rootAssets.css.map((name) =>
410
- fs.readFileSync(nodePath.join(contentDir, ...name.split("/")), "utf-8"),
425
+ stripBom(fs.readFileSync(nodePath.join(contentDir, ...name.split("/")), "utf-8")),
411
426
  ));
412
427
  const rootJsContents = oncePerBuild("root-js-contents", () => rootAssets.js.map((name) =>
413
- fs.readFileSync(nodePath.join(contentDir, ...name.split("/")), "utf-8"),
428
+ stripBom(fs.readFileSync(nodePath.join(contentDir, ...name.split("/")), "utf-8")),
414
429
  ));
415
430
  // public/*.css and public/*.js auto-load too, but as <link>/<script src>
416
431
  // pointing at their already-public URL (see findRootAssets()'s own
@@ -496,7 +511,7 @@ const fontExtraCss = [fontFaceCss, fontFallbackCss, fontWeightCss].filter(Boolea
496
511
  ---
497
512
 
498
513
  <!doctype html>
499
- <html lang={lang}>
514
+ <html lang={lang} data-theme={serverTheme(config.styles.appearance)}>
500
515
  <head>
501
516
  <meta charset="UTF-8" />
502
517
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
@@ -553,52 +568,31 @@ const fontExtraCss = [fontFaceCss, fontFallbackCss, fontWeightCss].filter(Boolea
553
568
  consent={consent}
554
569
  gatedScripts={[...config.scripts.head, ...config.scripts.body].filter(gated)}
555
570
  />
556
- <script is:inline>
557
- // Sets [data-theme] and [data-banner-dismissed] on <html> before
558
- // first paint, so the page never flashes the wrong mode or an
559
- // already-dismissed banner. Preference order for theme: an explicit
560
- // prior choice (localStorage), then the OS-level preference,
561
- // defaulting to light. The click handlers that update both live in
562
- // src/scripts/theme-toggle.ts and src/scripts/banner.ts.
563
- //
564
- // Also re-run on `astro:after-swap` (ClientRouter, see BaseLayout's
565
- // own import of it below), not just the initial load - Astro's
566
- // client-side navigation swaps <html>'s attributes wholesale to
567
- // match the newly-fetched page's raw server-rendered markup (see
568
- // swapRootAttributes() in astro/dist/transitions/swap-functions.js),
569
- // which has neither of these dataset attributes baked in since
570
- // they're pure client-side runtime state, not something the server
571
- // ever renders. Without re-running this after every swap, a
572
- // dismissed banner (or a chosen dark theme) would silently come
573
- // back on the very next navigation - both attributes get wiped, not
574
- // just reset to their default, so this has to unconditionally
575
- // reapply them from localStorage every time, exactly as it does on
576
- // first load. `astro:after-swap` fires synchronously right after
577
- // that swap and before the browser's next paint, so this still
578
- // prevents any visible flash on client-side navigations too.
579
- function wdApplyStoredPreferences() {
580
- try {
581
- var stored = localStorage.getItem("wd-theme");
582
- var theme =
583
- stored === "light" || stored === "dark"
584
- ? stored
585
- : window.matchMedia("(prefers-color-scheme: dark)").matches
586
- ? "dark"
587
- : "light";
588
- document.documentElement.dataset.theme = theme;
589
- } catch (e) {
590
- document.documentElement.dataset.theme = "light";
591
- }
592
- try {
593
- document.documentElement.dataset.bannerDismissed =
594
- localStorage.getItem("wd-banner-dismissed") === "true" ? "true" : "false";
595
- } catch (e) {
596
- document.documentElement.dataset.bannerDismissed = "false";
597
- }
598
- }
599
- wdApplyStoredPreferences();
600
- document.addEventListener("astro:after-swap", wdApplyStoredPreferences);
601
- </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
+ />
602
596
  <script>
603
597
  // Preserves the sidebar's own scroll position across a ClientRouter
604
598
  // navigation - see src/scripts/sidebar-scroll.ts for why this is
@@ -22,7 +22,7 @@
22
22
  // it doesn't wait for consent.
23
23
  import type { DocsConfig } from '../../lib/config';
24
24
  import { analyticsTags, scriptTags, consentLoaderScript } from '../../lib/consent.js';
25
- import { siteHref } from '../../lib/site-base';
25
+ import { siteHref } from '../../../packages/components/src/lib/site-base';
26
26
 
27
27
  interface Props {
28
28
  config: DocsConfig;
@@ -35,7 +35,7 @@ interface Props {
35
35
  const { config, consent = null, gatedScripts = [] } = Astro.props as Props;
36
36
 
37
37
  // A script's `src` can be a file in the site ("/js/stats.js"): it gets the
38
- // basePath (lib/site-base.ts).
38
+ // basePath (the components package's lib/site-base.ts).
39
39
  const withSrcBase = <T extends { src?: string }>(tag: T): T => (tag.src ? { ...tag, src: siteHref(tag.src) } : tag);
40
40
  const tags = analyticsTags(config.integrations).map(withSrcBase);
41
41
  const loader = consent ? consentLoaderScript([...tags, ...scriptTags(gatedScripts).map(withSrcBase)]) : null;
@@ -20,7 +20,7 @@
20
20
  // Iconify id for the exact same Font Awesome 6 glyph as react-icons'
21
21
  // `FaHouse`, without pulling React into an otherwise-static nav element.
22
22
  import { Icon } from 'astro-icon/components';
23
- import { siteHref } from '../../lib/site-base';
23
+ import { siteHref } from '../../../packages/components/src/lib/site-base';
24
24
 
25
25
  export interface BreadcrumbItem {
26
26
  label: string;
@@ -1,5 +1,5 @@
1
1
  ---
2
- import { siteHref } from '../../lib/site-base';
2
+ import { siteHref } from '../../../packages/components/src/lib/site-base';
3
3
  // writedocs.json's `integrations.consent` - the banner that asks before the
4
4
  // analytics integrations load. It's in the page hidden; src/scripts/
5
5
  // consent.ts shows it when the reader hasn't answered yet (window.wdConsent,
@@ -29,13 +29,14 @@
29
29
  // several unrelated concerns in one file - see that component's own
30
30
  // comment for the fuller rationale.
31
31
  import { isExternalHref, type DocsConfig, type Selector, type GlobalDropdownView } from '../../lib/config';
32
- import AppIcon from '../../components/AppIcon.astro';
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';
37
38
  import '../styles/mobile-menu.css';
38
- import { siteHref } from '../../lib/site-base';
39
+ import { siteHref } from '../../../packages/components/src/lib/site-base';
39
40
 
40
41
  interface Props {
41
42
  config: DocsConfig;
@@ -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
- <button type="button" class="wd-theme-toggle" aria-label="Toggle color theme">
97
- <AppIcon icon="sun" class="wd-theme-icon wd-theme-icon-sun" />
98
- <AppIcon icon="moon" class="wd-theme-icon wd-theme-icon-moon" />
99
- </button>
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) && (
@@ -1,8 +1,8 @@
1
1
  ---
2
2
  import { navTreeContainsSlug, isExternalHref, type NavTreeNode } from "../../lib/config";
3
- import AppIcon from "../../components/AppIcon.astro";
3
+ import AppIcon from "../../../packages/components/src/components/AppIcon.astro";
4
4
  import { methodClass } from "../../lib/openapi-render";
5
- import { siteHref } from '../../lib/site-base';
5
+ import { siteHref } from '../../../packages/components/src/lib/site-base';
6
6
 
7
7
  interface Props {
8
8
  nodes: NavTreeNode[];
@@ -2,7 +2,7 @@
2
2
  import { isExternalHref, type NavTreeNode, type Selector } from "../../lib/config";
3
3
  import NavTree from "./NavTree.astro";
4
4
  import { currentOption } from "../../lib/selector-placement.js";
5
- import AppIcon from "../../components/AppIcon.astro";
5
+ import AppIcon from "../../../packages/components/src/components/AppIcon.astro";
6
6
  // Two colorways of the same "Powered by writedocs" mark, not a
7
7
  // theme-matched pair in the wd-logo-light/wd-logo-dark sense (that
8
8
  // convention names each file by which theme it's *shown in*) - these
@@ -17,7 +17,7 @@ import AppIcon from "../../components/AppIcon.astro";
17
17
  // src="/..."> here would 404 on every real site build.
18
18
  import watermarkForLightTheme from "../../assets/wd_watermark_dark.png";
19
19
  import watermarkForDarkTheme from "../../assets/wd_watermark.png";
20
- import { siteHref } from '../../lib/site-base';
20
+ import { siteHref } from '../../../packages/components/src/lib/site-base';
21
21
 
22
22
  // Deploy-time-only opt-out, not a writedocs.json field: a site's own writedocs.json
23
23
  // is part of its checked-in content (something the author controls), while
@@ -10,10 +10,10 @@
10
10
  // several unrelated concerns in one file - see that component's own
11
11
  // comment for the fuller rationale.
12
12
  import { isExternalHref, type DocsConfig } from '../../lib/config';
13
- import AppIcon from '../../components/AppIcon.astro';
13
+ import AppIcon from '../../../packages/components/src/components/AppIcon.astro';
14
14
  import { logoWidth } from '../../lib/image-size.js';
15
15
  import '../styles/footer.css';
16
- import { siteHref } from '../../lib/site-base';
16
+ import { siteHref } from '../../../packages/components/src/lib/site-base';
17
17
 
18
18
  interface Props {
19
19
  config: DocsConfig;
@@ -12,12 +12,13 @@
12
12
  // several unrelated concerns in one file - see that component's own
13
13
  // comment for the fuller rationale.
14
14
  import { isExternalHref, type DocsConfig, type Selector, type GlobalDropdownView } from '../../lib/config';
15
- import AppIcon from '../../components/AppIcon.astro';
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';
20
- import { siteHref } from '../../lib/site-base';
21
+ import { siteHref } from '../../../packages/components/src/lib/site-base';
21
22
 
22
23
  interface Props {
23
24
  config: DocsConfig;
@@ -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
- <button type="button" class="wd-theme-toggle" aria-label="Toggle color theme">
210
- <AppIcon icon="sun" class="wd-theme-icon wd-theme-icon-sun" />
211
- <AppIcon icon="moon" class="wd-theme-icon wd-theme-icon-moon" />
212
- </button>
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
 
@@ -218,38 +218,11 @@ pre {
218
218
  display: block;
219
219
  }
220
220
 
221
- /* Astro's Shiki output (both the built-in MDX code-fence pipeline,
222
- configured with dual themes in astro.config.mjs, and the <Code />
223
- component used for the API playground's request/response snippets)
224
- bakes the "light" theme as each element's base color/background and
225
- stashes the dark equivalents in --shiki-dark/--shiki-dark-bg custom
226
- properties. Swapping to those on [data-theme='dark'] is the officially
227
- documented way to make Shiki-highlighted code follow a manual
228
- (non-media-query) theme toggle - see
229
- https://docs.astro.build/en/guides/syntax-highlighting/#setting-light-and-dark-mode-themes.
230
- Site-wide/article-content concern, unrelated to any one layout
231
- component, which is why it lives here rather than in any of the
232
- feature-specific stylesheets.
233
- The background goes on the <pre> only: Shiki gives the spans inside no
234
- background of their own, and painting the theme's background on every
235
- span (.line included) covered the highlighted, diff, error and focus
236
- line colors and the highlighted words in dark mode. */
237
- [data-theme="dark"] .astro-code {
238
- background-color: var(--shiki-dark-bg) !important;
239
- }
240
- [data-theme="dark"] .astro-code,
241
- [data-theme="dark"] .astro-code span {
242
- color: var(--shiki-dark) !important;
243
- font-style: var(--shiki-dark-font-style) !important;
244
- font-weight: var(--shiki-dark-font-weight) !important;
245
- text-decoration: var(--shiki-dark-text-decoration) !important;
246
- }
221
+ /* Shiki's dark theme on code blocks, the code blocks themselves, and icons
222
+ given as an image are the components package's
223
+ (packages/components/src/styles/components.css, imported by
224
+ BaseLayout.astro right after this file). */
247
225
 
248
- /* An icon given as an image URL or path (resolveIcon()'s 'image' kind,
249
- rendered by AppIcon.astro) - 1em square inside its caller's own icon
250
- span, so it sizes like a glyph icon would. Lives here rather than in
251
- AppIcon.astro so it's part of the shared stylesheet, not an inline
252
- <style> added to every page. */
253
226
  /* An image the build gave its file's width and height
254
227
  (lib/rehype-image-loading.js): those attributes reserve its place while
255
228
  it loads, and this keeps its height following its width when it's shown
@@ -280,12 +253,6 @@ pre {
280
253
  :where(img[data-wd-sized]) {
281
254
  height: auto;
282
255
  }
283
- img.wd-icon-img {
284
- display: block;
285
- width: 1em;
286
- height: 1em;
287
- object-fit: contain;
288
- }
289
256
 
290
257
  /* Scrollbars of the columns beside the page - the sidebar, the table of
291
258
  contents, the mobile menu and its option lists: a thin pill in the
@@ -1,7 +1,7 @@
1
1
  // `writedocs a11y`: accessibility problems that can be found in the source,
2
2
  // without building - the ones an author introduces and can fix in a page or
3
3
  // in writedocs.json. Plain JavaScript, loaded by plain Node from an
4
- // installed package (see lib/icons.js's comment on why not TypeScript).
4
+ // installed package (see packages/components/src/lib/icons.js's comment on why not TypeScript).
5
5
  //
6
6
  // - contrast (WCAG 2 AA, 4.5:1 for text) of the colors writedocs.json sets:
7
7
  // links (styles.colors.primary) on the light and dark backgrounds, body
@@ -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).replace(/^\uFEFF/, ''));
42
+ data = JSON.parse(stripBom(String(text)));
42
43
  } catch {
43
44
  return sizes;
44
45
  }
@@ -36,31 +36,10 @@ export function normalizeBasePath(value) {
36
36
  return value;
37
37
  }
38
38
 
39
- /** Whether `url` is a site address: a path from the site's root ("/a/b"),
40
- * not a protocol-relative "//host/a". */
41
- export function isSiteAddress(url) {
42
- return typeof url === 'string' && url.startsWith('/') && !url.startsWith('//');
43
- }
44
-
45
- /** `url` with the base in front, if it's a site address - "/a/" -> "/docs/a/",
46
- * "/" -> "/docs/". Anything else, and everything when there's no base,
47
- * comes back as it is. */
48
- export function withBase(base, url) {
49
- if (!base || !isSiteAddress(url)) return url;
50
- return base + url;
51
- }
52
-
53
- /** A `srcset` with the base in front of each site address in it. */
54
- export function srcsetWithBase(base, srcset) {
55
- if (!base || typeof srcset !== 'string') return srcset;
56
- return srcset
57
- .split(',')
58
- .map((candidate) => {
59
- const [url, ...descriptor] = candidate.trim().split(/\s+/);
60
- return [withBase(base, url), ...descriptor].join(' ');
61
- })
62
- .join(', ');
63
- }
39
+ // isSiteAddress(), withBase() and srcsetWithBase() - which addresses take the
40
+ // base, and putting it in front of them - live in the components package,
41
+ // whose components write addresses too.
42
+ export { isSiteAddress, withBase, srcsetWithBase } from '../../packages/components/src/lib/base-path.js';
64
43
 
65
44
  /** The address inside the site for a request path - "/docs/a/" -> "/a/",
66
45
  * "/docs" -> "/" - or null when the path isn't under the base. With no base,
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/color.js CHANGED
@@ -3,7 +3,7 @@
3
3
  // goes on a configured color, and by `writedocs a11y` (lib/a11y-check.js),
4
4
  // which checks that choice. One implementation, so the check always
5
5
  // measures what the site actually renders. Plain JavaScript, loaded by
6
- // plain Node from an installed package (see lib/icons.js's comment).
6
+ // plain Node from an installed package (see packages/components/src/lib/icons.js's comment).
7
7
 
8
8
  /** [r, g, b] for `#rgb`/`#rrggbb` (the `#` optional), or null. */
9
9
  export function parseHex(value) {
@@ -0,0 +1,21 @@
1
+ // The generator's answers to what the components package asks of its host
2
+ // (packages/components/src/host.js has the default and what each function
3
+ // means). astro.config.mjs passes this file to the package's integration,
4
+ // which puts it in place of the default. Both read the site being built:
5
+ // its writedocs.json and its files.
6
+ import { loadDocsConfig, resolveCodeblockTheme } from './config';
7
+ import { imageFileFor, imageSize as fileImageSize } from './image-size.js';
8
+
9
+ const contentDir = process.env.WRITEDOCS_CONTENT_DIR || process.cwd();
10
+
11
+ /** writedocs.json's styles.codeblocks themes - read on every call, so an
12
+ * edit during `writedocs dev` reaches the next render. */
13
+ export function codeblockTheme(): { light: string; dark: string } {
14
+ return resolveCodeblockTheme(loadDocsConfig(contentDir));
15
+ }
16
+
17
+ /** The size of the project's file at `src`, or null when it isn't one. */
18
+ export function imageSize(src: string): { width: number; height: number } | null {
19
+ const file = imageFileFor(src, contentDir);
20
+ return (file && fileImageSize(file, contentDir)) || null;
21
+ }
@@ -1,25 +1,38 @@
1
- // Reading writedocs.json (and the other JSON configs `writedocs convert`
2
- // reads) as text. Plain JavaScript, not TypeScript, so the CLI can load it
3
- // with plain Node - see lib/icons.js's comment.
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
- /** `text` without a leading UTF-8 byte order mark. */
13
- export function stripBom(text) {
14
- return text.charCodeAt(0) === 0xfeff ? text.slice(1) : text;
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 stripBom(fs.readFileSync(file, 'utf-8'));
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
+ }