@jant/core 1.1.0 → 1.2.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.
Files changed (116) hide show
  1. package/bin/lib/site-snapshot.js +10 -0
  2. package/dist/{app-CKQ9PqAr.js → app-CbOZ2Irh.js} +7988 -5166
  3. package/dist/client/.vite/manifest.json +47 -47
  4. package/dist/client/_assets/chunks/{create-editor-BQkrw8Ax.js → create-editor-hWapNE40.js} +25 -25
  5. package/dist/client/_assets/chunks/footnote-rail-9zwl6jtV.js +2 -0
  6. package/dist/client/_assets/chunks/{settings-paths-B0bNUaih.js → settings-paths-BBVT8zKj.js} +1 -1
  7. package/dist/client/_assets/chunks/{slug-format-CYmQE5ln.js → slug-format-BuCPjSrB.js} +1 -1
  8. package/dist/client/_assets/chunks/sortable-list-DJXwkaOv.js +2 -0
  9. package/dist/client/_assets/chunks/types-DrYFqJH2.js +1 -0
  10. package/dist/client/_assets/chunks/unsafe-svg-CUrgo4Xg.js +1 -0
  11. package/dist/client/_assets/{client-Dp46QSht.js → client-BO8GOFfT.js} +12 -12
  12. package/dist/client/_assets/client-D8Rrsd73.css +2 -0
  13. package/dist/client/_assets/{client-auth-CG55rWb5.js → client-auth-CsUGM718.js} +1 -1
  14. package/dist/client/_assets/client-author-QPm1whbO.css +2 -0
  15. package/dist/client/_assets/client-compose-BB4U7Wz0.js +2004 -0
  16. package/dist/client/_assets/{client-manage-DnYdQAhf.js → client-manage-CI9-eh-8.js} +1 -1
  17. package/dist/client/_assets/{client-settings-CHgsIqXJ.js → client-settings-BnGPuqwc.js} +1 -1
  18. package/dist/index.js +1 -1
  19. package/dist/node.js +3 -2
  20. package/package.json +1 -1
  21. package/src/app.tsx +9 -0
  22. package/src/client/components/compose-types.ts +1 -0
  23. package/src/client/components/jant-compose-dialog.ts +1 -0
  24. package/src/client/components/jant-compose-editor.ts +30 -0
  25. package/src/client/components/jant-compose-fullscreen.ts +1 -0
  26. package/src/client/compose-bridge.ts +61 -1
  27. package/src/client/feed-video-player.ts +6 -0
  28. package/src/client/tiptap/bubble-menu.ts +25 -2
  29. package/src/client/tiptap/create-editor.ts +2 -0
  30. package/src/client/tiptap/extensions.ts +4 -0
  31. package/src/client/video-processor.ts +106 -52
  32. package/src/client/view-confirm.ts +108 -0
  33. package/src/client.ts +1 -0
  34. package/src/db/migrations/0035_brainy_next_avengers.sql +9 -0
  35. package/src/db/migrations/0036_gorgeous_smasher.sql +7 -0
  36. package/src/db/migrations/meta/0035_snapshot.json +2877 -0
  37. package/src/db/migrations/meta/0036_snapshot.json +2924 -0
  38. package/src/db/migrations/meta/_journal.json +14 -0
  39. package/src/db/migrations/pg/0033_cute_magdalene.sql +10 -0
  40. package/src/db/migrations/pg/0034_material_agent_brand.sql +8 -0
  41. package/src/db/migrations/pg/meta/0033_snapshot.json +3719 -0
  42. package/src/db/migrations/pg/meta/0034_snapshot.json +3766 -0
  43. package/src/db/migrations/pg/meta/_journal.json +14 -0
  44. package/src/db/pg/schema.ts +40 -0
  45. package/src/db/schema.ts +61 -0
  46. package/src/i18n/locales/public/en.po +5 -0
  47. package/src/i18n/locales/public/en.ts +1 -1
  48. package/src/i18n/locales/public/zh-Hans.po +5 -0
  49. package/src/i18n/locales/public/zh-Hans.ts +1 -1
  50. package/src/i18n/locales/public/zh-Hant.po +5 -0
  51. package/src/i18n/locales/public/zh-Hant.ts +1 -1
  52. package/src/i18n/locales/settings/en.po +149 -0
  53. package/src/i18n/locales/settings/en.ts +1 -1
  54. package/src/i18n/locales/settings/zh-Hans.po +149 -0
  55. package/src/i18n/locales/settings/zh-Hans.ts +1 -1
  56. package/src/i18n/locales/settings/zh-Hant.po +149 -0
  57. package/src/i18n/locales/settings/zh-Hant.ts +1 -1
  58. package/src/lib/env.ts +20 -0
  59. package/src/lib/github-app-state.ts +1 -1
  60. package/src/lib/github-app.ts +183 -1
  61. package/src/lib/markdown-manager.ts +129 -1
  62. package/src/lib/post-meta.ts +13 -3
  63. package/src/lib/referrer-aliases.ts +311 -0
  64. package/src/lib/render.tsx +10 -0
  65. package/src/lib/resolve-config.ts +14 -0
  66. package/src/lib/summary.ts +152 -54
  67. package/src/lib/tiptap-render.ts +1 -0
  68. package/src/lib/upload.ts +21 -1
  69. package/src/lib/video-playback.ts +55 -0
  70. package/src/lib/view-buffer.ts +201 -0
  71. package/src/lib/view-counting.ts +539 -0
  72. package/src/lib/view-token.ts +177 -0
  73. package/src/lib/view.ts +13 -1
  74. package/src/middleware/view-counter.ts +140 -0
  75. package/src/node/runtime.ts +4 -0
  76. package/src/preset.css +31 -14
  77. package/src/routes/api/media.ts +21 -3
  78. package/src/routes/dash/settings.tsx +433 -168
  79. package/src/routes/pages/archive.tsx +1 -0
  80. package/src/routes/pages/collection.tsx +6 -0
  81. package/src/routes/pages/collections.tsx +1 -0
  82. package/src/routes/pages/featured.tsx +1 -0
  83. package/src/routes/pages/home.tsx +1 -0
  84. package/src/routes/pages/page.tsx +3 -0
  85. package/src/routes/pages/smart-collection.tsx +2 -0
  86. package/src/routes/pages/subscribe.tsx +1 -0
  87. package/src/routes/view-confirm.ts +63 -0
  88. package/src/services/export-theme/assets/client-site.js +1 -1
  89. package/src/services/export-theme/styles/main.css +39 -21
  90. package/src/services/export.ts +2 -0
  91. package/src/services/index.ts +17 -0
  92. package/src/services/mcp.ts +31 -1
  93. package/src/services/media.ts +63 -0
  94. package/src/services/stats.ts +1130 -0
  95. package/src/styles/components-author.css +177 -0
  96. package/src/styles/components.css +11 -3
  97. package/src/styles/tokens.css +65 -30
  98. package/src/styles/ui.css +56 -13
  99. package/src/types/app-context.ts +9 -0
  100. package/src/types/bindings.ts +5 -0
  101. package/src/types/config.ts +54 -0
  102. package/src/types/views.ts +6 -0
  103. package/src/ui/compose/ComposeDialog.tsx +6 -0
  104. package/src/ui/dash/settings/SettingsRootContent.tsx +24 -6
  105. package/src/ui/dash/settings/StatsContent.tsx +855 -0
  106. package/src/ui/font-themes.ts +39 -6
  107. package/src/ui/layouts/BaseLayout.tsx +4 -2
  108. package/src/ui/layouts/SiteLayout.tsx +8 -3
  109. package/src/ui/shared/MediaGallery.tsx +19 -3
  110. package/dist/client/_assets/chunks/footnote-rail-B08scyhW.js +0 -2
  111. package/dist/client/_assets/chunks/sortable-list-t11Ma1Ek.js +0 -2
  112. package/dist/client/_assets/chunks/types-D5iF3H1a.js +0 -1
  113. package/dist/client/_assets/chunks/unsafe-svg-DZl-yjas.js +0 -1
  114. package/dist/client/_assets/client-B0-s9sqx.css +0 -2
  115. package/dist/client/_assets/client-author-CGVvrNZ8.css +0 -2
  116. package/dist/client/_assets/client-compose-DkUTuavR.js +0 -2004
