@jant/core 1.2.0 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/commands/site/import.js +2 -1
- package/dist/{app-CbOZ2Irh.js → app-BuzORSgm.js} +1441 -686
- package/dist/client/.vite/manifest.json +72 -83
- package/dist/client/_assets/chunks/{confirm-CMe01lne.js → confirm-BQebcuNB.js} +1 -1
- package/dist/client/_assets/chunks/{create-editor-hWapNE40.js → create-editor-B8KuszlS.js} +38 -38
- package/dist/client/_assets/chunks/footnote-rail-DUrqz0Py.js +2 -0
- package/dist/client/_assets/chunks/{jant-locale-picker-2L4rkDaz.js → jant-locale-picker-CLq0iKDH.js} +1 -1
- package/dist/client/_assets/chunks/lit-BN4mrI3j.js +2 -0
- package/dist/client/_assets/chunks/{settings-paths-BBVT8zKj.js → settings-paths-_fry4yWW.js} +1 -1
- package/dist/client/_assets/chunks/{slug-format-BuCPjSrB.js → slug-format-bcJ2ozr4.js} +1 -1
- package/dist/client/_assets/chunks/{sortable-list-DJXwkaOv.js → sortable-list-Bz2Na2E9.js} +2 -2
- package/dist/client/_assets/chunks/{unsafe-html-CWos3v6N.js → unsafe-html-OJ9m154w.js} +1 -1
- package/dist/client/_assets/chunks/unsafe-svg-DVJ4NPcb.js +1 -0
- package/dist/client/_assets/client-CStDm2xD.js +354 -0
- package/dist/client/_assets/{client-D8Rrsd73.css → client-CoNHvipV.css} +1 -1
- package/dist/client/_assets/{client-auth-CsUGM718.js → client-auth-D53R-Wby.js} +99 -129
- package/dist/client/_assets/client-author-2WYBkgni.css +2 -0
- package/dist/client/_assets/{client-compose-BB4U7Wz0.js → client-compose-DYtDlYm-.js} +248 -244
- package/dist/client/_assets/{client-manage-CI9-eh-8.js → client-manage-BODyYP5Z.js} +157 -157
- package/dist/client/_assets/{client-settings-BnGPuqwc.js → client-settings-DDpe_tXJ.js} +166 -166
- package/dist/client/_assets/client-share-B9OsXx9p.js +149 -0
- package/dist/{constants-DEgyqWDl.js → constants-C2CdWAqt.js} +55 -5
- package/dist/index.js +1 -1
- package/dist/node.js +2 -2
- package/package.json +2 -1
- package/src/client/components/compose-types.ts +5 -1
- package/src/client/components/jant-compose-dialog.ts +25 -4
- package/src/client/components/jant-media-lightbox.ts +18 -3
- package/src/client/components/jant-nav-manager.ts +2 -0
- package/src/client/components/jant-post-menu.ts +42 -89
- package/src/client/components/nav-manager-types.ts +2 -0
- package/src/client/components/settings-types.ts +1 -1
- package/src/client/list-views.ts +382 -0
- package/src/client/media-lightbox-events.ts +12 -0
- package/src/client/runtime-paths.ts +12 -0
- package/src/client/share-capture.ts +63 -0
- package/src/client/share-launch.ts +36 -9
- package/src/client/tiptap/create-editor.ts +4 -0
- package/src/client/tiptap/embed-node.ts +3 -1
- package/src/client/tiptap/html-block-node.ts +3 -1
- package/src/client/tiptap/image-node.ts +3 -1
- package/src/client/view-confirm.ts +32 -4
- package/src/client.ts +1 -0
- package/src/db/migrations/0037_round_post.sql +16 -0
- package/src/db/migrations/0038_flashy_changeling.sql +15 -0
- package/src/db/migrations/0039_mixed_sister_grimm.sql +1 -0
- package/src/db/migrations/meta/0037_snapshot.json +2932 -0
- package/src/db/migrations/meta/0038_snapshot.json +2924 -0
- package/src/db/migrations/meta/0039_snapshot.json +2931 -0
- package/src/db/migrations/meta/_journal.json +21 -0
- package/src/db/migrations/pg/0035_flimsy_outlaw_kid.sql +3 -0
- package/src/db/migrations/pg/0036_milky_wolverine.sql +5 -0
- package/src/db/migrations/pg/0037_fuzzy_hammerhead.sql +1 -0
- package/src/db/migrations/pg/meta/0035_snapshot.json +3773 -0
- package/src/db/migrations/pg/meta/0036_snapshot.json +3766 -0
- package/src/db/migrations/pg/meta/0037_snapshot.json +3772 -0
- package/src/db/migrations/pg/meta/_journal.json +21 -0
- package/src/db/pg/schema.ts +1 -0
- package/src/db/post-visibility.ts +37 -6
- package/src/db/schema.ts +5 -2
- package/src/i18n/locales/glossary.zh-Hans.yml +4 -0
- package/src/i18n/locales/glossary.zh-Hant.yml +4 -0
- package/src/i18n/locales/public/en.po +21 -90
- package/src/i18n/locales/public/en.ts +1 -1
- package/src/i18n/locales/public/zh-Hans.po +20 -89
- package/src/i18n/locales/public/zh-Hans.ts +1 -1
- package/src/i18n/locales/public/zh-Hant.po +20 -89
- package/src/i18n/locales/public/zh-Hant.ts +1 -1
- package/src/i18n/locales/settings/en.po +84 -0
- package/src/i18n/locales/settings/en.ts +1 -1
- package/src/i18n/locales/settings/zh-Hans.po +130 -46
- package/src/i18n/locales/settings/zh-Hans.ts +1 -1
- package/src/i18n/locales/settings/zh-Hant.po +124 -40
- package/src/i18n/locales/settings/zh-Hant.ts +1 -1
- package/src/lib/api-public-posts.ts +10 -0
- package/src/lib/feed-policy.ts +2 -2
- package/src/lib/feed.ts +17 -1
- package/src/lib/filter-dimensions.ts +11 -4
- package/src/lib/hugo-markdown.ts +7 -0
- package/src/lib/icons.ts +2 -0
- package/src/lib/link-preview.ts +499 -0
- package/src/lib/list-views.ts +117 -0
- package/src/lib/markdown-manager.ts +40 -1
- package/src/lib/navigation.ts +1 -1
- package/src/lib/share-card.ts +4 -3
- package/src/lib/thread-query.ts +6 -2
- package/src/lib/timeline.ts +12 -11
- package/src/lib/url-fetch.ts +291 -74
- package/src/lib/view-buffer.ts +2 -2
- package/src/lib/view-counting.ts +7 -6
- package/src/lib/view-token.ts +29 -7
- package/src/lib/view.ts +17 -0
- package/src/middleware/view-counter.ts +10 -5
- package/src/preset.css +5 -3
- package/src/routes/api/search.ts +1 -1
- package/src/routes/dash/settings.tsx +1 -1
- package/src/routes/feed/sitemap.ts +1 -1
- package/src/routes/pages/archive.tsx +25 -19
- package/src/routes/pages/collection.tsx +3 -3
- package/src/routes/pages/page.tsx +16 -4
- package/src/routes/pages/search.tsx +3 -3
- package/src/routes/pages/subscribe.tsx +19 -3
- package/src/routes/view-confirm.ts +23 -15
- package/src/services/collection.ts +1 -1
- package/src/services/export-theme/assets/client-site.js +19 -19
- package/src/services/export-theme/layouts/archive/list.html +4 -2
- package/src/services/export-theme/layouts/partials/collection-members.html +4 -3
- package/src/services/export-theme/layouts/partials/featured-members.html +3 -3
- package/src/services/export-theme/layouts/partials/head.html +2 -0
- package/src/services/export-theme/layouts/partials/latest-members.html +4 -4
- package/src/services/export-theme/layouts/partials/post-card.html +1 -9
- package/src/services/export-theme/layouts/partials/reply.html +1 -9
- package/src/services/export-theme/layouts/partials/smart-collection-members.html +4 -3
- package/src/services/export-theme/layouts/post/list.html +1 -9
- package/src/services/export-theme/styles/main.css +2 -26
- package/src/services/export.ts +10 -3
- package/src/services/mcp.ts +5 -5
- package/src/services/navigation.ts +9 -4
- package/src/services/post.ts +102 -112
- package/src/services/search.ts +22 -7
- package/src/services/smart-collection.ts +3 -3
- package/src/services/stats.ts +12 -5
- package/src/services/thread.ts +6 -2
- package/src/styles/components-author.css +69 -7
- package/src/styles/tokens.css +28 -29
- package/src/styles/ui-author.css +140 -353
- package/src/styles/ui.css +34 -32
- package/src/types/constants.ts +68 -4
- package/src/types/entities.ts +1 -0
- package/src/types/views.ts +2 -0
- package/src/ui/compose/ComposeDialog.tsx +20 -1
- package/src/ui/dash/StatusBadge.tsx +11 -0
- package/src/ui/dash/appearance/FontThemeContent.tsx +1 -1
- package/src/ui/dash/appearance/NavigationContent.tsx +8 -0
- package/src/ui/dash/settings/StatsContent.tsx +29 -29
- package/src/ui/feed/LinkCard.tsx +19 -2
- package/src/ui/feed/PostStatusBadges.tsx +19 -3
- package/src/ui/feed/TimelineFeed.tsx +3 -0
- package/src/ui/font-themes.ts +32 -15
- package/src/ui/pages/ArchivePage.tsx +1 -0
- package/src/ui/pages/SubscribePage.tsx +9 -1
- package/src/ui/shared/PostFooter.tsx +1 -22
- package/src/ui/shared/custom-icons.ts +5 -5
- package/dist/client/_assets/chunks/footnote-rail-9zwl6jtV.js +0 -2
- package/dist/client/_assets/chunks/lit-Bw_voPoW.js +0 -2
- package/dist/client/_assets/chunks/runtime-paths-y0_gHowM.js +0 -1
- package/dist/client/_assets/chunks/unsafe-svg-CUrgo4Xg.js +0 -1
- package/dist/client/_assets/client-BO8GOFfT.js +0 -354
- package/dist/client/_assets/client-author-QPm1whbO.css +0 -2
- package/dist/client/_assets/client-share-C59mOLJ1.js +0 -149
package/src/lib/link-preview.ts
CHANGED
|
@@ -1,9 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Link previews: what a Link post's URL is, read from the page it points at.
|
|
3
|
+
*
|
|
4
|
+
* A preview is resolved once, when the post is saved, and stored with it. It
|
|
5
|
+
* has two parts. The site's icon stands in front of the domain on every Link
|
|
6
|
+
* post. A video page also gets its thumbnail, drawn with a play mark and,
|
|
7
|
+
* for a known provider, the provider's name.
|
|
8
|
+
*
|
|
9
|
+
* Whether a page is a video is read from the page itself — `og:type` of
|
|
10
|
+
* `video.*`, or an `og:video` player — so any site that declares it works
|
|
11
|
+
* without a rule of its own. YouTube is the one exception: its thumbnail
|
|
12
|
+
* follows from the video id, so youtube.com, whose watch pages are 1.4 MB with
|
|
13
|
+
* the tags 850 KB in and wall off datacenter addresses, is never fetched.
|
|
14
|
+
*
|
|
15
|
+
* Every fetch goes through `url-fetch.ts`, whose guard checks each redirect
|
|
16
|
+
* hop. Every image is checked by its signature, not its declared type, and
|
|
17
|
+
* only raster formats pass: a stored SVG would run script when opened
|
|
18
|
+
* directly, and a Worker can't rasterize one.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import {
|
|
22
|
+
assertPublicHttpUrl,
|
|
23
|
+
fetchHtmlHead,
|
|
24
|
+
fetchImageBytes,
|
|
25
|
+
} from "./url-fetch.js";
|
|
26
|
+
import type { ImageOptions } from "./image.js";
|
|
27
|
+
import { extractYouTubeVideoId, getYouTubeThumbnailUrls } from "./youtube.js";
|
|
28
|
+
|
|
1
29
|
const LINK_PREVIEW_PROVIDER_LABELS: Record<string, string> = {
|
|
2
30
|
youtube: "YouTube",
|
|
3
31
|
vimeo: "Vimeo",
|
|
4
32
|
bilibili: "Bilibili",
|
|
5
33
|
};
|
|
6
34
|
|
|
35
|
+
/**
|
|
36
|
+
* The provider a host belongs to, matched on the host itself or any of its
|
|
37
|
+
* subdomains. Matched on the URL a page was served from, after redirects, so
|
|
38
|
+
* a short link (`b23.tv`) or a mobile host (`m.bilibili.com`) lands on its
|
|
39
|
+
* provider either way.
|
|
40
|
+
*/
|
|
41
|
+
const PROVIDER_HOSTS: ReadonlyArray<readonly [string, string]> = [
|
|
42
|
+
["youtube.com", "youtube"],
|
|
43
|
+
["youtu.be", "youtube"],
|
|
44
|
+
["youtube-nocookie.com", "youtube"],
|
|
45
|
+
["bilibili.com", "bilibili"],
|
|
46
|
+
["b23.tv", "bilibili"],
|
|
47
|
+
["vimeo.com", "vimeo"],
|
|
48
|
+
];
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* YouTube's icon, for the path that never reads a YouTube page. The 144 px
|
|
52
|
+
* PNG is the one the page itself names; `/favicon.ico` (16 px) backs it up.
|
|
53
|
+
*/
|
|
54
|
+
const YOUTUBE_ICON_URLS = [
|
|
55
|
+
"https://www.gstatic.com/youtube/img/branding/favicon/favicon_144x144_v2.png",
|
|
56
|
+
"https://www.youtube.com/favicon.ico",
|
|
57
|
+
];
|
|
58
|
+
|
|
59
|
+
/** One deadline for a whole resolution: the page, its image, and its icon. */
|
|
60
|
+
const DEFAULT_TIMEOUT_MS = 3000;
|
|
61
|
+
/** The least an image fetch gets, however long the page took. */
|
|
62
|
+
const MIN_ASSET_TIMEOUT_MS = 1000;
|
|
63
|
+
/** How much of a page is read looking for its `</head>`. */
|
|
64
|
+
const MAX_HEAD_BYTES = 512 * 1024;
|
|
65
|
+
const MAX_PREVIEW_IMAGE_BYTES = 10 * 1024 * 1024;
|
|
66
|
+
const MAX_ICON_BYTES = 512 * 1024;
|
|
67
|
+
/** Icon URLs tried before giving up, so a page listing ten can't stall a save. */
|
|
68
|
+
const MAX_ICON_ATTEMPTS = 3;
|
|
69
|
+
/**
|
|
70
|
+
* YouTube serves a grey 120×90 placeholder, about 1 KB, for a size a video
|
|
71
|
+
* doesn't have. Anything this small is taken for one.
|
|
72
|
+
*/
|
|
73
|
+
const YOUTUBE_PLACEHOLDER_MAX_BYTES = 5000;
|
|
74
|
+
|
|
75
|
+
/** A raster image format, as recognized from a file's first bytes. */
|
|
76
|
+
export interface SniffedImage {
|
|
77
|
+
contentType: string;
|
|
78
|
+
ext: string;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** An image fetched for a preview, ready to store. */
|
|
82
|
+
export interface LinkPreviewImage extends SniffedImage {
|
|
83
|
+
bytes: Uint8Array;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** What the `<head>` of a linked page says about it. */
|
|
87
|
+
export interface LinkPageMetadata {
|
|
88
|
+
/** Whether the page declares itself a video. */
|
|
89
|
+
video: boolean;
|
|
90
|
+
/** Absolute URL of the page's own preview image, if it names one. */
|
|
91
|
+
imageUrl: string | null;
|
|
92
|
+
/** Absolute URLs of the site's icon, best first, ending with `/favicon.ico`. */
|
|
93
|
+
iconUrls: string[];
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** A resolved preview. Either part may be missing; both may be. */
|
|
97
|
+
export interface ResolvedLinkPreview {
|
|
98
|
+
/** A video's thumbnail; null for a page that isn't a video. */
|
|
99
|
+
image: LinkPreviewImage | null;
|
|
100
|
+
/** `"video"` when there is a thumbnail, else null. */
|
|
101
|
+
kind: "video" | null;
|
|
102
|
+
/** The known provider the thumbnail comes from, if any. */
|
|
103
|
+
provider: string | null;
|
|
104
|
+
/** The site's icon. */
|
|
105
|
+
icon: LinkPreviewImage | null;
|
|
106
|
+
}
|
|
107
|
+
|
|
7
108
|
/**
|
|
8
109
|
* Return the display label for a recognized Link preview provider.
|
|
9
110
|
*
|
|
@@ -23,3 +124,401 @@ export function getLinkPreviewProviderLabel(
|
|
|
23
124
|
? LINK_PREVIEW_PROVIDER_LABELS[normalizedProvider]
|
|
24
125
|
: undefined;
|
|
25
126
|
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* The transform a stored site icon is served through. The icon is drawn at
|
|
130
|
+
* 16 px, so 64 covers a 3x share image; an `.ico` passes untouched, since an
|
|
131
|
+
* image transform can't read one.
|
|
132
|
+
*
|
|
133
|
+
* @param key - The icon's storage key
|
|
134
|
+
* @returns Options for `getImageUrl`, or undefined to serve the file as is
|
|
135
|
+
* @example
|
|
136
|
+
* ```ts
|
|
137
|
+
* getLinkPreviewIconImageOptions("media/s/previews/p-icon-a.png"); // { width: 64, ... }
|
|
138
|
+
* getLinkPreviewIconImageOptions("media/s/previews/p-icon-a.ico"); // undefined
|
|
139
|
+
* ```
|
|
140
|
+
*/
|
|
141
|
+
export function getLinkPreviewIconImageOptions(
|
|
142
|
+
key: string,
|
|
143
|
+
): ImageOptions | undefined {
|
|
144
|
+
return key.toLowerCase().endsWith(".ico")
|
|
145
|
+
? undefined
|
|
146
|
+
: { width: 64, format: "auto", fit: "scale-down" };
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Return the known provider a URL's host belongs to.
|
|
151
|
+
*
|
|
152
|
+
* @param url - The URL, ideally as served after redirects
|
|
153
|
+
* @returns A provider identifier such as `bilibili`, or null
|
|
154
|
+
* @example
|
|
155
|
+
* ```ts
|
|
156
|
+
* getLinkPreviewProvider("https://m.bilibili.com/video/BV1GJ411x7h7"); // "bilibili"
|
|
157
|
+
* getLinkPreviewProvider("https://example.com/"); // null
|
|
158
|
+
* ```
|
|
159
|
+
*/
|
|
160
|
+
export function getLinkPreviewProvider(url: string | URL): string | null {
|
|
161
|
+
let host: string;
|
|
162
|
+
try {
|
|
163
|
+
host = new URL(url).hostname.toLowerCase().replace(/\.+$/, "");
|
|
164
|
+
} catch {
|
|
165
|
+
return null;
|
|
166
|
+
}
|
|
167
|
+
for (const [domain, provider] of PROVIDER_HOSTS) {
|
|
168
|
+
if (host === domain || host.endsWith(`.${domain}`)) return provider;
|
|
169
|
+
}
|
|
170
|
+
return null;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Recognize a raster image from its first bytes.
|
|
175
|
+
*
|
|
176
|
+
* @param bytes - The file's contents
|
|
177
|
+
* @returns Its MIME type and file extension, or null for anything else
|
|
178
|
+
* (SVG, HTML, an unknown format)
|
|
179
|
+
* @example
|
|
180
|
+
* ```ts
|
|
181
|
+
* sniffRasterImage(new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]));
|
|
182
|
+
* // { contentType: "image/png", ext: "png" }
|
|
183
|
+
* ```
|
|
184
|
+
*/
|
|
185
|
+
export function sniffRasterImage(bytes: Uint8Array): SniffedImage | null {
|
|
186
|
+
const at = (offset: number, ...expected: number[]) =>
|
|
187
|
+
expected.every((byte, index) => bytes[offset + index] === byte);
|
|
188
|
+
if (at(0, 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a)) {
|
|
189
|
+
return { contentType: "image/png", ext: "png" };
|
|
190
|
+
}
|
|
191
|
+
if (at(0, 0xff, 0xd8, 0xff)) return { contentType: "image/jpeg", ext: "jpg" };
|
|
192
|
+
if (at(0, 0x47, 0x49, 0x46, 0x38)) {
|
|
193
|
+
return { contentType: "image/gif", ext: "gif" };
|
|
194
|
+
}
|
|
195
|
+
if (at(0, 0x52, 0x49, 0x46, 0x46) && at(8, 0x57, 0x45, 0x42, 0x50)) {
|
|
196
|
+
return { contentType: "image/webp", ext: "webp" };
|
|
197
|
+
}
|
|
198
|
+
if (at(4, 0x66, 0x74, 0x79, 0x70, 0x61, 0x76, 0x69, 0x66)) {
|
|
199
|
+
return { contentType: "image/avif", ext: "avif" };
|
|
200
|
+
}
|
|
201
|
+
if (at(0, 0x00, 0x00, 0x01, 0x00)) {
|
|
202
|
+
return { contentType: "image/x-icon", ext: "ico" };
|
|
203
|
+
}
|
|
204
|
+
return null;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Read what a page's `<head>` says about it: whether it is a video, the
|
|
209
|
+
* image it offers for previews, and where the site's icon is.
|
|
210
|
+
*
|
|
211
|
+
* Icons the page names with `rel="icon"` come first, largest useful size
|
|
212
|
+
* first; then `apple-touch-icon`, which some sites fill with the page's own
|
|
213
|
+
* picture (Bilibili's is the video cover at 57 px); then the origin's
|
|
214
|
+
* `/favicon.ico`. SVG icons and Safari's `mask-icon` are left out.
|
|
215
|
+
*
|
|
216
|
+
* @param html - The page's head, as {@link fetchHtmlHead} returns it
|
|
217
|
+
* @param pageUrl - The URL the page was served from, for relative URLs
|
|
218
|
+
* @returns The page's preview metadata
|
|
219
|
+
* @example
|
|
220
|
+
* ```ts
|
|
221
|
+
* parseLinkPageHead(
|
|
222
|
+
* '<meta property="og:type" content="video.other"><meta property="og:image" content="/t.jpg">',
|
|
223
|
+
* new URL("https://example.com/v/1"),
|
|
224
|
+
* );
|
|
225
|
+
* // { video: true, imageUrl: "https://example.com/t.jpg",
|
|
226
|
+
* // iconUrls: ["https://example.com/favicon.ico"] }
|
|
227
|
+
* ```
|
|
228
|
+
*/
|
|
229
|
+
export function parseLinkPageHead(
|
|
230
|
+
html: string,
|
|
231
|
+
pageUrl: URL,
|
|
232
|
+
): LinkPageMetadata {
|
|
233
|
+
const meta = new Map<string, string>();
|
|
234
|
+
const icons: { url: string; score: number }[] = [];
|
|
235
|
+
const touchIcons: { url: string; score: number }[] = [];
|
|
236
|
+
let base = pageUrl;
|
|
237
|
+
|
|
238
|
+
for (const { name, attributes } of scanHeadTags(html)) {
|
|
239
|
+
if (name === "base") {
|
|
240
|
+
const href = attributes.get("href");
|
|
241
|
+
const resolved = href ? resolveHttpUrl(href, pageUrl) : null;
|
|
242
|
+
if (resolved) base = new URL(resolved);
|
|
243
|
+
continue;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
if (name === "meta") {
|
|
247
|
+
const key = (attributes.get("property") ?? attributes.get("name"))
|
|
248
|
+
?.trim()
|
|
249
|
+
.toLowerCase();
|
|
250
|
+
const content = attributes.get("content")?.trim();
|
|
251
|
+
if (key && content && !meta.has(key)) meta.set(key, content);
|
|
252
|
+
continue;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
const rel = (attributes.get("rel") ?? "").toLowerCase().split(/\s+/);
|
|
256
|
+
const href = attributes.get("href")?.trim();
|
|
257
|
+
if (!href) continue;
|
|
258
|
+
const type = attributes.get("type")?.toLowerCase() ?? "";
|
|
259
|
+
if (type.includes("svg") || /\.svgz?(?:[?#]|$)/i.test(href)) continue;
|
|
260
|
+
const isTouchIcon = rel.some((token) =>
|
|
261
|
+
token.startsWith("apple-touch-icon"),
|
|
262
|
+
);
|
|
263
|
+
if (!isTouchIcon && !rel.includes("icon")) continue;
|
|
264
|
+
// Resolved later, against a `<base>` that may come after this tag.
|
|
265
|
+
const entry = {
|
|
266
|
+
url: href,
|
|
267
|
+
score: scoreIcon(attributes.get("sizes"), type, href),
|
|
268
|
+
};
|
|
269
|
+
(isTouchIcon ? touchIcons : icons).push(entry);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
const ogType = meta.get("og:type")?.toLowerCase() ?? "";
|
|
273
|
+
const video =
|
|
274
|
+
ogType === "video" ||
|
|
275
|
+
ogType.startsWith("video.") ||
|
|
276
|
+
["og:video", "og:video:url", "og:video:secure_url"].some((key) =>
|
|
277
|
+
meta.has(key),
|
|
278
|
+
);
|
|
279
|
+
|
|
280
|
+
const imageUrl =
|
|
281
|
+
[
|
|
282
|
+
"og:image",
|
|
283
|
+
"og:image:url",
|
|
284
|
+
"og:image:secure_url",
|
|
285
|
+
"twitter:image",
|
|
286
|
+
"twitter:image:src",
|
|
287
|
+
]
|
|
288
|
+
.map((key) => meta.get(key))
|
|
289
|
+
.map((value) => (value ? resolveHttpUrl(value, base) : null))
|
|
290
|
+
.find((value): value is string => value !== null) ?? null;
|
|
291
|
+
|
|
292
|
+
const byScore = (a: { score: number }, b: { score: number }) =>
|
|
293
|
+
b.score - a.score;
|
|
294
|
+
const iconUrls = [...icons.sort(byScore), ...touchIcons.sort(byScore)]
|
|
295
|
+
.map(({ url }) => resolveHttpUrl(url, base))
|
|
296
|
+
.filter((url): url is string => url !== null);
|
|
297
|
+
iconUrls.push(new URL("/favicon.ico", pageUrl).toString());
|
|
298
|
+
|
|
299
|
+
return { video, imageUrl, iconUrls: [...new Set(iconUrls)] };
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Resolve a Link post's URL to its preview: fetch the page's head (or, for
|
|
304
|
+
* YouTube, nothing), then its thumbnail and the site's icon side by side, all
|
|
305
|
+
* within one deadline. Never throws: whatever can't be fetched is left null.
|
|
306
|
+
*
|
|
307
|
+
* @param rawUrl - The Link post's URL
|
|
308
|
+
* @param options - `timeoutMs`, the deadline for the whole resolution
|
|
309
|
+
* @returns The preview's parts
|
|
310
|
+
* @example
|
|
311
|
+
* ```ts
|
|
312
|
+
* const preview = await resolveLinkPreview("https://www.bilibili.com/video/BV1GJ411x7h7");
|
|
313
|
+
* // { kind: "video", provider: "bilibili", image: {...}, icon: {...} }
|
|
314
|
+
* ```
|
|
315
|
+
*/
|
|
316
|
+
export async function resolveLinkPreview(
|
|
317
|
+
rawUrl: string,
|
|
318
|
+
options: { timeoutMs?: number } = {},
|
|
319
|
+
): Promise<ResolvedLinkPreview> {
|
|
320
|
+
const deadline = Date.now() + (options.timeoutMs ?? DEFAULT_TIMEOUT_MS);
|
|
321
|
+
const assetTimeout = () =>
|
|
322
|
+
Math.max(MIN_ASSET_TIMEOUT_MS, deadline - Date.now());
|
|
323
|
+
const none: ResolvedLinkPreview = {
|
|
324
|
+
image: null,
|
|
325
|
+
kind: null,
|
|
326
|
+
provider: null,
|
|
327
|
+
icon: null,
|
|
328
|
+
};
|
|
329
|
+
|
|
330
|
+
const youTubeId = extractYouTubeVideoId(rawUrl);
|
|
331
|
+
if (youTubeId) {
|
|
332
|
+
const [image, icon] = await Promise.all([
|
|
333
|
+
fetchFirstImage(getYouTubeThumbnailUrls(youTubeId), {
|
|
334
|
+
maxBytes: MAX_PREVIEW_IMAGE_BYTES,
|
|
335
|
+
timeoutMs: assetTimeout,
|
|
336
|
+
accept: isPreviewImage,
|
|
337
|
+
minBytes: YOUTUBE_PLACEHOLDER_MAX_BYTES,
|
|
338
|
+
}),
|
|
339
|
+
fetchFirstImage(YOUTUBE_ICON_URLS, {
|
|
340
|
+
maxBytes: MAX_ICON_BYTES,
|
|
341
|
+
timeoutMs: assetTimeout,
|
|
342
|
+
accept: () => true,
|
|
343
|
+
}),
|
|
344
|
+
]);
|
|
345
|
+
return image
|
|
346
|
+
? { image, kind: "video", provider: "youtube", icon }
|
|
347
|
+
: { ...none, icon };
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
let pageUrl: URL;
|
|
351
|
+
try {
|
|
352
|
+
pageUrl = assertPublicHttpUrl(rawUrl, "page");
|
|
353
|
+
} catch {
|
|
354
|
+
return none;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
const page = await fetchHtmlHead(pageUrl, {
|
|
358
|
+
maxBytes: MAX_HEAD_BYTES,
|
|
359
|
+
timeoutMs: Math.max(0, deadline - Date.now()),
|
|
360
|
+
}).catch(() => null);
|
|
361
|
+
// A page that can't be read may still serve its icon.
|
|
362
|
+
const metadata = page
|
|
363
|
+
? parseLinkPageHead(page.html, page.url)
|
|
364
|
+
: {
|
|
365
|
+
video: false,
|
|
366
|
+
imageUrl: null,
|
|
367
|
+
iconUrls: [new URL("/favicon.ico", pageUrl).toString()],
|
|
368
|
+
};
|
|
369
|
+
|
|
370
|
+
const [image, icon] = await Promise.all([
|
|
371
|
+
metadata.video && metadata.imageUrl
|
|
372
|
+
? fetchFirstImage([metadata.imageUrl], {
|
|
373
|
+
maxBytes: MAX_PREVIEW_IMAGE_BYTES,
|
|
374
|
+
timeoutMs: assetTimeout,
|
|
375
|
+
accept: isPreviewImage,
|
|
376
|
+
})
|
|
377
|
+
: null,
|
|
378
|
+
fetchFirstImage(metadata.iconUrls.slice(0, MAX_ICON_ATTEMPTS), {
|
|
379
|
+
maxBytes: MAX_ICON_BYTES,
|
|
380
|
+
timeoutMs: assetTimeout,
|
|
381
|
+
accept: () => true,
|
|
382
|
+
}),
|
|
383
|
+
]);
|
|
384
|
+
|
|
385
|
+
return image
|
|
386
|
+
? {
|
|
387
|
+
image,
|
|
388
|
+
kind: "video",
|
|
389
|
+
provider: getLinkPreviewProvider(page?.url ?? pageUrl),
|
|
390
|
+
icon,
|
|
391
|
+
}
|
|
392
|
+
: { ...none, icon };
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/** A thumbnail is drawn large, so an icon format won't do. */
|
|
396
|
+
function isPreviewImage(image: SniffedImage): boolean {
|
|
397
|
+
return image.ext !== "ico";
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/** Fetch each URL in turn until one is a raster image `accept` takes. */
|
|
401
|
+
async function fetchFirstImage(
|
|
402
|
+
urls: readonly string[],
|
|
403
|
+
options: {
|
|
404
|
+
maxBytes: number;
|
|
405
|
+
timeoutMs: () => number;
|
|
406
|
+
accept: (image: SniffedImage) => boolean;
|
|
407
|
+
/** Smaller files are skipped as placeholders. */
|
|
408
|
+
minBytes?: number;
|
|
409
|
+
},
|
|
410
|
+
): Promise<LinkPreviewImage | null> {
|
|
411
|
+
for (const url of urls) {
|
|
412
|
+
try {
|
|
413
|
+
const { bytes } = await fetchImageBytes(assertPublicHttpUrl(url), {
|
|
414
|
+
maxBytes: options.maxBytes,
|
|
415
|
+
timeoutMs: options.timeoutMs(),
|
|
416
|
+
});
|
|
417
|
+
if (options.minBytes !== undefined && bytes.byteLength < options.minBytes)
|
|
418
|
+
continue;
|
|
419
|
+
const sniffed = sniffRasterImage(bytes);
|
|
420
|
+
if (sniffed && options.accept(sniffed)) return { ...sniffed, bytes };
|
|
421
|
+
} catch {
|
|
422
|
+
// Unreachable, too large, or refused by the guard — try the next one.
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
return null;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
/**
|
|
429
|
+
* Rank an icon by how well it draws at 16–32 px: a declared size from 32 to
|
|
430
|
+
* 256 is best, larger first; then a size above 256, then no size given (a
|
|
431
|
+
* raster file before an `.ico`), then anything under 32.
|
|
432
|
+
*/
|
|
433
|
+
function scoreIcon(
|
|
434
|
+
sizes: string | undefined,
|
|
435
|
+
type: string,
|
|
436
|
+
href: string,
|
|
437
|
+
): number {
|
|
438
|
+
const declared = (sizes ?? "")
|
|
439
|
+
.toLowerCase()
|
|
440
|
+
.split(/\s+/)
|
|
441
|
+
.map((size) => /^(\d+)x(\d+)$/.exec(size))
|
|
442
|
+
.filter((match): match is RegExpExecArray => match !== null)
|
|
443
|
+
.map((match) => Math.max(Number(match[1]), Number(match[2])));
|
|
444
|
+
const size = declared.length > 0 ? Math.max(...declared) : null;
|
|
445
|
+
if (size !== null && size >= 32 && size <= 256) return 1000 + size;
|
|
446
|
+
if (size !== null && size > 256) return 900;
|
|
447
|
+
if (size === null) {
|
|
448
|
+
const isIco = type.includes("icon") || /\.ico(?:[?#]|$)/i.test(href);
|
|
449
|
+
return isIco ? 500 : 600;
|
|
450
|
+
}
|
|
451
|
+
return size;
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
/** An absolute http(s) URL for `value` against `base`, or null. */
|
|
455
|
+
function resolveHttpUrl(value: string, base: URL): string | null {
|
|
456
|
+
try {
|
|
457
|
+
const url = new URL(value, base);
|
|
458
|
+
return url.protocol === "http:" || url.protocol === "https:"
|
|
459
|
+
? url.toString()
|
|
460
|
+
: null;
|
|
461
|
+
} catch {
|
|
462
|
+
return null;
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
/** Elements whose contents are not markup, and comments, which are skipped. */
|
|
467
|
+
const SKIPPED_CONTENT =
|
|
468
|
+
/<!--[\s\S]*?-->|<(script|style|noscript|template|textarea|title)\b[^>]*>[\s\S]*?<\/\1\s*>/gi;
|
|
469
|
+
|
|
470
|
+
/** A `<meta>`, `<link>`, or `<base>` tag; a quoted `>` doesn't end it. */
|
|
471
|
+
const HEAD_TAG = /<(meta|link|base)\b((?:[^>"']|"[^"]*"|'[^']*')*)>/gi;
|
|
472
|
+
|
|
473
|
+
const ATTRIBUTE =
|
|
474
|
+
/([^\s"'<>/=]+)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'=<>`]+)))?/g;
|
|
475
|
+
|
|
476
|
+
/**
|
|
477
|
+
* The `<meta>`, `<link>`, and `<base>` tags of a head, with their attributes
|
|
478
|
+
* (names lowercased, values entity-decoded). A scanner, not a parser: the
|
|
479
|
+
* head is read for a few tags, and a full parse of half a megabyte would
|
|
480
|
+
* spend a Worker's CPU budget.
|
|
481
|
+
*/
|
|
482
|
+
function* scanHeadTags(
|
|
483
|
+
html: string,
|
|
484
|
+
): Generator<{ name: string; attributes: Map<string, string> }> {
|
|
485
|
+
const markup = html.replace(SKIPPED_CONTENT, "");
|
|
486
|
+
for (const tag of markup.matchAll(HEAD_TAG)) {
|
|
487
|
+
const attributes = new Map<string, string>();
|
|
488
|
+
for (const attribute of (tag[2] ?? "").matchAll(ATTRIBUTE)) {
|
|
489
|
+
const name = attribute[1]?.toLowerCase();
|
|
490
|
+
if (!name || attributes.has(name)) continue;
|
|
491
|
+
const value = attribute[2] ?? attribute[3] ?? attribute[4] ?? "";
|
|
492
|
+
attributes.set(name, decodeHtmlEntities(value));
|
|
493
|
+
}
|
|
494
|
+
yield { name: (tag[1] ?? "").toLowerCase(), attributes };
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
const NAMED_ENTITIES: Record<string, string> = {
|
|
499
|
+
amp: "&",
|
|
500
|
+
lt: "<",
|
|
501
|
+
gt: ">",
|
|
502
|
+
quot: '"',
|
|
503
|
+
apos: "'",
|
|
504
|
+
nbsp: " ",
|
|
505
|
+
};
|
|
506
|
+
|
|
507
|
+
function decodeHtmlEntities(value: string): string {
|
|
508
|
+
return value.replace(
|
|
509
|
+
/&(?:#(\d+)|#x([0-9a-f]+)|([a-z]+));/gi,
|
|
510
|
+
(match, decimal: string, hex: string, name: string) => {
|
|
511
|
+
const code = decimal
|
|
512
|
+
? Number(decimal)
|
|
513
|
+
: hex
|
|
514
|
+
? Number.parseInt(hex, 16)
|
|
515
|
+
: null;
|
|
516
|
+
if (code !== null) {
|
|
517
|
+
return code > 0 && code <= 0x10ffff
|
|
518
|
+
? String.fromCodePoint(code)
|
|
519
|
+
: match;
|
|
520
|
+
}
|
|
521
|
+
return NAMED_ENTITIES[name.toLowerCase()] ?? match;
|
|
522
|
+
},
|
|
523
|
+
);
|
|
524
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which timeline items show a whole Thread, and how the page says so.
|
|
3
|
+
*
|
|
4
|
+
* A Thread read in full in a list counts as a view of it (`services/stats.ts`
|
|
5
|
+
* has the policy, `client/list-views.ts` what "read in full" takes). The first
|
|
6
|
+
* condition is the server's to decide, because only the server knows what it
|
|
7
|
+
* left out: the list must render all of the Thread — every post of it, and
|
|
8
|
+
* each card the whole of its post. A titled note shows its title and an
|
|
9
|
+
* excerpt; a Thread longer than the fold shows "N more posts" instead of
|
|
10
|
+
* them. Neither can be read in full in a list, so neither can count there:
|
|
11
|
+
* the reader who wants the rest opens the page, and the page counts.
|
|
12
|
+
*
|
|
13
|
+
* An item that does show a whole Thread carries {@link VIEW_UNIT_ATTRIBUTE},
|
|
14
|
+
* set to the Thread's root id. What is still folded on screen — a clamped
|
|
15
|
+
* note, a capped thread context — is the browser's to see, since the reader
|
|
16
|
+
* can unfold it in place. The view token lists the same ids, read back from
|
|
17
|
+
* the rendered page ({@link viewUnitsInHtml}), so the token and the page
|
|
18
|
+
* cannot disagree about what may be counted.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import type { PostView, TimelineItemView } from "../types.js";
|
|
22
|
+
import { ID_PREFIX, isTypeId } from "./ids.js";
|
|
23
|
+
import { foldReplyWindows } from "./thread-fold.js";
|
|
24
|
+
|
|
25
|
+
/** The attribute that marks a timeline item showing a whole Thread. */
|
|
26
|
+
export const VIEW_UNIT_ATTRIBUTE = "data-view-unit";
|
|
27
|
+
|
|
28
|
+
const VIEW_UNIT_PATTERN = new RegExp(
|
|
29
|
+
`\\s${VIEW_UNIT_ATTRIBUTE}="([^"]*)"`,
|
|
30
|
+
"g",
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Whether a feed card renders the whole of its post. A titled note is an
|
|
35
|
+
* article: the feed shows its title and an excerpt. Every other card in the
|
|
36
|
+
* feed carries its full body — an untitled note's tail is only clamped, and
|
|
37
|
+
* the reader can unfold it in place.
|
|
38
|
+
*/
|
|
39
|
+
function cardShowsWholePost(post: PostView): boolean {
|
|
40
|
+
return post.format !== "note" || !post.title;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* The Thread a timeline item shows whole, if it does.
|
|
45
|
+
*
|
|
46
|
+
* A lone post is a Thread of one. A thread preview shows a whole Thread when
|
|
47
|
+
* its fold hides nothing; a curated thread (Featured) when none of its
|
|
48
|
+
* segments has a gap above it. Either way every card must show its whole
|
|
49
|
+
* post. A post shown on its own while its Thread has others is part of a
|
|
50
|
+
* Thread, never the whole of it.
|
|
51
|
+
*
|
|
52
|
+
* @param item - A timeline item as the feed renders it
|
|
53
|
+
* @returns The Thread's root id, or `null` when the item shows only part
|
|
54
|
+
* @example
|
|
55
|
+
* ```ts
|
|
56
|
+
* wholeThreadOf({ post: untitledNote }); // "pst_…" (the note's own id)
|
|
57
|
+
* wholeThreadOf({ post: titledNote }); // null: the feed shows an excerpt
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
export function wholeThreadOf(item: TimelineItemView): string | null {
|
|
61
|
+
if (item.curatedThread) {
|
|
62
|
+
const { rootPost, segments } = item.curatedThread;
|
|
63
|
+
if (segments.length === 0) return null;
|
|
64
|
+
const whole = segments.every(
|
|
65
|
+
(segment) =>
|
|
66
|
+
segment.hiddenBeforeCount === 0 && cardShowsWholePost(segment.post),
|
|
67
|
+
);
|
|
68
|
+
return whole ? rootPost.id : null;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (item.threadPreview) {
|
|
72
|
+
const preview = item.threadPreview;
|
|
73
|
+
// The same fold `ThreadPreview` renders.
|
|
74
|
+
const fold = foldReplyWindows({
|
|
75
|
+
leadingReplies: preview.leadingReplies,
|
|
76
|
+
trailingReplies: preview.trailingReplies,
|
|
77
|
+
latestReply: preview.latestReply,
|
|
78
|
+
nextReply: null,
|
|
79
|
+
totalReplyCount: preview.totalReplyCount,
|
|
80
|
+
});
|
|
81
|
+
if (fold.hiddenCount > 0) return null;
|
|
82
|
+
const shown = [
|
|
83
|
+
item.post,
|
|
84
|
+
...fold.leadingReplies,
|
|
85
|
+
...fold.trailingReplies,
|
|
86
|
+
fold.latestReply,
|
|
87
|
+
];
|
|
88
|
+
return shown.every(cardShowsWholePost) ? item.post.id : null;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const { post } = item;
|
|
92
|
+
if (post.threadRootId || !post.isLastInThread) return null;
|
|
93
|
+
return cardShowsWholePost(post) ? post.id : null;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* The Threads a rendered page marks as shown whole, in order, once each.
|
|
98
|
+
*
|
|
99
|
+
* Read from the markup itself so that the token a page carries lists exactly
|
|
100
|
+
* the items the page marked. A value that is not a post id is ignored.
|
|
101
|
+
*
|
|
102
|
+
* @param html - The rendered page
|
|
103
|
+
* @returns Root ids of the Threads the page shows whole
|
|
104
|
+
* @example
|
|
105
|
+
* ```ts
|
|
106
|
+
* viewUnitsInHtml('<div data-view-unit="pst_01h455vb4pex5vsknk084sn02q">');
|
|
107
|
+
* // ["pst_01h455vb4pex5vsknk084sn02q"]
|
|
108
|
+
* ```
|
|
109
|
+
*/
|
|
110
|
+
export function viewUnitsInHtml(html: string): string[] {
|
|
111
|
+
const units = new Set<string>();
|
|
112
|
+
for (const match of html.matchAll(VIEW_UNIT_PATTERN)) {
|
|
113
|
+
const id = match[1];
|
|
114
|
+
if (id && isTypeId(id, ID_PREFIX.post)) units.add(id);
|
|
115
|
+
}
|
|
116
|
+
return [...units];
|
|
117
|
+
}
|
|
@@ -3,6 +3,7 @@ import {
|
|
|
3
3
|
Mark,
|
|
4
4
|
markInputRule,
|
|
5
5
|
markPasteRule,
|
|
6
|
+
mergeAttributes,
|
|
6
7
|
Node,
|
|
7
8
|
getSchema,
|
|
8
9
|
type AnyExtension,
|
|
@@ -15,6 +16,7 @@ import CodeBlock from "@tiptap/extension-code-block";
|
|
|
15
16
|
import { OrderedList } from "@tiptap/extension-list";
|
|
16
17
|
import HardBreak from "@tiptap/extension-hard-break";
|
|
17
18
|
import Paragraph from "@tiptap/extension-paragraph";
|
|
19
|
+
import Heading from "@tiptap/extension-heading";
|
|
18
20
|
import Bold from "@tiptap/extension-bold";
|
|
19
21
|
import Italic from "@tiptap/extension-italic";
|
|
20
22
|
import Strike from "@tiptap/extension-strike";
|
|
@@ -593,6 +595,42 @@ const MarkdownParagraph = Paragraph.extend({
|
|
|
593
595
|
},
|
|
594
596
|
});
|
|
595
597
|
|
|
598
|
+
/** Every level a Markdown or HTML heading can have. */
|
|
599
|
+
const ALL_HEADING_LEVELS = [1, 2, 3, 4, 5, 6] as const;
|
|
600
|
+
|
|
601
|
+
function clampHeadingLevel(level: unknown): number {
|
|
602
|
+
const value = typeof level === "number" ? Math.trunc(level) : 1;
|
|
603
|
+
return Math.min(Math.max(value, 1), 6);
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
/**
|
|
607
|
+
* Headings at every level the published post renders, created at three.
|
|
608
|
+
*
|
|
609
|
+
* `levels` limits what the author can make — the `#` to `###` input rules,
|
|
610
|
+
* the shortcuts and the heading commands. A post can still hold `####` and
|
|
611
|
+
* below: written through the API or MCP, imported, or pasted as Markdown.
|
|
612
|
+
* Tiptap draws a level outside `levels` as the first one and drops it on an
|
|
613
|
+
* HTML paste, so an `h4` opened in the editor looked like an `h1`, and one
|
|
614
|
+
* copied in from a web page became a paragraph. Here every level parses and
|
|
615
|
+
* renders as itself, the way `tiptap-render.ts` publishes it.
|
|
616
|
+
*/
|
|
617
|
+
const MarkdownHeading = Heading.extend({
|
|
618
|
+
parseHTML() {
|
|
619
|
+
return ALL_HEADING_LEVELS.map((level) => ({
|
|
620
|
+
tag: `h${level}`,
|
|
621
|
+
attrs: { level },
|
|
622
|
+
}));
|
|
623
|
+
},
|
|
624
|
+
|
|
625
|
+
renderHTML({ node, HTMLAttributes }) {
|
|
626
|
+
return [
|
|
627
|
+
`h${clampHeadingLevel(node.attrs.level)}`,
|
|
628
|
+
mergeAttributes(this.options.HTMLAttributes, HTMLAttributes),
|
|
629
|
+
0,
|
|
630
|
+
];
|
|
631
|
+
},
|
|
632
|
+
}).configure({ levels: [1, 2, 3] });
|
|
633
|
+
|
|
596
634
|
/**
|
|
597
635
|
* Ordered lists with CommonMark markers: digits only.
|
|
598
636
|
*
|
|
@@ -1229,7 +1267,7 @@ export function createMarkdownContentExtensions(
|
|
|
1229
1267
|
): Extensions {
|
|
1230
1268
|
return [
|
|
1231
1269
|
StarterKit.configure({
|
|
1232
|
-
heading:
|
|
1270
|
+
heading: false,
|
|
1233
1271
|
link: false,
|
|
1234
1272
|
codeBlock: false,
|
|
1235
1273
|
orderedList: false,
|
|
@@ -1241,6 +1279,7 @@ export function createMarkdownContentExtensions(
|
|
|
1241
1279
|
trailingNode: { notAfter: ["footnoteDefinition"] },
|
|
1242
1280
|
}),
|
|
1243
1281
|
MarkdownParagraph,
|
|
1282
|
+
MarkdownHeading,
|
|
1244
1283
|
MarkdownHardBreak,
|
|
1245
1284
|
MarkdownBold,
|
|
1246
1285
|
MarkdownItalic,
|