@writedocs/generator 0.11.0 → 0.12.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.
@@ -1,55 +1,80 @@
1
- // The <= 860px hamburger menu (MobileMenu.astro's own toggle button +
2
- // overlay panel). The accordion rows inside the panel need no JS of their
3
- // own - they're plain <details name="wd-mobile-accordion"> elements, which
4
- // get native click/keyboard toggling and (in browsers supporting the
5
- // `name` attribute's exclusive-accordion behavior) mutual exclusion for
6
- // free, same as NavTree.astro's own collapsible groups already rely on
7
- // <details> for. This function only owns the panel itself: open/close, the
8
- // backdrop, Escape, and body scroll locking while it's open - same pattern
9
- // initSearch() uses for its own modal.
10
- export function initMobileMenu(root: ParentNode) {
11
- const trigger = root.querySelector<HTMLButtonElement>('[data-mobile-menu-trigger]');
12
- const menu = root.querySelector<HTMLElement>('#wd-mobile-menu');
13
- const backdrop = menu?.querySelector<HTMLElement>('[data-mobile-menu-backdrop]');
14
- // The panel's own close button (.wd-mobile-menu-header, next to its
15
- // replica of the topbar brand) - the real hamburger-turned-X trigger up
16
- // in the topbar is covered by the panel itself once open, so this is the
17
- // only reachable way to close via click besides the backdrop.
18
- const closeBtn = menu?.querySelector<HTMLButtonElement>('[data-mobile-menu-close]');
19
- if (!trigger || !menu || !backdrop || trigger.dataset.wdInit) return;
20
- trigger.dataset.wdInit = 'true';
21
-
22
- function open() {
23
- menu!.classList.add('open');
24
- // `inert` (not just visibility/pointer-events, which only affect
25
- // vision/mouse) is what keeps the closed panel's links out of Tab
26
- // order and off a screen reader's radar - removed while open so its
27
- // content becomes reachable again.
28
- menu!.removeAttribute('inert');
29
- trigger!.classList.add('open');
30
- trigger!.setAttribute('aria-expanded', 'true');
31
- document.body.style.overflow = 'hidden';
32
- }
33
- function close() {
34
- menu!.classList.remove('open');
35
- menu!.setAttribute('inert', '');
36
- trigger!.classList.remove('open');
37
- trigger!.setAttribute('aria-expanded', 'false');
38
- document.body.style.overflow = '';
39
- }
40
-
41
- trigger.addEventListener('click', () => (menu.classList.contains('open') ? close() : open()));
42
- backdrop.addEventListener('click', close);
43
- closeBtn?.addEventListener('click', close);
44
- document.addEventListener('keydown', (e) => {
45
- if (e.key === 'Escape' && menu.classList.contains('open')) close();
46
- });
47
- // Resizing past the breakpoint while open (rotating a tablet, or
48
- // widening a desktop browser window) - the panel/backdrop are already
49
- // hard-hidden above 860px via CSS regardless, but without this the body
50
- // scroll lock and the trigger's own "open" state would stick around with
51
- // no visible panel left to close them.
52
- window.matchMedia('(min-width: 861px)').addEventListener('change', (e) => {
53
- if (e.matches && menu.classList.contains('open')) close();
54
- });
55
- }
1
+ // The <= 860px hamburger menu (MobileMenu.astro's own toggle button +
2
+ // overlay panel). The accordion rows inside the panel need no JS of their
3
+ // own - they're plain <details name="wd-mobile-accordion"> elements, which
4
+ // get native click/keyboard toggling and (in browsers supporting the
5
+ // `name` attribute's exclusive-accordion behavior) mutual exclusion for
6
+ // free, same as NavTree.astro's own collapsible groups already rely on
7
+ // <details> for. This function only owns the panel itself: open/close, the
8
+ // backdrop, Escape, and body scroll locking while it's open - same pattern
9
+ // initSearch() uses for its own modal.
10
+ //
11
+ // And the sidebar inside it: the page has one, in BaseLayout's
12
+ // `.wd-sidebar-col` (hidden at this width). Opening the menu moves it into
13
+ // the panel's `.wd-mobile-menu-sidebar`, where it stays while the menu
14
+ // closes (the panel slides out with it) and opens again; widening the window
15
+ // past 860px moves it back. Moved rather than drawn there by CSS: the panel
16
+ // scrolls its switchers and the sidebar as one list, under its own header,
17
+ // and a second box laid over it couldn't. Moving is also what keeps the
18
+ // styles exactly those of the copy the panel used to get (MobileMenu.astro):
19
+ // the same nodes under the same containers. Without JavaScript the menu
20
+ // doesn't open, so nothing is lost there. ClientRouter replaces the whole
21
+ // <body> on every navigation, so each page starts with the sidebar in its
22
+ // column again.
23
+ export function initMobileMenu(root: ParentNode) {
24
+ const trigger = root.querySelector<HTMLButtonElement>('[data-mobile-menu-trigger]');
25
+ const menu = root.querySelector<HTMLElement>('#wd-mobile-menu');
26
+ const backdrop = menu?.querySelector<HTMLElement>('[data-mobile-menu-backdrop]');
27
+ // The panel's own close button (.wd-mobile-menu-header, next to its
28
+ // replica of the topbar brand) - the real hamburger-turned-X trigger up
29
+ // in the topbar is covered by the panel itself once open, so this is the
30
+ // only reachable way to close via click besides the backdrop.
31
+ const closeBtn = menu?.querySelector<HTMLButtonElement>('[data-mobile-menu-close]');
32
+ if (!trigger || !menu || !backdrop || trigger.dataset.wdInit) return;
33
+ trigger.dataset.wdInit = 'true';
34
+ const column = root.querySelector<HTMLElement>('.wd-sidebar-col');
35
+ const drawerSidebar = menu.querySelector<HTMLElement>('.wd-mobile-menu-sidebar');
36
+ const narrow = window.matchMedia('(max-width: 860px)');
37
+ // Every child, not just .wd-sidebar: whatever the page put in the sidebar slot.
38
+ const moveSidebar = (from: HTMLElement | null, to: HTMLElement | null) => {
39
+ if (from && to) to.append(...from.childNodes);
40
+ };
41
+
42
+ function open() {
43
+ // Only at the width where the column is hidden.
44
+ if (narrow.matches) moveSidebar(column, drawerSidebar);
45
+ menu!.classList.add('open');
46
+ // `inert` (not just visibility/pointer-events, which only affect
47
+ // vision/mouse) is what keeps the closed panel's links out of Tab
48
+ // order and off a screen reader's radar - removed while open so its
49
+ // content becomes reachable again.
50
+ menu!.removeAttribute('inert');
51
+ trigger!.classList.add('open');
52
+ trigger!.setAttribute('aria-expanded', 'true');
53
+ document.body.style.overflow = 'hidden';
54
+ }
55
+ function close() {
56
+ menu!.classList.remove('open');
57
+ menu!.setAttribute('inert', '');
58
+ trigger!.classList.remove('open');
59
+ trigger!.setAttribute('aria-expanded', 'false');
60
+ document.body.style.overflow = '';
61
+ }
62
+
63
+ trigger.addEventListener('click', () => (menu.classList.contains('open') ? close() : open()));
64
+ backdrop.addEventListener('click', close);
65
+ closeBtn?.addEventListener('click', close);
66
+ document.addEventListener('keydown', (e) => {
67
+ if (e.key === 'Escape' && menu.classList.contains('open')) close();
68
+ });
69
+ // Resizing past the breakpoint while open (rotating a tablet, or
70
+ // widening a desktop browser window) - the panel/backdrop are already
71
+ // hard-hidden above 860px via CSS regardless, but without this the body
72
+ // scroll lock and the trigger's own "open" state would stick around with
73
+ // no visible panel left to close them.
74
+ // Wide again, the sidebar goes back to its column whether or not the menu
75
+ // was still open.
76
+ window.matchMedia('(min-width: 861px)').addEventListener('change', (e) => {
77
+ if (e.matches && menu.classList.contains('open')) close();
78
+ if (e.matches) moveSidebar(drawerSidebar, column);
79
+ });
80
+ }
@@ -47,10 +47,14 @@ export function initSearch(root: ParentNode) {
47
47
  // *non-literal* dynamic import specifier - a literal string like
48
48
  // '/pagefind/pagefind.js' still gets resolved at transform time
49
49
  // regardless of the comment, which crashes `writedocs dev` outright once
50
- // this file exists in the template. Routing the literal through a
51
- // variable first makes the specifier opaque to static analysis,
52
- // deferring resolution to the browser at runtime - where the .catch()
53
- // below handles "not built yet" gracefully instead.
50
+ // this file exists in the template. A variable alone isn't enough either:
51
+ // rolldown 1.2.15 inlines it and folds `${__WD_BASE__}/pagefind/...` back
52
+ // into a literal, and the build fails with "Can't find
53
+ // /pagefind/pagefind.js". So the specifier is a URL made in the browser -
54
+ // the same address the path resolves to, but nothing a bundler can
55
+ // evaluate - deferring resolution to runtime, where the .catch() below
56
+ // handles "not built yet" gracefully instead. The path itself stays a
57
+ // literal in the bundle (scripts/base-path-dist.js checks it).
54
58
  let pagefind: any = null;
55
59
  let loadPromise: Promise<any> | null = null;
56
60
  function ensurePagefind() {
@@ -61,7 +65,7 @@ export function initSearch(root: ParentNode) {
61
65
  // Under writedocs.json's `basePath` ("" or "/docs", astro.config.mjs) -
62
66
  // written into the strings so a site without one gets the same code.
63
67
  const pagefindUrl = space ? `${__WD_BASE__}/pagefind-spaces/${encodeURIComponent(space)}/pagefind.js` : `${__WD_BASE__}/pagefind/pagefind.js`;
64
- loadPromise = import(/* @vite-ignore */ pagefindUrl)
68
+ loadPromise = import(/* @vite-ignore */ new URL(pagefindUrl, location.href).href)
65
69
  .then((mod: any) => {
66
70
  pagefind = mod;
67
71
  return mod.init();
@@ -1,65 +1,61 @@
1
- // ClientRouter swaps <body> (and everything in it) on every navigation -
2
- // [...slug].astro re-renders Sidebar.astro/NavTree.astro fresh per page,
3
- // since the active-link highlight and each collapsible group's open/closed
4
- // state are computed server-side from the *new* URL (see NavTree.astro's
5
- // own comment on `open = isActive || navTreeContainsSlug(...)`). That's
6
- // exactly the behavior a reader wants for highlighting/auto-expanding the
7
- // right branch - but it also means .wd-sidebar is a brand-new DOM node on
8
- // every navigation, with its own independent scroll position (it's
9
- // `overflow-y: auto`, not the window) starting back at 0, same as a full
10
- // page reload would. For a page nested deep in a long nav tree, that reads
11
- // as the sidebar "jumping back to the top" on every click - disorienting,
12
- // since the reader loses their place in the tree they were navigating.
13
- // This module carries the old sidebar's scroll offset across the swap and
14
- // re-applies it to the new one, without touching anything about how the
15
- // highlight/expand state itself is computed (that server-rendered-per-page
16
- // behavior stays exactly as it is - only the scroll position is preserved).
17
- //
18
- // A module-scoped variable (not sessionStorage) is enough: this script's
19
- // own module graph is loaded once and never re-evaluated by a ClientRouter
20
- // swap (only <body>'s content is replaced), so the value naturally
21
- // survives from one navigation to the next without needing to serialize it
22
- // anywhere. It deliberately does NOT survive a real full-page reload
23
- // (Cmd+R/F5) - resetting to the top on an actual reload is ordinary,
24
- // expected browser behavior, not something this needs to fight.
25
- let savedScrollTop: number | null = null;
26
-
27
- /** The sidebar rendered inline in the page (inside `.wd-sidebar-col`, for
28
- * desktop widths) and the identical copy reused inside MobileMenu.astro's
29
- * own drawer both carry the same `.wd-sidebar` class and both always
30
- * exist in the DOM at once (see MobileMenu.astro's own comment) - a media
31
- * query just hides whichever one isn't the current layout's. Picking
32
- * "whichever one actually has a real layout box" is enough to find the
33
- * one the reader could actually see and scroll, without needing to ask
34
- * which layout mode is active. Returns null on a page with no sidebar at
35
- * all (`mode: custom`/`blank`, see BaseLayout.astro) - both copies are
36
- * simply absent there. */
37
- function visibleSidebar(): HTMLElement | null {
38
- const candidates = document.querySelectorAll<HTMLElement>('.wd-sidebar');
39
- for (const el of candidates) {
40
- if (el.offsetWidth > 0 || el.offsetHeight > 0) return el;
41
- }
42
- return null;
43
- }
44
-
45
- /** Reads the current scroll offset right before ClientRouter tears down
46
- * the old page's DOM - `astro:before-swap` is the last moment the old
47
- * `.wd-sidebar` is still attached and its `scrollTop` still means
48
- * anything. */
49
- export function saveSidebarScroll() {
50
- const sidebar = visibleSidebar();
51
- savedScrollTop = sidebar ? sidebar.scrollTop : null;
52
- }
53
-
54
- /** Re-applies the saved offset onto the new page's own `.wd-sidebar`.
55
- * Bound to `astro:after-swap` (fires synchronously right after the swap,
56
- * before the browser's next paint - see BaseLayout.astro's own
57
- * `wdApplyStoredPreferences` for the identical timing rationale) rather
58
- * than the later `astro:page-load`, so the sidebar never visibly flashes
59
- * at the top before jumping to the preserved position. */
60
- export function restoreSidebarScroll() {
61
- if (savedScrollTop === null) return;
62
- const sidebar = visibleSidebar();
63
- if (sidebar) sidebar.scrollTop = savedScrollTop;
64
- savedScrollTop = null;
65
- }
1
+ // ClientRouter swaps <body> (and everything in it) on every navigation -
2
+ // [...slug].astro re-renders Sidebar.astro/NavTree.astro fresh per page,
3
+ // since the active-link highlight and each collapsible group's open/closed
4
+ // state are computed server-side from the *new* URL (see NavTree.astro's
5
+ // own comment on `open = isActive || navTreeContainsSlug(...)`). That's
6
+ // exactly the behavior a reader wants for highlighting/auto-expanding the
7
+ // right branch - but it also means .wd-sidebar is a brand-new DOM node on
8
+ // every navigation, with its own independent scroll position (it's
9
+ // `overflow-y: auto`, not the window) starting back at 0, same as a full
10
+ // page reload would. For a page nested deep in a long nav tree, that reads
11
+ // as the sidebar "jumping back to the top" on every click - disorienting,
12
+ // since the reader loses their place in the tree they were navigating.
13
+ // This module carries the old sidebar's scroll offset across the swap and
14
+ // re-applies it to the new one, without touching anything about how the
15
+ // highlight/expand state itself is computed (that server-rendered-per-page
16
+ // behavior stays exactly as it is - only the scroll position is preserved).
17
+ //
18
+ // A module-scoped variable (not sessionStorage) is enough: this script's
19
+ // own module graph is loaded once and never re-evaluated by a ClientRouter
20
+ // swap (only <body>'s content is replaced), so the value naturally
21
+ // survives from one navigation to the next without needing to serialize it
22
+ // anywhere. It deliberately does NOT survive a real full-page reload
23
+ // (Cmd+R/F5) - resetting to the top on an actual reload is ordinary,
24
+ // expected browser behavior, not something this needs to fight.
25
+ let savedScrollTop: number | null = null;
26
+
27
+ /** The page's one `.wd-sidebar` - in `.wd-sidebar-col` on desktop, or
28
+ * moved into MobileMenu.astro's drawer once that has been opened at phone
29
+ * width (src/scripts/mobile-menu.ts) - when it has a real layout box: in
30
+ * the column at phone width it's hidden, and there's no scroll position to
31
+ * carry. Returns null then, and on a page with no sidebar at all (`mode:
32
+ * custom`/`blank`, see BaseLayout.astro). */
33
+ function visibleSidebar(): HTMLElement | null {
34
+ const candidates = document.querySelectorAll<HTMLElement>('.wd-sidebar');
35
+ for (const el of candidates) {
36
+ if (el.offsetWidth > 0 || el.offsetHeight > 0) return el;
37
+ }
38
+ return null;
39
+ }
40
+
41
+ /** Reads the current scroll offset right before ClientRouter tears down
42
+ * the old page's DOM - `astro:before-swap` is the last moment the old
43
+ * `.wd-sidebar` is still attached and its `scrollTop` still means
44
+ * anything. */
45
+ export function saveSidebarScroll() {
46
+ const sidebar = visibleSidebar();
47
+ savedScrollTop = sidebar ? sidebar.scrollTop : null;
48
+ }
49
+
50
+ /** Re-applies the saved offset onto the new page's own `.wd-sidebar`.
51
+ * Bound to `astro:after-swap` (fires synchronously right after the swap,
52
+ * before the browser's next paint - see BaseLayout.astro's own
53
+ * `wdApplyStoredPreferences` for the identical timing rationale) rather
54
+ * than the later `astro:page-load`, so the sidebar never visibly flashes
55
+ * at the top before jumping to the preserved position. */
56
+ export function restoreSidebarScroll() {
57
+ if (savedScrollTop === null) return;
58
+ const sidebar = visibleSidebar();
59
+ if (sidebar) sidebar.scrollTop = savedScrollTop;
60
+ savedScrollTop = null;
61
+ }