@@ -0,0 +1,311 @@
1
+ /**
2
+ * The address a source is listed under, whatever address a reader came from.
3
+ *
4
+ * A reader from Reddit arrives from `reddit.com`, `old.reddit.com`,
5
+ * `out.reddit.com`, `redd.it` or the Android app, and a referrer list that
6
+ * keeps hostnames gives each its own line. This table folds the addresses a
7
+ * well-known site goes by into the one it is known by, so the list reads as
8
+ * sources. What is kept is still a domain, not a display name: a source the
9
+ * table does not know is a domain already, and one kind of value stays
10
+ * sortable, copyable and, later, filterable.
11
+ *
12
+ * A source has up to three kinds of entry. A family is a domain whose every
13
+ * subdomain is this source (`m.facebook.com`, `l.facebook.com`,
14
+ * `business.facebook.com`); a host folds to the nearest family above it. An
15
+ * alias is one host or word that names the source under another name: a
16
+ * short link (`t.co`), an old domain (`chat.openai.com`), the package id an
17
+ * Android app reports when it opens a link, or a word authors and platforms
18
+ * put in `utm_source` (`twitter`, `ig`). A pattern matches a whole host for a
19
+ * site with a domain per country (`google.co.uk`, `pinterest.de`). Lookup
20
+ * order is alias, pattern, family. Google has no family on purpose:
21
+ * `mail.google.com`, `docs.google.com` and `gemini.google.com` are sources of
22
+ * their own. A site that only ever appears under one address needs no entry.
23
+ *
24
+ * Which sites are worth an entry follows Plausible's source list and the
25
+ * Snowplow referer-parser database, plus the Chinese platforms they lack.
26
+ * Folding to a host rather than a name is this module's own choice.
27
+ */
28
+
29
+ interface Source {
30
+ /** The address the source is listed under. */
31
+ readonly host: string;
32
+ /** Domains whose every subdomain is this source. */
33
+ readonly families?: readonly string[];
34
+ /** Hosts, Android package ids and `utm_source` words that name this source. */
35
+ readonly aliases?: readonly string[];
36
+ /** Whole hosts, for a site with a domain per country. */
37
+ readonly patterns?: readonly RegExp[];
38
+ }
39
+
40
+ const SOURCES: readonly Source[] = [
41
+ // Social
42
+ {
43
+ host: "reddit.com",
44
+ families: ["reddit.com"],
45
+ aliases: [
46
+ "redd.it",
47
+ "com.reddit.frontpage",
48
+ "com.laurencedawson.reddit_sync",
49
+ "reddit",
50
+ ],
51
+ },
52
+ {
53
+ host: "facebook.com",
54
+ families: ["facebook.com"],
55
+ aliases: [
56
+ "fb.me",
57
+ "fb.com",
58
+ "com.facebook.katana",
59
+ "com.facebook.orca",
60
+ "facebook",
61
+ "fb",
62
+ ],
63
+ },
64
+ {
65
+ host: "instagram.com",
66
+ families: ["instagram.com"],
67
+ aliases: ["instagr.am", "com.instagram.android", "instagram", "ig"],
68
+ },
69
+ {
70
+ host: "threads.com",
71
+ families: ["threads.com", "threads.net"],
72
+ aliases: ["com.instagram.barcelona", "threads"],
73
+ },
74
+ {
75
+ host: "x.com",
76
+ families: ["x.com", "twitter.com"],
77
+ aliases: ["t.co", "com.twitter.android", "twitter", "x"],
78
+ },
79
+ {
80
+ host: "youtube.com",
81
+ families: ["youtube.com"],
82
+ aliases: ["youtu.be", "com.google.android.youtube", "youtube", "yt"],
83
+ },
84
+ {
85
+ host: "linkedin.com",
86
+ families: ["linkedin.com"],
87
+ aliases: ["lnkd.in", "com.linkedin.android", "linkedin"],
88
+ },
89
+ {
90
+ host: "pinterest.com",
91
+ families: ["pinterest.com"],
92
+ aliases: ["pin.it", "com.pinterest", "pinterest"],
93
+ patterns: [/^pinterest\.(?:co\.|com\.)?[a-z]{2}$/],
94
+ },
95
+ {
96
+ host: "tiktok.com",
97
+ families: ["tiktok.com"],
98
+ aliases: ["com.zhiliaoapp.musically", "tiktok"],
99
+ },
100
+ {
101
+ host: "bsky.app",
102
+ families: ["bsky.app"],
103
+ aliases: ["xyz.blueskyweb.app", "bluesky", "bsky"],
104
+ },
105
+ {
106
+ host: "tumblr.com",
107
+ families: ["tumblr.com"],
108
+ aliases: ["t.umblr.com", "tumblr"],
109
+ },
110
+ { host: "vk.com", families: ["vk.com"], aliases: ["vkontakte.ru"] },
111
+ { host: "medium.com", families: ["medium.com"] },
112
+ {
113
+ host: "wikipedia.org",
114
+ families: ["wikipedia.org"],
115
+ aliases: ["org.wikipedia", "wikipedia"],
116
+ },
117
+ {
118
+ host: "news.ycombinator.com",
119
+ aliases: [
120
+ "hn.algolia.com",
121
+ "io.github.hidroh.materialistic",
122
+ "hackernews",
123
+ "hn",
124
+ ],
125
+ },
126
+ { host: "github.com", families: ["github.com"], aliases: ["github"] },
127
+ { host: "disqus.com", families: ["disqus.com"], aliases: ["disq.us"] },
128
+ { host: "quora.com", families: ["quora.com"] },
129
+
130
+ // Messaging
131
+ {
132
+ host: "t.me",
133
+ families: ["t.me", "telegram.org"],
134
+ aliases: [
135
+ "telegram.me",
136
+ "org.telegram.messenger",
137
+ "org.telegram.messenger.web",
138
+ "org.telegram.plus",
139
+ "telegram",
140
+ "tg",
141
+ ],
142
+ },
143
+ {
144
+ host: "whatsapp.com",
145
+ families: ["whatsapp.com"],
146
+ aliases: ["wa.me", "com.whatsapp", "whatsapp"],
147
+ },
148
+ {
149
+ host: "discord.com",
150
+ families: ["discord.com", "discordapp.com"],
151
+ aliases: ["com.discord", "discord"],
152
+ },
153
+ {
154
+ host: "slack.com",
155
+ families: ["slack.com"],
156
+ aliases: ["com.slack", "slack"],
157
+ },
158
+ {
159
+ host: "teams.microsoft.com",
160
+ aliases: ["statics.teams.cdn.office.net", "com.microsoft.teams", "teams"],
161
+ },
162
+
163
+ // Search
164
+ {
165
+ host: "google.com",
166
+ aliases: ["com.google.android.googlequicksearchbox", "google"],
167
+ patterns: [/^google\.(?:com|[a-z]{2,3}|co\.[a-z]{2}|com\.[a-z]{2})$/],
168
+ },
169
+ {
170
+ host: "bing.com",
171
+ families: ["bing.com"],
172
+ aliases: ["ntp.msn.com", "cc.bingj.com", "bing"],
173
+ },
174
+ {
175
+ host: "yahoo.com",
176
+ aliases: ["yahoo"],
177
+ patterns: [/^(?:[a-z]{2}\.)?search\.yahoo\.com$/],
178
+ },
179
+ {
180
+ host: "duckduckgo.com",
181
+ families: ["duckduckgo.com"],
182
+ aliases: ["com.duckduckgo.mobile.android", "duckduckgo", "ddg"],
183
+ },
184
+ {
185
+ host: "yandex.ru",
186
+ aliases: ["ya.ru", "yandex"],
187
+ patterns: [/^(?:clck\.)?yandex\.(?:ru|com|ua|by|kz|uz|tm|fr|eu|com\.tr)$/],
188
+ },
189
+ {
190
+ host: "baidu.com",
191
+ families: ["baidu.com"],
192
+ aliases: ["com.baidu.searchbox", "baidu"],
193
+ },
194
+ { host: "sogou.com", families: ["sogou.com"], aliases: ["sogou"] },
195
+ { host: "so.com", families: ["so.com"], aliases: ["so.360.cn"] },
196
+ { host: "sm.cn", families: ["sm.cn"] },
197
+ { host: "naver.com", families: ["naver.com"], aliases: ["naver"] },
198
+
199
+ // AI assistants
200
+ {
201
+ host: "chatgpt.com",
202
+ families: ["chatgpt.com"],
203
+ aliases: ["chat.openai.com", "com.openai.chatgpt", "chatgpt", "openai"],
204
+ },
205
+ { host: "claude.ai", aliases: ["com.anthropic.claude", "claude"] },
206
+ {
207
+ host: "perplexity.ai",
208
+ families: ["perplexity.ai"],
209
+ aliases: ["pplx.ai", "ai.perplexity.app", "perplexity"],
210
+ },
211
+ { host: "gemini.google.com", aliases: ["gemini"] },
212
+ { host: "copilot.microsoft.com", aliases: ["copilot.com", "copilot"] },
213
+ { host: "grok.com", aliases: ["x.ai", "grok"] },
214
+ { host: "deepseek.com", aliases: ["deepseek"] },
215
+
216
+ // Chinese platforms
217
+ {
218
+ host: "weibo.com",
219
+ families: ["weibo.com", "weibo.cn"],
220
+ aliases: ["t.cn", "com.sina.weibo", "weibo"],
221
+ },
222
+ {
223
+ host: "weixin.qq.com",
224
+ families: ["weixin.qq.com"],
225
+ aliases: ["com.tencent.mm", "wechat", "weixin"],
226
+ },
227
+ {
228
+ host: "zhihu.com",
229
+ families: ["zhihu.com"],
230
+ aliases: ["com.zhihu.android", "zhihu"],
231
+ },
232
+ {
233
+ host: "bilibili.com",
234
+ families: ["bilibili.com"],
235
+ aliases: ["b23.tv", "tv.danmaku.bili", "bilibili"],
236
+ },
237
+ {
238
+ host: "douban.com",
239
+ families: ["douban.com"],
240
+ aliases: ["com.douban.frodo", "douban"],
241
+ },
242
+ {
243
+ host: "xiaohongshu.com",
244
+ families: ["xiaohongshu.com"],
245
+ aliases: ["xhslink.com", "com.xingin.xhs", "xiaohongshu", "xhs"],
246
+ },
247
+ {
248
+ host: "douyin.com",
249
+ families: ["douyin.com"],
250
+ aliases: ["com.ss.android.ugc.aweme", "douyin"],
251
+ },
252
+
253
+ // Mail and feed readers
254
+ { host: "mail.google.com", aliases: ["com.google.android.gm", "gmail"] },
255
+ {
256
+ host: "outlook.com",
257
+ aliases: [
258
+ "mail.live.com",
259
+ "outlook.live.com",
260
+ "outlook.office.com",
261
+ "outlook.office365.com",
262
+ "com.microsoft.office.outlook",
263
+ "outlook",
264
+ ],
265
+ },
266
+ { host: "mail.yahoo.com", aliases: ["com.yahoo.mobile.client.android.mail"] },
267
+ { host: "feedly.com", families: ["feedly.com"], aliases: ["feedly"] },
268
+ { host: "inoreader.com", families: ["inoreader.com"] },
269
+ ];
270
+
271
+ // Maps, not plain objects: a tag word like `constructor` must find nothing.
272
+ const ALIASES = new Map<string, string>();
273
+ const FAMILIES = new Map<string, string>();
274
+ const PATTERNS: Array<readonly [RegExp, string]> = [];
275
+ for (const source of SOURCES) {
276
+ for (const alias of source.aliases ?? []) ALIASES.set(alias, source.host);
277
+ for (const family of source.families ?? []) FAMILIES.set(family, source.host);
278
+ for (const pattern of source.patterns ?? [])
279
+ PATTERNS.push([pattern, source.host]);
280
+ }
281
+
282
+ /**
283
+ * The address a source is listed under.
284
+ *
285
+ * @param host - A host, Android package id or tag word, already lowercased
286
+ * and without a leading `www.`
287
+ * @returns The address the stats list it under, or `host` itself when the
288
+ * table has no entry for it
289
+ * @example
290
+ * ```ts
291
+ * canonicalSource("old.reddit.com"); // "reddit.com"
292
+ * canonicalSource("t.co"); // "x.com"
293
+ * canonicalSource("google.co.uk"); // "google.com"
294
+ * canonicalSource("mail.google.com"); // "mail.google.com"
295
+ * canonicalSource("example.org"); // "example.org"
296
+ * ```
297
+ */
298
+ export function canonicalSource(host: string): string {
299
+ const alias = ALIASES.get(host);
300
+ if (alias) return alias;
301
+ for (const [pattern, target] of PATTERNS) {
302
+ if (pattern.test(host)) return target;
303
+ }
304
+ for (let rest = host; ;) {
305
+ const family = FAMILIES.get(rest);
306
+ if (family) return family;
307
+ const dot = rest.indexOf(".");
308
+ if (dot === -1) return host;
309
+ rest = rest.slice(dot + 1);
310
+ }
311
+ }
@@ -108,6 +108,15 @@ export interface RenderPublicPageOptions {
108
108
  composeContextLanguage?: string | null;
109
109
  }
