@adatechnology/content-contracts 0.2.0 → 0.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/dist/index.js CHANGED
@@ -6,13 +6,36 @@ var POST_STATUS = {
6
6
  DRAFT: "draft",
7
7
  SCHEDULED: "scheduled",
8
8
  PUBLISHED: "published",
9
- ARCHIVED: "archived"
9
+ ARCHIVED: "archived",
10
+ /** Post da comunidade enviado e aguardando a equipe (moderação `before`). */
11
+ PENDING_REVIEW: "pending_review",
12
+ /** Post da comunidade recusado ou despublicado pela equipe; o autor pode editar e reenviar. */
13
+ REJECTED: "rejected"
10
14
  };
11
15
  var POST_STATUSES = Object.values(POST_STATUS);
12
16
  function isPostStatus(value) {
13
17
  return typeof value === "string" && POST_STATUSES.includes(value);
14
18
  }
15
19
  __name(isPostStatus, "isPostStatus");
20
+ var POST_ORIGIN = {
21
+ STAFF: "staff",
22
+ COMMUNITY: "community"
23
+ };
24
+ var POST_ORIGINS = Object.values(POST_ORIGIN);
25
+ var COMMUNITY_MODERATION = {
26
+ BEFORE: "before",
27
+ AFTER: "after"
28
+ };
29
+ var COMMUNITY_MODERATIONS = Object.values(COMMUNITY_MODERATION);
30
+ var COMMUNITY_REVISION_STATE = {
31
+ PENDING: "pending",
32
+ REJECTED: "rejected"
33
+ };
34
+ var DEFAULT_CONTENT_SETTINGS = {
35
+ communityModeration: COMMUNITY_MODERATION.BEFORE,
36
+ communityDailyLimit: 3,
37
+ maxImagesPerPost: 12
38
+ };
16
39
  var RECURRENCE_FREQUENCY = {
17
40
  WEEKLY: "weekly",
18
41
  MONTHLY: "monthly"
@@ -27,8 +50,9 @@ var ContentError = class extends Error {
27
50
  statusCode;
28
51
  code;
29
52
  details;
30
- constructor(message, statusCode, code, details) {
31
- super(message), this.statusCode = statusCode, this.code = code, this.details = details;
53
+ publicDetails;
54
+ constructor(message, statusCode, code, details, publicDetails) {
55
+ super(message), this.statusCode = statusCode, this.code = code, this.details = details, this.publicDetails = publicDetails;
32
56
  this.name = "ContentError";
33
57
  }
34
58
  };
@@ -43,7 +67,16 @@ var CONTENT_ERROR_CODES = {
43
67
  TEMPLATE_NOT_PUBLISHABLE: "CONTENT_TEMPLATE_NOT_PUBLISHABLE",
44
68
  NOT_PUBLISHED: "CONTENT_NOT_PUBLISHED",
45
69
  REPUBLISH_DATE_IN_FUTURE: "CONTENT_REPUBLISH_DATE_IN_FUTURE",
46
- RECURRENCE_NOT_FOUND: "CONTENT_RECURRENCE_NOT_FOUND"
70
+ RECURRENCE_NOT_FOUND: "CONTENT_RECURRENCE_NOT_FOUND",
71
+ COMMUNITY_UNAUTHENTICATED: "CONTENT_COMMUNITY_UNAUTHENTICATED",
72
+ COMMUNITY_AUTHOR_NOT_ELIGIBLE: "CONTENT_COMMUNITY_AUTHOR_NOT_ELIGIBLE",
73
+ COMMUNITY_DAILY_LIMIT: "CONTENT_COMMUNITY_DAILY_LIMIT",
74
+ COMMUNITY_TERMS_REQUIRED: "CONTENT_COMMUNITY_TERMS_REQUIRED",
75
+ COMMUNITY_TOO_MANY_IMAGES: "CONTENT_COMMUNITY_TOO_MANY_IMAGES",
76
+ COMMUNITY_IMAGE_NOT_ALLOWED: "CONTENT_COMMUNITY_IMAGE_NOT_ALLOWED",
77
+ INVALID_TRANSITION: "CONTENT_INVALID_TRANSITION",
78
+ COMMUNITY_POST_READ_ONLY: "CONTENT_COMMUNITY_POST_READ_ONLY",
79
+ REPORT_NOT_FOUND: "CONTENT_REPORT_NOT_FOUND"
47
80
  };
48
81
  var PostNotFoundError = class extends ContentError {
49
82
  static {
@@ -160,6 +193,97 @@ var RecurrenceNotFoundError = class extends ContentError {
160
193
  }), this.reference = reference;
161
194
  }
162
195
  };
196
+ var CommunityUnauthenticatedError = class extends ContentError {
197
+ static {
198
+ __name(this, "CommunityUnauthenticatedError");
199
+ }
200
+ constructor() {
201
+ super("Entre na sua conta para continuar.", 401, CONTENT_ERROR_CODES.COMMUNITY_UNAUTHENTICATED);
202
+ }
203
+ };
204
+ var CommunityAuthorNotEligibleError = class extends ContentError {
205
+ static {
206
+ __name(this, "CommunityAuthorNotEligibleError");
207
+ }
208
+ constructor() {
209
+ super("Sua conta ainda n\xE3o pode publicar na comunidade.", 403, CONTENT_ERROR_CODES.COMMUNITY_AUTHOR_NOT_ELIGIBLE);
210
+ }
211
+ };
212
+ var CommunityDailyLimitError = class extends ContentError {
213
+ static {
214
+ __name(this, "CommunityDailyLimitError");
215
+ }
216
+ limit;
217
+ retryAfterSeconds;
218
+ constructor(limit, retryAfterSeconds) {
219
+ super("Limite de posts nas \xFAltimas 24 horas atingido.", 429, CONTENT_ERROR_CODES.COMMUNITY_DAILY_LIMIT, void 0, {
220
+ limit,
221
+ retryAfterSeconds
222
+ }), this.limit = limit, this.retryAfterSeconds = retryAfterSeconds;
223
+ }
224
+ };
225
+ var CommunityTermsRequiredError = class extends ContentError {
226
+ static {
227
+ __name(this, "CommunityTermsRequiredError");
228
+ }
229
+ constructor() {
230
+ super("Aceite os termos para enviar o post.", 422, CONTENT_ERROR_CODES.COMMUNITY_TERMS_REQUIRED);
231
+ }
232
+ };
233
+ var CommunityTooManyImagesError = class extends ContentError {
234
+ static {
235
+ __name(this, "CommunityTooManyImagesError");
236
+ }
237
+ max;
238
+ constructor(max) {
239
+ super("O post tem mais fotos do que o permitido.", 422, CONTENT_ERROR_CODES.COMMUNITY_TOO_MANY_IMAGES, void 0, {
240
+ max
241
+ }), this.max = max;
242
+ }
243
+ };
244
+ var CommunityImageNotAllowedError = class extends ContentError {
245
+ static {
246
+ __name(this, "CommunityImageNotAllowedError");
247
+ }
248
+ constructor() {
249
+ super("Use apenas fotos enviadas por voc\xEA neste site.", 422, CONTENT_ERROR_CODES.COMMUNITY_IMAGE_NOT_ALLOWED);
250
+ }
251
+ };
252
+ var InvalidPostTransitionError = class extends ContentError {
253
+ static {
254
+ __name(this, "InvalidPostTransitionError");
255
+ }
256
+ from;
257
+ action;
258
+ constructor(from, action) {
259
+ super("Esta a\xE7\xE3o n\xE3o \xE9 poss\xEDvel no estado atual do post.", 409, CONTENT_ERROR_CODES.INVALID_TRANSITION, void 0, {
260
+ from,
261
+ action
262
+ }), this.from = from, this.action = action;
263
+ }
264
+ };
265
+ var CommunityPostReadOnlyError = class extends ContentError {
266
+ static {
267
+ __name(this, "CommunityPostReadOnlyError");
268
+ }
269
+ postId;
270
+ constructor(postId) {
271
+ super("Post da comunidade s\xF3 pode ser moderado, n\xE3o editado.", 409, CONTENT_ERROR_CODES.COMMUNITY_POST_READ_ONLY, {
272
+ postId
273
+ }), this.postId = postId;
274
+ }
275
+ };
276
+ var ReportNotFoundError = class extends ContentError {
277
+ static {
278
+ __name(this, "ReportNotFoundError");
279
+ }
280
+ reference;
281
+ constructor(reference) {
282
+ super("Den\xFAncia n\xE3o encontrada.", 404, CONTENT_ERROR_CODES.REPORT_NOT_FOUND, {
283
+ reference
284
+ }), this.reference = reference;
285
+ }
286
+ };
163
287
 
164
288
  // src/schemas.ts
165
289
  import { z } from "zod";
@@ -289,8 +413,20 @@ var POST_LIMITS = {
289
413
  SEO_DESCRIPTION: 320,
290
414
  SEARCH: 120,
291
415
  ADMIN_PAGE_SIZE: 100,
292
- PUBLIC_PAGE_SIZE: 50
416
+ PUBLIC_PAGE_SIZE: 50,
417
+ DISPLAY_NAME_MIN: 2,
418
+ DISPLAY_NAME_MAX: 40,
419
+ REVIEW_NOTE: 500,
420
+ REPORT_REASON_MIN: 3,
421
+ REPORT_REASON_MAX: 300,
422
+ BULK_IDS: 100
293
423
  };
424
+ var POST_ORIGIN_FILTERS = [
425
+ "staff",
426
+ "community",
427
+ "all"
428
+ ];
429
+ var originFilterSchema = z.enum(POST_ORIGIN_FILTERS).default("staff");
294
430
  var slugSchema = z.string().trim().toLowerCase().max(SLUG_MAX_LENGTH).refine(isValidSlug, {
295
431
  message: "Use letras min\xFAsculas, n\xFAmeros e h\xEDfen."
296
432
  });
@@ -349,12 +485,14 @@ var listPostsQuerySchema = z.object({
349
485
  "true",
350
486
  "false"
351
487
  ])
352
- ]).transform((value) => value === true || value === "true").optional()
488
+ ]).transform((value) => value === true || value === "true").optional(),
489
+ origin: originFilterSchema
353
490
  });
