@connextar/house 0.23.0 → 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 +27 -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/dist/rate-limit/prisma-index.d.ts +1 -1
- package/dist/rate-limit/prisma-index.d.ts.map +1 -1
- package/dist/rate-limit/prisma-index.js.map +1 -1
- package/dist/rate-limit/prisma.d.ts +10 -1
- package/dist/rate-limit/prisma.d.ts.map +1 -1
- package/dist/rate-limit/prisma.js +2 -1
- package/dist/rate-limit/prisma.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,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether an error means "the database (or the file store) can't be reached
|
|
3
|
+
* right now", as opposed to a bug. A connection refused during a reboot, a pool
|
|
4
|
+
* that timed out, Postgres terminating connections as it shuts down.
|
|
5
|
+
*
|
|
6
|
+
* Errors arrive wrapped — Prisma's adapter puts the driver's error under
|
|
7
|
+
* `meta.driverAdapterError`, fetch puts the socket's under `cause`, the AWS SDK
|
|
8
|
+
* nests it again — so the walk follows those links a few levels down.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/** Prisma: can't reach the server, timed out, connection closed, pool timeout. */
|
|
12
|
+
const PRISMA_CODES = new Set(["P1001", "P1002", "P1017", "P2024"]);
|
|
13
|
+
/**
|
|
14
|
+
* SQLSTATE: admin/crash shutdown, cannot connect now (starting or stopping),
|
|
15
|
+
* the connection-exception class, too many connections.
|
|
16
|
+
*/
|
|
17
|
+
const SQLSTATES = new Set(["57P01", "57P02", "57P03", "08000", "08001", "08003", "08004", "08006", "53300"]);
|
|
18
|
+
/** Node's socket errors. */
|
|
19
|
+
const ERRNOS = new Set([
|
|
20
|
+
"ECONNREFUSED",
|
|
21
|
+
"ECONNRESET",
|
|
22
|
+
"ETIMEDOUT",
|
|
23
|
+
"EHOSTUNREACH",
|
|
24
|
+
"ENETUNREACH",
|
|
25
|
+
"EPIPE",
|
|
26
|
+
"EAI_AGAIN",
|
|
27
|
+
"UND_ERR_CONNECT_TIMEOUT",
|
|
28
|
+
"UND_ERR_SOCKET",
|
|
29
|
+
]);
|
|
30
|
+
const MESSAGES = [
|
|
31
|
+
/connection terminated unexpectedly/i,
|
|
32
|
+
/timeout exceeded when trying to connect/i,
|
|
33
|
+
/terminating connection due to administrator command/i,
|
|
34
|
+
/the database system is (starting up|shutting down)/i,
|
|
35
|
+
/can't reach database server/i,
|
|
36
|
+
/connection (refused|reset)/i,
|
|
37
|
+
];
|
|
38
|
+
|
|
39
|
+
function children(error: Record<string, unknown>): unknown[] {
|
|
40
|
+
const meta = error.meta as Record<string, unknown> | undefined;
|
|
41
|
+
const adapter = meta?.driverAdapterError as Record<string, unknown> | undefined;
|
|
42
|
+
return [
|
|
43
|
+
error.cause,
|
|
44
|
+
error.originalError,
|
|
45
|
+
adapter,
|
|
46
|
+
adapter?.cause,
|
|
47
|
+
...(Array.isArray(error.errors) ? error.errors : []),
|
|
48
|
+
];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function isUnavailableError(error: unknown, depth = 0): boolean {
|
|
52
|
+
if (depth > 6 || typeof error !== "object" || error === null) return false;
|
|
53
|
+
const e = error as Record<string, unknown>;
|
|
54
|
+
for (const key of ["code", "errno", "originalCode"]) {
|
|
55
|
+
const v = e[key];
|
|
56
|
+
if (typeof v === "string" && (PRISMA_CODES.has(v) || SQLSTATES.has(v) || ERRNOS.has(v))) return true;
|
|
57
|
+
}
|
|
58
|
+
if (e.kind === "DatabaseNotReachable" || e.kind === "ConnectionClosed" || e.kind === "SocketTimeout") return true;
|
|
59
|
+
if (typeof e.message === "string" && MESSAGES.some((re) => re.test(e.message as string))) return true;
|
|
60
|
+
return children(e).some((child) => child !== undefined && isUnavailableError(child, depth + 1));
|
|
61
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { createElement, isValidElement, type ComponentType, type ReactNode } from "react";
|
|
2
|
+
|
|
3
|
+
import { cx } from "../internal/cx.js";
|
|
4
|
+
import type { GuideDiagram, GuideFigureBlock, GuideFigures } from "./figures.js";
|
|
5
|
+
|
|
6
|
+
/** Every element a guide's picture draws. Each value replaces the default for that element. */
|
|
7
|
+
export interface GuideFigureClassNames {
|
|
8
|
+
figure: string;
|
|
9
|
+
/** Every screenshot `<img>`. */
|
|
10
|
+
image: string;
|
|
11
|
+
/** Added to a screenshot with no dark capture: a light picture on a dark page, toned down so it doesn't glare. */
|
|
12
|
+
imageWithoutDark: string;
|
|
13
|
+
/** Added to the light capture when there is a dark one. */
|
|
14
|
+
imageLight: string;
|
|
15
|
+
/** Added to the dark capture. */
|
|
16
|
+
imageDark: string;
|
|
17
|
+
/** The frame a diagram sits in. */
|
|
18
|
+
diagram: string;
|
|
19
|
+
caption: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** The house look, on the shared theme tokens (PharmaLineAI's). */
|
|
23
|
+
export const GUIDE_FIGURE_CLASS_NAMES: GuideFigureClassNames = {
|
|
24
|
+
figure: "my-8",
|
|
25
|
+
// No display utility here: a dark capture is `hidden dark:block`, and two display utilities on one element fight.
|
|
26
|
+
image: "h-auto w-full rounded-xl border border-border bg-card align-top shadow-sm",
|
|
27
|
+
imageWithoutDark: "dark:brightness-[0.92]",
|
|
28
|
+
imageLight: "dark:hidden",
|
|
29
|
+
imageDark: "hidden dark:block",
|
|
30
|
+
diagram: "rounded-xl border border-border bg-card/60 px-3 py-5 sm:px-6",
|
|
31
|
+
caption: "mt-2.5 text-center text-sm text-muted-foreground",
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/** A component (function, `memo`, `forwardRef`) rather than an element or text. */
|
|
35
|
+
function isComponent(entry: GuideDiagram): entry is ComponentType<{ label: string }> {
|
|
36
|
+
if (typeof entry === "function") return true;
|
|
37
|
+
return typeof entry === "object" && entry !== null && !isValidElement(entry) && "$$typeof" in entry;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* A registered diagram, drawn: a component is given the alt text as `label`; an element is
|
|
42
|
+
* wrapped in `role="img"` with the alt text as its name. Null when the name isn't registered.
|
|
43
|
+
*/
|
|
44
|
+
export function drawGuideDiagram(figures: GuideFigures | undefined, name: string, label: string): ReactNode {
|
|
45
|
+
const diagrams = figures?.diagrams;
|
|
46
|
+
if (!diagrams || !Object.prototype.hasOwnProperty.call(diagrams, name)) return null;
|
|
47
|
+
const entry = diagrams[name];
|
|
48
|
+
if (isComponent(entry)) return createElement(entry, { label });
|
|
49
|
+
return (
|
|
50
|
+
<div role="img" aria-label={label}>
|
|
51
|
+
{entry}
|
|
52
|
+
</div>
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* How wide a guide's screenshot is drawn, for the browser to choose among its narrower copies: the article's
|
|
58
|
+
* width (`max-w-3xl`, 48rem) on a wide screen, and the screen less a 1rem gutter either side on a phone. An
|
|
59
|
+
* estimate on the large side only costs a larger file than needed, never a blurry picture.
|
|
60
|
+
*/
|
|
61
|
+
export const GUIDE_FIGURE_SIZES = "(min-width: 48rem) 48rem, calc(100vw - 2rem)";
|
|
62
|
+
|
|
63
|
+
export interface GuideFigureMediaProps {
|
|
64
|
+
figure: GuideFigureBlock;
|
|
65
|
+
figures?: GuideFigures;
|
|
66
|
+
classNames?: Partial<GuideFigureClassNames>;
|
|
67
|
+
/** Load the screenshot straight away rather than when it nears the screen. Default: lazy. */
|
|
68
|
+
eager?: boolean;
|
|
69
|
+
/**
|
|
70
|
+
* The `sizes` that goes with a screenshot's narrower copies (`GuideScreenshot.smaller`): how wide the app's
|
|
71
|
+
* layout draws it. Default `GUIDE_FIGURE_SIZES`. `false` loads the original only — the enlarged view does.
|
|
72
|
+
*/
|
|
73
|
+
sizes?: string | false;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* The picture itself, without its caption: a screenshot at its registered size (and its dark
|
|
78
|
+
* capture beside it when there is one, each shown by the theme's `dark:` variant), or a diagram
|
|
79
|
+
* in its frame.
|
|
80
|
+
*/
|
|
81
|
+
export function GuideFigureMedia({
|
|
82
|
+
figure,
|
|
83
|
+
figures,
|
|
84
|
+
classNames,
|
|
85
|
+
eager,
|
|
86
|
+
sizes = GUIDE_FIGURE_SIZES,
|
|
87
|
+
}: GuideFigureMediaProps): ReactNode {
|
|
88
|
+
const c = { ...GUIDE_FIGURE_CLASS_NAMES, ...classNames };
|
|
89
|
+
if (figure.kind === "diagram") {
|
|
90
|
+
return <div className={c.diagram}>{drawGuideDiagram(figures, figure.name, figure.alt)}</div>;
|
|
91
|
+
}
|
|
92
|
+
const img = (src: string, srcSet: string | undefined, className: string) => (
|
|
93
|
+
// A plain <img>: the package imports no app's image pipeline. The registered size reserves the space, and a
|
|
94
|
+
// `srcset` of the app's narrower copies lets a phone load a smaller file. `srcSet` before `src`, so a browser
|
|
95
|
+
// reading attributes in order never starts on the original first.
|
|
96
|
+
<img
|
|
97
|
+
{...(srcSet && sizes !== false ? { srcSet, sizes } : {})}
|
|
98
|
+
src={src}
|
|
99
|
+
alt={figure.alt}
|
|
100
|
+
width={figure.width}
|
|
101
|
+
height={figure.height}
|
|
102
|
+
loading={eager ? "eager" : "lazy"}
|
|
103
|
+
decoding="async"
|
|
104
|
+
className={className}
|
|
105
|
+
/>
|
|
106
|
+
);
|
|
107
|
+
if (!figure.dark) return img(figure.src, figure.srcSet, cx(c.image, c.imageWithoutDark));
|
|
108
|
+
return (
|
|
109
|
+
<>
|
|
110
|
+
{img(figure.src, figure.srcSet, cx(c.image, c.imageLight))}
|
|
111
|
+
{img(figure.dark, figure.darkSrcSet, cx(c.image, c.imageDark))}
|
|
112
|
+
</>
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export interface GuideFigureViewProps extends GuideFigureMediaProps {
|
|
117
|
+
/** Drawn in place of the plain picture — `GuideArticle` puts its zoom button here. */
|
|
118
|
+
media?: ReactNode;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** A picture with its caption, as a `<figure>`. Server-safe; no zoom (`GuideArticle` adds that). */
|
|
122
|
+
export function GuideFigureView({ figure, figures, classNames, eager, sizes, media }: GuideFigureViewProps): ReactNode {
|
|
123
|
+
const c = { ...GUIDE_FIGURE_CLASS_NAMES, ...classNames };
|
|
124
|
+
return (
|
|
125
|
+
<figure className={c.figure}>
|
|
126
|
+
{media ?? (
|
|
127
|
+
<GuideFigureMedia figure={figure} figures={figures} classNames={classNames} eager={eager} sizes={sizes} />
|
|
128
|
+
)}
|
|
129
|
+
{figure.caption ? <figcaption className={c.caption}>{figure.caption}</figcaption> : null}
|
|
130
|
+
</figure>
|
|
131
|
+
);
|
|
132
|
+
}
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
import type { ComponentType, ReactNode } from "react";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Pictures in the guides: screenshots and diagrams, written in a guide's Markdown as
|
|
5
|
+
* an image on a line of its own, with a blank line either side:
|
|
6
|
+
*
|
|
7
|
+
* 
|
|
8
|
+
* 
|
|
9
|
+
*
|
|
10
|
+
* Only pictures in the app's registry are drawn — a guide can't pull an image in from
|
|
11
|
+
* anywhere else, and a typo never shows a broken image — each with its width and height
|
|
12
|
+
* so the page doesn't jump as it loads. `guideFigureProblems` is the check an app's test
|
|
13
|
+
* asserts empty: an unregistered picture, missing alt text, or a picture inside a list or
|
|
14
|
+
* paragraph.
|
|
15
|
+
*
|
|
16
|
+
* Lifted from PharmaLineAI v0.23.1 (`lib/guide-figures.ts`). Pure: no file access, no
|
|
17
|
+
* React at run time.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/** A narrower copy of a screenshot (and of its dark capture), for a phone to load instead of the full-size file. */
|
|
21
|
+
export interface GuideScreenshotCopy {
|
|
22
|
+
/** Its width in pixels; the height follows the screenshot's own proportions. */
|
|
23
|
+
width: number;
|
|
24
|
+
src: string;
|
|
25
|
+
/** The dark capture at the same width. Without one, a dark page picks from the dark copies there are. */
|
|
26
|
+
dark?: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** A screenshot in the app's `public/guide-images/`, with its size so the page doesn't jump. */
|
|
30
|
+
export interface GuideScreenshot {
|
|
31
|
+
width: number;
|
|
32
|
+
height: number;
|
|
33
|
+
/** A dark-mode capture of the same screen, at the same size, shown instead under `dark:`. */
|
|
34
|
+
dark?: string;
|
|
35
|
+
/**
|
|
36
|
+
* Narrower copies of the same picture (since 0.20.0). With them the `<img>` gets a `srcset`, and the browser
|
|
37
|
+
* loads the smallest that is sharp at the size it draws the picture: a phone takes an 800 or 1200 pixel copy
|
|
38
|
+
* rather than the 1600 pixel original. The enlarged view always loads the original.
|
|
39
|
+
*/
|
|
40
|
+
smaller?: readonly GuideScreenshotCopy[];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* A diagram: an element to draw as it is, or a component given the picture's alt text as
|
|
45
|
+
* `label` (to put on its own `<svg role="img" aria-label>`).
|
|
46
|
+
*/
|
|
47
|
+
export type GuideDiagram = ReactNode | ComponentType<{ label: string }>;
|
|
48
|
+
|
|
49
|
+
/** Every picture a guide may show. */
|
|
50
|
+
export interface GuideFigures {
|
|
51
|
+
/** By public path: `"/guide-images/calls-log.webp": { width: 1600, height: 986 }`. */
|
|
52
|
+
screenshots: Readonly<Record<string, GuideScreenshot>>;
|
|
53
|
+
/** By name, as a guide writes it after `diagram:`. */
|
|
54
|
+
diagrams?: Readonly<Record<string, GuideDiagram>>;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** A picture line as written, before it is looked up. */
|
|
58
|
+
export interface GuideFigureLine {
|
|
59
|
+
alt: string;
|
|
60
|
+
src: string;
|
|
61
|
+
caption?: string;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export type GuideFigureBlock =
|
|
65
|
+
| {
|
|
66
|
+
kind: "screenshot";
|
|
67
|
+
src: string;
|
|
68
|
+
alt: string;
|
|
69
|
+
caption?: string;
|
|
70
|
+
width: number;
|
|
71
|
+
height: number;
|
|
72
|
+
dark?: string;
|
|
73
|
+
/** `srcset` for the light picture, its copies and itself by width, when it has narrower copies. */
|
|
74
|
+
srcSet?: string;
|
|
75
|
+
/** `srcset` for the dark capture, when it has narrower dark copies. */
|
|
76
|
+
darkSrcSet?: string;
|
|
77
|
+
}
|
|
78
|
+
| { kind: "diagram"; name: string; alt: string; caption?: string };
|
|
79
|
+
|
|
80
|
+
export type GuideBlock = { kind: "markdown"; content: string } | GuideFigureBlock;
|
|
81
|
+
|
|
82
|
+
/** An image alone on its line: `` or ``. */
|
|
83
|
+
const IMAGE_LINE = /^!\[([^\]]*)\]\(\s*(\S+?)(?:\s+"([^"]*)")?\s*\)$/;
|
|
84
|
+
/** An image anywhere in a line, for the check that one isn't written inside a paragraph. */
|
|
85
|
+
const INLINE_IMAGE = /!\[[^\]]*\]\([^)]*\)/;
|
|
86
|
+
export const DIAGRAM_PREFIX = "diagram:";
|
|
87
|
+
|
|
88
|
+
const isFence = (line: string) => line.trim().startsWith("```");
|
|
89
|
+
const own = <T>(record: Readonly<Record<string, T>> | undefined, key: string): T | undefined =>
|
|
90
|
+
record && Object.prototype.hasOwnProperty.call(record, key) ? record[key] : undefined;
|
|
91
|
+
|
|
92
|
+
/** A picture line, or null when the line isn't one. */
|
|
93
|
+
export function parseFigureLine(line: string): GuideFigureLine | null {
|
|
94
|
+
const match = line.trim().match(IMAGE_LINE);
|
|
95
|
+
if (!match) return null;
|
|
96
|
+
const caption = match[3]?.trim();
|
|
97
|
+
return { alt: (match[1] ?? "").trim(), src: match[2] ?? "", ...(caption ? { caption } : {}) };
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** The picture a line names, or null when it names nothing in the registry or has no alt text. */
|
|
101
|
+
export function resolveGuideFigure(line: GuideFigureLine, figures: GuideFigures | undefined): GuideFigureBlock | null {
|
|
102
|
+
if (!line.alt || !figures) return null;
|
|
103
|
+
const caption = line.caption ? { caption: line.caption } : {};
|
|
104
|
+
if (line.src.startsWith(DIAGRAM_PREFIX)) {
|
|
105
|
+
const name = line.src.slice(DIAGRAM_PREFIX.length);
|
|
106
|
+
return own(figures.diagrams, name) === undefined ? null : { kind: "diagram", name, alt: line.alt, ...caption };
|
|
107
|
+
}
|
|
108
|
+
const shot = own(figures.screenshots, line.src);
|
|
109
|
+
if (!shot) return null;
|
|
110
|
+
const srcSet = screenshotSrcSet(shot.smaller, (copy) => copy.src, line.src, shot.width);
|
|
111
|
+
const darkSrcSet = shot.dark ? screenshotSrcSet(shot.smaller, (copy) => copy.dark, shot.dark, shot.width) : undefined;
|
|
112
|
+
return {
|
|
113
|
+
kind: "screenshot",
|
|
114
|
+
src: line.src,
|
|
115
|
+
alt: line.alt,
|
|
116
|
+
...caption,
|
|
117
|
+
width: shot.width,
|
|
118
|
+
height: shot.height,
|
|
119
|
+
...(shot.dark ? { dark: shot.dark } : {}),
|
|
120
|
+
...(srcSet ? { srcSet } : {}),
|
|
121
|
+
...(darkSrcSet ? { darkSrcSet } : {}),
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* A `srcset` of the narrower copies and the original, narrowest first — or undefined when there is no narrower
|
|
127
|
+
* copy to choose (a copy as wide as the original or wider is left out: it would never be the better choice).
|
|
128
|
+
*/
|
|
129
|
+
function screenshotSrcSet(
|
|
130
|
+
copies: readonly GuideScreenshotCopy[] | undefined,
|
|
131
|
+
pick: (copy: GuideScreenshotCopy) => string | undefined,
|
|
132
|
+
original: string,
|
|
133
|
+
width: number,
|
|
134
|
+
): string | undefined {
|
|
135
|
+
const narrower = (copies ?? [])
|
|
136
|
+
.map((copy) => ({ src: pick(copy), width: copy.width }))
|
|
137
|
+
.filter((copy): copy is { src: string; width: number } => !!copy.src && copy.width > 0 && copy.width < width)
|
|
138
|
+
.sort((a, b) => a.width - b.width);
|
|
139
|
+
if (!narrower.length) return undefined;
|
|
140
|
+
return [...narrower, { src: original, width }].map((copy) => `${copy.src} ${copy.width}w`).join(", ");
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* A guide body as runs of Markdown and the pictures between them. A picture line that
|
|
145
|
+
* names nothing in the registry is dropped; inside a code fence a picture line is just
|
|
146
|
+
* code. `GuideMarkdown` draws figures in the same places itself, keeping every heading's
|
|
147
|
+
* id — this is the same split as data, for an app's own layout or its tests.
|
|
148
|
+
*/
|
|
149
|
+
export function splitGuideFigures(body: string, figures: GuideFigures | undefined): GuideBlock[] {
|
|
150
|
+
const blocks: GuideBlock[] = [];
|
|
151
|
+
let run: string[] = [];
|
|
152
|
+
let inFence = false;
|
|
153
|
+
const flush = () => {
|
|
154
|
+
const content = run.join("\n");
|
|
155
|
+
if (content.trim()) blocks.push({ kind: "markdown", content });
|
|
156
|
+
run = [];
|
|
157
|
+
};
|
|
158
|
+
for (const line of body.replace(/\r\n/g, "\n").split("\n")) {
|
|
159
|
+
if (isFence(line)) inFence = !inFence;
|
|
160
|
+
const parsed = inFence ? null : parseFigureLine(line);
|
|
161
|
+
if (!parsed) {
|
|
162
|
+
run.push(line);
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
flush();
|
|
166
|
+
const figure = resolveGuideFigure(parsed, figures);
|
|
167
|
+
if (figure) blocks.push(figure);
|
|
168
|
+
}
|
|
169
|
+
flush();
|
|
170
|
+
return blocks;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export interface GuideFigureCheckOptions {
|
|
174
|
+
/** The shortest alt text accepted. Default 1 — any; PharmaLine asks for more than 20. */
|
|
175
|
+
minAltLength?: number;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Every way a guide's pictures would fail to show as written, as sentences with line
|
|
180
|
+
* numbers. An app's test asserts it is empty for each guide:
|
|
181
|
+
*
|
|
182
|
+
* expect(guideFigureProblems(body, GUIDE_FIGURES)).toEqual([]);
|
|
183
|
+
*
|
|
184
|
+
* - a picture that isn't registered (a screenshot path or a `diagram:` name);
|
|
185
|
+
* - a picture with no alt text, or shorter than `minAltLength`;
|
|
186
|
+
* - a picture without a blank line either side — inside a list it would end the list and
|
|
187
|
+
* restart its numbering after it; inside a paragraph it would split it;
|
|
188
|
+
* - an image written inside a line of text, which is never drawn.
|
|
189
|
+
*
|
|
190
|
+
* Lines inside code fences, and images inside inline code, are left alone.
|
|
191
|
+
*/
|
|
192
|
+
export function guideFigureProblems(
|
|
193
|
+
body: string,
|
|
194
|
+
figures: GuideFigures | undefined,
|
|
195
|
+
{ minAltLength = 1 }: GuideFigureCheckOptions = {},
|
|
196
|
+
): string[] {
|
|
197
|
+
const lines = body.replace(/\r\n/g, "\n").split("\n");
|
|
198
|
+
const problems: string[] = [];
|
|
199
|
+
let inFence = false;
|
|
200
|
+
|
|
201
|
+
lines.forEach((line, i) => {
|
|
202
|
+
if (isFence(line)) {
|
|
203
|
+
inFence = !inFence;
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
if (inFence) return;
|
|
207
|
+
const at = `Line ${i + 1}`;
|
|
208
|
+
const parsed = parseFigureLine(line);
|
|
209
|
+
if (!parsed) {
|
|
210
|
+
if (INLINE_IMAGE.test(line.replace(/`[^`]*`/g, ""))) {
|
|
211
|
+
problems.push(`${at}: a picture inside a line of text is never drawn; put it on a line of its own.`);
|
|
212
|
+
}
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
const what = `\`${parsed.src}\``;
|
|
216
|
+
if (parsed.src.startsWith(DIAGRAM_PREFIX)) {
|
|
217
|
+
if (own(figures?.diagrams, parsed.src.slice(DIAGRAM_PREFIX.length)) === undefined) {
|
|
218
|
+
problems.push(`${at}: ${what} isn't a registered diagram.`);
|
|
219
|
+
}
|
|
220
|
+
} else if (!own(figures?.screenshots, parsed.src)) {
|
|
221
|
+
problems.push(`${at}: ${what} isn't a registered screenshot.`);
|
|
222
|
+
}
|
|
223
|
+
if (!parsed.alt) problems.push(`${at}: ${what} has no alt text.`);
|
|
224
|
+
else if (parsed.alt.length < minAltLength) {
|
|
225
|
+
problems.push(`${at}: ${what} has alt text shorter than ${minAltLength} characters.`);
|
|
226
|
+
}
|
|
227
|
+
if ((lines[i - 1] ?? "").trim() !== "" || (lines[i + 1] ?? "").trim() !== "") {
|
|
228
|
+
problems.push(`${at}: ${what} needs a blank line before and after it (a picture can't sit inside a list).`);
|
|
229
|
+
} else if (/^\s/.test(line)) {
|
|
230
|
+
problems.push(
|
|
231
|
+
`${at}: ${what} is indented as if inside a list; a picture ends the list, so start it at the margin.`,
|
|
232
|
+
);
|
|
233
|
+
}
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
return problems;
|
|
237
|
+
}
|