110
110
 
111
+ /**
112
+ * Pages whose content sets its own width, wider than the reading column, and
113
+ * so keep the wide frame where reader pages narrow theirs below 1024px.
114
+ */
115
+ const WIDE_FRAME_PAGES: ReadonlySet<PageName> = new Set([
116
+ "settings",
117
+ "compose",
118
+ ]);
119
+
111
120
  /**
112
121
  * Render a public page with the standard layout stack.
113
122
  *
@@ -178,6 +187,7 @@ export function renderPublicPage(c: Context, options: RenderPublicPageOptions) {
178
187
  siteFooterHtml: navData.siteFooterHtml,
179
188
  showHomeBranding,
180
189
  sidebar,
190
+ wideFrame: WIDE_FRAME_PAGES.has(page),
181
191
  uploadMaxFileSize: appConfig.uploadMaxFileSize,
182
192
  showComposeDialog,
183
193
  showHeader,
@@ -424,6 +424,20 @@ export function resolveConfig(
424
424
  siteAvatarThumbUrl,
425
425
  faviconVersion: allSettings["SITE_FAVICON_VERSION"] ?? "",
426
426
 
427
+ // View stats. The daily limit exists for one runtime: free-tier D1 allows
428
+ // 100,000 rows written per UTC day and refuses every query, reads too,
429
+ // once that is spent — so a well-read day must never be able to take the
430
+ // site down with it. SQLite and Postgres on Node have no such quota, and
431
+ // an unset variable there means no limit rather than the Cloudflare one.
432
+ stats: {
433
+ enabled: resolve("STATS_ENABLED", allSettings, env) === "true",
434
+ dailyLimit:
435
+ readEnvInteger(env, "STATS_DAILY_LIMIT") ??
436
+ (env.NODE_DATABASE || env.NODE_SQLITE
437
+ ? 0
438
+ : Number(CONFIG_FIELDS.STATS_DAILY_LIMIT.defaultValue)),
439
+ },
440
+
427
441
  // Rate limiting (ENV only). Defaults are conservative enough for a
428
442
  // human typing in the search UI but reject bot floods.
429
443
  rateLimit: {
@@ -39,7 +39,9 @@ export const NOTE_SUMMARY_MIN_HIDDEN_CHARS = 200;
39
39
 
40
40
  /**
41
41
  * Block node types that carry user-visible content for summary extraction.
42
- * Structural nodes (horizontalRule, moreBreak, image) are excluded.
42
+ * Structural nodes (horizontalRule, moreBreak) are excluded. Images are not
43
+ * content blocks either: they never count toward a summary's limits, and only
44
+ * an HTML summary that asks for them keeps any (see `SummaryImageOptions`).
43
45
  */
