@writedocs/generator 0.21.0 → 0.22.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.
@@ -0,0 +1,189 @@
1
+ ---
2
+ // The tag pages (frontmatter `tags` - lib/tags.js): `/tags/` lists every tag,
3
+ // `/tags/<tag>/` every page with that tag. What they list, and which are made,
4
+ // is lib/tag-index.ts.
5
+ //
6
+ // A route of its own, in the shape of 404.astro (BaseLayout in "frame" mode:
7
+ // the topbar, no sidebar - a tag page belongs to no section), rather than
8
+ // another kind of route in [...slug].astro, which renders a page's content
9
+ // throughout. That makes this route outrank [...slug] for an address under
10
+ // /tags/ - so the two must never make the same address (Astro warns of a
11
+ // conflict): tagRoutes() leaves an address to a page or redirect already
12
+ // there, and [...slug].astro's folder redirects keep off the tag pages'.
13
+ //
14
+ // Not in search (no data-pagefind-body - a list of titles would only repeat
15
+ // the pages' own results), and not in llms.txt or the MCP index, which list
16
+ // pages. In the sitemap like any page.
17
+ import { loadDocsConfig, mergeSeo } from "../../lib/config";
18
+ import { tagIndex, tagPageChrome, type Tag } from "../../lib/tag-index";
19
+ import { tagHref } from "../../lib/tags.js";
20
+ import BaseLayout from "../../layout/BaseLayout.astro";
21
+ import { siteHref } from "../../../packages/components/src/lib/site-base";
22
+
23
+ export async function getStaticPaths() {
24
+ const index = await tagIndex();
25
+ return [
26
+ ...(index.index ? [{ params: { tag: undefined }, props: { tag: null } }] : []),
27
+ ...index.routed.map((tag) => ({ params: { tag: tag.slug }, props: { tag } })),
28
+ ];
29
+ }
30
+
31
+ interface Props {
32
+ // The tag whose page this is; null for the list of tags.
33
+ tag: Tag | null;
34
+ }
35
+ const { tag } = Astro.props as Props;
36
+
37
+ const contentDir = process.env.WRITEDOCS_CONTENT_DIR || process.cwd();
38
+ const config = loadDocsConfig(contentDir);
39
+ const index = await tagIndex();
40
+ const { selectors, globalDropdowns } = await tagPageChrome();
41
+
42
+ const pageCount = (n: number) => (n === 1 ? "1 page" : `${n} pages`);
43
+ const title = tag ? `Tagged “${tag.label}”` : "Tags";
44
+ const description = tag
45
+ ? `${pageCount(tag.pages.length)} tagged “${tag.label}”.`
46
+ : `Every tag of ${config.name}, with the pages under each.`;
47
+ // The list of tags links only to tags that have a page.
48
+ const listed = index.tags.filter((t) => index.linked.has(t.slug));
49
+ ---
50
+
51
+ <BaseLayout
52
+ config={config}
53
+ title={title}
54
+ description={description}
55
+ path={tagHref(tag?.slug)}
56
+ seo={mergeSeo(config.seo, undefined)}
57
+ selectors={selectors}
58
+ globalDropdowns={globalDropdowns}
59
+ mode="frame"
60
+ >
61
+ <div class="wd-tags-page">
62
+ {
63
+ tag ? (
64
+ <>
65
+ <p class="wd-tags-eyebrow">Tag</p>
66
+ <h1>{tag.label}</h1>
67
+ <p class="wd-tags-count">
68
+ {pageCount(tag.pages.length)}
69
+ {index.index && (
70
+ <>
71
+ {" · "}
72
+ <a href={siteHref(tagHref())}>All tags</a>
73
+ </>
74
+ )}
75
+ </p>
76
+ <ul class="wd-tags-pages">
77
+ {tag.pages.map((page) => (
78
+ <li>
79
+ <a class="wd-tags-page-link" href={siteHref(page.href)}>
80
+ {page.where.length > 0 && <span class="wd-tags-page-where">{page.where.join(" › ")}</span>}
81
+ <span class="wd-tags-page-title">{page.title}</span>
82
+ {page.description && <span class="wd-tags-page-description">{page.description}</span>}
83
+ </a>
84
+ </li>
85
+ ))}
86
+ </ul>
87
+ </>
88
+ ) : (
89
+ <>
90
+ <h1>Tags</h1>
91
+ <ul class="wd-tags-list">
92
+ {listed.map((t) => (
93
+ <li>
94
+ <a class="wd-tag" href={siteHref(tagHref(t.slug))}>
95
+ {t.label}
96
+ <span class="wd-tag-count" aria-label={pageCount(t.pages.length)}>
97
+ {t.pages.length}
98
+ </span>
99
+ </a>
100
+ </li>
101
+ ))}
102
+ </ul>
103
+ </>
104
+ )
105
+ }
106
+ </div>
107
+ </BaseLayout>
108
+
109
+ <style>
110
+ .wd-tags-page {
111
+ max-width: 760px;
112
+ margin: 0 auto;
113
+ padding: 2.5rem 1.5rem 5rem;
114
+ }
115
+ .wd-tags-page h1 {
116
+ margin: 0 0 0.5rem;
117
+ font-size: 1.9rem;
118
+ line-height: 1.2;
119
+ overflow-wrap: anywhere;
120
+ color: var(--wd-heading);
121
+ }
122
+ .wd-tags-eyebrow {
123
+ margin: 0 0 0.35rem;
124
+ font-size: 0.8rem;
125
+ font-weight: 600;
126
+ letter-spacing: 0.08em;
127
+ text-transform: uppercase;
128
+ color: var(--wd-primary);
129
+ }
130
+ .wd-tags-count {
131
+ margin: 0 0 1.75rem;
132
+ color: var(--wd-text-muted);
133
+ }
134
+ .wd-tags-count a {
135
+ color: var(--wd-primary);
136
+ text-decoration: none;
137
+ font-weight: 500;
138
+ }
139
+ .wd-tags-count a:hover {
140
+ text-decoration: underline;
141
+ }
142
+ .wd-tags-pages {
143
+ list-style: none;
144
+ margin: 0;
145
+ padding: 0;
146
+ display: grid;
147
+ gap: 0.75rem;
148
+ }
149
+ .wd-tags-page-link {
150
+ display: flex;
151
+ flex-direction: column;
152
+ gap: 0.25rem;
153
+ padding: 0.9rem 1.1rem;
154
+ border: 1px solid var(--wd-border);
155
+ border-radius: 0.6rem;
156
+ color: inherit;
157
+ text-decoration: none;
158
+ transition: border-color 0.15s ease;
159
+ }
160
+ .wd-tags-page-link:hover,
161
+ .wd-tags-page-link:focus-visible {
162
+ border-color: var(--wd-primary);
163
+ }
164
+ .wd-tags-page-where {
165
+ font-size: 0.78rem;
166
+ color: var(--wd-text-muted);
167
+ }
168
+ .wd-tags-page-title {
169
+ font-weight: 600;
170
+ color: var(--wd-heading);
171
+ }
172
+ .wd-tags-page-description {
173
+ font-size: 0.92rem;
174
+ color: var(--wd-text-muted);
175
+ }
176
+ .wd-tags-list {
177
+ list-style: none;
178
+ margin: 1.5rem 0 0;
179
+ padding: 0;
180
+ display: flex;
181
+ flex-wrap: wrap;
182
+ gap: 0.5rem;
183
+ }
184
+ .wd-tag-count {
185
+ margin-left: 0.4rem;
186
+ font-size: 0.75rem;
187
+ color: var(--wd-text-muted);
188
+ }
189
+ </style>
@@ -0,0 +1,60 @@
1
+ // Remembers what a reader ticked in a page's task lists (`- [ ] item`), in
2
+ // their browser's localStorage - so a checklist (a recipe's ingredients, an
3
+ // install's steps) is still ticked when they come back to the page. Started
4
+ // from BaseLayout.astro, on load and after every view-transition swap.
5
+ //
6
+ // The boxes come out of the build already tickable (lib/rehype-task-lists.js);
7
+ // without this script they still tick, they just aren't remembered. What's
8
+ // stored, and under which name, is lib/task-lists.js. localStorage being
9
+ // unavailable (blocked, private mode, full) only means nothing is remembered.
10
+ import { taskKeys, taskStorageKey, readTaskStates, hasTaskState, taskStatesToStore } from '../lib/task-lists.js';
11
+
12
+ const BOX_SELECTOR = '.wd-article li.task-list-item input[type="checkbox"]';
13
+
14
+ /** The item's own text: its label (the box and the text up to any nested
15
+ * list - see rehype-task-lists.js), or the item without its nested lists. */
16
+ function itemText(box: HTMLInputElement): string {
17
+ const label = box.closest('label');
18
+ if (label) return label.textContent ?? '';
19
+ const li = box.closest('li');
20
+ if (!li) return '';
21
+ const copy = li.cloneNode(true) as HTMLElement;
22
+ copy.querySelectorAll('ul, ol').forEach((list) => list.remove());
23
+ return copy.textContent ?? '';
24
+ }
25
+
26
+ export function initTaskLists(root: ParentNode) {
27
+ const boxes = Array.from(root.querySelectorAll<HTMLInputElement>(BOX_SELECTOR));
28
+ if (!boxes.length) return;
29
+ const keys = taskKeys(boxes.map(itemText));
30
+ const storageKey = taskStorageKey(location.pathname);
31
+
32
+ let saved = readTaskStates(null);
33
+ try {
34
+ saved = readTaskStates(localStorage.getItem(storageKey));
35
+ } catch {
36
+ // localStorage unavailable - the page's own states stand
37
+ }
38
+
39
+ const save = () => {
40
+ const states = taskStatesToStore(
41
+ boxes.map((box, i) => ({ key: keys[i], checked: box.checked, defaultChecked: box.defaultChecked })),
42
+ );
43
+ try {
44
+ if (states) localStorage.setItem(storageKey, JSON.stringify(states));
45
+ else localStorage.removeItem(storageKey);
46
+ } catch {
47
+ // localStorage unavailable - the tick still shows, it just isn't kept
48
+ }
49
+ };
50
+
51
+ boxes.forEach((box, i) => {
52
+ // Set either way, not only when something was saved: a browser that
53
+ // restores form state on reload (Firefox) could otherwise show a tick
54
+ // that isn't stored anywhere.
55
+ box.checked = hasTaskState(saved, keys[i]) ? saved[keys[i]] : box.defaultChecked;
56
+ if (box.dataset.wdInit) return;
57
+ box.dataset.wdInit = 'true';
58
+ box.addEventListener('change', save);
59
+ });
60
+ }