@openpeepshq/common 0.7.0 → 0.9.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.
@@ -3,6 +3,7 @@ import validator from 'validator';
3
3
  import ISO6391 from 'iso-639-1';
4
4
  import { extendZodWithOpenApi } from '@asteasolutions/zod-to-openapi';
5
5
  import { isEndBeforeStart } from '../lib/eventTime.js';
6
+ import { hasVisibleText, postContentWithinBudget } from '../lib/contentLength.js';
6
7
  import { groupRelationships } from './capabilities.js';
7
8
  import { capabilitiesSchema } from './config.js';
8
9
  import { HANDLE_RESERVED_MESSAGE, additionalForbiddenHandlesSchema, classifyAdditionalForbiddenHandle, collectAdditionalForbiddenHandles, forbiddenHandles, handleRegex, handleRegexBase, isDefaultForbiddenHandle, isForbiddenHandle, normalizeForbiddenHandle, } from './forbiddenHandles.js';
@@ -342,8 +343,7 @@ export const customEmojiSchema = z
342
343
  category: z.string(),
343
344
  })
344
345
  .openapi('CustomEmoji');
345
- export const noteSchema = z
346
- .object({
346
+ const noteBaseSchema = z.object({
347
347
  type: z.literal('note'),
348
348
  content: z.string().optional(),
349
349
  sensitive: z.boolean().optional(),
@@ -351,11 +351,29 @@ export const noteSchema = z
351
351
  attachments: z.array(mediaAttachmentDataSchema).optional(),
352
352
  customEmojis: z.array(customEmojiSchema).optional(),
353
353
  language: z.enum(ISO6391.getAllCodes()).optional(),
354
- })
355
- .openapi('Note');
354
+ });
355
+ // A note must carry something a reader can see: visible text or an
356
+ // attachment. Whitespace-only text renders nothing, so it is not enough.
357
+ const hasContentRefine = {
358
+ message: 'posts.form.contentRequired',
359
+ path: ['content'],
360
+ };
361
+ // Note/question carry the visible-text + raw budgets; the longer post
362
+ // flavors (article, resource, event) extend the base without them.
363
+ const contentBudgetRefine = {
364
+ message: 'posts.form.contentTooLong',
365
+ path: ['content'],
366
+ };
367
+ // The refines are write-side checks only: they must live on the input
368
+ // schemas the write endpoints validate against, not on the members of
369
+ // `postDataUnionSchema`. That union also validates API *responses*
370
+ // (`publicPostSchema`, reply/entry data), where a contentless note is
371
+ // legitimate — repost shells and system "left" notes have neither
372
+ // content nor attachments, and output validation must not reject them.
373
+ export const noteSchema = noteBaseSchema.openapi('Note');
356
374
  /** Poll choices are stored short so they fit the vote UI on small screens. */
357
375
  export const POLL_OPTION_MAX_LENGTH = 30;
358
- export const questionSchema = noteSchema
376
+ export const questionSchema = noteBaseSchema
359
377
  .extend({
360
378
  type: z.literal('question'),
361
379
  options: z
@@ -369,7 +387,13 @@ export const questionSchema = noteSchema
369
387
  expiresAt: z.iso.datetime().optional(),
370
388
  })
371
389
  .openapi('Question');