44
46
  const SUMMARY_BLOCK_TYPES = new Set([
45
47
  "paragraph",
@@ -51,6 +53,36 @@ const SUMMARY_BLOCK_TYPES = new Set([
51
53
  "table",
52
54
  ]);
53
55
 
56
+ /**
57
+ * How much room a run of text takes on the page, in Latin characters.
58
+ *
59
+ * A Han, kana or hangul character is a full em wide where a Latin letter is
60
+ * about half of one, so a 500-character Chinese teaser stands twice as tall as
61
+ * a 500-character English one. Each East Asian wide or fullwidth character
62
+ * counts as two, which keeps the character budgets below meaning the same
63
+ * amount of screen in either script.
64
+ *
65
+ * @param text - Plain text
66
+ * @returns Its length with wide characters counted twice
67
+ *
68
+ * @example
69
+ * ```ts
70
+ * textWidth("hello"); // 5
71
+ * textWidth("你好"); // 4
72
+ * ```
73
+ */
74
+ export function textWidth(text: string): number {
75
+ let width = 0;
76
+ for (const char of text) {
77
+ width += WIDE_CHAR.test(char) ? 2 : 1;
78
+ }
79
+ return width;
80
+ }
81
+
82
+ /** Han, kana, hangul, CJK punctuation and fullwidth forms: one em wide. */
83
+ const WIDE_CHAR =
84
+ /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}\u3000-\u303F\uFF00-\uFF60\uFFE0-\uFFE6]/u;
85
+
54
86
  /**
55
87
  * Recursively extracts plain text from a Tiptap node, ignoring marks.
56
88
  */
