@connextar/house 0.23.1 → 0.23.3
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/README.md +22 -3
- package/dist/analytics/browser.d.ts +1 -0
- package/dist/analytics/browser.d.ts.map +1 -1
- package/dist/analytics/browser.js +20 -3
- package/dist/analytics/browser.js.map +1 -1
- package/dist/analytics/config.d.ts +5 -0
- package/dist/analytics/config.d.ts.map +1 -1
- package/dist/analytics/config.js +4 -1
- package/dist/analytics/config.js.map +1 -1
- package/dist/analytics/react.d.ts.map +1 -1
- package/dist/analytics/react.js +4 -2
- package/dist/analytics/react.js.map +1 -1
- package/dist/analytics/redact.d.ts.map +1 -1
- package/dist/analytics/redact.js +6 -1
- package/dist/analytics/redact.js.map +1 -1
- package/package.json +5 -2
- package/src/account/data-contract.ts +112 -0
- package/src/account/index.ts +11 -0
- package/src/admin/access.ts +267 -0
- package/src/admin/audit.ts +238 -0
- package/src/admin/bootstrap.ts +220 -0
- package/src/admin/client-index.ts +19 -0
- package/src/admin/client.tsx +187 -0
- package/src/admin/index.ts +91 -0
- package/src/admin/proof.ts +155 -0
- package/src/admin/second-factor-index.ts +16 -0
- package/src/admin/second-factor.ts +403 -0
- package/src/admin/testing-index.ts +13 -0
- package/src/admin/testing.ts +299 -0
- package/src/admin/view-as.ts +511 -0
- package/src/ai/config-index.ts +40 -0
- package/src/ai/config.ts +176 -0
- package/src/ai/images.ts +18 -0
- package/src/ai/index.ts +21 -0
- package/src/ai/pricing.ts +107 -0
- package/src/ai/provider.ts +403 -0
- package/src/analytics/browser.ts +400 -0
- package/src/analytics/client-index.ts +12 -0
- package/src/analytics/config.ts +89 -0
- package/src/analytics/index.ts +17 -0
- package/src/analytics/ingest.ts +81 -0
- package/src/analytics/react-index.ts +16 -0
- package/src/analytics/react.tsx +241 -0
- package/src/analytics/redact.ts +146 -0
- package/src/analytics/server-index.ts +17 -0
- package/src/analytics/server.ts +277 -0
- package/src/auth/index.ts +30 -0
- package/src/auth/second-factor-index.ts +16 -0
- package/src/auth/second-factor-next.ts +136 -0
- package/src/auth/second-factor-routes.ts +40 -0
- package/src/auth/second-factor.ts +226 -0
- package/src/auth/step-up-client-index.ts +13 -0
- package/src/auth/step-up-client.ts +23 -0
- package/src/auth/step-up-index.ts +19 -0
- package/src/auth/step-up-routes.ts +99 -0
- package/src/auth/step-up.ts +432 -0
- package/src/auth/two-factor-policy.ts +112 -0
- package/src/changelog/changelog.tsx +367 -0
- package/src/changelog/index.ts +12 -0
- package/src/editor/block-handle.tsx +312 -0
- package/src/editor/blocks.ts +169 -0
- package/src/editor/bubble-toolbar.tsx +208 -0
- package/src/editor/collapsible.ts +159 -0
- package/src/editor/content-index.ts +1 -0
- package/src/editor/content.tsx +343 -0
- package/src/editor/editor.tsx +333 -0
- package/src/editor/emoji-index.ts +1 -0
- package/src/editor/emoji-picker.tsx +114 -0
- package/src/editor/emoji.ts +436 -0
- package/src/editor/index.ts +6 -0
- package/src/editor/sections.ts +83 -0
- package/src/editor/suggestion-popup.tsx +136 -0
- package/src/editor/suggestions.ts +309 -0
- package/src/editor/ui.tsx +171 -0
- package/src/errors/error-shell.tsx +72 -0
- package/src/errors/global-error-shell.tsx +116 -0
- package/src/errors/index.ts +2 -0
- package/src/grid/columns.ts +48 -0
- package/src/grid/compare.ts +35 -0
- package/src/grid/hidden-columns.ts +95 -0
- package/src/grid/index.ts +27 -0
- package/src/grid/use-grid.ts +282 -0
- package/src/grid/views.ts +70 -0
- package/src/guides/guide-article.tsx +92 -0
- package/src/guides/guide-figure-zoom.tsx +533 -0
- package/src/guides/guide-figure.tsx +68 -0
- package/src/guides/guide-toc.tsx +133 -0
- package/src/guides/guides-landing.tsx +57 -0
- package/src/guides/guides-search.tsx +160 -0
- package/src/guides/guides-shell.tsx +60 -0
- package/src/guides/guides-sidebar.tsx +93 -0
- package/src/guides/index.ts +28 -0
- package/src/guides/library.ts +165 -0
- package/src/health/health.ts +115 -0
- package/src/health/index.ts +11 -0
- package/src/internal/cx.ts +4 -0
- package/src/internal/next-link.ts +23 -0
- package/src/jobs/counts.ts +151 -0
- package/src/jobs/cron-auth.ts +60 -0
- package/src/jobs/health.ts +315 -0
- package/src/jobs/idle.ts +86 -0
- package/src/jobs/index.ts +120 -0
- package/src/jobs/job-health-list.tsx +316 -0
- package/src/jobs/next-index.ts +4 -0
- package/src/jobs/next.ts +65 -0
- package/src/jobs/prisma-index.ts +12 -0
- package/src/jobs/prisma.ts +170 -0
- package/src/jobs/prune.ts +79 -0
- package/src/jobs/react-index.ts +35 -0
- package/src/jobs/registry.ts +189 -0
- package/src/jobs/scheduler.ts +437 -0
- package/src/jobs/store.ts +212 -0
- package/src/jobs/windows.ts +157 -0
- package/src/maintenance/core.ts +270 -0
- package/src/maintenance/gate.ts +171 -0
- package/src/maintenance/index.ts +40 -0
- package/src/maintenance/page.ts +76 -0
- package/src/maintenance/proxy.ts +88 -0
- package/src/maintenance/react-index.ts +7 -0
- package/src/maintenance/react.tsx +190 -0
- package/src/maintenance/route.ts +82 -0
- package/src/maintenance/unavailable.ts +61 -0
- package/src/markdown/figure.tsx +132 -0
- package/src/markdown/figures.ts +237 -0
- package/src/markdown/guide.tsx +536 -0
- package/src/markdown/index.ts +53 -0
- package/src/markdown/table-view.tsx +74 -0
- package/src/markdown/table.ts +90 -0
- package/src/password/common.ts +58 -0
- package/src/password/estimate-index.ts +18 -0
- package/src/password/estimate.ts +715 -0
- package/src/password/index.ts +40 -0
- package/src/password/password-input.tsx +116 -0
- package/src/password/password-strength.tsx +157 -0
- package/src/password/reveal.ts +151 -0
- package/src/rate-limit/better-auth.ts +94 -0
- package/src/rate-limit/index.ts +32 -0
- package/src/rate-limit/keys.ts +110 -0
- package/src/rate-limit/limiter.ts +276 -0
- package/src/rate-limit/prisma-index.ts +12 -0
- package/src/rate-limit/prisma.ts +157 -0
- package/src/rate-limit/store.ts +47 -0
- package/src/release/changelog.ts +364 -0
- package/src/release/index.ts +32 -0
- package/src/release/version.ts +64 -0
- package/src/setup-guide/bits.tsx +420 -0
- package/src/setup-guide/card.tsx +358 -0
- package/src/setup-guide/class-names.ts +144 -0
- package/src/setup-guide/context.tsx +239 -0
- package/src/setup-guide/guide.ts +271 -0
- package/src/setup-guide/index.ts +76 -0
- package/src/setup-guide/launcher.tsx +207 -0
- package/src/setup-guide/strip.tsx +127 -0
- package/src/styles/house.css +384 -0
- package/src/trail/index.ts +12 -0
- package/src/trail/rules.ts +84 -0
- package/src/trail/trail.tsx +367 -0
- package/src/ux/errors.ts +32 -0
- package/src/ux/feedback.tsx +41 -0
- package/src/ux/index.ts +44 -0
- package/src/ux/navigation-index.ts +16 -0
- package/src/ux/navigation-progress.tsx +270 -0
- package/src/ux/optimistic.ts +415 -0
- package/src/ux/use-action.ts +155 -0
- package/src/ux/use-optimistic-list.ts +227 -0
- package/src/ux/use-optimistic-value.ts +136 -0
- package/src/web-search/index.ts +13 -0
- package/src/web-search/providers.ts +177 -0
- package/src/web-search/types.ts +98 -0
- package/src/whats-new/body.tsx +190 -0
- package/src/whats-new/index.ts +43 -0
- package/src/whats-new/seen.ts +40 -0
- package/src/whats-new/select.ts +230 -0
- package/src/whats-new/use-whats-new.ts +109 -0
- package/src/wizard/index.ts +61 -0
- package/src/wizard/invalid.ts +152 -0
- package/src/wizard/react-hook-form-index.ts +11 -0
- package/src/wizard/rhf-adapter.ts +67 -0
- package/src/wizard/rules.ts +137 -0
- package/src/wizard/wizard.tsx +442 -0
|
@@ -0,0 +1,536 @@
|
|
|
1
|
+
import { Fragment, type ReactNode } from "react";
|
|
2
|
+
|
|
3
|
+
import { Link } from "../internal/next-link.js";
|
|
4
|
+
import { GuideFigureView, type GuideFigureClassNames } from "./figure.js";
|
|
5
|
+
import { parseFigureLine, resolveGuideFigure, type GuideFigureBlock, type GuideFigures } from "./figures.js";
|
|
6
|
+
import { readTable, isTableStart } from "./table.js";
|
|
7
|
+
import { MarkdownTableView, type MarkdownTableClassNames } from "./table-view.js";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The help-centre guide renderer: Markdown files under `docs/guides/` drawn as a
|
|
11
|
+
* public page, with no Markdown library.
|
|
12
|
+
*
|
|
13
|
+
* Five apps each carried a copy of this, and the copies had drifted in the ways
|
|
14
|
+
* copies do. Two drew tables and three printed them as a paragraph of pipes; one
|
|
15
|
+
* kept a wrapped list line in its item and four broke it into a stray paragraph;
|
|
16
|
+
* one refused `javascript:` links and four passed them through; four numbered a
|
|
17
|
+
* repeated heading `-2` and one `-1`; and three let an `####` heading shift the
|
|
18
|
+
* numbering of the `##` headings after it, so a "Jump to" link could land on the
|
|
19
|
+
* wrong section. This is the union of the fixes.
|
|
20
|
+
*
|
|
21
|
+
* The syntax is the constrained subset guides use: `##`–`####` headings,
|
|
22
|
+
* paragraphs, blockquotes, ordered and unordered lists, tables, fenced code,
|
|
23
|
+
* horizontal rules, inline bold, italic, code and links, and pictures from the
|
|
24
|
+
* app's registry on a line of their own (`figures.ts`). Raw HTML is never
|
|
25
|
+
* interpreted, so a guide cannot put markup on a public page.
|
|
26
|
+
*
|
|
27
|
+
* Server-safe: no hooks, no client boundary.
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
export interface ParsedGuide {
|
|
31
|
+
/** The H1, with any trailing " — User Guide" removed. */
|
|
32
|
+
title: string;
|
|
33
|
+
/** The blockquote straight under the title — the page's lede — if there is one. */
|
|
34
|
+
lede?: string;
|
|
35
|
+
/** Everything after the title and lede. */
|
|
36
|
+
body: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface GuideHeading {
|
|
40
|
+
id: string;
|
|
41
|
+
text: string;
|
|
42
|
+
level: 2 | 3;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface HowToStep {
|
|
46
|
+
name: string;
|
|
47
|
+
text: string;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const lines = (markdown: string) => markdown.replace(/\r\n/g, "\n").split("\n");
|
|
51
|
+
const isFence = (line: string) => line.trim().startsWith("```");
|
|
52
|
+
const isRule = (line: string) => /^(-{3,}|\*{3,}|_{3,})$/.test(line.trim());
|
|
53
|
+
const isHeading = (line: string) => /^#{2,4}\s+/.test(line.trim());
|
|
54
|
+
const isOrderedItem = (line: string) => /^\s*\d+\.\s+/.test(line);
|
|
55
|
+
const isUnorderedItem = (line: string) => /^\s*[-*]\s+/.test(line);
|
|
56
|
+
|
|
57
|
+
/** Split a raw guide into its title, lede and body. */
|
|
58
|
+
export function parseGuideFrontmatter(markdown: string): ParsedGuide {
|
|
59
|
+
const all = lines(markdown);
|
|
60
|
+
let i = 0;
|
|
61
|
+
let title = "";
|
|
62
|
+
let lede: string | undefined;
|
|
63
|
+
|
|
64
|
+
while (i < all.length && (all[i] ?? "").trim() === "") i++;
|
|
65
|
+
if ((all[i] ?? "").startsWith("# ")) {
|
|
66
|
+
title = (all[i] ?? "")
|
|
67
|
+
.slice(2)
|
|
68
|
+
.trim()
|
|
69
|
+
.replace(/\s*[—-]\s*User Guide$/i, "");
|
|
70
|
+
i++;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
while (i < all.length && (all[i] ?? "").trim() === "") i++;
|
|
74
|
+
if ((all[i] ?? "").trim().startsWith(">")) {
|
|
75
|
+
const quote: string[] = [];
|
|
76
|
+
while (i < all.length && (all[i] ?? "").trim().startsWith(">")) {
|
|
77
|
+
quote.push((all[i] ?? "").trim().replace(/^>\s?/, ""));
|
|
78
|
+
i++;
|
|
79
|
+
}
|
|
80
|
+
lede = quote.join(" ").trim();
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return { title, ...(lede === undefined ? {} : { lede }), body: all.slice(i).join("\n") };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Inline Markdown reduced to plain text, for structured data and search. */
|
|
87
|
+
export function stripInline(text: string): string {
|
|
88
|
+
return text
|
|
89
|
+
.replace(/\[([^\]]+)\]\([^)]+\)/g, "$1")
|
|
90
|
+
.replace(/\*\*([^*]+)\*\*/g, "$1")
|
|
91
|
+
.replace(/\*([^*]+)\*/g, "$1")
|
|
92
|
+
.replace(/_([^_]+)_/g, "$1")
|
|
93
|
+
.replace(/`([^`]+)`/g, "$1")
|
|
94
|
+
.replace(/\s+/g, " ")
|
|
95
|
+
.trim();
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** A heading's text as a URL fragment, before de-duplication. */
|
|
99
|
+
export function headingSlug(text: string): string {
|
|
100
|
+
return (
|
|
101
|
+
stripInline(text)
|
|
102
|
+
.toLowerCase()
|
|
103
|
+
.replace(/[^a-z0-9]+/g, "-")
|
|
104
|
+
.replace(/^-+|-+$/g, "") || "section"
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Hands out unique fragments in document order: the second "Tips" is `tips-2`.
|
|
110
|
+
* The renderer and `extractHeadings` each take one, and both feed it exactly the
|
|
111
|
+
* `##` and `###` headings outside code fences — which is what keeps a contents
|
|
112
|
+
* link and the heading it points at in step.
|
|
113
|
+
*/
|
|
114
|
+
export function createHeadingIds(): (text: string) => string {
|
|
115
|
+
const seen = new Map<string, number>();
|
|
116
|
+
return (text) => {
|
|
117
|
+
const base = headingSlug(text);
|
|
118
|
+
const count = seen.get(base) ?? 0;
|
|
119
|
+
seen.set(base, count + 1);
|
|
120
|
+
return count === 0 ? base : `${base}-${count + 1}`;
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** The `##` and `###` headings, in order, with the ids the renderer gives them. */
|
|
125
|
+
export function extractHeadings(body: string): GuideHeading[] {
|
|
126
|
+
const nextId = createHeadingIds();
|
|
127
|
+
const headings: GuideHeading[] = [];
|
|
128
|
+
let inFence = false;
|
|
129
|
+
|
|
130
|
+
for (const raw of lines(body)) {
|
|
131
|
+
if (isFence(raw)) {
|
|
132
|
+
inFence = !inFence;
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
if (inFence) continue;
|
|
136
|
+
const match = raw.trim().match(/^(#{2,3})\s+(.*)$/);
|
|
137
|
+
if (!match) continue;
|
|
138
|
+
const source = match[2] ?? "";
|
|
139
|
+
headings.push({ id: nextId(source), text: stripInline(source), level: (match[1] ?? "").length as 2 | 3 });
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return headings;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* The `###` steps of a guide's "## Step by step" section as plain text, for
|
|
147
|
+
* HowTo structured data. Each step's text is its prose and list lines, capped
|
|
148
|
+
* so the schema stays lean. Empty when the guide has no such section.
|
|
149
|
+
*/
|
|
150
|
+
export function extractHowToSteps(body: string): HowToStep[] {
|
|
151
|
+
const all = lines(body);
|
|
152
|
+
const start = all.findIndex((line) => /^##\s+Step by step\s*$/i.test(line.trim()));
|
|
153
|
+
if (start === -1) return [];
|
|
154
|
+
let end = all.length;
|
|
155
|
+
for (let i = start + 1; i < all.length; i++) {
|
|
156
|
+
if (/^##\s+/.test((all[i] ?? "").trim())) {
|
|
157
|
+
end = i;
|
|
158
|
+
break;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const steps: HowToStep[] = [];
|
|
163
|
+
let current: { name: string; parts: string[] } | null = null;
|
|
164
|
+
const flush = () => {
|
|
165
|
+
if (!current) return;
|
|
166
|
+
const text = stripInline(current.parts.join(" "));
|
|
167
|
+
steps.push({ name: current.name, text: text.length > 320 ? `${text.slice(0, 317)}…` : text || current.name });
|
|
168
|
+
current = null;
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
for (let i = start + 1; i < end; i++) {
|
|
172
|
+
const line = (all[i] ?? "").trim();
|
|
173
|
+
const step = line.match(/^###\s+(.*)$/);
|
|
174
|
+
if (step) {
|
|
175
|
+
flush();
|
|
176
|
+
current = { name: stripInline(step[1] ?? ""), parts: [] };
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
if (!current || line === "" || line.startsWith(">")) continue;
|
|
180
|
+
current.parts.push(line.replace(/^\s*([-*]|\d+\.)\s+/, ""));
|
|
181
|
+
}
|
|
182
|
+
flush();
|
|
183
|
+
|
|
184
|
+
return steps;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export interface GuideLinkOptions {
|
|
188
|
+
/** The public route guides live under. */
|
|
189
|
+
guidesPath?: string;
|
|
190
|
+
/** The file name (without `.md`) of the guide index, which links to `guidesPath` itself. */
|
|
191
|
+
indexSlug?: string;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Where a link in a guide goes. `other-guide.md#part` becomes
|
|
196
|
+
* `/guides/other-guide#part`; same-site paths, fragments, `mailto:` and web
|
|
197
|
+
* addresses pass through; anything else — `javascript:`, `data:`, a
|
|
198
|
+
* protocol-relative `//host` — becomes `#`.
|
|
199
|
+
*/
|
|
200
|
+
export function resolveGuideHref(href: string, { guidesPath = "/guides", indexSlug }: GuideLinkOptions = {}): string {
|
|
201
|
+
const guide = href.match(/^([\w-]+)\.md(#.*)?$/);
|
|
202
|
+
if (guide) {
|
|
203
|
+
const slug = guide[1] ?? "";
|
|
204
|
+
const hash = guide[2] ?? "";
|
|
205
|
+
return slug === indexSlug ? `${guidesPath}${hash}` : `${guidesPath}/${slug}${hash}`;
|
|
206
|
+
}
|
|
207
|
+
const samePath = href.startsWith("/") && !href.startsWith("//") && !href.startsWith("/\\");
|
|
208
|
+
if (samePath || href.startsWith("#") || href.startsWith("mailto:") || /^https?:\/\//i.test(href)) return href;
|
|
209
|
+
return "#";
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Every element a guide draws. Each value replaces the default for that element.
|
|
214
|
+
*
|
|
215
|
+
* `table` and `figure` are each a set of parts, merged over the house's, and optional: an app that
|
|
216
|
+
* writes out a whole `GuideClassNames` (KRP's palette classes) needn't name a part it doesn't
|
|
217
|
+
* restyle, and a set of parts added in a later release can't break its typecheck.
|
|
218
|
+
*/
|
|
219
|
+
export interface GuideClassNames {
|
|
220
|
+
h2: string;
|
|
221
|
+
h3: string;
|
|
222
|
+
h4: string;
|
|
223
|
+
paragraph: string;
|
|
224
|
+
blockquote: string;
|
|
225
|
+
orderedList: string;
|
|
226
|
+
unorderedList: string;
|
|
227
|
+
listItem: string;
|
|
228
|
+
link: string;
|
|
229
|
+
strong: string;
|
|
230
|
+
code: string;
|
|
231
|
+
pre: string;
|
|
232
|
+
hr: string;
|
|
233
|
+
table?: Partial<MarkdownTableClassNames>;
|
|
234
|
+
/** Left out, the house's picture classes (`GUIDE_FIGURE_CLASS_NAMES`). */
|
|
235
|
+
figure?: Partial<GuideFigureClassNames>;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/** The house look, on the shared theme tokens. Every part named, so `GUIDE_CLASS_NAMES.figure` is always there. */
|
|
239
|
+
export const GUIDE_CLASS_NAMES: Required<GuideClassNames> = {
|
|
240
|
+
h2: "mt-12 scroll-mt-24 text-2xl font-bold tracking-tight text-foreground",
|
|
241
|
+
h3: "mt-8 scroll-mt-24 text-lg font-semibold text-foreground",
|
|
242
|
+
h4: "mt-6 text-base font-semibold text-foreground",
|
|
243
|
+
paragraph: "my-4 leading-relaxed text-muted-foreground",
|
|
244
|
+
blockquote: "my-6 border-l-4 border-primary/60 bg-card/60 py-3 pr-3 pl-4 text-muted-foreground italic",
|
|
245
|
+
orderedList: "my-4 ml-5 list-decimal space-y-2 text-muted-foreground marker:text-muted-foreground",
|
|
246
|
+
unorderedList: "my-4 ml-5 list-disc space-y-2 text-muted-foreground marker:text-muted-foreground",
|
|
247
|
+
listItem: "pl-1 leading-relaxed",
|
|
248
|
+
link: "font-medium text-primary hover:underline",
|
|
249
|
+
strong: "font-semibold text-foreground",
|
|
250
|
+
// Allowed to break anywhere: a web address or a path in inline code has no spaces, and one in an AltEd guide pushed
|
|
251
|
+
// the page 131px past a 390px phone's edge. Code blocks keep `pre`'s white-space, so they scroll instead.
|
|
252
|
+
code: "rounded bg-muted px-1.5 py-0.5 font-mono text-[0.85em] text-foreground [overflow-wrap:anywhere]",
|
|
253
|
+
pre: "my-6 overflow-x-auto rounded-xl border border-border bg-muted/60 p-4 font-mono text-[0.85em] leading-relaxed text-foreground",
|
|
254
|
+
hr: "my-8 border-border",
|
|
255
|
+
table: {},
|
|
256
|
+
figure: {},
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
export interface GuideMarkdownProps extends GuideLinkOptions {
|
|
260
|
+
content: string;
|
|
261
|
+
classNames?: Partial<GuideClassNames>;
|
|
262
|
+
/**
|
|
263
|
+
* The pictures a guide may show. A picture line (``
|
|
264
|
+
* or ``, alone on its line) is drawn only when it is registered here
|
|
265
|
+
* and has alt text; otherwise it is left out, never drawn as a broken image.
|
|
266
|
+
*/
|
|
267
|
+
figures?: GuideFigures;
|
|
268
|
+
/** Draws each registered picture. Default: `GuideFigureView`, a plain `<figure>`; `GuideArticle` adds zoom. */
|
|
269
|
+
renderFigure?: (figure: GuideFigureBlock) => ReactNode;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/** Render a guide body. */
|
|
273
|
+
export function GuideMarkdown({
|
|
274
|
+
content,
|
|
275
|
+
guidesPath,
|
|
276
|
+
indexSlug,
|
|
277
|
+
classNames,
|
|
278
|
+
figures,
|
|
279
|
+
renderFigure,
|
|
280
|
+
}: GuideMarkdownProps): ReactNode {
|
|
281
|
+
// `table` and `figure` are each a set of parts, so an override of one part keeps the rest, and leaving one out
|
|
282
|
+
// (or passing it as undefined) keeps the house's.
|
|
283
|
+
const c: Required<GuideClassNames> = {
|
|
284
|
+
...GUIDE_CLASS_NAMES,
|
|
285
|
+
...classNames,
|
|
286
|
+
table: { ...GUIDE_CLASS_NAMES.table, ...classNames?.table },
|
|
287
|
+
figure: { ...GUIDE_CLASS_NAMES.figure, ...classNames?.figure },
|
|
288
|
+
};
|
|
289
|
+
const drawFigure =
|
|
290
|
+
renderFigure ??
|
|
291
|
+
((figure: GuideFigureBlock) => <GuideFigureView figure={figure} figures={figures} classNames={c.figure} />);
|
|
292
|
+
const linkOptions = {
|
|
293
|
+
...(guidesPath === undefined ? {} : { guidesPath }),
|
|
294
|
+
...(indexSlug === undefined ? {} : { indexSlug }),
|
|
295
|
+
};
|
|
296
|
+
const inline = (text: string, keyPrefix: string) => renderInline(text, keyPrefix, c, linkOptions);
|
|
297
|
+
const all = lines(content);
|
|
298
|
+
const nextId = createHeadingIds();
|
|
299
|
+
const blocks: ReactNode[] = [];
|
|
300
|
+
let i = 0;
|
|
301
|
+
let key = 0;
|
|
302
|
+
|
|
303
|
+
/** A line that ends the paragraph or list item before it. */
|
|
304
|
+
const startsBlock = (at: number) => {
|
|
305
|
+
const line = all[at] ?? "";
|
|
306
|
+
const trimmed = line.trim();
|
|
307
|
+
return (
|
|
308
|
+
trimmed === "" ||
|
|
309
|
+
isHeading(line) ||
|
|
310
|
+
trimmed.startsWith(">") ||
|
|
311
|
+
isRule(line) ||
|
|
312
|
+
isFence(line) ||
|
|
313
|
+
isTableStart(all, at) ||
|
|
314
|
+
parseFigureLine(line) !== null
|
|
315
|
+
);
|
|
316
|
+
};
|
|
317
|
+
|
|
318
|
+
/** A list: each item, with any wrapped lines that follow it folded in. */
|
|
319
|
+
const readList = (isItem: (line: string) => boolean, marker: RegExp) => {
|
|
320
|
+
const items: string[] = [];
|
|
321
|
+
while (i < all.length && isItem(all[i] ?? "")) {
|
|
322
|
+
let item = (all[i] ?? "").replace(marker, "");
|
|
323
|
+
i++;
|
|
324
|
+
while (i < all.length && !startsBlock(i) && !isOrderedItem(all[i] ?? "") && !isUnorderedItem(all[i] ?? "")) {
|
|
325
|
+
item += ` ${(all[i] ?? "").trim()}`;
|
|
326
|
+
i++;
|
|
327
|
+
}
|
|
328
|
+
items.push(item);
|
|
329
|
+
}
|
|
330
|
+
return items;
|
|
331
|
+
};
|
|
332
|
+
|
|
333
|
+
while (i < all.length) {
|
|
334
|
+
const line = all[i] ?? "";
|
|
335
|
+
const trimmed = line.trim();
|
|
336
|
+
|
|
337
|
+
if (trimmed === "") {
|
|
338
|
+
i++;
|
|
339
|
+
continue;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
if (isRule(line)) {
|
|
343
|
+
blocks.push(<hr key={key++} className={c.hr} />);
|
|
344
|
+
i++;
|
|
345
|
+
continue;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
if (isFence(line)) {
|
|
349
|
+
const code: string[] = [];
|
|
350
|
+
i++;
|
|
351
|
+
while (i < all.length && !isFence(all[i] ?? "")) {
|
|
352
|
+
code.push(all[i] ?? "");
|
|
353
|
+
i++;
|
|
354
|
+
}
|
|
355
|
+
i++; // the closing fence; an unclosed one simply runs to the end
|
|
356
|
+
blocks.push(
|
|
357
|
+
<pre key={key++} className={c.pre}>
|
|
358
|
+
<code>{code.join("\n")}</code>
|
|
359
|
+
</pre>,
|
|
360
|
+
);
|
|
361
|
+
continue;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
const heading = trimmed.match(/^(#{2,4})\s+(.*)$/);
|
|
365
|
+
if (heading) {
|
|
366
|
+
const level = (heading[1] ?? "").length;
|
|
367
|
+
const source = heading[2] ?? "";
|
|
368
|
+
const nodes = inline(source, `h${key}`);
|
|
369
|
+
if (level === 2) {
|
|
370
|
+
blocks.push(
|
|
371
|
+
<h2 key={key++} id={nextId(source)} className={c.h2}>
|
|
372
|
+
{nodes}
|
|
373
|
+
</h2>,
|
|
374
|
+
);
|
|
375
|
+
} else if (level === 3) {
|
|
376
|
+
blocks.push(
|
|
377
|
+
<h3 key={key++} id={nextId(source)} className={c.h3}>
|
|
378
|
+
{nodes}
|
|
379
|
+
</h3>,
|
|
380
|
+
);
|
|
381
|
+
} else {
|
|
382
|
+
blocks.push(
|
|
383
|
+
<h4 key={key++} className={c.h4}>
|
|
384
|
+
{nodes}
|
|
385
|
+
</h4>,
|
|
386
|
+
);
|
|
387
|
+
}
|
|
388
|
+
i++;
|
|
389
|
+
continue;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
if (trimmed.startsWith(">")) {
|
|
393
|
+
const quote: string[] = [];
|
|
394
|
+
while (i < all.length && (all[i] ?? "").trim().startsWith(">")) {
|
|
395
|
+
quote.push((all[i] ?? "").trim().replace(/^>\s?/, ""));
|
|
396
|
+
i++;
|
|
397
|
+
}
|
|
398
|
+
blocks.push(
|
|
399
|
+
<blockquote key={key++} className={c.blockquote}>
|
|
400
|
+
{inline(quote.join(" "), `q${key}`)}
|
|
401
|
+
</blockquote>,
|
|
402
|
+
);
|
|
403
|
+
continue;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
// A picture on a line of its own. Unregistered or undescribed, it is left out rather than drawn broken.
|
|
407
|
+
const picture = parseFigureLine(line);
|
|
408
|
+
if (picture) {
|
|
409
|
+
const figure = resolveGuideFigure(picture, figures);
|
|
410
|
+
if (figure) blocks.push(<Fragment key={key++}>{drawFigure(figure)}</Fragment>);
|
|
411
|
+
i++;
|
|
412
|
+
continue;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
const table = readTable(all, i);
|
|
416
|
+
if (table) {
|
|
417
|
+
const tableKey = key++;
|
|
418
|
+
blocks.push(
|
|
419
|
+
<MarkdownTableView
|
|
420
|
+
key={tableKey}
|
|
421
|
+
table={table.table}
|
|
422
|
+
classNames={c.table}
|
|
423
|
+
renderCell={(text, cellKey) => inline(text, `t${tableKey}-${cellKey}`)}
|
|
424
|
+
/>,
|
|
425
|
+
);
|
|
426
|
+
i = table.next;
|
|
427
|
+
continue;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
if (isOrderedItem(line)) {
|
|
431
|
+
const listKey = key++;
|
|
432
|
+
const items = readList(isOrderedItem, /^\s*\d+\.\s+/);
|
|
433
|
+
blocks.push(
|
|
434
|
+
<ol key={listKey} className={c.orderedList}>
|
|
435
|
+
{items.map((item, n) => (
|
|
436
|
+
<li key={n} className={c.listItem}>
|
|
437
|
+
{inline(item, `ol${listKey}-${n}`)}
|
|
438
|
+
</li>
|
|
439
|
+
))}
|
|
440
|
+
</ol>,
|
|
441
|
+
);
|
|
442
|
+
continue;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
if (isUnorderedItem(line)) {
|
|
446
|
+
const listKey = key++;
|
|
447
|
+
const items = readList(isUnorderedItem, /^\s*[-*]\s+/);
|
|
448
|
+
blocks.push(
|
|
449
|
+
<ul key={listKey} className={c.unorderedList}>
|
|
450
|
+
{items.map((item, n) => (
|
|
451
|
+
<li key={n} className={c.listItem}>
|
|
452
|
+
{inline(item, `ul${listKey}-${n}`)}
|
|
453
|
+
</li>
|
|
454
|
+
))}
|
|
455
|
+
</ul>,
|
|
456
|
+
);
|
|
457
|
+
continue;
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
const paragraph: string[] = [];
|
|
461
|
+
while (
|
|
462
|
+
i < all.length &&
|
|
463
|
+
!startsBlock(i) &&
|
|
464
|
+
(paragraph.length === 0 || (!isOrderedItem(all[i] ?? "") && !isUnorderedItem(all[i] ?? "")))
|
|
465
|
+
) {
|
|
466
|
+
paragraph.push((all[i] ?? "").trim());
|
|
467
|
+
i++;
|
|
468
|
+
}
|
|
469
|
+
const paragraphKey = key++;
|
|
470
|
+
blocks.push(
|
|
471
|
+
<p key={paragraphKey} className={c.paragraph}>
|
|
472
|
+
{inline(paragraph.join(" "), `p${paragraphKey}`)}
|
|
473
|
+
</p>,
|
|
474
|
+
);
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
return <>{blocks}</>;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
/** Bold, italic, code and links, as React nodes. Links are matched first so their text isn't parsed twice. */
|
|
481
|
+
function renderInline(text: string, keyPrefix: string, c: GuideClassNames, linkOptions: GuideLinkOptions): ReactNode[] {
|
|
482
|
+
const nodes: ReactNode[] = [];
|
|
483
|
+
const pattern = /(\[([^\]]+)\]\(([^)]+)\))|(\*\*([^*]+)\*\*)|(`([^`]+)`)|(\*([^*]+)\*)|(_([^_]+)_)/g;
|
|
484
|
+
let lastIndex = 0;
|
|
485
|
+
let n = 0;
|
|
486
|
+
|
|
487
|
+
for (let match = pattern.exec(text); match !== null; match = pattern.exec(text)) {
|
|
488
|
+
if (match.index > lastIndex) nodes.push(text.slice(lastIndex, match.index));
|
|
489
|
+
const key = `${keyPrefix}-${n++}`;
|
|
490
|
+
|
|
491
|
+
if (match[1]) {
|
|
492
|
+
const href = resolveGuideHref(match[3] ?? "", linkOptions);
|
|
493
|
+
const label = match[2];
|
|
494
|
+
if (href.startsWith("/") || href.startsWith("#")) {
|
|
495
|
+
nodes.push(
|
|
496
|
+
<Link key={key} href={href} className={c.link}>
|
|
497
|
+
{label}
|
|
498
|
+
</Link>,
|
|
499
|
+
);
|
|
500
|
+
} else if (/^https?:\/\//i.test(href)) {
|
|
501
|
+
nodes.push(
|
|
502
|
+
<a key={key} href={href} className={c.link} target="_blank" rel="noopener noreferrer">
|
|
503
|
+
{label}
|
|
504
|
+
</a>,
|
|
505
|
+
);
|
|
506
|
+
} else {
|
|
507
|
+
nodes.push(
|
|
508
|
+
<a key={key} href={href} className={c.link}>
|
|
509
|
+
{label}
|
|
510
|
+
</a>,
|
|
511
|
+
);
|
|
512
|
+
}
|
|
513
|
+
} else if (match[4]) {
|
|
514
|
+
nodes.push(
|
|
515
|
+
<strong key={key} className={c.strong}>
|
|
516
|
+
{match[5]}
|
|
517
|
+
</strong>,
|
|
518
|
+
);
|
|
519
|
+
} else if (match[6]) {
|
|
520
|
+
nodes.push(
|
|
521
|
+
<code key={key} className={c.code}>
|
|
522
|
+
{match[7]}
|
|
523
|
+
</code>,
|
|
524
|
+
);
|
|
525
|
+
} else if (match[8]) {
|
|
526
|
+
nodes.push(<em key={key}>{match[9]}</em>);
|
|
527
|
+
} else if (match[10]) {
|
|
528
|
+
nodes.push(<em key={key}>{match[11]}</em>);
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
lastIndex = pattern.lastIndex;
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
if (lastIndex < text.length) nodes.push(text.slice(lastIndex));
|
|
535
|
+
return nodes;
|
|
536
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
export {
|
|
2
|
+
createHeadingIds,
|
|
3
|
+
extractHeadings,
|
|
4
|
+
extractHowToSteps,
|
|
5
|
+
GUIDE_CLASS_NAMES,
|
|
6
|
+
GuideMarkdown,
|
|
7
|
+
headingSlug,
|
|
8
|
+
parseGuideFrontmatter,
|
|
9
|
+
resolveGuideHref,
|
|
10
|
+
stripInline,
|
|
11
|
+
type GuideClassNames,
|
|
12
|
+
type GuideHeading,
|
|
13
|
+
type GuideLinkOptions,
|
|
14
|
+
type GuideMarkdownProps,
|
|
15
|
+
type HowToStep,
|
|
16
|
+
type ParsedGuide,
|
|
17
|
+
} from "./guide.js";
|
|
18
|
+
export {
|
|
19
|
+
alignmentOf,
|
|
20
|
+
isTableRow,
|
|
21
|
+
isTableSeparator,
|
|
22
|
+
isTableStart,
|
|
23
|
+
readTable,
|
|
24
|
+
splitTableRow,
|
|
25
|
+
type MarkdownTable,
|
|
26
|
+
type TableAlign,
|
|
27
|
+
} from "./table.js";
|
|
28
|
+
export { MARKDOWN_TABLE_CLASS_NAMES, MarkdownTableView, type MarkdownTableClassNames } from "./table-view.js";
|
|
29
|
+
export {
|
|
30
|
+
DIAGRAM_PREFIX,
|
|
31
|
+
guideFigureProblems,
|
|
32
|
+
parseFigureLine,
|
|
33
|
+
resolveGuideFigure,
|
|
34
|
+
splitGuideFigures,
|
|
35
|
+
type GuideBlock,
|
|
36
|
+
type GuideDiagram,
|
|
37
|
+
type GuideFigureBlock,
|
|
38
|
+
type GuideFigureCheckOptions,
|
|
39
|
+
type GuideFigureLine,
|
|
40
|
+
type GuideFigures,
|
|
41
|
+
type GuideScreenshot,
|
|
42
|
+
type GuideScreenshotCopy,
|
|
43
|
+
} from "./figures.js";
|
|
44
|
+
export {
|
|
45
|
+
drawGuideDiagram,
|
|
46
|
+
GUIDE_FIGURE_CLASS_NAMES,
|
|
47
|
+
GUIDE_FIGURE_SIZES,
|
|
48
|
+
GuideFigureMedia,
|
|
49
|
+
GuideFigureView,
|
|
50
|
+
type GuideFigureClassNames,
|
|
51
|
+
type GuideFigureMediaProps,
|
|
52
|
+
type GuideFigureViewProps,
|
|
53
|
+
} from "./figure.js";
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { ReactNode } from "react";
|
|
2
|
+
|
|
3
|
+
import { cx } from "../internal/cx.js";
|
|
4
|
+
import type { MarkdownTable, TableAlign } from "./table.js";
|
|
5
|
+
|
|
6
|
+
const ALIGN: Record<TableAlign, string> = { left: "text-left", center: "text-center", right: "text-right" };
|
|
7
|
+
|
|
8
|
+
/** The parts of a table an app can restyle. Each replaces the default for that part. */
|
|
9
|
+
export interface MarkdownTableClassNames {
|
|
10
|
+
wrapper: string;
|
|
11
|
+
table: string;
|
|
12
|
+
head: string;
|
|
13
|
+
headerCell: string;
|
|
14
|
+
row: string;
|
|
15
|
+
cell: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export const MARKDOWN_TABLE_CLASS_NAMES: MarkdownTableClassNames = {
|
|
19
|
+
wrapper: "my-6 overflow-x-auto rounded-xl border border-border",
|
|
20
|
+
table: "w-full border-collapse text-sm",
|
|
21
|
+
head: "bg-muted/60",
|
|
22
|
+
headerCell: "px-3 py-2 align-top font-semibold text-foreground",
|
|
23
|
+
row: "border-t border-border",
|
|
24
|
+
cell: "px-3 py-2 align-top leading-relaxed text-muted-foreground",
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* A parsed Markdown table, drawn with the house token classes so it takes the
|
|
29
|
+
* host app's look. Cells arrive as Markdown text; `renderCell` is the host
|
|
30
|
+
* renderer's own inline pass (bold, code, links), so a table cell reads exactly
|
|
31
|
+
* like the prose around it and link rewriting stays the app's business.
|
|
32
|
+
*
|
|
33
|
+
* An app whose theme has no tokens (a plain Tailwind palette) passes
|
|
34
|
+
* `classNames`. Server-safe: no hooks, no client boundary.
|
|
35
|
+
*/
|
|
36
|
+
export function MarkdownTableView({
|
|
37
|
+
table,
|
|
38
|
+
renderCell,
|
|
39
|
+
className,
|
|
40
|
+
classNames,
|
|
41
|
+
}: {
|
|
42
|
+
table: MarkdownTable;
|
|
43
|
+
renderCell: (text: string, key: string) => ReactNode;
|
|
44
|
+
className?: string;
|
|
45
|
+
classNames?: Partial<MarkdownTableClassNames>;
|
|
46
|
+
}) {
|
|
47
|
+
const c = { ...MARKDOWN_TABLE_CLASS_NAMES, ...classNames };
|
|
48
|
+
return (
|
|
49
|
+
<div className={cx(c.wrapper, className)}>
|
|
50
|
+
<table className={c.table}>
|
|
51
|
+
<thead className={c.head}>
|
|
52
|
+
<tr>
|
|
53
|
+
{table.header.map((cell, col) => (
|
|
54
|
+
<th key={col} scope="col" className={cx(c.headerCell, ALIGN[table.aligns[col] ?? "left"])}>
|
|
55
|
+
{renderCell(cell, `th-${col}`)}
|
|
56
|
+
</th>
|
|
57
|
+
))}
|
|
58
|
+
</tr>
|
|
59
|
+
</thead>
|
|
60
|
+
<tbody>
|
|
61
|
+
{table.rows.map((row, r) => (
|
|
62
|
+
<tr key={r} className={c.row}>
|
|
63
|
+
{row.map((cell, col) => (
|
|
64
|
+
<td key={col} className={cx(c.cell, ALIGN[table.aligns[col] ?? "left"])}>
|
|
65
|
+
{renderCell(cell, `td-${r}-${col}`)}
|
|
66
|
+
</td>
|
|
67
|
+
))}
|
|
68
|
+
</tr>
|
|
69
|
+
))}
|
|
70
|
+
</tbody>
|
|
71
|
+
</table>
|
|
72
|
+
</div>
|
|
73
|
+
);
|
|
74
|
+
}
|