354
491
  var listPublishedPostsQuerySchema = z.object({
355
492
  page: pageSchema,
356
493
  pageSize: z.coerce.number().int().min(1).max(POST_LIMITS.PUBLIC_PAGE_SIZE).default(10),
357
- tag: z.string().trim().min(1).max(TAG_MAX_LENGTH).optional()
494
+ tag: z.string().trim().min(1).max(TAG_MAX_LENGTH).optional(),
495
+ origin: originFilterSchema
358
496
  });
359
497
  var isoInstant = z.string().datetime({
360
498
  offset: true
@@ -379,6 +517,66 @@ var updateRecurrenceSchema = z.object({
379
517
  isActive: z.boolean().optional(),
380
518
  nextRunAt: isoInstant.optional()
381
519
  }).strict();
520
+ var communityDisplayNameSchema = z.string().trim().min(POST_LIMITS.DISPLAY_NAME_MIN).max(POST_LIMITS.DISPLAY_NAME_MAX).refine((value) => !value.includes("@") && !/\d{5,}/.test(value) && !/http/i.test(value), {
521
+ message: "O nome n\xE3o pode ter e-mail, telefone ou endere\xE7o de site."
522
+ });
523
+ var communityFieldsSchema = z.object({
524
+ title: z.string().trim().min(1).max(POST_LIMITS.TITLE),
525
+ body: z.string().max(POST_LIMITS.BODY),
526
+ excerpt: optionalText(POST_LIMITS.EXCERPT),
527
+ displayName: communityDisplayNameSchema.optional()
528
+ });
529
+ var createCommunityPostSchema = communityFieldsSchema.strict();
530
+ var updateCommunityPostSchema = communityFieldsSchema.partial().strict();
531
+ var submitCommunityPostSchema = z.object({
532
+ termsAccepted: z.boolean()
533
+ }).strict();
534
+ var rejectPostSchema = z.object({
535
+ note: z.string().trim().min(1).max(POST_LIMITS.REVIEW_NOTE)
536
+ }).strict();
537
+ var unpublishCommunityPostSchema = z.object({
538
+ note: z.string().trim().min(1).max(POST_LIMITS.REVIEW_NOTE).optional()
539
+ }).strict();
540
+ var BULK_MODERATION_ACTIONS = [
541
+ "approve",
542
+ "reject",
543
+ "unpublish",
544
+ "archive"
545
+ ];
546
+ var bulkModerationSchema = z.object({
547
+ action: z.enum(BULK_MODERATION_ACTIONS),
548
+ ids: z.array(z.string().uuid()).min(1).max(POST_LIMITS.BULK_IDS),
549
+ note: z.string().trim().min(1).max(POST_LIMITS.REVIEW_NOTE).optional()
550
+ }).strict();
551
+ var contentSettingsSchema = z.object({
552
+ communityModeration: z.enum(COMMUNITY_MODERATIONS),
553
+ communityDailyLimit: z.number().int().min(1).max(50),
554
+ maxImagesPerPost: z.number().int().min(1).max(30)
555
+ }).strict();
556
+ var createPostReportSchema = z.object({
557
+ reason: z.string().trim().min(POST_LIMITS.REPORT_REASON_MIN).max(POST_LIMITS.REPORT_REASON_MAX)
558
+ }).strict();
559
+ var REPORT_STATUSES = [
560
+ "open",
561
+ "resolved"
562
+ ];
563
+ var listPostReportsQuerySchema = z.object({
564
+ page: pageSchema,
565
+ pageSize: z.coerce.number().int().min(1).max(POST_LIMITS.ADMIN_PAGE_SIZE).default(20),
566
+ status: z.enum(REPORT_STATUSES).default("open")
567
+ });
568
+ var communityRevisionSchema = z.object({
569
+ title: z.string(),
570
+ excerpt: z.string().nullable(),
571
+ body: z.string(),
572
+ state: z.enum([
573
+ COMMUNITY_REVISION_STATE.PENDING,
574
+ COMMUNITY_REVISION_STATE.REJECTED
575
+ ]),
576
+ submittedAt: z.string().datetime({
577
+ offset: true
578
+ })
579
+ });
382
580
 
383
581
  // src/providers.ts
384
582
  var POST_EVENT = {
@@ -797,6 +995,47 @@ function parseDestination(text, start) {
797
995
  }
798
996
  __name(parseDestination, "parseDestination");
799
997
 
998
+ // src/markdown/paragraphToBlock.ts
999
+ function isFiller(node) {
1000
+ return node.type === "break" || node.type === "text" && !node.value.trim();
1001
+ }
1002
+ __name(isFiller, "isFiller");
1003
+ function paragraphToBlock(children) {
1004
+ const meaningful = children.filter((node) => !isFiller(node));
1005
+ const images = meaningful.filter((node) => node.type === "image");
1006
+ if (images.length === 0 || images.length !== meaningful.length) return {
1007
+ type: "paragraph",
1008
+ children
1009
+ };
1010
+ const [only] = images;
1011
+ if (images.length === 1 && only?.title) {
1012
+ return {
1013
+ type: "figure",
1014
+ image: {
1015
+ src: only.src,
1016
+ alt: only.alt
1017
+ },
1018
+ caption: only.title
1019
+ };
1020
+ }
1021
+ if (images.length < 2) return {
1022
+ type: "paragraph",
1023
+ children
1024
+ };
1025
+ const gallery = images.map((image) => ({
1026
+ src: image.src,
1027
+ alt: image.alt,
1028
+ ...image.title ? {
1029
+ caption: image.title
1030
+ } : {}
1031
+ }));
1032
+ return {
1033
+ type: "gallery",
1034
+ images: gallery
1035
+ };
1036
+ }
1037
+ __name(paragraphToBlock, "paragraphToBlock");
1038
+
800
1039
  // src/markdown/parseMarkdown.ts
801
1040
  var MAX_BLOCK_DEPTH = 6;
802
1041
  var FENCE_PATTERN = /^ {0,3}(`{3,}|~{3,})\s*([^\s`]*)[^`]*$/;
@@ -918,10 +1157,7 @@ function parseParagraph(lines, index, depth) {
918
1157
  cursor += 1;
919
1158
  }
920
1159
  return {
921
- block: {
922
- type: "paragraph",
923
- children: parseInline(content.join("\n").trimEnd())
924
- },
1160
+ block: paragraphToBlock(parseInline(content.join("\n").trimEnd())),
925
1161
  next: cursor
926
1162
  };
927
1163
  }
@@ -1033,7 +1269,10 @@ function blockToPlainText(block) {
1033
1269
  return block.children.map(blockToPlainText).join("\n\n");
1034
1270
  case "list":
1035
1271
  return block.items.map((item) => item.map(blockToPlainText).join("\n")).join("\n");
1272
+ case "figure":
1273
+ return block.caption;
1036
1274
  case "thematicBreak":
1275
+ case "gallery":
1037
1276
  return "";
1038
1277
  }
1039
1278
  }
@@ -1129,6 +1368,12 @@ function renderInlineNode(node, context) {
1129
1368
  }
1130
1369
  }
1131
1370
  __name(renderInlineNode, "renderInlineNode");
1371
+ function renderFigure(image, caption) {
1372
+ const img = `<img src="${escapeHtml(image.src)}" alt="${escapeHtml(image.alt)}" loading="lazy" decoding="async">`;
1373
+ const figcaption = caption ? `<figcaption>${escapeHtml(caption)}</figcaption>` : "";
1374
+ return `<figure class="adc-figure">${img}${figcaption}</figure>`;
1375
+ }
1376
+ __name(renderFigure, "renderFigure");
1132
1377
  function renderBlock(block, context, tight = false) {
1133
1378
  switch (block.type) {
1134
1379
  case "heading": {
@@ -1146,6 +1391,10 @@ function renderBlock(block, context, tight = false) {
1146
1391
  }
1147
1392
  case "thematicBreak":
1148
1393
  return "<hr>";
1394
+ case "figure":
1395
+ return renderFigure(block.image, block.caption);
1396
+ case "gallery":
1397
+ return `<div class="adc-gallery" data-count="${block.images.length}">${block.images.map((image) => renderFigure(image, image.caption)).join("")}</div>`;
1149
1398
  case "list": {
1150
1399
  const tag = block.ordered ? "ol" : "ul";
1151
1400
  const start = block.ordered && block.start !== void 0 && block.start !== 1 ? ` start="${block.start}"` : "";
@@ -1168,6 +1417,45 @@ function renderMarkdownToHtml(markdown, options = {}) {
1168
1417
  }
1169
1418
  __name(renderMarkdownToHtml, "renderMarkdownToHtml");
1170
1419
 
1420
+ // src/markdown/collectImageSources.ts
1421
+ function collectFromInline(nodes, sources) {
1422
+ for (const node of nodes) {
1423
+ if (node.type === "image") sources.push(node.src);
1424
+ else if ("children" in node) collectFromInline(node.children, sources);
1425
+ }
1426
+ }
1427
+ __name(collectFromInline, "collectFromInline");
1428
+ function collectFromBlock(block, sources) {
1429
+ switch (block.type) {
1430
+ case "heading":
1431
+ case "paragraph":
1432
+ collectFromInline(block.children, sources);
1433
+ return;
1434
+ case "blockquote":
1435
+ for (const child of block.children) collectFromBlock(child, sources);
1436
+ return;
1437
+ case "list":
1438
+ for (const item of block.items) for (const child of item) collectFromBlock(child, sources);
1439
+ return;
1440
+ case "figure":
1441
+ sources.push(block.image.src);
1442
+ return;
1443
+ case "gallery":
1444
+ for (const image of block.images) sources.push(image.src);
1445
+ return;
1446
+ case "codeBlock":
1447
+ case "thematicBreak":
1448
+ return;
1449
+ }
1450
+ }
1451
+ __name(collectFromBlock, "collectFromBlock");
1452
+ function collectImageSources(document) {
1453
+ const sources = [];
1454
+ for (const block of document.children) collectFromBlock(block, sources);
1455
+ return sources;
1456
+ }
1457
+ __name(collectImageSources, "collectImageSources");
1458
+
1171
1459
  // src/feed.ts
1172
1460
  var DEFAULT_POST_PATH_TEMPLATE = "/blog/{slug}";
1173
1461
  var XML_ESCAPES = {
@@ -1263,10 +1551,22 @@ function buildSitemapXml(entries) {
1263
1551
  }
1264
1552
  __name(buildSitemapXml, "buildSitemapXml");
1265
1553
  export {
1554
+ BULK_MODERATION_ACTIONS,
1555
+ COMMUNITY_MODERATION,
1556
+ COMMUNITY_MODERATIONS,
1557
+ COMMUNITY_REVISION_STATE,
1266
1558
  CONTENT_ERROR_CODES,
1559
+ CommunityAuthorNotEligibleError,
1560
+ CommunityDailyLimitError,
1561
+ CommunityImageNotAllowedError,
1562
+ CommunityPostReadOnlyError,
1563
+ CommunityTermsRequiredError,
1564
+ CommunityTooManyImagesError,
1565
+ CommunityUnauthenticatedError,
1267
1566
  ConfigMissingError,
1268
1567
  ContentError,
1269
1568
  DEFAULT_ADMIN_SCOPES,
1569
+ DEFAULT_CONTENT_SETTINGS,
1270
1570
  DEFAULT_LINK_REL,
1271
1571
  DEFAULT_POST_PATH_TEMPLATE,
1272
1572
  DEFAULT_PUBLIC_CACHE_CONTROL,
@@ -1275,10 +1575,14 @@ export {
1275
1575
  DuplicateSlugError,
1276
1576
  ImageStorageDisabledError,
1277
1577
  InvalidCoverImageError,
1578
+ InvalidPostTransitionError,
1278
1579
  InvalidSlugError,
1279
1580
  NotATemplateError,
1280
1581
  POST_EVENT,
1281
1582
  POST_LIMITS,
1583
+ POST_ORIGIN,
1584
+ POST_ORIGINS,
1585
+ POST_ORIGIN_FILTERS,
1282
1586
  POST_SORT_FIELDS,
1283
1587
  POST_STATUS,
1284
1588
  POST_STATUSES,
@@ -1287,7 +1591,9 @@ export {
1287
1591
  PostNotPublishedError,
1288
1592
  RECURRENCE_FREQUENCIES,
1289
1593
  RECURRENCE_FREQUENCY,
1594
+ REPORT_STATUSES,
1290
1595
  RecurrenceNotFoundError,
1596
+ ReportNotFoundError,
1291
1597
  RepublishDateInFutureError,
1292
1598
  SHORT_ID_LENGTH,
1293
1599
  SLUG_MAX_LENGTH,
@@ -1303,9 +1609,16 @@ export {
1303
1609
  buildShortSlug,
1304
1610
  buildSitemapXml,
1305
1611
  buildTagLabels,
1612
+ bulkModerationSchema,
1613
+ collectImageSources,
1614
+ communityDisplayNameSchema,
1615
+ communityRevisionSchema,
1616
+ contentSettingsSchema,
1306
1617
  countWords,
1618
+ createCommunityPostSchema,
1307
1619
  createFromTemplateSchema,
1308
1620
  createHeadingIdGenerator,
1621
+ createPostReportSchema,
1309
1622
  createPostSchema,
1310
1623
  createRecurrenceSchema,
1311
1624
  documentToPlainText,
@@ -1320,6 +1633,7 @@ export {
1320
1633
  isPostStatus,
1321
1634
  isShortId,
1322
1635
  isValidSlug,
1636
+ listPostReportsQuerySchema,
1323
1637
  listPostsQuerySchema,
1324
1638
  listPublishedPostsQuerySchema,
1325
1639
  markdownToPlainText,
@@ -1331,6 +1645,7 @@ export {
1331
1645
  parsePostRef,
1332
1646
  postFieldsSchema,
1333
1647
  publishPostSchema,
1648
+ rejectPostSchema,
1334
1649
  renderMarkdownDocument,
1335
1650
  renderMarkdownToHtml,
1336
1651
  republishPostSchema,
@@ -1340,6 +1655,9 @@ export {
1340
1655
  saveAsTemplateSchema,
1341
1656
  slugSchema,
1342
1657
  slugify,
1658
+ submitCommunityPostSchema,
1659
+ unpublishCommunityPostSchema,
1660
+ updateCommunityPostSchema,
1343
1661
  updatePostSchema,
1344
1662
  updateRecurrenceSchema,
1345
1663
  withSlugSuffix