@@ -222,6 +254,33 @@ export function extractSummary(
222
254
  return blocks.length > 0 ? blocks.join("\n\n") : null;
223
255
  }
224
256
 
257
+ /**
258
+ * What an HTML summary keeps beyond the block and character limits.
259
+ */
260
+ export interface SummaryOptions {
261
+ /**
262
+ * Keep images in the excerpt, each in its place. Before a `moreBreak`
263
+ * marker every image is kept, since the author chose that range. Otherwise
264
+ * the first image ahead of the cut is kept — one that sits after the last
265
+ * kept block but before the next dropped one counts as ahead of it — and
266
+ * the rest are left for the full page. Images never count toward the block
267
+ * or character limit, so keeping one never pushes text out. Default
268
+ * `false`: the excerpt is text only.
269
+ */
270
+ includeImages?: boolean;
271
+ /**
272
+ * Stop ahead of the first heading once the summary holds a text block, so
273
+ * the summary is the post's lead: a heading opens the body proper, and a
274
+ * teaser that ran past one showed a section title with its first paragraph
275
+ * and nothing of the section. Headings the post opens with are kept, so a
276
+ * post that starts with one still has a summary. This cut is structural,
277
+ * never softened by `minHiddenChars`. Default `false`: headings are blocks
278
+ * like any other, which is what a note wants — its body is the content and
279
+ * the limits only cap runaway length.
280
+ */
281
+ stopAtHeading?: boolean;
282
+ }
283
+
225
284
  /**
226
285
  * Extracts an HTML summary from a Tiptap JSON body by taking the first
227
286
  * N content-bearing block nodes and rendering them as HTML.
@@ -229,9 +288,14 @@ export function extractSummary(
229
288
  * Unlike the plain-text `extractSummary`, this preserves the original
230
289
  * structure (lists, blockquotes, headings, etc.) for rich previews.
231
290
  *
291
+ * Character limits count East Asian wide characters as two
292
+ * ({@link textWidth}), so a Chinese teaser takes the room an English one
293
+ * does. With `stopAtHeading`, the summary is the post's lead and ends ahead
294
+ * of its first heading; see {@link SummaryOptions}.
295
+ *
232
296
  * @param bodyJson - Tiptap JSON string
233
297
  * @param maxBlocks - Maximum number of top-level blocks to include
234
- * @param maxChars - Maximum total plain-text character count
298
+ * @param maxChars - Maximum total plain-text width, in Latin characters
235
299
  * @param minHiddenChars - Tolerance for limit-based truncation: when > 0 and a
236
300
  * block/char limit would hide a tail shorter than this many plain-text
237
301
  * characters, the truncation is cancelled — all remaining content blocks are
@@ -239,15 +303,21 @@ export function extractSummary(
239
303
  * sliver of text. Explicit `moreBreak` markers reflect author intent and are
240
304
  * never subject to this tolerance.
241
305
  * @param renderOptions - Renderer options such as the post footnote namespace
306
+ * @param options - Whether the excerpt keeps images and whether it stops at
307
+ * a heading; see {@link SummaryOptions}
242
308
  * @returns HTML summary, whether content was truncated, and the index in
243
309
  * `doc.content` where the content after the summary boundary begins, or null.
244
310
  * `breakAtIndex` lets callers align the summary with the full-body rendering
245
311
  * when splitting at the "read more" boundary (e.g. to insert an anchor).
312
+ * With images kept, `hasMore` is also true when an image was left out.
246
313
  *
247
314
  * @example
248
315
  * ```ts
249
316
  * const result = extractSummaryHtml(body, 5, 500);
250
317
  * // { html: "<ul><li><p>Item</p></li></ul>", hasMore: true, breakAtIndex: 1 }
318
+ *
319
+ * extractSummaryHtml(body, 5, 500, 0, {}, { includeImages: true });
320
+ * // { html: "<figure><img src=\"…\"></figure><p>Intro</p>", hasMore: true, breakAtIndex: 2 }
251
321
  * ```
252
322
  */
