@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.
- package/astro.config.mjs +14 -2
- package/package.json +1 -1
- package/packages/components/src/lib/inline-markdown.js +34 -8
- package/src/layout/BaseLayout.astro +6 -0
- package/src/layout/styles/base.css +102 -0
- package/src/lib/config-schema.js +19 -1
- package/src/lib/config-schema.ts +32 -1
- package/src/lib/link-check.js +10 -2
- package/src/lib/readme-convert.js +127 -15
- package/src/lib/readme-fetch.js +162 -10
- package/src/lib/readme-html.js +62 -0
- package/src/lib/readme-mdx.js +165 -28
- package/src/lib/readme-recipes.js +1 -1
- package/src/lib/rehype-task-lists.js +62 -0
- package/src/lib/tag-index.ts +203 -0
- package/src/lib/tags.js +103 -0
- package/src/lib/task-lists.js +78 -0
- package/src/pages/[...slug].astro +40 -2
- package/src/pages/tags/[...tag].astro +189 -0
- package/src/scripts/task-lists.ts +60 -0
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
// Task lists - `- [ ] item` / `- [x] item` - as checklists a reader can tick.
|
|
2
|
+
//
|
|
3
|
+
// GFM turns each such item into `<li class="task-list-item">` holding a
|
|
4
|
+
// disabled `<input type="checkbox">` and the item's text. This plugin:
|
|
5
|
+
//
|
|
6
|
+
// - drops `disabled`, so the box can be ticked;
|
|
7
|
+
// - wraps the box and the item's own text (up to its first block - a nested
|
|
8
|
+
// list, a second paragraph) in `<label class="wd-task">`, so clicking the
|
|
9
|
+
// text ticks it too (the box alone is a small target on a phone) and the
|
|
10
|
+
// box gets the text as its accessible name. In a loose list (blank lines
|
|
11
|
+
// between items) the box is inside the item's first <p>, and the label
|
|
12
|
+
// goes inside that <p>.
|
|
13
|
+
//
|
|
14
|
+
// What a reader ticks is remembered per page in their browser - that's
|
|
15
|
+
// src/scripts/task-lists.ts, which finds these boxes by
|
|
16
|
+
// `li.task-list-item input[type="checkbox"]`. The bullet is hidden in base.css.
|
|
17
|
+
//
|
|
18
|
+
// Only Markdown task items are touched: a list written as HTML/JSX in an .mdx
|
|
19
|
+
// page (`<li className="task-list-item">`) is the author's own markup.
|
|
20
|
+
|
|
21
|
+
const BLOCK_TAGS = new Set([
|
|
22
|
+
'ul', 'ol', 'p', 'div', 'pre', 'table', 'blockquote', 'figure', 'hr',
|
|
23
|
+
'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'details', 'dl', 'section',
|
|
24
|
+
]);
|
|
25
|
+
|
|
26
|
+
const isElement = (node, tag) => node?.type === 'element' && (!tag || node.tagName === tag);
|
|
27
|
+
|
|
28
|
+
const isTaskItem = (node) => isElement(node, 'li') && [].concat(node.properties?.className ?? []).includes('task-list-item');
|
|
29
|
+
|
|
30
|
+
const isCheckbox = (node) => isElement(node, 'input') && node.properties?.type === 'checkbox';
|
|
31
|
+
|
|
32
|
+
const isBlock = (node) => (isElement(node) && BLOCK_TAGS.has(node.tagName)) || node.type === 'mdxJsxFlowElement';
|
|
33
|
+
|
|
34
|
+
const isBlank = (node) => node.type === 'text' && !node.value.trim();
|
|
35
|
+
|
|
36
|
+
function makeTickable(li) {
|
|
37
|
+
// The box is the item's first thing - or, in a loose list, its first <p>'s.
|
|
38
|
+
const first = li.children.find((child) => !isBlank(child));
|
|
39
|
+
const container = isElement(first, 'p') ? first : li;
|
|
40
|
+
const start = container.children.findIndex((child) => !isBlank(child));
|
|
41
|
+
if (start === -1 || !isCheckbox(container.children[start])) return;
|
|
42
|
+
|
|
43
|
+
let end = start + 1;
|
|
44
|
+
while (end < container.children.length && !isBlock(container.children[end])) end++;
|
|
45
|
+
// The line break before a nested list stays outside the label.
|
|
46
|
+
while (end > start + 1 && isBlank(container.children[end - 1])) end--;
|
|
47
|
+
|
|
48
|
+
const box = container.children[start];
|
|
49
|
+
delete box.properties.disabled;
|
|
50
|
+
const label = { type: 'element', tagName: 'label', properties: { className: ['wd-task'] }, children: container.children.slice(start, end) };
|
|
51
|
+
container.children.splice(start, end - start, label);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function rehypeTaskLists() {
|
|
55
|
+
return (tree) => {
|
|
56
|
+
const visit = (node) => {
|
|
57
|
+
if (isTaskItem(node)) makeTickable(node);
|
|
58
|
+
if (Array.isArray(node.children)) node.children.forEach(visit);
|
|
59
|
+
};
|
|
60
|
+
visit(tree);
|
|
61
|
+
};
|
|
62
|
+
}
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
// The site's tags (frontmatter `tags` - see lib/tags.js): which pages each
|
|
2
|
+
// tag page lists, which tag pages are made, and the topbar they show. Used by
|
|
3
|
+
// src/pages/tags/[...tag].astro (the tag pages) and src/pages/[...slug].astro
|
|
4
|
+
// (the tags under a page, and the folder redirects, which keep off the tag
|
|
5
|
+
// pages' addresses). Once per build (oncePerBuild()).
|
|
6
|
+
//
|
|
7
|
+
// A tag page is public and lists only what anyone can find in the
|
|
8
|
+
// navigation's public part - a page is left out when it's:
|
|
9
|
+
// - behind sign-in (`access` - lib/protected-sections.js);
|
|
10
|
+
// - only in hidden sections (lib/hidden-sections.js) - "nothing lists one";
|
|
11
|
+
// - `hidden` (out of the sidebar), a `url` page (a link to another site), or
|
|
12
|
+
// a Scalar group's operation page (lib/scalar-reference.js).
|
|
13
|
+
// Its tags still show on the page itself, linked when the tag has a page
|
|
14
|
+
// (other pages have the tag), plain when it hasn't.
|
|
15
|
+
//
|
|
16
|
+
// A page that isn't in the navigation at all is listed: it's a page of the
|
|
17
|
+
// site like any other, just without a sidebar. A draft is only there when
|
|
18
|
+
// drafts are built (`writedocs dev`, `build --drafts`) - lib/drafts.js.
|
|
19
|
+
import { getCollection, type CollectionEntry } from "astro:content";
|
|
20
|
+
import fs from "node:fs";
|
|
21
|
+
import path from "node:path";
|
|
22
|
+
import {
|
|
23
|
+
loadDocsConfig,
|
|
24
|
+
resolveSections,
|
|
25
|
+
resolveLandings,
|
|
26
|
+
resolveGlobalDropdowns,
|
|
27
|
+
buildSelectors,
|
|
28
|
+
buildGlobalDropdowns,
|
|
29
|
+
flattenNav,
|
|
30
|
+
fileIdForEntry,
|
|
31
|
+
normalizeEntryId,
|
|
32
|
+
findAllPages,
|
|
33
|
+
type Selector,
|
|
34
|
+
type GlobalDropdownView,
|
|
35
|
+
} from "./config";
|
|
36
|
+
import { writedocsTempDir } from "./writedocs-temp-dir.js";
|
|
37
|
+
import { protectedPages } from "./protected-sections.js";
|
|
38
|
+
import { spaceOf } from "./hidden-sections.js";
|
|
39
|
+
import { itemLabel } from "./item-pages.js";
|
|
40
|
+
import { readScalarReferences, scalarOperationTargets } from "./scalar-reference.js";
|
|
41
|
+
import { oncePerBuild } from "./build-memo.js";
|
|
42
|
+
import { pageTags, collectTags, tagRoutes, TAGS_PATH } from "./tags.js";
|
|
43
|
+
|
|
44
|
+
type DocsEntry = CollectionEntry<"pages"> | CollectionEntry<"generatedDocs">;
|
|
45
|
+
|
|
46
|
+
export interface TaggedPage {
|
|
47
|
+
title: string;
|
|
48
|
+
description?: string;
|
|
49
|
+
href: string;
|
|
50
|
+
// The page's tab/version/language/... names, when the site has more than
|
|
51
|
+
// one section - so two pages with the same title (a page in two versions)
|
|
52
|
+
// can be told apart. Empty otherwise.
|
|
53
|
+
where: string[];
|
|
54
|
+
tags: { label: string; slug: string }[];
|
|
55
|
+
}
|
|
56
|
+
export interface Tag {
|
|
57
|
+
slug: string;
|
|
58
|
+
label: string;
|
|
59
|
+
pages: TaggedPage[];
|
|
60
|
+
}
|
|
61
|
+
export interface TagIndex {
|
|
62
|
+
// Every tag of a listed page, in name order.
|
|
63
|
+
tags: Tag[];
|
|
64
|
+
// The tags that get a page (`/tags/<slug>/`), and whether `/tags/` is made.
|
|
65
|
+
routed: Tag[];
|
|
66
|
+
index: boolean;
|
|
67
|
+
// Addresses a tag page would have had, kept by a page or redirect there.
|
|
68
|
+
skipped: string[];
|
|
69
|
+
// The slugs of `routed` - a page's tag links only to one of these.
|
|
70
|
+
linked: Set<string>;
|
|
71
|
+
// Every tag page's address, without slashes - the folder redirects keep
|
|
72
|
+
// off them ([...slug].astro).
|
|
73
|
+
addresses: Set<string>;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async function docsEntries(contentDir: string): Promise<DocsEntry[]> {
|
|
77
|
+
const hasGeneratedDocs = fs.existsSync(path.join(writedocsTempDir(contentDir), "generated-docs"));
|
|
78
|
+
const hasPages = findAllPages(contentDir).length > 0;
|
|
79
|
+
const [pages, generated] = await Promise.all([
|
|
80
|
+
hasPages ? getCollection("pages") : Promise.resolve([]),
|
|
81
|
+
hasGeneratedDocs ? getCollection("generatedDocs") : Promise.resolve([]),
|
|
82
|
+
]);
|
|
83
|
+
return [...pages, ...generated];
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const hrefOf = (entry: DocsEntry) => {
|
|
87
|
+
const id = normalizeEntryId(entry.id);
|
|
88
|
+
return id === "index" ? "/" : `/${id}/`;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
async function computeTagIndex(): Promise<TagIndex> {
|
|
92
|
+
const contentDir = process.env.WRITEDOCS_CONTENT_DIR || process.cwd();
|
|
93
|
+
const packageRoot = process.env.WRITEDOCS_PACKAGE_ROOT || process.cwd();
|
|
94
|
+
const config = loadDocsConfig(contentDir);
|
|
95
|
+
const entries = await docsEntries(contentDir);
|
|
96
|
+
const sections = resolveSections(config.navigation);
|
|
97
|
+
const landings = resolveLandings(config.navigation);
|
|
98
|
+
const protectedRules = protectedPages(config.navigation);
|
|
99
|
+
const scalarTargets = scalarOperationTargets(readScalarReferences(contentDir));
|
|
100
|
+
|
|
101
|
+
// Where each page is in the navigation: the paths of the sections whose
|
|
102
|
+
// sidebar lists it and of the items it's the own page of.
|
|
103
|
+
const pathsOf = new Map<string, (typeof sections)[number]["path"][]>();
|
|
104
|
+
const addPath = (fileId: string, p: (typeof sections)[number]["path"]) => {
|
|
105
|
+
if (!pathsOf.has(fileId)) pathsOf.set(fileId, []);
|
|
106
|
+
pathsOf.get(fileId)!.push(p);
|
|
107
|
+
};
|
|
108
|
+
for (const section of sections) for (const { slug } of flattenNav(section.pages)) addPath(slug, section.path);
|
|
109
|
+
for (const landing of landings) addPath(landing.slug, landing.path);
|
|
110
|
+
const manySections = sections.length > 1;
|
|
111
|
+
|
|
112
|
+
const pages: TaggedPage[] = [];
|
|
113
|
+
for (const entry of entries) {
|
|
114
|
+
const tags = pageTags((entry.data as { tags?: unknown }).tags);
|
|
115
|
+
if (tags.length === 0) continue;
|
|
116
|
+
const fileId = fileIdForEntry(contentDir, packageRoot, entry);
|
|
117
|
+
const data = entry.data;
|
|
118
|
+
if (data.hidden || data.url || scalarTargets.has(fileId) || protectedRules.has(fileId)) continue;
|
|
119
|
+
const paths = pathsOf.get(fileId);
|
|
120
|
+
// In the navigation: only where a public (not hidden) section has it.
|
|
121
|
+
const publicPath = paths ? paths.find((p) => spaceOf(p) === null) : [];
|
|
122
|
+
if (!publicPath) continue;
|
|
123
|
+
pages.push({
|
|
124
|
+
title: data.title,
|
|
125
|
+
description: data.description,
|
|
126
|
+
href: hrefOf(entry),
|
|
127
|
+
where: manySections ? publicPath.map((segment) => itemLabel(segment.items[segment.index])) : [],
|
|
128
|
+
tags,
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const tags = collectTags(pages) as Tag[];
|
|
133
|
+
// What's already at an address: every page (listed or not) and every
|
|
134
|
+
// redirect of writedocs.json.
|
|
135
|
+
const taken = new Set<string>([
|
|
136
|
+
...entries.map((entry) => normalizeEntryId(entry.id)),
|
|
137
|
+
...(config.redirects ?? []).map((r) => r.source.replace(/^\/+|\/+$/g, "")),
|
|
138
|
+
]);
|
|
139
|
+
const routes = tagRoutes(tags, taken);
|
|
140
|
+
return {
|
|
141
|
+
tags,
|
|
142
|
+
routed: routes.tags,
|
|
143
|
+
index: routes.index,
|
|
144
|
+
skipped: routes.skipped,
|
|
145
|
+
linked: new Set(routes.tags.map((tag: Tag) => tag.slug)),
|
|
146
|
+
addresses: new Set([
|
|
147
|
+
...(routes.index ? [TAGS_PATH] : []),
|
|
148
|
+
...routes.tags.map((tag: Tag) => `${TAGS_PATH}/${tag.slug}`),
|
|
149
|
+
]),
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** The site's tags - once per build. */
|
|
154
|
+
export function tagIndex(): Promise<TagIndex> {
|
|
155
|
+
return oncePerBuild("tags:index", computeTagIndex);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const SWITCHER_PLACEHOLDER: Record<Selector["kind"], string> = {
|
|
159
|
+
tab: "Tabs",
|
|
160
|
+
version: "Version",
|
|
161
|
+
language: "Language",
|
|
162
|
+
dropdown: "Menu",
|
|
163
|
+
product: "Products",
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
/** The topbar of a tag page: the navigation's first level (the tabs, say)
|
|
167
|
+
* with none chosen - a tag page belongs to none of them - and the global
|
|
168
|
+
* dropdowns. */
|
|
169
|
+
export async function tagPageChrome(): Promise<{ selectors: Selector[]; globalDropdowns: GlobalDropdownView[] }> {
|
|
170
|
+
const contentDir = process.env.WRITEDOCS_CONTENT_DIR || process.cwd();
|
|
171
|
+
const packageRoot = process.env.WRITEDOCS_PACKAGE_ROOT || process.cwd();
|
|
172
|
+
const config = loadDocsConfig(contentDir);
|
|
173
|
+
const entries = await docsEntries(contentDir);
|
|
174
|
+
const entryByFileId = new Map(entries.map((e) => [fileIdForEntry(contentDir, packageRoot, e), e]));
|
|
175
|
+
const hrefForSlug = (slug: string) => {
|
|
176
|
+
const entry = entryByFileId.get(slug);
|
|
177
|
+
const id = normalizeEntryId(entry?.id ?? slug);
|
|
178
|
+
return id === "index" ? "/" : `/${id}/`;
|
|
179
|
+
};
|
|
180
|
+
const titleForSlug = (slug: string) => {
|
|
181
|
+
const data = entryByFileId.get(slug)?.data;
|
|
182
|
+
return data?.sidebarTitle ?? data?.title ?? slug;
|
|
183
|
+
};
|
|
184
|
+
const first = resolveSections(config.navigation)[0];
|
|
185
|
+
const selectors = first?.path.length
|
|
186
|
+
? buildSelectors(first.path.slice(0, 1), hrefForSlug).map((selector) => ({
|
|
187
|
+
...selector,
|
|
188
|
+
hidden: false,
|
|
189
|
+
// What a switcher (versions, languages, products) shows with nothing
|
|
190
|
+
// chosen - tabs show none.
|
|
191
|
+
placeholder: { label: SWITCHER_PLACEHOLDER[selector.kind] },
|
|
192
|
+
options: selector.options.map((option) => ({ ...option, active: false })),
|
|
193
|
+
}))
|
|
194
|
+
: [];
|
|
195
|
+
const globalDropdowns = buildGlobalDropdowns(
|
|
196
|
+
resolveGlobalDropdowns(config.navigation),
|
|
197
|
+
"",
|
|
198
|
+
titleForSlug,
|
|
199
|
+
hrefForSlug,
|
|
200
|
+
(slug) => entryByFileId.get(slug)?.data,
|
|
201
|
+
);
|
|
202
|
+
return { selectors, globalDropdowns };
|
|
203
|
+
}
|
package/src/lib/tags.js
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
// Page tags - `tags: ["billing", "webhooks"]` in a page's frontmatter, as in
|
|
2
|
+
// Docusaurus. A page shows its tags at the bottom, each linking to the tag's
|
|
3
|
+
// own page (`/tags/<tag>/`), which lists every page with that tag; `/tags/`
|
|
4
|
+
// lists every tag.
|
|
5
|
+
//
|
|
6
|
+
// This file is the plain part - what a tag is called and where it lives, and
|
|
7
|
+
// the lists the tag pages show - so scripts/tags.test.js can check it. Which
|
|
8
|
+
// pages a tag page may list is decided from the navigation in
|
|
9
|
+
// lib/tag-index.ts; the pages themselves are src/pages/tags/[...tag].astro,
|
|
10
|
+
// and the tags under a page are in src/pages/[...slug].astro.
|
|
11
|
+
//
|
|
12
|
+
// - A tag is a string, or Docusaurus' `{ label, permalink }` object. Its
|
|
13
|
+
// address comes from its name - lower case, accents dropped, anything
|
|
14
|
+
// that isn't a letter or digit a single "-" ("Pão de queijo" ->
|
|
15
|
+
// "pao-de-queijo") - or from `permalink` when it has one.
|
|
16
|
+
// - Two spellings with the same address ("Queijo", "queijo") are one tag,
|
|
17
|
+
// shown with the spelling met first.
|
|
18
|
+
// - A tag page is only made where nothing else is: a page, a redirect, or
|
|
19
|
+
// another tag page already at that address keeps it, and the tag is left
|
|
20
|
+
// without a page (its name is still shown on its pages, not linked).
|
|
21
|
+
|
|
22
|
+
/** The folder the tag pages are in: `/tags/`, `/tags/<tag>/`. */
|
|
23
|
+
export const TAGS_PATH = 'tags';
|
|
24
|
+
|
|
25
|
+
/** A tag's address from its name or permalink - "" when nothing is left. */
|
|
26
|
+
export function tagSlug(text) {
|
|
27
|
+
return String(text ?? '')
|
|
28
|
+
.normalize('NFKD')
|
|
29
|
+
.replace(/\p{M}+/gu, '')
|
|
30
|
+
.toLowerCase()
|
|
31
|
+
.replace(/[^\p{L}\p{N}]+/gu, '-')
|
|
32
|
+
.replace(/^-+|-+$/g, '');
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** A page's `tags` frontmatter as `{ label, slug }`s, in its order: strings
|
|
36
|
+
* and `{ label, permalink }` objects; empty ones and repeats (by address)
|
|
37
|
+
* left out. Anything that isn't a list is no tags. */
|
|
38
|
+
export function pageTags(value) {
|
|
39
|
+
if (!Array.isArray(value)) return [];
|
|
40
|
+
const seen = new Set();
|
|
41
|
+
const tags = [];
|
|
42
|
+
for (const item of value) {
|
|
43
|
+
let label;
|
|
44
|
+
let slug;
|
|
45
|
+
if (typeof item === 'string') {
|
|
46
|
+
label = item.trim();
|
|
47
|
+
slug = tagSlug(label);
|
|
48
|
+
} else if (item && typeof item === 'object' && typeof item.label === 'string') {
|
|
49
|
+
label = item.label.trim();
|
|
50
|
+
slug = tagSlug(typeof item.permalink === 'string' && item.permalink.trim() ? item.permalink : label);
|
|
51
|
+
} else {
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
if (!label || !slug || seen.has(slug)) continue;
|
|
55
|
+
seen.add(slug);
|
|
56
|
+
tags.push({ label, slug });
|
|
57
|
+
}
|
|
58
|
+
return tags;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** The address of a tag's page, or of the list of tags (no slug) - without
|
|
62
|
+
* slashes, the way routes are written. */
|
|
63
|
+
export const tagAddress = (slug) => (slug ? `${TAGS_PATH}/${slug}` : TAGS_PATH);
|
|
64
|
+
|
|
65
|
+
/** The link to a tag's page, or to the list of tags (no slug). */
|
|
66
|
+
export const tagHref = (slug) => `/${tagAddress(slug)}/`;
|
|
67
|
+
|
|
68
|
+
const collator = new Intl.Collator(undefined, { sensitivity: 'base', numeric: true });
|
|
69
|
+
|
|
70
|
+
/** Every tag of the listed pages, with its pages: `[{ slug, label, pages }]`,
|
|
71
|
+
* tags in name order, each tag's pages in title order. `pages` are
|
|
72
|
+
* `{ tags, ...anything }` - `tags` from pageTags(); the rest (title, href,
|
|
73
|
+
* description, where) is passed through for the tag page to show. */
|
|
74
|
+
export function collectTags(pages) {
|
|
75
|
+
const bySlug = new Map();
|
|
76
|
+
for (const page of pages) {
|
|
77
|
+
for (const { label, slug } of page.tags ?? []) {
|
|
78
|
+
if (!bySlug.has(slug)) bySlug.set(slug, { slug, label, pages: [] });
|
|
79
|
+
bySlug.get(slug).pages.push(page);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
const tags = [...bySlug.values()];
|
|
83
|
+
for (const tag of tags) tag.pages.sort((a, b) => collator.compare(a.title ?? '', b.title ?? ''));
|
|
84
|
+
return tags.sort((a, b) => collator.compare(a.label, b.label) || collator.compare(a.slug, b.slug));
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Which tag pages are made, given the addresses already taken (pages,
|
|
88
|
+
* redirects - without slashes, "index" for "/"): `{ index, tags, skipped }`
|
|
89
|
+
* - `index` is whether `/tags/` is made, `tags` the tags that get a page,
|
|
90
|
+
* `skipped` the addresses left to what's there. No tags, no pages at all. */
|
|
91
|
+
export function tagRoutes(tags, taken) {
|
|
92
|
+
if (tags.length === 0) return { index: false, tags: [], skipped: [] };
|
|
93
|
+
const skipped = [];
|
|
94
|
+
const index = !taken.has(TAGS_PATH);
|
|
95
|
+
if (!index) skipped.push(TAGS_PATH);
|
|
96
|
+
const made = [];
|
|
97
|
+
for (const tag of tags) {
|
|
98
|
+
const address = tagAddress(tag.slug);
|
|
99
|
+
if (taken.has(address)) skipped.push(address);
|
|
100
|
+
else made.push(tag);
|
|
101
|
+
}
|
|
102
|
+
return { index, tags: made, skipped };
|
|
103
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
// What src/scripts/task-lists.ts remembers about a page's task lists, and
|
|
2
|
+
// under which name. Plain JavaScript, so scripts/task-lists.test.js can check
|
|
3
|
+
// it. (The boxes themselves are made tickable at build time -
|
|
4
|
+
// lib/rehype-task-lists.js.)
|
|
5
|
+
//
|
|
6
|
+
// One localStorage entry per page: `wd-tasks:<path>`, holding
|
|
7
|
+
// `{ "<item key>": true|false }`.
|
|
8
|
+
//
|
|
9
|
+
// - An item's key is its text, whitespace collapsed - not its position - so
|
|
10
|
+
// adding, removing or moving another item doesn't move what a reader
|
|
11
|
+
// ticked onto a different line. Two items with the same text on one page
|
|
12
|
+
// are told apart by order: the second is "<text>\n2" (a newline can't be
|
|
13
|
+
// in a collapsed text, so it can't clash with another item's own text).
|
|
14
|
+
// - Only the items whose state differs from the page's own (`[ ]` / `[x]`)
|
|
15
|
+
// are stored. A page the reader has put back as written has no entry at
|
|
16
|
+
// all, and an item whose text was edited or removed is dropped from the
|
|
17
|
+
// entry on the next tick.
|
|
18
|
+
|
|
19
|
+
const PREFIX = 'wd-tasks:';
|
|
20
|
+
|
|
21
|
+
/** The text an item is known by: whitespace collapsed and trimmed. */
|
|
22
|
+
export const taskText = (text) => String(text ?? '').replace(/\s+/g, ' ').trim();
|
|
23
|
+
|
|
24
|
+
/** One key per item, in page order, from the items' texts. */
|
|
25
|
+
export function taskKeys(texts) {
|
|
26
|
+
const seen = new Map();
|
|
27
|
+
return texts.map((text) => {
|
|
28
|
+
const key = taskText(text);
|
|
29
|
+
const count = (seen.get(key) ?? 0) + 1;
|
|
30
|
+
seen.set(key, count);
|
|
31
|
+
return count === 1 ? key : `${key}\n${count}`;
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** The localStorage name for the page at `pathname`. `/a/b`, `/a/b/` and
|
|
36
|
+
* `/a/b/index.html` are the same page; so are `/a/b.html` and `/a/b`. */
|
|
37
|
+
export function taskStorageKey(pathname) {
|
|
38
|
+
let path = String(pathname || '/');
|
|
39
|
+
path = path.replace(/\/index\.html$/, '/').replace(/\.html$/, '');
|
|
40
|
+
if (path.length > 1) path = path.replace(/\/+$/, '');
|
|
41
|
+
return PREFIX + (path || '/');
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** The saved states from a stored value - an object of key -> boolean, with
|
|
45
|
+
* no prototype (so an item called "constructor" is just a key). Anything
|
|
46
|
+
* unreadable - missing, not JSON, not an object - is no saved states. */
|
|
47
|
+
export function readTaskStates(raw) {
|
|
48
|
+
const states = Object.create(null);
|
|
49
|
+
if (typeof raw !== 'string') return states;
|
|
50
|
+
let parsed;
|
|
51
|
+
try {
|
|
52
|
+
parsed = JSON.parse(raw);
|
|
53
|
+
} catch {
|
|
54
|
+
return states;
|
|
55
|
+
}
|
|
56
|
+
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) return states;
|
|
57
|
+
for (const [key, value] of Object.entries(parsed)) {
|
|
58
|
+
if (typeof value === 'boolean') states[key] = value;
|
|
59
|
+
}
|
|
60
|
+
return states;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Whether `key` has a saved state. */
|
|
64
|
+
export const hasTaskState = (states, key) => Object.prototype.hasOwnProperty.call(states, key);
|
|
65
|
+
|
|
66
|
+
/** What to store for a page's items (`{ key, checked, defaultChecked }`, in
|
|
67
|
+
* page order): the ones that differ from the page as written, or null when
|
|
68
|
+
* none do - then the entry is removed. */
|
|
69
|
+
export function taskStatesToStore(items) {
|
|
70
|
+
const states = {};
|
|
71
|
+
let any = false;
|
|
72
|
+
for (const { key, checked, defaultChecked } of items) {
|
|
73
|
+
if (Boolean(checked) === Boolean(defaultChecked)) continue;
|
|
74
|
+
states[key] = Boolean(checked);
|
|
75
|
+
any = true;
|
|
76
|
+
}
|
|
77
|
+
return any ? states : null;
|
|
78
|
+
}
|
|
@@ -95,6 +95,9 @@ import { folderRedirects } from "../lib/folder-redirects.js";
|
|
|
95
95
|
import { readMovedApiPages, protectedMovedApiPages } from "../lib/moved-api-pages.js";
|
|
96
96
|
import { DRAFT_BANNER_HTML } from "../lib/drafts.js";
|
|
97
97
|
import { oncePerBuild } from "../lib/build-memo.js";
|
|
98
|
+
import { tagIndex } from "../lib/tag-index";
|
|
99
|
+
import { pageTags, tagHref } from "../lib/tags.js";
|
|
100
|
+
import { inlineMarkdown } from "../../packages/components/src/lib/inline-markdown.js";
|
|
98
101
|
// writedocs.json's `basePath` - the addresses below stay site addresses
|
|
99
102
|
// ("/guides/a/"); the base goes in where one is written (the components package's lib/site-base.ts).
|
|
100
103
|
import { siteHref } from "../../packages/components/src/lib/site-base";
|
|
@@ -362,7 +365,11 @@ export async function getStaticPaths() {
|
|
|
362
365
|
.sort((a, b) => orderOf(a) - orderOf(b))
|
|
363
366
|
.map(slugOf),
|
|
364
367
|
];
|
|
368
|
+
// The tag pages (src/pages/tags/[...tag].astro, lib/tag-index.ts) are
|
|
369
|
+
// taken too: a folder redirect at /tags/ would clash with the list of tags.
|
|
370
|
+
const tagAddresses = (await tagIndex()).addresses;
|
|
365
371
|
const takenAddresses = new Set([
|
|
372
|
+
...tagAddresses,
|
|
366
373
|
...[...pageRoutes, ...landingRoutes, ...hiddenRoutes].map(slugOf),
|
|
367
374
|
...(config.redirects ?? []).map((r) => r.source.replace(/^\/+|\/+$/g, "")),
|
|
368
375
|
]);
|
|
@@ -457,6 +464,13 @@ const activeSection = landing
|
|
|
457
464
|
: (sections[sectionIndex] ?? { pages: [], path: [] });
|
|
458
465
|
|
|
459
466
|
const { Content, headings } = await render(entry);
|
|
467
|
+
// The page's <h1>: its title with its inline Markdown (`code`, **bold**,
|
|
468
|
+
// *italic*), escaped otherwise. `title` itself is plain text - the schema
|
|
469
|
+
// keeps the Markdown in `titleMarkdown` (withPlainTitles(), lib/config-schema.ts).
|
|
470
|
+
const titleHtml = inlineMarkdown(entry.data.titleMarkdown ?? entry.data.title);
|
|
471
|
+
// The page's tags, shown under it, and which of them have a page.
|
|
472
|
+
const pageTagList = pageTags(entry.data.tags);
|
|
473
|
+
const pageTagIndex = await tagIndex();
|
|
460
474
|
const tocHeadings = headings.filter((h) => h.depth === 2 || h.depth === 3);
|
|
461
475
|
|
|
462
476
|
// The lookup tables below are the same for every page, so a build makes
|
|
@@ -821,13 +835,14 @@ const components = {
|
|
|
821
835
|
{
|
|
822
836
|
// Frontmatter `deprecated` puts a label beside the <h1>, not in
|
|
823
837
|
// it - the <h1>'s text is the search index's page title.
|
|
838
|
+
// The title's inline Markdown shows here only (titleHtml below).
|
|
824
839
|
entry.data.deprecated ? (
|
|
825
840
|
<div class="wd-article-title">
|
|
826
|
-
<h1 data-pagefind-meta="title"
|
|
841
|
+
<h1 data-pagefind-meta="title" set:html={titleHtml} />
|
|
827
842
|
<span class="wd-deprecated-badge">Deprecated</span>
|
|
828
843
|
</div>
|
|
829
844
|
) : (
|
|
830
|
-
<h1 data-pagefind-meta="title"
|
|
845
|
+
<h1 data-pagefind-meta="title" set:html={titleHtml} />
|
|
831
846
|
)
|
|
832
847
|
}
|
|
833
848
|
{showCopyPageMenu && (
|
|
@@ -853,6 +868,29 @@ const components = {
|
|
|
853
868
|
>
|
|
854
869
|
{pageSectionTrail && <span data-pagefind-meta="section">{pageSectionTrail}</span>}
|
|
855
870
|
</div>
|
|
871
|
+
{
|
|
872
|
+
// Frontmatter `tags` (lib/tags.js): each links to its tag's page
|
|
873
|
+
// when it has one (lib/tag-index.ts). Searchable - a search for a
|
|
874
|
+
// tag finds its pages - but not the "Tags" label.
|
|
875
|
+
pageTagList.length > 0 && (
|
|
876
|
+
<div class="wd-page-tags" data-markdown-ignore>
|
|
877
|
+
<span class="wd-page-tags-label" data-pagefind-ignore>Tags</span>
|
|
878
|
+
<ul class="wd-page-tags-list">
|
|
879
|
+
{pageTagList.map((tag) => (
|
|
880
|
+
<li>
|
|
881
|
+
{pageTagIndex.linked.has(tag.slug) ? (
|
|
882
|
+
<a class="wd-tag" href={siteHref(tagHref(tag.slug))}>
|
|
883
|
+
{tag.label}
|
|
884
|
+
</a>
|
|
885
|
+
) : (
|
|
886
|
+
<span class="wd-tag">{tag.label}</span>
|
|
887
|
+
)}
|
|
888
|
+
</li>
|
|
889
|
+
))}
|
|
890
|
+
</ul>
|
|
891
|
+
</div>
|
|
892
|
+
)
|
|
893
|
+
}
|
|
856
894
|
{!entry.data.hideFooterPagination && (
|
|
857
895
|
<nav class="wd-prevnext" data-pagefind-ignore data-markdown-ignore>
|
|
858
896
|
{prev && (
|