372
- export const articleSchema = noteSchema
390
+ export const noteInputSchema = noteBaseSchema
391
+ .refine((data) => hasVisibleText(data.content) || (data.attachments?.length ?? 0) > 0, hasContentRefine)
392
+ .refine((data) => postContentWithinBudget(data.content), contentBudgetRefine);
393
+ export const questionInputSchema = questionSchema
394
+ .refine((data) => hasVisibleText(data.content) || (data.attachments?.length ?? 0) > 0, hasContentRefine)
395
+ .refine((data) => postContentWithinBudget(data.content), contentBudgetRefine);
396
+ export const articleSchema = noteBaseSchema
373
397
  .extend({
374
398
  type: z.literal('article'),
375
399
  image: z.url().optional(),
@@ -378,7 +402,7 @@ export const articleSchema = noteSchema
378
402
  .openapi('Article');
379
403
  /** Resource titles are library headings, so they stay much shorter than the description. */
380
404
  export const RESOURCE_TITLE_MAX_LENGTH = 200;
381
- export const resourcePostSchema = noteSchema
405
+ export const resourcePostSchema = noteBaseSchema
382
406
  .extend({
383
407
  type: z.literal('resource'),
384
408
  title: z.string().max(RESOURCE_TITLE_MAX_LENGTH),
@@ -428,7 +452,7 @@ export const eventOccurrenceExceptionSchema = z
428
452
  path: ['end'],
429
453
  })
430
454
  .openapi('EventOccurrenceException');
431
- export const eventSchema = noteSchema
455
+ export const eventSchema = noteBaseSchema
432
456
  .extend({
433
457
  type: z.literal('event'),
434
458
  name: z.string().optional(),
@@ -460,6 +484,16 @@ export const postDataUnionSchema = z.discriminatedUnion('type', [
460
484
  articleSchema,
461
485
  resourcePostSchema,
462
486
  ]);
487
+ // Write-side union: the server POST/PUT endpoints validate *incoming*
488
+ // post payloads with this. The budget/requirement refines must not sit
489
+ // on the response-facing `postDataUnionSchema` members (see above).
490
+ export const postDataInputUnionSchema = z.discriminatedUnion('type', [
491
+ noteInputSchema,
492
+ questionInputSchema,
493
+ eventSchema,
494
+ articleSchema,
495
+ resourcePostSchema,
496
+ ]);
463
497
  export const postDataSchema = z.object({
464
498
  type: postTypeSchema,
465
499
  visibility: visibilityTypeSchema,
@@ -49,9 +49,9 @@ export type ObjectStatsWithAll = z.infer<typeof objectStatsWithAllSchema>;
49
49
  export declare const publicPostWithActivityScoreSchema: z.ZodObject<{
50
50
  id: z.ZodString;
51
51
  type: z.ZodEnum<{
52
- resource: "resource";
53
52
  note: "note";
54
53
  question: "question";
54
+ resource: "resource";
55
55
  event: "event";
56
56
  article: "article";
57
57
  }>;
@@ -1968,9 +1968,9 @@ export declare const publicPostWithActivityScoreSchema: z.ZodObject<{
1968
1968
  latestReplies: z.ZodOptional<z.ZodArray<z.ZodObject<{
1969
1969
  id: z.ZodString;
1970
1970
  type: z.ZodEnum<{
1971
- resource: "resource";
1972
1971
  note: "note";
1973
1972
  question: "question";
1973
+ resource: "resource";
1974
1974
  event: "event";
1975
1975
  article: "article";
1976
1976
  }>;
@@ -3659,9 +3659,9 @@ export declare const publicPostWithActivityScoreSchema: z.ZodObject<{
3659
3659
  replyTo: z.ZodNullable<z.ZodOptional<z.ZodObject<{
3660
3660
  id: z.ZodString;
3661
3661
  type: z.ZodEnum<{
3662
- resource: "resource";
3663
3662
  note: "note";
3664
3663
  question: "question";
3664
+ resource: "resource";
3665
3665
  event: "event";
3666
3666
  article: "article";
3667
3667
  }>;
@@ -5616,9 +5616,9 @@ export declare const publicPostWithActivityScoreSchema: z.ZodObject<{
5616
5616
  replyTo: z.ZodNullable<z.ZodOptional<z.ZodObject<{
5617
5617
  id: z.ZodString;
5618
5618
  type: z.ZodEnum<{
5619
- resource: "resource";
5620
5619
  note: "note";
5621
5620
  question: "question";
5621
+ resource: "resource";
5622
5622
  event: "event";
5623
5623
  article: "article";
5624
5624
  }>;
@@ -7307,9 +7307,9 @@ export declare const publicPostWithActivityScoreSchema: z.ZodObject<{
7307
7307
  replyTo: z.ZodNullable<z.ZodOptional<z.ZodObject<{
7308
7308
  id: z.ZodString;
7309
7309
  type: z.ZodEnum<{
7310
- resource: "resource";
7311
7310
  note: "note";
7312
7311
  question: "question";
7312
+ resource: "resource";
7313
7313
  event: "event";
7314
7314
  article: "article";
7315
7315
  }>;
@@ -9000,9 +9000,9 @@ export declare const publicPostWithActivityScoreSchema: z.ZodObject<{
9000
9000
  repost: z.ZodNullable<z.ZodOptional<z.ZodObject<{
9001
9001
  id: z.ZodString;
9002
9002
  type: z.ZodEnum<{
9003
- resource: "resource";
9004
9003
  note: "note";
9005
9004
  question: "question";
9005
+ resource: "resource";
9006
9006
  event: "event";
9007
9007
  article: "article";
9008
9008
  }>;
@@ -10690,9 +10690,9 @@ export declare const publicPostWithActivityScoreSchema: z.ZodObject<{
10690
10690
  replyTo: z.ZodNullable<z.ZodOptional<z.ZodObject<{
10691
10691
  id: z.ZodString;
10692
10692
  type: z.ZodEnum<{
10693
- resource: "resource";
10694
10693
  note: "note";
10695
10694
  question: "question";
10695
+ resource: "resource";
10696
10696
  event: "event";
10697
10697
  article: "article";
10698
10698
  }>;
@@ -12612,9 +12612,9 @@ export declare const publicPostWithActivityScoreSchema: z.ZodObject<{
12612
12612
  latestReplies: z.ZodOptional<z.ZodArray<z.ZodObject<{
12613
12613
  id: z.ZodString;
12614
12614
  type: z.ZodEnum<{
12615
- resource: "resource";
12616
12615
  note: "note";
12617
12616
  question: "question";
12617
+ resource: "resource";
12618
12618
  event: "event";
12619
12619
  article: "article";
12620
12620
  }>;
@@ -14303,9 +14303,9 @@ export declare const publicPostWithActivityScoreSchema: z.ZodObject<{
14303
14303
  replyTo: z.ZodNullable<z.ZodOptional<z.ZodObject<{
14304
14304
  id: z.ZodString;
14305
14305
  type: z.ZodEnum<{
14306
- resource: "resource";
14307
14306
  note: "note";
14308
14307
  question: "question";
14308
+ resource: "resource";
14309
14309
  event: "event";
14310
14310
  article: "article";
14311
14311
  }>;
@@ -16376,9 +16376,9 @@ export type TopListKey = 'day' | 'week' | 'month' | 'quarter' | 'year';
16376
16376
  declare const topPostsListSchema: z.ZodArray<z.ZodObject<{
16377
16377
  id: z.ZodString;
16378
16378
  type: z.ZodEnum<{
16379
- resource: "resource";
16380
16379
  note: "note";
16381
16380
  question: "question";
16381
+ resource: "resource";
16382
16382
  event: "event";
16383
16383
  article: "article";
16384
16384
  }>;
@@ -18295,9 +18295,9 @@ declare const topPostsListSchema: z.ZodArray<z.ZodObject<{
18295
18295
  latestReplies: z.ZodOptional<z.ZodArray<z.ZodObject<{
18296
18296
  id: z.ZodString;
18297
18297
  type: z.ZodEnum<{
18298
- resource: "resource";
18299
18298
  note: "note";
18300
18299
  question: "question";
18300
+ resource: "resource";
18301
18301
  event: "event";
18302
18302
  article: "article";
18303
18303
  }>;
@@ -19986,9 +19986,9 @@ declare const topPostsListSchema: z.ZodArray<z.ZodObject<{
19986
19986
  replyTo: z.ZodNullable<z.ZodOptional<z.ZodObject<{
19987
19987
  id: z.ZodString;
19988
19988
  type: z.ZodEnum<{
19989
- resource: "resource";
19990
19989
  note: "note";
19991
19990
  question: "question";
19991
+ resource: "resource";
19992
19992
  event: "event";
19993
19993
  article: "article";
19994
19994
  }>;
@@ -21943,9 +21943,9 @@ declare const topPostsListSchema: z.ZodArray<z.ZodObject<{
21943
21943
  replyTo: z.ZodNullable<z.ZodOptional<z.ZodObject<{
21944
21944
  id: z.ZodString;
21945
21945
  type: z.ZodEnum<{
21946
- resource: "resource";
21947
21946
  note: "note";
21948
21947
  question: "question";
21948
+ resource: "resource";
21949
21949
  event: "event";
21950
21950
  article: "article";
21951
21951
  }>;
@@ -23634,9 +23634,9 @@ declare const topPostsListSchema: z.ZodArray<z.ZodObject<{
23634
23634
  replyTo: z.ZodNullable<z.ZodOptional<z.ZodObject<{
23635
23635
  id: z.ZodString;
23636
23636
  type: z.ZodEnum<{
23637
- resource: "resource";
23638
23637
  note: "note";
23639
23638
  question: "question";
23639
+ resource: "resource";
23640
23640
  event: "event";
23641
23641
  article: "article";
23642
23642
  }>;
@@ -25327,9 +25327,9 @@ declare const topPostsListSchema: z.ZodArray<z.ZodObject<{
25327
25327
  repost: z.ZodNullable<z.ZodOptional<z.ZodObject<{
25328
25328
  id: z.ZodString;
25329
25329
  type: z.ZodEnum<{
25330
- resource: "resource";
25331
25330
  note: "note";
25332
25331
  question: "question";
25332
+ resource: "resource";
25333
25333
  event: "event";
25334
25334
  article: "article";
25335
25335
  }>;
@@ -27017,9 +27017,9 @@ declare const topPostsListSchema: z.ZodArray<z.ZodObject<{
27017
27017
  replyTo: z.ZodNullable<z.ZodOptional<z.ZodObject<{
27018
27018
  id: z.ZodString;
27019
27019
  type: z.ZodEnum<{
27020
- resource: "resource";
27021
27020
  note: "note";
27022
27021
  question: "question";
27022
+ resource: "resource";
27023
27023
  event: "event";
27024
27024
  article: "article";
27025
27025
  }>;
@@ -28939,9 +28939,9 @@ declare const topPostsListSchema: z.ZodArray<z.ZodObject<{
28939
28939
  latestReplies: z.ZodOptional<z.ZodArray<z.ZodObject<{
28940
28940
  id: z.ZodString;
28941
28941
  type: z.ZodEnum<{
28942
- resource: "resource";
28943
28942
  note: "note";
28944
28943
  question: "question";
28944
+ resource: "resource";
28945
28945
  event: "event";
28946
28946
  article: "article";
28947
28947
  }>;
@@ -30630,9 +30630,9 @@ declare const topPostsListSchema: z.ZodArray<z.ZodObject<{
30630
30630
  replyTo: z.ZodNullable<z.ZodOptional<z.ZodObject<{
30631
30631
  id: z.ZodString;
30632
30632
  type: z.ZodEnum<{
30633
- resource: "resource";
30634
30633
  note: "note";
30635
30634
  question: "question";
30635
+ resource: "resource";
30636
30636
  event: "event";
30637
30637
  article: "article";
30638
30638
  }>;
@@ -32973,9 +32973,9 @@ export declare const profileActivitySummarySchema: z.ZodObject<{
32973
32973
  topPosts: z.ZodArray<z.ZodObject<{
32974
32974
  id: z.ZodString;
32975
32975
  type: z.ZodEnum<{
32976
- resource: "resource";
32977
32976
  note: "note";
32978
32977
  question: "question";
32978
+ resource: "resource";
32979
32979
  event: "event";
32980
32980
  article: "article";
32981
32981
  }>;
@@ -34892,9 +34892,9 @@ export declare const profileActivitySummarySchema: z.ZodObject<{
34892
34892
  latestReplies: z.ZodOptional<z.ZodArray<z.ZodObject<{
34893
34893
  id: z.ZodString;
34894
34894
  type: z.ZodEnum<{
34895
- resource: "resource";
34896
34895
  note: "note";
34897
34896
  question: "question";
34897
+ resource: "resource";
34898
34898
  event: "event";
34899
34899
  article: "article";
34900
34900
  }>;
@@ -36583,9 +36583,9 @@ export declare const profileActivitySummarySchema: z.ZodObject<{
36583
36583
  replyTo: z.ZodNullable<z.ZodOptional<z.ZodObject<{
36584
36584
  id: z.ZodString;
36585
36585
  type: z.ZodEnum<{
36586
- resource: "resource";
36587
36586
  note: "note";
36588
36587
  question: "question";
36588
+ resource: "resource";
36589
36589
  event: "event";
36590
36590
  article: "article";
36591
36591
  }>;
@@ -38540,9 +38540,9 @@ export declare const profileActivitySummarySchema: z.ZodObject<{
38540
38540
  replyTo: z.ZodNullable<z.ZodOptional<z.ZodObject<{
38541
38541
  id: z.ZodString;
38542
38542
  type: z.ZodEnum<{
38543
- resource: "resource";
38544
38543
  note: "note";
38545
38544
  question: "question";
38545
+ resource: "resource";
38546
38546
  event: "event";
38547
38547
  article: "article";
38548
38548
  }>;
@@ -40231,9 +40231,9 @@ export declare const profileActivitySummarySchema: z.ZodObject<{
40231
40231
  replyTo: z.ZodNullable<z.ZodOptional<z.ZodObject<{
40232
40232
  id: z.ZodString;
40233
40233
  type: z.ZodEnum<{
40234
- resource: "resource";
40235
40234
  note: "note";
40236
40235
  question: "question";
40236
+ resource: "resource";
40237
40237
  event: "event";
40238
40238
  article: "article";
40239
40239
  }>;
@@ -41924,9 +41924,9 @@ export declare const profileActivitySummarySchema: z.ZodObject<{
41924
41924
  repost: z.ZodNullable<z.ZodOptional<z.ZodObject<{
41925
41925
  id: z.ZodString;
41926
41926
  type: z.ZodEnum<{
41927
- resource: "resource";
41928
41927
  note: "note";
41929
41928
  question: "question";
41929
+ resource: "resource";
41930
41930
  event: "event";
41931
41931
  article: "article";
41932
41932
  }>;
@@ -43614,9 +43614,9 @@ export declare const profileActivitySummarySchema: z.ZodObject<{
43614
43614
  replyTo: z.ZodNullable<z.ZodOptional<z.ZodObject<{
43615
43615
  id: z.ZodString;
43616
43616
  type: z.ZodEnum<{
43617
- resource: "resource";
43618
43617
  note: "note";
43619
43618
  question: "question";
43619
+ resource: "resource";
43620
43620
  event: "event";
43621
43621
  article: "article";
43622
43622
  }>;
@@ -45536,9 +45536,9 @@ export declare const profileActivitySummarySchema: z.ZodObject<{
45536
45536
  latestReplies: z.ZodOptional<z.ZodArray<z.ZodObject<{
45537
45537
  id: z.ZodString;
45538
45538
  type: z.ZodEnum<{
45539
- resource: "resource";
45540
45539
  note: "note";
45541
45540
  question: "question";
45541
+ resource: "resource";
45542
45542
  event: "event";
45543
45543
  article: "article";
45544
45544
  }>;
@@ -47227,9 +47227,9 @@ export declare const profileActivitySummarySchema: z.ZodObject<{
47227
47227
  replyTo: z.ZodNullable<z.ZodOptional<z.ZodObject<{
47228
47228
  id: z.ZodString;
47229
47229
  type: z.ZodEnum<{
47230
- resource: "resource";
47231
47230
  note: "note";
47232
47231
  question: "question";
47232
+ resource: "resource";
47233
47233
  event: "event";
47234
47234
  article: "article";
47235
47235
  }>;
@@ -49311,9 +49311,9 @@ export declare const adminProfileSummarySchema: z.ZodObject<{
49311
49311
  topPosts: z.ZodArray<z.ZodObject<{
49312
49312
  id: z.ZodString;
49313
49313
  type: z.ZodEnum<{
49314
- resource: "resource";
49315
49314
  note: "note";
49316
49315
  question: "question";
49316
+ resource: "resource";
49317
49317
  event: "event";
49318
49318
  article: "article";
49319
49319
  }>;
@@ -51230,9 +51230,9 @@ export declare const adminProfileSummarySchema: z.ZodObject<{
51230
51230
  latestReplies: z.ZodOptional<z.ZodArray<z.ZodObject<{
51231
51231
  id: z.ZodString;
51232
51232
  type: z.ZodEnum<{
51233
- resource: "resource";
51234
51233
  note: "note";
51235
51234
  question: "question";
51235
+ resource: "resource";
51236
51236
  event: "event";
51237
51237
  article: "article";
51238
51238
  }>;
@@ -52921,9 +52921,9 @@ export declare const adminProfileSummarySchema: z.ZodObject<{
52921
52921
  replyTo: z.ZodNullable<z.ZodOptional<z.ZodObject<{
52922
52922
  id: z.ZodString;
52923
52923
  type: z.ZodEnum<{
52924
- resource: "resource";
52925
52924
  note: "note";
52926
52925
  question: "question";
52926
+ resource: "resource";
52927
52927
  event: "event";
52928
52928
  article: "article";
52929
52929
  }>;
@@ -54878,9 +54878,9 @@ export declare const adminProfileSummarySchema: z.ZodObject<{
54878
54878
  replyTo: z.ZodNullable<z.ZodOptional<z.ZodObject<{
54879
54879
  id: z.ZodString;
54880
54880
  type: z.ZodEnum<{
54881
- resource: "resource";
54882
54881
  note: "note";
54883
54882
  question: "question";
54883
+ resource: "resource";
54884
54884
  event: "event";
54885
54885
  article: "article";
54886
54886
  }>;
@@ -56569,9 +56569,9 @@ export declare const adminProfileSummarySchema: z.ZodObject<{
56569
56569
  replyTo: z.ZodNullable<z.ZodOptional<z.ZodObject<{
56570
56570
  id: z.ZodString;
56571
56571
  type: z.ZodEnum<{
56572
- resource: "resource";
56573
56572
  note: "note";
56574
56573
  question: "question";
56574
+ resource: "resource";
56575
56575
  event: "event";
56576
56576
  article: "article";
56577
56577
  }>;
@@ -58262,9 +58262,9 @@ export declare const adminProfileSummarySchema: z.ZodObject<{
58262
58262
  repost: z.ZodNullable<z.ZodOptional<z.ZodObject<{
58263
58263
  id: z.ZodString;
58264
58264
  type: z.ZodEnum<{
58265
- resource: "resource";
58266
58265
  note: "note";
58267
58266
  question: "question";
58267
+ resource: "resource";
58268
58268
  event: "event";
58269
58269
  article: "article";
58270
58270
  }>;
@@ -59952,9 +59952,9 @@ export declare const adminProfileSummarySchema: z.ZodObject<{
59952
59952
  replyTo: z.ZodNullable<z.ZodOptional<z.ZodObject<{
59953
59953
  id: z.ZodString;
59954
59954
  type: z.ZodEnum<{
59955
- resource: "resource";
59956
59955
  note: "note";
59957
59956
  question: "question";
59957
+ resource: "resource";
59958
59958
  event: "event";
59959
59959
  article: "article";
59960
59960
  }>;
@@ -61874,9 +61874,9 @@ export declare const adminProfileSummarySchema: z.ZodObject<{
61874
61874
  latestReplies: z.ZodOptional<z.ZodArray<z.ZodObject<{
61875
61875
  id: z.ZodString;
61876
61876
  type: z.ZodEnum<{
61877
- resource: "resource";
61878
61877
  note: "note";
61879
61878
  question: "question";
61879
+ resource: "resource";
61880
61880
  event: "event";
61881
61881
  article: "article";
61882
61882
  }>;
@@ -63565,9 +63565,9 @@ export declare const adminProfileSummarySchema: z.ZodObject<{
63565
63565
  replyTo: z.ZodNullable<z.ZodOptional<z.ZodObject<{
63566
63566
  id: z.ZodString;
63567
63567
  type: z.ZodEnum<{
63568
- resource: "resource";
63569
63568
  note: "note";
63570
63569
  question: "question";
63570
+ resource: "resource";
63571
63571
  event: "event";
63572
63572
  article: "article";
63573
63573
  }>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openpeepshq/common",
3
- "version": "0.7.0",
3
+ "version": "0.9.0",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -0,0 +1,139 @@
1
+ /**
2
+ * Visible-text measurement for the post content budget.
3
+ *
4
+ * Counts what a reader sees, not the raw markdown source:
5
+ * - fenced code counts its content verbatim (it renders as visible code)
6
+ * - links and images count their text/alt, not the URL
7
+ * - structural line syntax (table cells/pipes, list markers, setext
8
+ * underlines, thematic breaks, headings, blockquotes) and HTML tags
9
+ * render no characters and do not count
10
+ * - whitespace runs collapse and line edges trim, so the count tracks
11
+ * what renders
12
+ *
13
+ * This is a budget estimate, not a markdown parser: unbounded content is
14
+ * guarded by the raw-length cap (`POST_MAX_RAW_LENGTH`), not by this
15
+ * function.
16
+ */
17
+ const FENCE_RE = /^[ \t]*```[^\n]*\n?([\s\S]*?)^[ \t]*```[ \t]*$/gm;
18
+
19
+ // Built from a string, not a regex literal: ESLint's no-control-regex
20
+ // rejects control characters in regex sources. U+FFFE is a noncharacter
21
+ // no renderer emits, and stray occurrences are stripped from the content
22
+ // before fencing, so it cannot collide with real input.
23
+ const FENCE_SENTINEL_RE = () => new RegExp('\\uFFFE(\\d+)\\uFFFE', 'g');
24
+
25
+ /**
26
+ * A line that renders nothing: table delimiter rows (`| --- | --- |`),
27
+ * setext underlines (`---`, `===`), and thematic breaks (`---`, `***`,
28
+ * `___`). The character class mirrors their allowed symbols; `-` is last
29
+ * so it cannot form a range.
30
+ */
31
+ const INVISIBLE_LINE_RE = /^[ \t]*\|?[ \t]*[-=*_][ \t:|=*_-]*\|?[ \t]*$/;
32
+
33
+ export const visibleTextLength = (markdown?: string | null): number => {
34
+ if (!markdown) return 0;
35
+ // Stray sentinels in user content must not collide with the fence
36
+ // sentinel (U+FFFE is a noncharacter no renderer emits).
37
+ let text = markdown
38
+ .replace(/\r\n?/g, '\n')
39
+ .replaceAll('\uFFFE', '')
40
+ .replaceAll('\u0000', '');
41
+ // Fenced code renders verbatim, so hold its content aside while the
42
+ // line-structural passes run on the surrounding text.
43
+ const fenced: string[] = [];
44
+ text = text.replace(FENCE_RE, (_match, content: string) => {
45
+ fenced.push(content);
46
+ return `\uFFFE${fenced.length - 1}\uFFFE`;
47
+ });
48
+ // Inline code counts its content, not the backticks.
49
+ text = text.replace(/`([^`]+)`/g, '$1');
50
+ // Autolinks: the URL is the visible text.
51
+ text = text.replace(/<(https?:\/\/[^>\s]+|mailto:[^>\s]+)>/gi, '$1');
52
+ // Images: count the alt text, drop the marker and URL.
53
+ text = text.replace(/!\[([^\]]*)\]\(((?:[^()]+|\([^()]*\))*)\)/g, '$1');
54
+ // Links: count the text, drop the URL (one level of nested parens ok).
55
+ text = text.replace(/\[([^\]]+)\]\(((?:[^()]+|\([^()]*\))*)\)/g, '$1');
56
+ // Reference-style links: count the text, drop the reference.
57
+ text = text.replace(/\[([^\]]+)\]\[[^\]]*\]/g, '$1');
58
+ // HTML tags: drop the tags, keep the inner text.
59
+ text = text.replace(/<\/?[a-zA-Z][^>]*>/g, '');
60
+ text = text
61
+ .split('\n')
62
+ .map((line) => {
63
+ if (INVISIBLE_LINE_RE.test(line)) return '';
64
+ return (
65
+ line
66
+ // Heading and blockquote markers.
67
+ .replace(/^[ \t]*(?:#{1,6}|>)[ \t]*/, '')
68
+ // List markers ("-", "*", "+", "1.", "1)").
69
+ .replace(/^[ \t]*(?:[-*+]|\d{1,9}[.)])[ \t]+/, '')
70
+ // Table cells: the pipes and their padding are syntax.
71
+ .replace(/^[ \t]*\|/, '')
72
+ .replace(/\|[ \t]*$/, '')
73
+ .replace(/\|/g, ' ')
74
+ .replace(/^[ \t]+|[\t ]+$/g, '')
75
+ );
76
+ })
77
+ .filter((line) => line.length > 0)
78
+ .join('\n');
79
+ // Emphasis/strikethrough markers. `_` inside a word (snake_case) is kept.
80
+ text = text.replace(/[*~]{1,3}/g, '');
81
+ text = text.replace(/(^|\s)_{1,3}(\S)/gm, '$1$2');
82
+ text = text.replace(/(\S)_{1,3}(\s|$)/gm, '$1$2');
83
+ // Whitespace runs collapse to single spaces (the renderer collapses
84
+ // them), so the count tracks what renders.
85
+ text = text.replace(/[\t ]{2,}/g, ' ');
86
+ // Restore the fenced code content verbatim.
87
+ text = text.replace(FENCE_SENTINEL_RE(), (_match, index: string) => {
88
+ return fenced[Number(index)] ?? '';
89
+ });
90
+ // Count code points so emoji and other astral characters cost what they
91
+ // render, not their UTF-16 surrogate pair.
92
+ return [...text].length;
93
+ };
94
+
95
+ /** Visible-text budget for note/question content. */
96
+ export const POST_MAX_VISIBLE_LENGTH = 500;
97
+ /** Raw markdown backstop for note/question content. */
98
+ export const POST_MAX_RAW_LENGTH = 5000;
99
+
100
+ /**
101
+ * Whitespace-only content is measured as empty (nothing renders), so the
102
+ * same "is there anything to send" test must ignore whitespace everywhere
103
+ * a composer or schema checks it.
104
+ */
105
+ export const hasVisibleText = (content?: string | null): boolean =>
106
+ visibleTextLength(content) > 0;
107
+
108
+ /** True when note/question content fits the visible + raw budgets. */
109
+ export const postContentWithinBudget = (content?: string | null): boolean => {
110
+ const raw = content ?? '';
111
+ return (
112
+ visibleTextLength(raw) <= POST_MAX_VISIBLE_LENGTH &&
113
+ raw.length <= POST_MAX_RAW_LENGTH
114
+ );
115
+ };
116
+
117
+ /**
118
+ * Shared composer submit gate over the visible budget. Question composers
119
+ * need visible text for their poll to make sense; note composers accept an
120
+ * attachment instead. In-flight state (submitting, pending uploads,
121
+ * audience/group requirements) stays at the call site.
122
+ */
123
+ export const canSubmitPostContent = ({
124
+ visibleContentLength,
125
+ attachmentsLength,
126
+ composerType,
127
+ pollOptionsValid = true,
128
+ }: {
129
+ visibleContentLength: number;
130
+ attachmentsLength: number;
131
+ composerType?: 'note' | 'question';
132
+ pollOptionsValid?: boolean;
133
+ }): boolean =>
134
+ composerType === 'question'
135
+ ? visibleContentLength > 0 &&
136
+ visibleContentLength <= POST_MAX_VISIBLE_LENGTH &&
137
+ pollOptionsValid
138
+ : visibleContentLength <= POST_MAX_VISIBLE_LENGTH &&
139
+ (visibleContentLength > 0 || attachmentsLength > 0);
package/src/lib/index.ts CHANGED
@@ -11,6 +11,7 @@ export * from './groupHelpers';
11
11
  export * from './groupTemplateHelpers';
12
12
  export * from './colors';
13
13
  export * from './markdownHelpers';
14
+ export * from './contentLength';
14
15
  export * from './attachmentHelpers';
15
16
  export * from './resources';
16
17
  export * from './eventIcs';