253
323
  export function extractSummaryHtml(
@@ -256,6 +326,7 @@ export function extractSummaryHtml(
256
326
  maxChars: number = 500,
257
327
  minHiddenChars: number = 0,
258
328
  renderOptions: TiptapRenderOptions = {},
329
+ options: SummaryOptions = {},
259
330
  ): { html: string; hasMore: boolean; breakAtIndex: number } | null {
260
331
  let doc: TiptapNode;
261
332
  try {
@@ -267,26 +338,38 @@ export function extractSummaryHtml(
267
338
  if (doc.type !== "doc" || !doc.content) return null;
268
339
 
269
340
  const nodes = doc.content;
270
- const totalContentNodes = nodes.filter((n) =>
271
- SUMMARY_BLOCK_TYPES.has(n.type),
272
- ).length;
273
-
274
- // Check for moreBreak — take all content nodes before it
275
- const moreBreakIdx = nodes.findIndex((n) => n.type === "moreBreak");
276
- if (moreBreakIdx !== -1) {
277
- const selected = nodes
278
- .slice(0, moreBreakIdx)
279
- .filter((n) => SUMMARY_BLOCK_TYPES.has(n.type));
280
- if (selected.length === 0) return null;
341
+ const includeImages = options.includeImages === true;
342
+ const stopAtHeading = options.stopAtHeading === true;
343
+ const render = (indices: number[]): string => {
281
344
  const subDoc: JSONContent = {
282
345
  type: "doc",
283
346
  content: [
284
- ...selected,
347
+ ...indices
348
+ .sort((a, b) => a - b)
349
+ .map((i) => nodes[i])
350
+ .filter((node): node is TiptapNode => node !== undefined),
285
351
  ...nodes.filter((node) => node.type === "footnoteDefinition"),
286
352
  ] as JSONContent[],
287
353
  };
354
+ return renderTiptapDocument(subDoc, renderOptions);
355
+ };
356
+
357
+ // Check for moreBreak — take all content nodes before it
358
+ const moreBreakIdx = nodes.findIndex((n) => n.type === "moreBreak");
359
+ if (moreBreakIdx !== -1) {
360
+ const selected: number[] = [];
361
+ for (let i = 0; i < moreBreakIdx; i++) {
362
+ const type = nodes[i]?.type ?? "";
363
+ if (
364
+ SUMMARY_BLOCK_TYPES.has(type) ||
365
+ (includeImages && type === "image")
366
+ ) {
367
+ selected.push(i);
368
+ }
369
+ }
370
+ if (selected.length === 0) return null;
288
371
  return {
289
- html: renderTiptapDocument(subDoc, renderOptions),
372
+ html: render(selected),
290
373
  hasMore: true,
291
374
  // Anchor goes in place of the moreBreak marker, so the marker itself
292
375
  // is NOT part of the pre-anchor body. It remains in the post-anchor
@@ -305,63 +388,77 @@ export function extractSummaryHtml(
305
388
  if (!node || !SUMMARY_BLOCK_TYPES.has(node.type)) continue;
306
389
  const text = extractPlainText(node).trim();
307
390
  contentText.set(i, text);
308
- totalContentChars += text.length;
391
+ totalContentChars += textWidth(text);
309
392
  }
310
393
 
311
- const selected: TiptapNode[] = [];
394
+ const selected: number[] = [];
312
395
  let totalChars = 0;
313
396
  let lastSelectedIdx = -1;
397
+ let hasTextBlock = false;
398
+ let cutAtHeading = false;
399
+ // Index of the first content block left out, or the end of the document
400
+ // when every block made it in.
401
+ let cutIdx = nodes.length;
402
+
403
+ for (const [i, text] of contentText) {
404
+ const isHeading = nodes[i]?.type === "heading";
405
+ const width = textWidth(text);
406
+ if (selected.length > 0) {
407
+ if (stopAtHeading && isHeading && hasTextBlock) {
408
+ cutIdx = i;
409
+ cutAtHeading = true;
410
+ break;
411
+ }
412
+ if (selected.length >= maxBlocks || totalChars + width > maxChars) {
413
+ cutIdx = i;
414
+ break;
415
+ }
416
+ }
314
417
 
315
- for (let i = 0; i < nodes.length; i++) {
316
- const node = nodes[i];
317
- if (!node || !SUMMARY_BLOCK_TYPES.has(node.type)) continue;
318
-
319
- const text = contentText.get(i) ?? "";
320
- if (
321
- (selected.length >= maxBlocks || totalChars + text.length > maxChars) &&
322
- selected.length > 0
323
- )
324
- break;
325
-
326
- selected.push(node);
327
- totalChars += text.length;
418
+ selected.push(i);
419
+ totalChars += width;
328
420
  lastSelectedIdx = i;
421
+ if (!isHeading) hasTextBlock = true;
329
422
  }
330
423
 
331
- if (selected.length === 0) return null;
332
-
333
- let hasMore = selected.length < totalContentNodes;
424
+ let hasMore = selected.length < contentText.size;
334
425
 
335
426
  // Tolerance: don't truncate just to hide a tiny tail. When a block/char limit
336
427
  // triggered the cut and the hidden content is shorter than `minHiddenChars`,
337
428
  // include the remaining content blocks instead. `moreBreak` is handled above
338
- // and never reaches this path.
429
+ // and never reaches this path; a heading cut is structural and stands.
339
430
  if (
340
431
  hasMore &&
432
+ !cutAtHeading &&
341
433
  minHiddenChars > 0 &&
342
434
  totalContentChars - totalChars < minHiddenChars
343
435
  ) {
344
- for (let i = lastSelectedIdx + 1; i < nodes.length; i++) {
345
- const node = nodes[i];
346
- if (!node || !SUMMARY_BLOCK_TYPES.has(node.type)) continue;
347
- selected.push(node);
436
+ for (const i of contentText.keys()) {
437
+ if (i <= lastSelectedIdx) continue;
438
+ selected.push(i);
348
439
  lastSelectedIdx = i;
349
440
  }
441
+ cutIdx = nodes.length;
350
442
  hasMore = false;
351
443
  }
352
444
 
353
- const subDoc: JSONContent = {
354
- type: "doc",
355
- content: [
356
- ...selected,
357
- ...nodes.filter((node) => node.type === "footnoteDefinition"),
358
- ] as JSONContent[],
359
- };
360
- return {
361
- html: renderTiptapDocument(subDoc, renderOptions),
362
- hasMore,
363
- breakAtIndex: lastSelectedIdx + 1,
364
- };
445
+ let breakAtIndex = lastSelectedIdx + 1;
446
+ if (includeImages) {
447
+ const imageIdx = nodes.findIndex(
448
+ (node, i) => i < cutIdx && node.type === "image",
449
+ );
450
+ if (imageIdx !== -1) {
451
+ selected.push(imageIdx);
452
+ breakAtIndex = Math.max(breakAtIndex, imageIdx + 1);
453
+ }
454
+ if (nodes.some((node, i) => node.type === "image" && i !== imageIdx)) {
455
+ hasMore = true;
456
+ }
457
+ }
458
+
459
+ if (selected.length === 0) return null;
460
+
461
+ return { html: render(selected), hasMore, breakAtIndex };
365
462
  }
366
463
 
367
464
  /**
@@ -369,10 +466,10 @@ export function extractSummaryHtml(
369
466
  * the timeline uses.
370
467
  *
371
468
  * Wraps {@link extractSummaryHtml} with the limits that decide where a card
372
- * stops: a titled post is an article whose excerpt is a teaser, an untitled one
373
- * is a note whose body is the content and only needs a cap. Callers that need
374
- * that boundary — the feed renderer, the theme exporter — share this rather
375
- * than repeating five numbers each.
469
+ * stops: a titled post is an article whose excerpt is its lead, cut ahead of
470
+ * the first heading, an untitled one is a note whose body is the content and
471
+ * only needs a cap. Callers that need that boundary — the feed renderer, the
472
+ * theme exporter — share this rather than repeating five numbers each.
376
473
  *
377
474
  * @param bodyJson - TipTap JSON body, or null/undefined for a post without one
378
475
  * @param hasTitle - Whether the post has a title, which makes it an article
@@ -395,6 +492,7 @@ export function extractTimelineSummary(
395
492
  hasTitle ? ARTICLE_SUMMARY_MAX_CHARS : NOTE_SUMMARY_MAX_CHARS,
396
493
  hasTitle ? 0 : NOTE_SUMMARY_MIN_HIDDEN_CHARS,
397
494
  renderOptions,
495
+ { stopAtHeading: hasTitle },
398
496
  );
399
497
  return result ? { html: result.html, hasMore: result.hasMore } : null;
400
498
  }
@@ -318,6 +318,7 @@ function renderDocumentContent(
318
318
  }
319
319
 
320
320
  const MARK_RENDERERS: Record<string, MarkRenderer> = {
321
+ highlight: (html) => `<mark>${html}</mark>`,
321
322
  bold: (html) => `<strong>${html}</strong>`,
322
323
  italic: (html) => `<em>${html}</em>`,
323
324
  strike: (html) => `<s>${html}</s>`,