@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,343 @@
|
|
|
1
|
+
import { Link } from "../internal/next-link.js";
|
|
2
|
+
import type { ReactNode } from "react";
|
|
3
|
+
|
|
4
|
+
import { cx } from "../internal/cx.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Read-only display for what `RichTextEditor` writes — the Markdown dialect the
|
|
8
|
+
* editor produces (headings, lists incl. task lists, quotes, code fences,
|
|
9
|
+
* bold/italic/strike/inline code, links, `@`-references, `#hashtags`, images from this site), with a
|
|
10
|
+
* bare newline shown as a line break so legacy plain-text values in the same
|
|
11
|
+
* columns read exactly as they always did.
|
|
12
|
+
*
|
|
13
|
+
* Separate from a guide renderer deliberately: a help centre wants guide-scale
|
|
14
|
+
* headings and link rewriting; a note wants task lists, fences and line breaks.
|
|
15
|
+
* Folding both dialects and both type scales into one would serve neither well.
|
|
16
|
+
*
|
|
17
|
+
* This is a trust boundary: whatever is stored in a text column is drawn here,
|
|
18
|
+
* often in front of a *different* person. Everything comes out as React
|
|
19
|
+
* elements with checked hrefs — never as markup.
|
|
20
|
+
*
|
|
21
|
+
* Server-safe: no hooks, so views can render bodies without a client boundary.
|
|
22
|
+
* Styled by `.house-prose` in `@connextar/house/house.css`.
|
|
23
|
+
*/
|
|
24
|
+
export function RichTextContent({ content, className }: { content: string; className?: string }) {
|
|
25
|
+
return <div className={cx("house-prose", className)}>{renderBlocks(content)}</div>;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/* ── Inline ───────────────────────────────────────────────────────────── */
|
|
29
|
+
|
|
30
|
+
// Link labels exclude `[` and both halves are bounded: `[^\]]+` let every `[`
|
|
31
|
+
// of a long run swallow the rest and backtrack, so a 100 KB run of `[` stalled
|
|
32
|
+
// a server for seconds (AltEd security round 8, 8.1).
|
|
33
|
+
const INLINE =
|
|
34
|
+
/(`([^`\n]+)`)|(\[([^[\]\n]{1,500})\]\(([^)\s]{1,2048})\))|(\*\*([^*]+)\*\*)|(~~([^~]+)~~)|(\*([^*\n]+)\*)|(\b_([^_\n]+)_\b)|((?:^|(?<=[\s(]))#([a-z0-9][a-z0-9_-]{0,47})\b)|(!\[([^[\]\n]{0,500})\]\(([^)\s]{1,2048})\))/gim;
|
|
35
|
+
|
|
36
|
+
const ESCAPED = /\\([\\`*_{}[\]()#+\-.!>~|])/g;
|
|
37
|
+
|
|
38
|
+
/** The editor's serialiser backslash-escapes Markdown punctuation in literal text (`\#`, `\*`). */
|
|
39
|
+
function unescapeMarkdown(text: string): string {
|
|
40
|
+
return text.replace(ESCAPED, "$1");
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Only hrefs safe to hand a browser; anything else renders as plain text. */
|
|
44
|
+
export function safeHref(href: string): string | null {
|
|
45
|
+
if (href.startsWith("/")) {
|
|
46
|
+
// "Internal" must mean same-origin after every normalisation a browser
|
|
47
|
+
// applies — `//evil.com` is protocol-relative and `/\evil.com` becomes
|
|
48
|
+
// `https://evil.com/` because browsers treat `\` as `/` in special-scheme
|
|
49
|
+
// URLs. Resolving against a sentinel origin catches all of these at once.
|
|
50
|
+
try {
|
|
51
|
+
return new URL(href, "https://internal.invalid").origin === "https://internal.invalid" ? href : null;
|
|
52
|
+
} catch {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
try {
|
|
57
|
+
return ["http:", "https:", "mailto:"].includes(new URL(href).protocol) ? href : null;
|
|
58
|
+
} catch {
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** An image address that may be shown: a path on this site, never another origin. */
|
|
64
|
+
export function imageSrc(src: string): string | null {
|
|
65
|
+
const safe = safeHref(src);
|
|
66
|
+
return safe?.startsWith("/") ? safe : null;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function renderInline(text: string, keyPrefix: string): ReactNode[] {
|
|
70
|
+
const nodes: ReactNode[] = [];
|
|
71
|
+
let last = 0;
|
|
72
|
+
let n = 0;
|
|
73
|
+
INLINE.lastIndex = 0;
|
|
74
|
+
|
|
75
|
+
for (const match of text.matchAll(INLINE)) {
|
|
76
|
+
const index = match.index ?? 0;
|
|
77
|
+
if (index > last) nodes.push(unescapeMarkdown(text.slice(last, index)));
|
|
78
|
+
const key = `${keyPrefix}-${n++}`;
|
|
79
|
+
|
|
80
|
+
if (match[1]) {
|
|
81
|
+
nodes.push(<code key={key}>{match[2]}</code>);
|
|
82
|
+
} else if (match[3]) {
|
|
83
|
+
const href = safeHref(match[5] ?? "");
|
|
84
|
+
const label = renderInline(match[4] ?? "", key);
|
|
85
|
+
if (!href) {
|
|
86
|
+
nodes.push(<span key={key}>{label}</span>);
|
|
87
|
+
} else if (href.startsWith("/")) {
|
|
88
|
+
// Internal — an `@`-reference from the editor, or a hand-written app
|
|
89
|
+
// link. Marked so the navigation trail carries the way back across the
|
|
90
|
+
// jump: the trail provider listens for clicks on the attribute, which
|
|
91
|
+
// is what lets this renderer stay hook-free.
|
|
92
|
+
nodes.push(
|
|
93
|
+
<Link key={key} href={href} data-trail-carry="">
|
|
94
|
+
{label}
|
|
95
|
+
</Link>,
|
|
96
|
+
);
|
|
97
|
+
} else {
|
|
98
|
+
nodes.push(
|
|
99
|
+
<a key={key} href={href} target="_blank" rel="noopener noreferrer">
|
|
100
|
+
{label}
|
|
101
|
+
</a>,
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
} else if (match[6]) {
|
|
105
|
+
nodes.push(<strong key={key}>{renderInline(match[7] ?? "", key)}</strong>);
|
|
106
|
+
} else if (match[8]) {
|
|
107
|
+
nodes.push(<s key={key}>{renderInline(match[9] ?? "", key)}</s>);
|
|
108
|
+
} else if (match[10]) {
|
|
109
|
+
nodes.push(<em key={key}>{renderInline(match[11] ?? "", key)}</em>);
|
|
110
|
+
} else if (match[12]) {
|
|
111
|
+
nodes.push(<em key={key}>{renderInline(match[13] ?? "", key)}</em>);
|
|
112
|
+
} else if (match[14]) {
|
|
113
|
+
nodes.push(
|
|
114
|
+
<span key={key} className="house-hashtag">
|
|
115
|
+
#{match[15]}
|
|
116
|
+
</span>,
|
|
117
|
+
);
|
|
118
|
+
} else if (match[16]) {
|
|
119
|
+
// Images only from this site: an outside address would tell its owner who read the note, and when.
|
|
120
|
+
const src = imageSrc(match[18] ?? "");
|
|
121
|
+
const alt = unescapeMarkdown(match[17] ?? "");
|
|
122
|
+
nodes.push(
|
|
123
|
+
src ? (
|
|
124
|
+
// The app's own, access-checked image route: a plain <img>, not a framework image optimiser.
|
|
125
|
+
<img key={key} src={src} alt={alt} loading="lazy" className="house-image" />
|
|
126
|
+
) : (
|
|
127
|
+
<span key={key}>{alt ? `[image: ${alt}]` : "[image]"}</span>
|
|
128
|
+
),
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
last = index + match[0].length;
|
|
132
|
+
}
|
|
133
|
+
if (last < text.length) nodes.push(unescapeMarkdown(text.slice(last)));
|
|
134
|
+
return nodes;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** Paragraph text where a bare newline is a visible break. */
|
|
138
|
+
function renderWithBreaks(lines: string[], keyPrefix: string): ReactNode[] {
|
|
139
|
+
return lines.flatMap((line, index) => {
|
|
140
|
+
const rendered = renderInline(line, `${keyPrefix}-l${index}`);
|
|
141
|
+
return index === 0 ? rendered : [<br key={`${keyPrefix}-b${index}`} />, ...rendered];
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/* ── Lists ────────────────────────────────────────────────────────────── */
|
|
146
|
+
|
|
147
|
+
interface ListLine {
|
|
148
|
+
indent: number;
|
|
149
|
+
ordered: boolean;
|
|
150
|
+
/** null = not a task item; otherwise its checked state. */
|
|
151
|
+
task: boolean | null;
|
|
152
|
+
text: string;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const LIST_LINE = /^(\s*)(?:([-*+])|(\d+)[.)])\s+(?:\[([ xX])\]\s+)?(.*)$/;
|
|
156
|
+
|
|
157
|
+
function parseListLine(line: string): ListLine | null {
|
|
158
|
+
const match = line.match(LIST_LINE);
|
|
159
|
+
if (!match) return null;
|
|
160
|
+
return {
|
|
161
|
+
indent: match[1]?.length ?? 0,
|
|
162
|
+
ordered: match[3] !== undefined,
|
|
163
|
+
task: match[4] === undefined ? null : match[4] !== " ",
|
|
164
|
+
text: match[5] ?? "",
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** Build a (possibly nested) list from consecutive list lines. */
|
|
169
|
+
function renderList(items: ListLine[], keyPrefix: string): ReactNode {
|
|
170
|
+
const render = (from: number, to: number, key: string): ReactNode => {
|
|
171
|
+
const first = items[from];
|
|
172
|
+
if (!first) return null;
|
|
173
|
+
const rows: ReactNode[] = [];
|
|
174
|
+
let i = from;
|
|
175
|
+
let n = 0;
|
|
176
|
+
|
|
177
|
+
while (i < to) {
|
|
178
|
+
const item = items[i];
|
|
179
|
+
if (!item) break;
|
|
180
|
+
// Deeper lines nest under the previous row.
|
|
181
|
+
let childEnd = i + 1;
|
|
182
|
+
while (childEnd < to && (items[childEnd]?.indent ?? 0) > first.indent) childEnd++;
|
|
183
|
+
const children = childEnd > i + 1 ? render(i + 1, childEnd, `${key}-c${n}`) : null;
|
|
184
|
+
|
|
185
|
+
rows.push(
|
|
186
|
+
item.task !== null ? (
|
|
187
|
+
<li key={n} className={cx(item.task && "house-task-done")}>
|
|
188
|
+
<input type="checkbox" checked={item.task} readOnly disabled aria-label={item.text} />
|
|
189
|
+
<span>
|
|
190
|
+
{renderInline(item.text, `${key}-i${n}`)}
|
|
191
|
+
{children}
|
|
192
|
+
</span>
|
|
193
|
+
</li>
|
|
194
|
+
) : (
|
|
195
|
+
<li key={n}>
|
|
196
|
+
{renderInline(item.text, `${key}-i${n}`)}
|
|
197
|
+
{children}
|
|
198
|
+
</li>
|
|
199
|
+
),
|
|
200
|
+
);
|
|
201
|
+
i = childEnd;
|
|
202
|
+
n++;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
if (first.task !== null) {
|
|
206
|
+
return (
|
|
207
|
+
<ul key={key} className="house-task-list">
|
|
208
|
+
{rows}
|
|
209
|
+
</ul>
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
return first.ordered ? <ol key={key}>{rows}</ol> : <ul key={key}>{rows}</ul>;
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
return render(0, items.length, keyPrefix);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/* ── Blocks ───────────────────────────────────────────────────────────── */
|
|
219
|
+
|
|
220
|
+
const RULE = /^(-{3,}|\*{3,}|_{3,})$/;
|
|
221
|
+
const HEADING = /^(#{1,6})\s+(.*)$/;
|
|
222
|
+
|
|
223
|
+
function renderBlocks(content: string): ReactNode[] {
|
|
224
|
+
const lines = content.replace(/\r\n/g, "\n").split("\n");
|
|
225
|
+
const at = (index: number) => lines[index] ?? "";
|
|
226
|
+
const blocks: ReactNode[] = [];
|
|
227
|
+
let i = 0;
|
|
228
|
+
let key = 0;
|
|
229
|
+
|
|
230
|
+
while (i < lines.length) {
|
|
231
|
+
const line = at(i);
|
|
232
|
+
const trimmed = line.trim();
|
|
233
|
+
|
|
234
|
+
if (trimmed === "") {
|
|
235
|
+
i++;
|
|
236
|
+
continue;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// Code fence — verbatim until the closing fence (or the end).
|
|
240
|
+
if (trimmed.startsWith("```")) {
|
|
241
|
+
const body: string[] = [];
|
|
242
|
+
i++;
|
|
243
|
+
while (i < lines.length && !at(i).trim().startsWith("```")) {
|
|
244
|
+
body.push(at(i));
|
|
245
|
+
i++;
|
|
246
|
+
}
|
|
247
|
+
i++; // closing fence
|
|
248
|
+
blocks.push(
|
|
249
|
+
<pre key={key++}>
|
|
250
|
+
<code>{body.join("\n")}</code>
|
|
251
|
+
</pre>,
|
|
252
|
+
);
|
|
253
|
+
continue;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
if (RULE.test(trimmed)) {
|
|
257
|
+
blocks.push(<hr key={key++} />);
|
|
258
|
+
i++;
|
|
259
|
+
continue;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
const heading = trimmed.match(HEADING);
|
|
263
|
+
if (heading) {
|
|
264
|
+
const level = Math.min(heading[1]?.length ?? 1, 3);
|
|
265
|
+
const nodes = renderInline(heading[2] ?? "", `h${key}`);
|
|
266
|
+
blocks.push(
|
|
267
|
+
level === 1 ? (
|
|
268
|
+
<h1 key={key++}>{nodes}</h1>
|
|
269
|
+
) : level === 2 ? (
|
|
270
|
+
<h2 key={key++}>{nodes}</h2>
|
|
271
|
+
) : (
|
|
272
|
+
<h3 key={key++}>{nodes}</h3>
|
|
273
|
+
),
|
|
274
|
+
);
|
|
275
|
+
i++;
|
|
276
|
+
continue;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
if (trimmed.startsWith(">")) {
|
|
280
|
+
const quote: string[] = [];
|
|
281
|
+
while (i < lines.length && at(i).trim().startsWith(">")) {
|
|
282
|
+
quote.push(at(i).trim().replace(/^>\s?/, ""));
|
|
283
|
+
i++;
|
|
284
|
+
}
|
|
285
|
+
blocks.push(<blockquote key={key++}>{renderWithBreaks(quote, `q${key}`)}</blockquote>);
|
|
286
|
+
continue;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
if (parseListLine(line)) {
|
|
290
|
+
const items: ListLine[] = [];
|
|
291
|
+
while (i < lines.length) {
|
|
292
|
+
const item = parseListLine(at(i));
|
|
293
|
+
if (!item) break;
|
|
294
|
+
items.push(item);
|
|
295
|
+
i++;
|
|
296
|
+
}
|
|
297
|
+
blocks.push(<div key={key++}>{renderList(items, `list${key}`)}</div>);
|
|
298
|
+
continue;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// Paragraph — consecutive plain lines, each newline a visible break.
|
|
302
|
+
const para: string[] = [];
|
|
303
|
+
while (i < lines.length) {
|
|
304
|
+
const next = at(i);
|
|
305
|
+
const t = next.trim();
|
|
306
|
+
if (
|
|
307
|
+
t === "" ||
|
|
308
|
+
t.startsWith("```") ||
|
|
309
|
+
t.startsWith(">") ||
|
|
310
|
+
HEADING.test(t) ||
|
|
311
|
+
RULE.test(t) ||
|
|
312
|
+
LIST_LINE.test(next)
|
|
313
|
+
) {
|
|
314
|
+
break;
|
|
315
|
+
}
|
|
316
|
+
para.push(next);
|
|
317
|
+
i++;
|
|
318
|
+
}
|
|
319
|
+
blocks.push(<p key={key++}>{renderWithBreaks(para, `p${key}`)}</p>);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
return blocks;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* A plain-text preview of a Markdown body — for list cards and search rows.
|
|
327
|
+
* Strips block and inline syntax rather than rendering it.
|
|
328
|
+
*/
|
|
329
|
+
export function richTextPreview(content: string, max = 160): string {
|
|
330
|
+
// A preview needs the start of the note, not all 100 KB of it.
|
|
331
|
+
const text = content
|
|
332
|
+
.slice(0, Math.max(2000, max * 12))
|
|
333
|
+
.replace(/```[\s\S]*?(```|$)/g, " ")
|
|
334
|
+
.replace(/^#{1,6}\s+/gm, "")
|
|
335
|
+
.replace(/^\s*(?:[-*+]|\d+[.)])\s+(?:\[[ xX]\]\s+)?/gm, "")
|
|
336
|
+
.replace(/^>\s?/gm, "")
|
|
337
|
+
.replace(/\[([^[\]\n]{1,500})\]\([^)\s]{1,2048}\)/g, "$1")
|
|
338
|
+
.replace(/(\*\*|~~|`|\*|_)/g, "")
|
|
339
|
+
.replace(ESCAPED, "$1")
|
|
340
|
+
.replace(/\s+/g, " ")
|
|
341
|
+
.trim();
|
|
342
|
+
return text.length > max ? `${text.slice(0, max - 1)}…` : text;
|
|
343
|
+
}
|
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { Image } from "@tiptap/extension-image";
|
|
4
|
+
import { TaskItem } from "@tiptap/extension-task-item";
|
|
5
|
+
import { TaskList } from "@tiptap/extension-task-list";
|
|
6
|
+
import { Placeholder } from "@tiptap/extensions";
|
|
7
|
+
import type { EditorView } from "@tiptap/pm/view";
|
|
8
|
+
import { EditorContent, useEditor } from "@tiptap/react";
|
|
9
|
+
import { StarterKit } from "@tiptap/starter-kit";
|
|
10
|
+
import { useRouter } from "next/navigation.js";
|
|
11
|
+
import { useEffect, useMemo, useRef, useState, type MouseEvent } from "react";
|
|
12
|
+
import { Markdown, type MarkdownStorage } from "tiptap-markdown";
|
|
13
|
+
|
|
14
|
+
import { cx } from "../internal/cx.js";
|
|
15
|
+
import { carryTrail } from "../trail/trail.js";
|
|
16
|
+
import { BlockHandle } from "./block-handle.js";
|
|
17
|
+
import { BubbleToolbar } from "./bubble-toolbar.js";
|
|
18
|
+
import { Collapsible } from "./collapsible.js";
|
|
19
|
+
import { imageSrc } from "./content.js";
|
|
20
|
+
import { SuggestionPopup } from "./suggestion-popup.js";
|
|
21
|
+
import {
|
|
22
|
+
createSuggestions,
|
|
23
|
+
DEFAULT_SUGGESTION_OPTIONS,
|
|
24
|
+
type SuggestionItem,
|
|
25
|
+
type SuggestionOptions,
|
|
26
|
+
type SuggestionSession,
|
|
27
|
+
type SuggestionSources,
|
|
28
|
+
type SuggestionUiHandle,
|
|
29
|
+
} from "./suggestions.js";
|
|
30
|
+
|
|
31
|
+
/** An image block that draws only addresses on this site; anything else shows as its description. */
|
|
32
|
+
const SameSiteImage = Image.extend({
|
|
33
|
+
renderHTML({ HTMLAttributes }) {
|
|
34
|
+
const src = imageSrc(String(HTMLAttributes.src ?? ""));
|
|
35
|
+
const alt = String(HTMLAttributes.alt ?? "");
|
|
36
|
+
return src
|
|
37
|
+
? ["img", { src, alt, class: "house-image", loading: "lazy", draggable: "false" }]
|
|
38
|
+
: ["span", { class: "house-image-blocked" }, alt ? `[image: ${alt}]` : "[image]"];
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Notion-style writing anywhere an app asks for prose: notes, descriptions,
|
|
44
|
+
* instructions, feedback.
|
|
45
|
+
*
|
|
46
|
+
* **Blocks, not a toolbar.** There is no standing formatting bar. Formatting
|
|
47
|
+
* appears as a bubble over a text selection (with Turn into); new blocks come
|
|
48
|
+
* from typing `/` — a filterable menu — or from the `+` beside any block; every
|
|
49
|
+
* block has a grip to drag it elsewhere or open its menu (turn into, duplicate,
|
|
50
|
+
* move up, move down, delete); a heading carries everything under it when it is
|
|
51
|
+
* dragged or moved, and folds it away with the chevron beside it (sections.ts,
|
|
52
|
+
* collapsible.ts); `:` completes an emoji shortcode. Images can be pasted or
|
|
53
|
+
* dropped in when the app passes `onImageFiles`, which stores them and says
|
|
54
|
+
* where each is shown — only addresses on the app's own site are ever drawn. `@` references
|
|
55
|
+
* and `#` tags work when the app passes `sources`. Markdown shortcuts still work
|
|
56
|
+
* while typing (`# `, `- `, `1. `, `[] `, `> `, `**bold**`, "```").
|
|
57
|
+
*
|
|
58
|
+
* It reads and writes **Markdown strings**, deliberately: the fields it replaces
|
|
59
|
+
* are plain-text columns, Markdown needs no sanitiser because nothing HTML-shaped
|
|
60
|
+
* is stored or rendered, and old plain-text values open unchanged (a bare
|
|
61
|
+
* newline stays a line break). That is also why the block vocabulary is exactly
|
|
62
|
+
* what Markdown can say. Read-only display is `RichTextContent`.
|
|
63
|
+
*/
|
|
64
|
+
export interface RichTextEditorProps {
|
|
65
|
+
value: string;
|
|
66
|
+
onChange: (markdown: string) => void;
|
|
67
|
+
onBlur?: () => void;
|
|
68
|
+
placeholder?: string;
|
|
69
|
+
/**
|
|
70
|
+
* "field" sits inside a form like a big textarea (bubble and `/`, no gutter);
|
|
71
|
+
* "document" is a full-page editor (block grips, wider gutter, taller).
|
|
72
|
+
*/
|
|
73
|
+
variant?: "field" | "document";
|
|
74
|
+
disabled?: boolean;
|
|
75
|
+
autoFocus?: boolean;
|
|
76
|
+
/** Where `@` and `#` get their rows. Omit either and that trigger stays inert. */
|
|
77
|
+
sources?: SuggestionSources;
|
|
78
|
+
/**
|
|
79
|
+
* Pasted or dropped image files: store them, and say where each is shown — a path on this site
|
|
80
|
+
* (`/api/…/image`). Omit it and images can't be pasted. Resolve to fewer entries (or none) for files
|
|
81
|
+
* that couldn't be kept; the app tells the person why.
|
|
82
|
+
*/
|
|
83
|
+
onImageFiles?: (files: File[]) => Promise<{ src: string; alt: string }[]>;
|
|
84
|
+
/**
|
|
85
|
+
* How a link in the text is followed. "click": an `@`-reference opens on a
|
|
86
|
+
* plain click, as in Notion — for a page that autosaves. "modifier" (the
|
|
87
|
+
* default): only Ctrl/⌘-click follows, so a form with unsaved fields is never
|
|
88
|
+
* left by a stray click on a mention. Any link opens with Ctrl/⌘ — in a new tab.
|
|
89
|
+
*/
|
|
90
|
+
followLinks?: "click" | "modifier";
|
|
91
|
+
/** Which internal links count as references for `followLinks="click"`. Defaults to an `@` label. */
|
|
92
|
+
isReference?: (href: string, text: string) => boolean;
|
|
93
|
+
/** How today's date is written by the `/` menu. */
|
|
94
|
+
formatDate?: (date: Date) => string;
|
|
95
|
+
id?: string;
|
|
96
|
+
className?: string;
|
|
97
|
+
"aria-invalid"?: boolean;
|
|
98
|
+
"aria-describedby"?: string;
|
|
99
|
+
"aria-label"?: string;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const defaultIsReference = (_href: string, text: string) => text.startsWith("@");
|
|
103
|
+
|
|
104
|
+
export function RichTextEditor({
|
|
105
|
+
value,
|
|
106
|
+
onChange,
|
|
107
|
+
onBlur,
|
|
108
|
+
placeholder = "Write something…",
|
|
109
|
+
variant = "field",
|
|
110
|
+
disabled = false,
|
|
111
|
+
autoFocus = false,
|
|
112
|
+
sources,
|
|
113
|
+
onImageFiles,
|
|
114
|
+
followLinks = "modifier",
|
|
115
|
+
isReference = defaultIsReference,
|
|
116
|
+
formatDate,
|
|
117
|
+
id,
|
|
118
|
+
className,
|
|
119
|
+
...aria
|
|
120
|
+
}: RichTextEditorProps) {
|
|
121
|
+
const router = useRouter();
|
|
122
|
+
// The last Markdown this editor emitted (or accepted). Guards the external
|
|
123
|
+
// value sync below from re-parsing the document on every keystroke echo.
|
|
124
|
+
const lastMarkdown = useRef(value);
|
|
125
|
+
|
|
126
|
+
const [session, setSession] = useState<SuggestionSession | null>(null);
|
|
127
|
+
const [activeIndex, setActiveIndex] = useState(0);
|
|
128
|
+
|
|
129
|
+
// The extension is built once per editor and reads these through refs, which
|
|
130
|
+
// an every-render effect keeps pointed at the current render's values.
|
|
131
|
+
const uiRef = useRef<SuggestionUiHandle>({ onOpen: () => {}, onClose: () => {}, onKeyDown: () => false });
|
|
132
|
+
const sourcesRef = useRef<SuggestionSources>(sources ?? {});
|
|
133
|
+
const optionsRef = useRef<SuggestionOptions>({
|
|
134
|
+
...DEFAULT_SUGGESTION_OPTIONS,
|
|
135
|
+
...(formatDate ? { formatDate } : {}),
|
|
136
|
+
});
|
|
137
|
+
const placeholderRef = useRef(placeholder);
|
|
138
|
+
const imageFilesRef = useRef(onImageFiles);
|
|
139
|
+
useEffect(() => {
|
|
140
|
+
imageFilesRef.current = onImageFiles;
|
|
141
|
+
sourcesRef.current = sources ?? {};
|
|
142
|
+
optionsRef.current = { ...DEFAULT_SUGGESTION_OPTIONS, ...(formatDate ? { formatDate } : {}) };
|
|
143
|
+
placeholderRef.current = placeholder;
|
|
144
|
+
uiRef.current = {
|
|
145
|
+
onOpen: (next) => {
|
|
146
|
+
setSession(next);
|
|
147
|
+
setActiveIndex((current) => Math.min(current, Math.max(0, next.items.length - 1)));
|
|
148
|
+
},
|
|
149
|
+
onClose: () => {
|
|
150
|
+
setSession(null);
|
|
151
|
+
setActiveIndex(0);
|
|
152
|
+
},
|
|
153
|
+
onKeyDown: (event) => {
|
|
154
|
+
if (!session || session.items.length === 0) return false;
|
|
155
|
+
if (event.key === "ArrowDown") {
|
|
156
|
+
setActiveIndex((activeIndex + 1) % session.items.length);
|
|
157
|
+
return true;
|
|
158
|
+
}
|
|
159
|
+
if (event.key === "ArrowUp") {
|
|
160
|
+
setActiveIndex((activeIndex - 1 + session.items.length) % session.items.length);
|
|
161
|
+
return true;
|
|
162
|
+
}
|
|
163
|
+
if (event.key === "Enter" || event.key === "Tab") {
|
|
164
|
+
const item = session.items[activeIndex];
|
|
165
|
+
if (item) session.command(item);
|
|
166
|
+
return true;
|
|
167
|
+
}
|
|
168
|
+
if (event.key === "Escape") {
|
|
169
|
+
setSession(null);
|
|
170
|
+
return true;
|
|
171
|
+
}
|
|
172
|
+
return false;
|
|
173
|
+
},
|
|
174
|
+
};
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
const extensions = useMemo(
|
|
178
|
+
() => [
|
|
179
|
+
StarterKit.configure({
|
|
180
|
+
heading: { levels: [1, 2, 3] },
|
|
181
|
+
link: { openOnClick: false, autolink: true },
|
|
182
|
+
dropcursor: { color: "var(--house-primary, var(--primary))", width: 2 },
|
|
183
|
+
}),
|
|
184
|
+
TaskList,
|
|
185
|
+
TaskItem.configure({ nested: true }),
|
|
186
|
+
Placeholder.configure({
|
|
187
|
+
// The empty document says what the field is for; an empty block in a
|
|
188
|
+
// document that has content says how to get a block — the Notion hint.
|
|
189
|
+
placeholder: ({ editor: current, node }) => {
|
|
190
|
+
if (current.isEmpty) return placeholderRef.current;
|
|
191
|
+
if (variant === "document" && node.type.name === "paragraph") return "Type / for blocks";
|
|
192
|
+
return "";
|
|
193
|
+
},
|
|
194
|
+
}),
|
|
195
|
+
Markdown.configure({
|
|
196
|
+
// No HTML in, no HTML out: pasted markup is flattened to text, so the
|
|
197
|
+
// stored string can never smuggle an element past a renderer.
|
|
198
|
+
html: false,
|
|
199
|
+
// A bare newline stays a visible line break, which is what every legacy
|
|
200
|
+
// plain-text value in these columns meant by one.
|
|
201
|
+
breaks: true,
|
|
202
|
+
linkify: true,
|
|
203
|
+
transformPastedText: true,
|
|
204
|
+
}),
|
|
205
|
+
createSuggestions(uiRef, sourcesRef, optionsRef),
|
|
206
|
+
Collapsible,
|
|
207
|
+
SameSiteImage.configure({ inline: false, allowBase64: false }),
|
|
208
|
+
],
|
|
209
|
+
// The extension set is per mount; everything that changes is read through a ref.
|
|
210
|
+
[],
|
|
211
|
+
);
|
|
212
|
+
|
|
213
|
+
/** Hand image files to the app; once stored, put an image block for each where they were pasted or dropped. */
|
|
214
|
+
const takeImages = (view: EditorView, files: File[], at?: number): boolean => {
|
|
215
|
+
const store = imageFilesRef.current;
|
|
216
|
+
const images = files.filter((file) => file.type.startsWith("image/"));
|
|
217
|
+
if (!store || images.length === 0) return false;
|
|
218
|
+
const mark = at ?? view.state.selection.to;
|
|
219
|
+
const startDoc = view.state.doc;
|
|
220
|
+
void store(images).then((stored) => {
|
|
221
|
+
if (view.isDestroyed) return;
|
|
222
|
+
const type = view.state.schema.nodes.image;
|
|
223
|
+
if (!type) return;
|
|
224
|
+
// Wherever that spot has moved to while the files were being stored.
|
|
225
|
+
let tr = view.state.tr;
|
|
226
|
+
let pos = view.state.doc === startDoc ? mark : Math.min(mark, view.state.doc.content.size);
|
|
227
|
+
for (const image of stored) {
|
|
228
|
+
const src = imageSrc(image.src);
|
|
229
|
+
if (!src) continue;
|
|
230
|
+
tr = tr.replaceRangeWith(pos, pos, type.create({ src, alt: image.alt }));
|
|
231
|
+
pos = tr.mapping.map(pos);
|
|
232
|
+
}
|
|
233
|
+
if (tr.docChanged) view.dispatch(tr);
|
|
234
|
+
});
|
|
235
|
+
return true;
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
const editor = useEditor({
|
|
239
|
+
extensions,
|
|
240
|
+
content: value,
|
|
241
|
+
editable: !disabled,
|
|
242
|
+
autofocus: autoFocus ? "end" : false,
|
|
243
|
+
// The editor renders client-side only; the server markup comes from the page.
|
|
244
|
+
immediatelyRender: false,
|
|
245
|
+
editorProps: {
|
|
246
|
+
// Image files pasted or dropped in go to the app, which stores them; anything else is handled as usual.
|
|
247
|
+
handlePaste: (view, event) => takeImages(view, Array.from(event.clipboardData?.files ?? [])),
|
|
248
|
+
handleDrop: (view, event, _slice, moved) => {
|
|
249
|
+
if (moved) return false;
|
|
250
|
+
const at = view.posAtCoords({ left: event.clientX, top: event.clientY })?.pos;
|
|
251
|
+
return takeImages(view, Array.from(event.dataTransfer?.files ?? []), at);
|
|
252
|
+
},
|
|
253
|
+
},
|
|
254
|
+
onUpdate: ({ editor: current }) => {
|
|
255
|
+
// tiptap-markdown registers no Storage augmentation, hence the cast.
|
|
256
|
+
const markdown = (current.storage as unknown as { markdown: MarkdownStorage }).markdown.getMarkdown();
|
|
257
|
+
lastMarkdown.current = markdown;
|
|
258
|
+
onChange(markdown);
|
|
259
|
+
},
|
|
260
|
+
onBlur: () => onBlur?.(),
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
// External resets (a form reset, switching records) re-seed the document; our
|
|
264
|
+
// own keystrokes come back identical and are skipped.
|
|
265
|
+
useEffect(() => {
|
|
266
|
+
if (!editor || value === lastMarkdown.current) return;
|
|
267
|
+
lastMarkdown.current = value;
|
|
268
|
+
editor.commands.setContent(value, { emitUpdate: false });
|
|
269
|
+
}, [editor, value]);
|
|
270
|
+
|
|
271
|
+
// Only on a real change, and silently: `setEditable` emits an update by
|
|
272
|
+
// default, which reported a change on every mount — a form turned dirty and
|
|
273
|
+
// an autosave fired the moment a note was opened, before anyone typed.
|
|
274
|
+
useEffect(() => {
|
|
275
|
+
if (editor && editor.isEditable === disabled) editor.setEditable(!disabled, false);
|
|
276
|
+
}, [editor, disabled]);
|
|
277
|
+
|
|
278
|
+
const select = (item: SuggestionItem) => session?.command(item);
|
|
279
|
+
|
|
280
|
+
/** Follow a link in the text — see `followLinks`. Internal ones carry the trail. */
|
|
281
|
+
const onClickCapture = (event: MouseEvent<HTMLDivElement>) => {
|
|
282
|
+
const anchor = event.target instanceof Element ? event.target.closest("a[href]") : null;
|
|
283
|
+
if (!anchor) return;
|
|
284
|
+
const href = anchor.getAttribute("href") ?? "";
|
|
285
|
+
const modifier = event.metaKey || event.ctrlKey;
|
|
286
|
+
const internal = href.startsWith("/") && !href.startsWith("//") && !href.startsWith("/\\");
|
|
287
|
+
const reference = internal && isReference(href, anchor.textContent ?? "");
|
|
288
|
+
if (!modifier && !(followLinks === "click" && reference)) return;
|
|
289
|
+
|
|
290
|
+
event.preventDefault();
|
|
291
|
+
event.stopPropagation();
|
|
292
|
+
if (internal && !modifier) {
|
|
293
|
+
carryTrail();
|
|
294
|
+
router.push(href);
|
|
295
|
+
} else if (internal) {
|
|
296
|
+
// Ctrl/⌘-click means "somewhere else", as everywhere on the web: a new tab, so a form with unsaved fields
|
|
297
|
+
// isn't left behind without its "Leave without saving?".
|
|
298
|
+
window.open(href, "_blank", "noopener");
|
|
299
|
+
} else if (/^https?:\/\//i.test(href)) {
|
|
300
|
+
window.open(href, "_blank", "noopener,noreferrer");
|
|
301
|
+
}
|
|
302
|
+
};
|
|
303
|
+
|
|
304
|
+
return (
|
|
305
|
+
<div
|
|
306
|
+
id={id}
|
|
307
|
+
{...aria}
|
|
308
|
+
onClickCapture={onClickCapture}
|
|
309
|
+
className={cx(
|
|
310
|
+
"house-rich-text relative text-sm",
|
|
311
|
+
variant === "field" &&
|
|
312
|
+
"rounded-md border border-input bg-background shadow-xs focus-within:ring-2 focus-within:ring-ring focus-within:ring-offset-2",
|
|
313
|
+
variant === "document" && "house-rich-text--document",
|
|
314
|
+
aria["aria-invalid"] && "border-destructive",
|
|
315
|
+
disabled && "opacity-60",
|
|
316
|
+
className,
|
|
317
|
+
)}
|
|
318
|
+
>
|
|
319
|
+
<EditorContent editor={editor} className="house-prose" />
|
|
320
|
+
{editor && !disabled ? <BubbleToolbar editor={editor} /> : null}
|
|
321
|
+
{editor && !disabled && variant === "document" ? <BlockHandle editor={editor} /> : null}
|
|
322
|
+
{session ? (
|
|
323
|
+
<SuggestionPopup
|
|
324
|
+
items={session.items}
|
|
325
|
+
activeIndex={activeIndex}
|
|
326
|
+
clientRect={session.clientRect}
|
|
327
|
+
onSelect={select}
|
|
328
|
+
onHover={setActiveIndex}
|
|
329
|
+
/>
|
|
330
|
+
) : null}
|
|
331
|
+
</div>
|
|
332
|
+
);
|
|
333
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { EMOJI, EMOJI_GROUP_LABELS, searchEmoji, type EmojiEntry, type EmojiGroup } from "./emoji.js";
|