@adatechnology/content-contracts 0.0.0-stage → 0.1.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/README.md +54 -2
- package/dist/index.cjs +1175 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +634 -0
- package/dist/index.d.ts +634 -0
- package/dist/index.js +1096 -0
- package/dist/index.js.map +1 -0
- package/package.json +33 -3
package/dist/index.js
ADDED
|
@@ -0,0 +1,1096 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
|
|
4
|
+
// src/content.types.ts
|
|
5
|
+
var POST_STATUS = {
|
|
6
|
+
DRAFT: "draft",
|
|
7
|
+
SCHEDULED: "scheduled",
|
|
8
|
+
PUBLISHED: "published",
|
|
9
|
+
ARCHIVED: "archived"
|
|
10
|
+
};
|
|
11
|
+
var POST_STATUSES = Object.values(POST_STATUS);
|
|
12
|
+
function isPostStatus(value) {
|
|
13
|
+
return typeof value === "string" && POST_STATUSES.includes(value);
|
|
14
|
+
}
|
|
15
|
+
__name(isPostStatus, "isPostStatus");
|
|
16
|
+
|
|
17
|
+
// src/errors.ts
|
|
18
|
+
var ContentError = class extends Error {
|
|
19
|
+
static {
|
|
20
|
+
__name(this, "ContentError");
|
|
21
|
+
}
|
|
22
|
+
statusCode;
|
|
23
|
+
code;
|
|
24
|
+
details;
|
|
25
|
+
constructor(message, statusCode, code, details) {
|
|
26
|
+
super(message), this.statusCode = statusCode, this.code = code, this.details = details;
|
|
27
|
+
this.name = "ContentError";
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
var CONTENT_ERROR_CODES = {
|
|
31
|
+
POST_NOT_FOUND: "CONTENT_POST_NOT_FOUND",
|
|
32
|
+
DUPLICATE_SLUG: "CONTENT_DUPLICATE_SLUG",
|
|
33
|
+
INVALID_SLUG: "CONTENT_INVALID_SLUG",
|
|
34
|
+
CONFIG_MISSING: "CONTENT_CONFIG_MISSING",
|
|
35
|
+
INVALID_IMAGE: "CONTENT_INVALID_IMAGE",
|
|
36
|
+
IMAGE_STORAGE_DISABLED: "CONTENT_IMAGE_STORAGE_DISABLED"
|
|
37
|
+
};
|
|
38
|
+
var PostNotFoundError = class extends ContentError {
|
|
39
|
+
static {
|
|
40
|
+
__name(this, "PostNotFoundError");
|
|
41
|
+
}
|
|
42
|
+
reference;
|
|
43
|
+
constructor(reference) {
|
|
44
|
+
super("Post n\xE3o encontrado.", 404, CONTENT_ERROR_CODES.POST_NOT_FOUND, {
|
|
45
|
+
reference
|
|
46
|
+
}), this.reference = reference;
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
var DuplicateSlugError = class extends ContentError {
|
|
50
|
+
static {
|
|
51
|
+
__name(this, "DuplicateSlugError");
|
|
52
|
+
}
|
|
53
|
+
slug;
|
|
54
|
+
constructor(slug) {
|
|
55
|
+
super("J\xE1 existe post com este endere\xE7o (slug).", 409, CONTENT_ERROR_CODES.DUPLICATE_SLUG, {
|
|
56
|
+
slug
|
|
57
|
+
}), this.slug = slug;
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
var InvalidSlugError = class extends ContentError {
|
|
61
|
+
static {
|
|
62
|
+
__name(this, "InvalidSlugError");
|
|
63
|
+
}
|
|
64
|
+
slug;
|
|
65
|
+
constructor(slug) {
|
|
66
|
+
super("Endere\xE7o (slug) inv\xE1lido.", 400, CONTENT_ERROR_CODES.INVALID_SLUG, {
|
|
67
|
+
slug
|
|
68
|
+
}), this.slug = slug;
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
var ConfigMissingError = class extends ContentError {
|
|
72
|
+
static {
|
|
73
|
+
__name(this, "ConfigMissingError");
|
|
74
|
+
}
|
|
75
|
+
field;
|
|
76
|
+
constructor(field) {
|
|
77
|
+
super(`Configura\xE7\xE3o ausente ou inv\xE1lida: ${field}.`, 500, CONTENT_ERROR_CODES.CONFIG_MISSING, {
|
|
78
|
+
field
|
|
79
|
+
}), this.field = field;
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
var InvalidCoverImageError = class extends ContentError {
|
|
83
|
+
static {
|
|
84
|
+
__name(this, "InvalidCoverImageError");
|
|
85
|
+
}
|
|
86
|
+
reason;
|
|
87
|
+
constructor(reason) {
|
|
88
|
+
super("Imagem de capa inv\xE1lida.", 400, CONTENT_ERROR_CODES.INVALID_IMAGE, {
|
|
89
|
+
reason
|
|
90
|
+
}), this.reason = reason;
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
var ImageStorageDisabledError = class extends ContentError {
|
|
94
|
+
static {
|
|
95
|
+
__name(this, "ImageStorageDisabledError");
|
|
96
|
+
}
|
|
97
|
+
constructor() {
|
|
98
|
+
super("Envio de imagem n\xE3o configurado.", 501, CONTENT_ERROR_CODES.IMAGE_STORAGE_DISABLED);
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
// src/schemas.ts
|
|
103
|
+
import { z } from "zod";
|
|
104
|
+
|
|
105
|
+
// src/slugify.ts
|
|
106
|
+
var SLUG_MAX_LENGTH = 120;
|
|
107
|
+
var SLUG_PATTERN = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
|
|
108
|
+
function slugify(value) {
|
|
109
|
+
const base = value.normalize("NFD").replace(/[\u0300-\u036f]/g, "").toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "");
|
|
110
|
+
if (base.length <= SLUG_MAX_LENGTH) return base;
|
|
111
|
+
const truncated = base.slice(0, SLUG_MAX_LENGTH);
|
|
112
|
+
const lastHyphen = truncated.lastIndexOf("-");
|
|
113
|
+
return (lastHyphen > 0 ? truncated.slice(0, lastHyphen) : truncated).replace(/-+$/g, "");
|
|
114
|
+
}
|
|
115
|
+
__name(slugify, "slugify");
|
|
116
|
+
function isValidSlug(value) {
|
|
117
|
+
return value.length > 0 && value.length <= SLUG_MAX_LENGTH && SLUG_PATTERN.test(value);
|
|
118
|
+
}
|
|
119
|
+
__name(isValidSlug, "isValidSlug");
|
|
120
|
+
function withSlugSuffix(slug, attempt) {
|
|
121
|
+
if (attempt <= 1) return slug;
|
|
122
|
+
const suffix = `-${attempt}`;
|
|
123
|
+
return `${slug.slice(0, SLUG_MAX_LENGTH - suffix.length).replace(/-+$/g, "")}${suffix}`;
|
|
124
|
+
}
|
|
125
|
+
__name(withSlugSuffix, "withSlugSuffix");
|
|
126
|
+
|
|
127
|
+
// src/tags.ts
|
|
128
|
+
var TAG_MAX_LENGTH = 40;
|
|
129
|
+
var TAGS_MAX_COUNT = 20;
|
|
130
|
+
function normalizeTag(value) {
|
|
131
|
+
return value.trim().replace(/\s+/g, " ").toLowerCase().slice(0, TAG_MAX_LENGTH);
|
|
132
|
+
}
|
|
133
|
+
__name(normalizeTag, "normalizeTag");
|
|
134
|
+
function normalizeTags(values) {
|
|
135
|
+
const seen = /* @__PURE__ */ new Set();
|
|
136
|
+
const result = [];
|
|
137
|
+
for (const value of values) {
|
|
138
|
+
const tag = normalizeTag(value);
|
|
139
|
+
if (!tag || seen.has(tag)) continue;
|
|
140
|
+
seen.add(tag);
|
|
141
|
+
result.push(tag);
|
|
142
|
+
}
|
|
143
|
+
return result.slice(0, TAGS_MAX_COUNT);
|
|
144
|
+
}
|
|
145
|
+
__name(normalizeTags, "normalizeTags");
|
|
146
|
+
|
|
147
|
+
// src/schemas.ts
|
|
148
|
+
var POST_LIMITS = {
|
|
149
|
+
TITLE: 200,
|
|
150
|
+
EXCERPT: 500,
|
|
151
|
+
BODY: 1e5,
|
|
152
|
+
URL: 2048,
|
|
153
|
+
IMAGE_ALT: 300,
|
|
154
|
+
AUTHOR_NAME: 120,
|
|
155
|
+
SEO_TITLE: 200,
|
|
156
|
+
SEO_DESCRIPTION: 320,
|
|
157
|
+
SEARCH: 120,
|
|
158
|
+
ADMIN_PAGE_SIZE: 100,
|
|
159
|
+
PUBLIC_PAGE_SIZE: 50
|
|
160
|
+
};
|
|
161
|
+
var slugSchema = z.string().trim().toLowerCase().max(SLUG_MAX_LENGTH).refine(isValidSlug, {
|
|
162
|
+
message: "Use letras min\xFAsculas, n\xFAmeros e h\xEDfen."
|
|
163
|
+
});
|
|
164
|
+
var httpUrlSchema = z.string().trim().max(POST_LIMITS.URL).url().refine((value) => /^https?:\/\//i.test(value), {
|
|
165
|
+
message: "Use um endere\xE7o http(s)."
|
|
166
|
+
});
|
|
167
|
+
var optionalText = /* @__PURE__ */ __name((max) => z.string().trim().max(max).nullable().optional(), "optionalText");
|
|
168
|
+
var postFieldsSchema = z.object({
|
|
169
|
+
title: z.string().trim().min(1).max(POST_LIMITS.TITLE),
|
|
170
|
+
slug: slugSchema.optional(),
|
|
171
|
+
excerpt: optionalText(POST_LIMITS.EXCERPT),
|
|
172
|
+
body: z.string().max(POST_LIMITS.BODY).default(""),
|
|
173
|
+
coverImageUrl: httpUrlSchema.nullable().optional(),
|
|
174
|
+
coverImageAlt: optionalText(POST_LIMITS.IMAGE_ALT),
|
|
175
|
+
tags: z.array(z.string().trim().min(1).max(TAG_MAX_LENGTH)).max(TAGS_MAX_COUNT).optional(),
|
|
176
|
+
authorName: optionalText(POST_LIMITS.AUTHOR_NAME),
|
|
177
|
+
seoTitle: optionalText(POST_LIMITS.SEO_TITLE),
|
|
178
|
+
seoDescription: optionalText(POST_LIMITS.SEO_DESCRIPTION),
|
|
179
|
+
canonicalUrl: httpUrlSchema.nullable().optional()
|
|
180
|
+
});
|
|
181
|
+
var createPostSchema = postFieldsSchema.strict();
|
|
182
|
+
var updatePostSchema = postFieldsSchema.extend({
|
|
183
|
+
body: z.string().max(POST_LIMITS.BODY)
|
|
184
|
+
}).partial().strict();
|
|
185
|
+
var publishPostSchema = z.object({
|
|
186
|
+
publishedAt: z.string().datetime({
|
|
187
|
+
offset: true
|
|
188
|
+
}).optional()
|
|
189
|
+
}).strict();
|
|
190
|
+
var pageSchema = z.coerce.number().int().min(1).max(1e4).default(1);
|
|
191
|
+
var listPostsQuerySchema = z.object({
|
|
192
|
+
page: pageSchema,
|
|
193
|
+
pageSize: z.coerce.number().int().min(1).max(POST_LIMITS.ADMIN_PAGE_SIZE).default(20),
|
|
194
|
+
status: z.enum(POST_STATUSES).optional(),
|
|
195
|
+
tag: z.string().trim().min(1).max(TAG_MAX_LENGTH).optional(),
|
|
196
|
+
search: z.string().trim().min(1).max(POST_LIMITS.SEARCH).optional()
|
|
197
|
+
});
|
|
198
|
+
var listPublishedPostsQuerySchema = z.object({
|
|
199
|
+
page: pageSchema,
|
|
200
|
+
pageSize: z.coerce.number().int().min(1).max(POST_LIMITS.PUBLIC_PAGE_SIZE).default(10),
|
|
201
|
+
tag: z.string().trim().min(1).max(TAG_MAX_LENGTH).optional()
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
// src/providers.ts
|
|
205
|
+
var POST_EVENT = {
|
|
206
|
+
CREATED: "created",
|
|
207
|
+
UPDATED: "updated",
|
|
208
|
+
PUBLISHED: "published",
|
|
209
|
+
UNPUBLISHED: "unpublished",
|
|
210
|
+
ARCHIVED: "archived"
|
|
211
|
+
};
|
|
212
|
+
var DEFAULT_ADMIN_SCOPES = [
|
|
213
|
+
"content:admin"
|
|
214
|
+
];
|
|
215
|
+
var DEFAULT_PUBLIC_CACHE_CONTROL = "public, max-age=60, s-maxage=300, stale-while-revalidate=600";
|
|
216
|
+
|
|
217
|
+
// src/markdown/sanitizeUrl.ts
|
|
218
|
+
var LINK_SCHEMES = /* @__PURE__ */ new Set([
|
|
219
|
+
"http",
|
|
220
|
+
"https",
|
|
221
|
+
"mailto",
|
|
222
|
+
"tel"
|
|
223
|
+
]);
|
|
224
|
+
var IMAGE_SCHEMES = /* @__PURE__ */ new Set([
|
|
225
|
+
"http",
|
|
226
|
+
"https"
|
|
227
|
+
]);
|
|
228
|
+
var SCHEME_PATTERN = /^([a-z][a-z0-9+.-]*):/i;
|
|
229
|
+
var INVISIBLE_PATTERN = new RegExp(
|
|
230
|
+
// eslint-disable-next-line no-control-regex -- casar caractere de controle é o objetivo
|
|
231
|
+
"[\\u0000-\\u0020\\u007f-\\u009f\\u00ad\\u200b-\\u200f\\u2028\\u2029\\ufeff]+",
|
|
232
|
+
"g"
|
|
233
|
+
);
|
|
234
|
+
function sanitize(url, allowedSchemes) {
|
|
235
|
+
const normalized = url.replace(INVISIBLE_PATTERN, "");
|
|
236
|
+
if (!normalized) return void 0;
|
|
237
|
+
const scheme = SCHEME_PATTERN.exec(normalized)?.[1];
|
|
238
|
+
if (scheme === void 0) return normalized;
|
|
239
|
+
return allowedSchemes.has(scheme.toLowerCase()) ? normalized : void 0;
|
|
240
|
+
}
|
|
241
|
+
__name(sanitize, "sanitize");
|
|
242
|
+
function sanitizeLinkUrl(url) {
|
|
243
|
+
return sanitize(url, LINK_SCHEMES);
|
|
244
|
+
}
|
|
245
|
+
__name(sanitizeLinkUrl, "sanitizeLinkUrl");
|
|
246
|
+
function sanitizeImageUrl(url) {
|
|
247
|
+
return sanitize(url, IMAGE_SCHEMES);
|
|
248
|
+
}
|
|
249
|
+
__name(sanitizeImageUrl, "sanitizeImageUrl");
|
|
250
|
+
function isAbsoluteHttpUrl(url) {
|
|
251
|
+
return /^https?:\/\//i.test(url) || url.startsWith("//");
|
|
252
|
+
}
|
|
253
|
+
__name(isAbsoluteHttpUrl, "isAbsoluteHttpUrl");
|
|
254
|
+
|
|
255
|
+
// src/markdown/parseInline.ts
|
|
256
|
+
var MAX_NESTING = 8;
|
|
257
|
+
var MAX_BRACKET_SCAN = 2e3;
|
|
258
|
+
var ESCAPABLE = new Set("\\`*_{}[]()#+-.!|~<>\"'");
|
|
259
|
+
var DELIMITERS = [
|
|
260
|
+
{
|
|
261
|
+
marker: "**",
|
|
262
|
+
type: "strong"
|
|
263
|
+
},
|
|
264
|
+
{
|
|
265
|
+
marker: "__",
|
|
266
|
+
type: "strong"
|
|
267
|
+
},
|
|
268
|
+
{
|
|
269
|
+
marker: "~~",
|
|
270
|
+
type: "strikethrough"
|
|
271
|
+
},
|
|
272
|
+
{
|
|
273
|
+
marker: "*",
|
|
274
|
+
type: "emphasis"
|
|
275
|
+
},
|
|
276
|
+
{
|
|
277
|
+
marker: "_",
|
|
278
|
+
type: "emphasis"
|
|
279
|
+
}
|
|
280
|
+
];
|
|
281
|
+
function parseInline(text, depth = 0) {
|
|
282
|
+
return new InlineParser({
|
|
283
|
+
text,
|
|
284
|
+
depth,
|
|
285
|
+
noCloserFrom: /* @__PURE__ */ new Map()
|
|
286
|
+
}).parse();
|
|
287
|
+
}
|
|
288
|
+
__name(parseInline, "parseInline");
|
|
289
|
+
function isWordCharacter(character) {
|
|
290
|
+
return character !== void 0 && /[\p{L}\p{N}]/u.test(character);
|
|
291
|
+
}
|
|
292
|
+
__name(isWordCharacter, "isWordCharacter");
|
|
293
|
+
function isWhitespace(character) {
|
|
294
|
+
return character === void 0 || /\s/.test(character);
|
|
295
|
+
}
|
|
296
|
+
__name(isWhitespace, "isWhitespace");
|
|
297
|
+
var InlineParser = class InlineParser2 {
|
|
298
|
+
static {
|
|
299
|
+
__name(this, "InlineParser");
|
|
300
|
+
}
|
|
301
|
+
state;
|
|
302
|
+
nodes = [];
|
|
303
|
+
buffer = "";
|
|
304
|
+
position = 0;
|
|
305
|
+
constructor(state) {
|
|
306
|
+
this.state = state;
|
|
307
|
+
}
|
|
308
|
+
parse() {
|
|
309
|
+
const { text } = this.state;
|
|
310
|
+
while (this.position < text.length) {
|
|
311
|
+
if (!this.tryToken()) {
|
|
312
|
+
this.buffer += text[this.position];
|
|
313
|
+
this.position += 1;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
this.flush();
|
|
317
|
+
return this.nodes;
|
|
318
|
+
}
|
|
319
|
+
tryToken() {
|
|
320
|
+
const character = this.state.text[this.position];
|
|
321
|
+
if (character === "\\") return this.tryEscape();
|
|
322
|
+
if (character === "\n") return this.tryLineBreak();
|
|
323
|
+
if (character === "`") return this.tryCodeSpan();
|
|
324
|
+
if (character === "!" && this.state.text[this.position + 1] === "[") return this.tryLinkOrImage(true);
|
|
325
|
+
if (character === "[") return this.tryLinkOrImage(false);
|
|
326
|
+
if (character === "<") return this.tryAutolink();
|
|
327
|
+
if (character === "*" || character === "_" || character === "~") return this.tryDelimited();
|
|
328
|
+
return false;
|
|
329
|
+
}
|
|
330
|
+
push(node) {
|
|
331
|
+
this.flush();
|
|
332
|
+
this.nodes.push(node);
|
|
333
|
+
}
|
|
334
|
+
flush() {
|
|
335
|
+
if (!this.buffer) return;
|
|
336
|
+
this.nodes.push({
|
|
337
|
+
type: "text",
|
|
338
|
+
value: this.buffer
|
|
339
|
+
});
|
|
340
|
+
this.buffer = "";
|
|
341
|
+
}
|
|
342
|
+
tryEscape() {
|
|
343
|
+
const next = this.state.text[this.position + 1];
|
|
344
|
+
if (next === "\n") {
|
|
345
|
+
this.push({
|
|
346
|
+
type: "break"
|
|
347
|
+
});
|
|
348
|
+
this.position += 2;
|
|
349
|
+
return true;
|
|
350
|
+
}
|
|
351
|
+
if (next === void 0 || !ESCAPABLE.has(next)) return false;
|
|
352
|
+
this.buffer += next;
|
|
353
|
+
this.position += 2;
|
|
354
|
+
return true;
|
|
355
|
+
}
|
|
356
|
+
/** Dois espaços antes da quebra = `<br>`; quebra simples vira espaço, como no CommonMark. */
|
|
357
|
+
tryLineBreak() {
|
|
358
|
+
if (this.buffer.endsWith(" ")) {
|
|
359
|
+
this.buffer = this.buffer.replace(/ +$/, "");
|
|
360
|
+
this.push({
|
|
361
|
+
type: "break"
|
|
362
|
+
});
|
|
363
|
+
} else {
|
|
364
|
+
this.buffer = `${this.buffer.replace(/ +$/, "")} `;
|
|
365
|
+
}
|
|
366
|
+
this.position += 1;
|
|
367
|
+
return true;
|
|
368
|
+
}
|
|
369
|
+
tryCodeSpan() {
|
|
370
|
+
const { text } = this.state;
|
|
371
|
+
let runEnd = this.position;
|
|
372
|
+
while (text[runEnd] === "`") runEnd += 1;
|
|
373
|
+
const run = text.slice(this.position, runEnd);
|
|
374
|
+
const memoKey = `code:${run.length}`;
|
|
375
|
+
if ((this.state.noCloserFrom.get(memoKey) ?? Infinity) <= runEnd) return this.literal(run);
|
|
376
|
+
let search = runEnd;
|
|
377
|
+
while (true) {
|
|
378
|
+
const found = text.indexOf(run, search);
|
|
379
|
+
if (found === -1) {
|
|
380
|
+
this.state.noCloserFrom.set(memoKey, runEnd);
|
|
381
|
+
return this.literal(run);
|
|
382
|
+
}
|
|
383
|
+
const closerEnd = found + run.length;
|
|
384
|
+
if (text[closerEnd] === "`" || text[found - 1] === "`") {
|
|
385
|
+
search = closerEnd;
|
|
386
|
+
while (text[search] === "`") search += 1;
|
|
387
|
+
continue;
|
|
388
|
+
}
|
|
389
|
+
let value = text.slice(runEnd, found).replace(/\n/g, " ");
|
|
390
|
+
if (value.length > 2 && value.startsWith(" ") && value.endsWith(" ") && value.trim()) value = value.slice(1, -1);
|
|
391
|
+
this.push({
|
|
392
|
+
type: "inlineCode",
|
|
393
|
+
value
|
|
394
|
+
});
|
|
395
|
+
this.position = closerEnd;
|
|
396
|
+
return true;
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
literal(value) {
|
|
400
|
+
this.buffer += value;
|
|
401
|
+
this.position += value.length;
|
|
402
|
+
return true;
|
|
403
|
+
}
|
|
404
|
+
tryLinkOrImage(isImage) {
|
|
405
|
+
const { text } = this.state;
|
|
406
|
+
const open = this.position + (isImage ? 1 : 0);
|
|
407
|
+
const close = findClosingBracket(text, open);
|
|
408
|
+
if (close === void 0 || text[close + 1] !== "(") return false;
|
|
409
|
+
const destination = parseDestination(text, close + 2);
|
|
410
|
+
if (!destination) return false;
|
|
411
|
+
const label = text.slice(open + 1, close);
|
|
412
|
+
this.position = destination.end;
|
|
413
|
+
if (isImage) {
|
|
414
|
+
this.pushImage(label, destination);
|
|
415
|
+
return true;
|
|
416
|
+
}
|
|
417
|
+
const children = this.parseNested(label);
|
|
418
|
+
const href = sanitizeLinkUrl(destination.url);
|
|
419
|
+
if (!href) {
|
|
420
|
+
this.flush();
|
|
421
|
+
this.nodes.push(...children);
|
|
422
|
+
return true;
|
|
423
|
+
}
|
|
424
|
+
this.push({
|
|
425
|
+
type: "link",
|
|
426
|
+
href,
|
|
427
|
+
...destination.title ? {
|
|
428
|
+
title: destination.title
|
|
429
|
+
} : {},
|
|
430
|
+
children
|
|
431
|
+
});
|
|
432
|
+
return true;
|
|
433
|
+
}
|
|
434
|
+
pushImage(alt, destination) {
|
|
435
|
+
const plainAlt = alt.replace(/[\\*_`~[\]]/g, "");
|
|
436
|
+
const src = sanitizeImageUrl(destination.url);
|
|
437
|
+
if (!src) {
|
|
438
|
+
this.buffer += plainAlt;
|
|
439
|
+
return;
|
|
440
|
+
}
|
|
441
|
+
this.push({
|
|
442
|
+
type: "image",
|
|
443
|
+
src,
|
|
444
|
+
alt: plainAlt,
|
|
445
|
+
...destination.title ? {
|
|
446
|
+
title: destination.title
|
|
447
|
+
} : {}
|
|
448
|
+
});
|
|
449
|
+
}
|
|
450
|
+
tryAutolink() {
|
|
451
|
+
const { text } = this.state;
|
|
452
|
+
const close = text.indexOf(">", this.position);
|
|
453
|
+
if (close === -1 || close - this.position > MAX_BRACKET_SCAN) return false;
|
|
454
|
+
const candidate = text.slice(this.position + 1, close);
|
|
455
|
+
if (!/^(https?:\/\/|mailto:)[^\s<>]+$/i.test(candidate)) return false;
|
|
456
|
+
const href = sanitizeLinkUrl(candidate);
|
|
457
|
+
if (!href) return false;
|
|
458
|
+
this.push({
|
|
459
|
+
type: "link",
|
|
460
|
+
href,
|
|
461
|
+
children: [
|
|
462
|
+
{
|
|
463
|
+
type: "text",
|
|
464
|
+
value: candidate
|
|
465
|
+
}
|
|
466
|
+
]
|
|
467
|
+
});
|
|
468
|
+
this.position = close + 1;
|
|
469
|
+
return true;
|
|
470
|
+
}
|
|
471
|
+
tryDelimited() {
|
|
472
|
+
const { text } = this.state;
|
|
473
|
+
const rule = DELIMITERS.find((candidate) => text.startsWith(candidate.marker, this.position));
|
|
474
|
+
if (!rule || this.state.depth >= MAX_NESTING) return false;
|
|
475
|
+
const contentStart = this.position + rule.marker.length;
|
|
476
|
+
if (isWhitespace(text[contentStart])) return false;
|
|
477
|
+
if (rule.marker.startsWith("_") && isWordCharacter(text[this.position - 1])) return false;
|
|
478
|
+
const closer = this.findCloser(rule, contentStart);
|
|
479
|
+
if (closer === void 0) return false;
|
|
480
|
+
this.push({
|
|
481
|
+
type: rule.type,
|
|
482
|
+
children: this.parseNested(text.slice(contentStart, closer))
|
|
483
|
+
});
|
|
484
|
+
this.position = closer + rule.marker.length;
|
|
485
|
+
return true;
|
|
486
|
+
}
|
|
487
|
+
findCloser(rule, contentStart) {
|
|
488
|
+
const { text, noCloserFrom } = this.state;
|
|
489
|
+
if ((noCloserFrom.get(rule.marker) ?? Infinity) <= contentStart) return void 0;
|
|
490
|
+
let search = contentStart + 1;
|
|
491
|
+
while (true) {
|
|
492
|
+
const found = text.indexOf(rule.marker, search);
|
|
493
|
+
if (found === -1) {
|
|
494
|
+
noCloserFrom.set(rule.marker, contentStart);
|
|
495
|
+
return void 0;
|
|
496
|
+
}
|
|
497
|
+
const after = text[found + rule.marker.length];
|
|
498
|
+
const valid = !isWhitespace(text[found - 1]) && // `*` simples não fecha no primeiro `*` de um `**`: aquele par é de negrito.
|
|
499
|
+
!(rule.marker.length === 1 && (after === rule.marker || text[found - 1] === rule.marker)) && !(rule.marker.startsWith("_") && isWordCharacter(after));
|
|
500
|
+
if (valid) return found;
|
|
501
|
+
search = found + 1;
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
parseNested(text) {
|
|
505
|
+
return new InlineParser2({
|
|
506
|
+
text,
|
|
507
|
+
depth: this.state.depth + 1,
|
|
508
|
+
noCloserFrom: /* @__PURE__ */ new Map()
|
|
509
|
+
}).parse();
|
|
510
|
+
}
|
|
511
|
+
};
|
|
512
|
+
function findClosingBracket(text, open) {
|
|
513
|
+
let depth = 0;
|
|
514
|
+
const limit = Math.min(text.length, open + MAX_BRACKET_SCAN);
|
|
515
|
+
for (let index = open; index < limit; index += 1) {
|
|
516
|
+
const character = text[index];
|
|
517
|
+
if (character === "\\") {
|
|
518
|
+
index += 1;
|
|
519
|
+
} else if (character === "[") {
|
|
520
|
+
depth += 1;
|
|
521
|
+
} else if (character === "]") {
|
|
522
|
+
depth -= 1;
|
|
523
|
+
if (depth === 0) return index;
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
return void 0;
|
|
527
|
+
}
|
|
528
|
+
__name(findClosingBracket, "findClosingBracket");
|
|
529
|
+
function parseDestination(text, start) {
|
|
530
|
+
const limit = Math.min(text.length, start + MAX_BRACKET_SCAN);
|
|
531
|
+
let index = start;
|
|
532
|
+
while (index < limit && text[index] === " ") index += 1;
|
|
533
|
+
let url = "";
|
|
534
|
+
if (text[index] === "<") {
|
|
535
|
+
const close = text.indexOf(">", index);
|
|
536
|
+
if (close === -1 || close >= limit) return void 0;
|
|
537
|
+
url = text.slice(index + 1, close);
|
|
538
|
+
index = close + 1;
|
|
539
|
+
} else {
|
|
540
|
+
let depth = 0;
|
|
541
|
+
while (index < limit) {
|
|
542
|
+
const character = text[index];
|
|
543
|
+
if (/\s/.test(character)) break;
|
|
544
|
+
if (character === "(") depth += 1;
|
|
545
|
+
if (character === ")") {
|
|
546
|
+
if (depth === 0) break;
|
|
547
|
+
depth -= 1;
|
|
548
|
+
}
|
|
549
|
+
url += character;
|
|
550
|
+
index += 1;
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
while (index < limit && text[index] === " ") index += 1;
|
|
554
|
+
let title;
|
|
555
|
+
const quote = text[index];
|
|
556
|
+
if (quote === '"' || quote === "'") {
|
|
557
|
+
const close = text.indexOf(quote, index + 1);
|
|
558
|
+
if (close === -1 || close >= limit) return void 0;
|
|
559
|
+
title = text.slice(index + 1, close);
|
|
560
|
+
index = close + 1;
|
|
561
|
+
while (index < limit && text[index] === " ") index += 1;
|
|
562
|
+
}
|
|
563
|
+
if (text[index] !== ")") return void 0;
|
|
564
|
+
return {
|
|
565
|
+
url,
|
|
566
|
+
...title ? {
|
|
567
|
+
title
|
|
568
|
+
} : {},
|
|
569
|
+
end: index + 1
|
|
570
|
+
};
|
|
571
|
+
}
|
|
572
|
+
__name(parseDestination, "parseDestination");
|
|
573
|
+
|
|
574
|
+
// src/markdown/parseMarkdown.ts
|
|
575
|
+
var MAX_BLOCK_DEPTH = 6;
|
|
576
|
+
var FENCE_PATTERN = /^ {0,3}(`{3,}|~{3,})\s*([^\s`]*)[^`]*$/;
|
|
577
|
+
var HEADING_PATTERN = /^ {0,3}(#{1,6})(?:[ \t]+(.*?))?(?:[ \t]+#+)?[ \t]*$/;
|
|
578
|
+
var THEMATIC_BREAK_PATTERN = /^ {0,3}([-*_])(?:[ \t]*\1){2,}[ \t]*$/;
|
|
579
|
+
var QUOTE_PATTERN = /^ {0,3}> ?(.*)$/;
|
|
580
|
+
var LIST_ITEM_PATTERN = /^( {0,3})([-*+]|\d{1,9}[.)])([ \t]+|$)(.*)$/;
|
|
581
|
+
function parseMarkdown(source) {
|
|
582
|
+
const normalized = source.replace(/\r\n?/g, "\n").replace(/\u0000/g, "\uFFFD").replace(/\t/g, " ");
|
|
583
|
+
return {
|
|
584
|
+
type: "root",
|
|
585
|
+
children: parseBlocks(normalized.split("\n"), 0)
|
|
586
|
+
};
|
|
587
|
+
}
|
|
588
|
+
__name(parseMarkdown, "parseMarkdown");
|
|
589
|
+
function parseBlocks(lines, depth) {
|
|
590
|
+
const blocks = [];
|
|
591
|
+
let index = 0;
|
|
592
|
+
while (index < lines.length) {
|
|
593
|
+
const line = lines[index];
|
|
594
|
+
if (!line.trim()) {
|
|
595
|
+
index += 1;
|
|
596
|
+
continue;
|
|
597
|
+
}
|
|
598
|
+
const parsed = parseBlockAt(lines, index, depth);
|
|
599
|
+
blocks.push(parsed.block);
|
|
600
|
+
index = parsed.next;
|
|
601
|
+
}
|
|
602
|
+
return blocks;
|
|
603
|
+
}
|
|
604
|
+
__name(parseBlocks, "parseBlocks");
|
|
605
|
+
function parseBlockAt(lines, index, depth) {
|
|
606
|
+
const line = lines[index];
|
|
607
|
+
if (FENCE_PATTERN.test(line)) return parseFence(lines, index);
|
|
608
|
+
const heading = HEADING_PATTERN.exec(line);
|
|
609
|
+
if (heading) {
|
|
610
|
+
const level = heading[1].length;
|
|
611
|
+
return {
|
|
612
|
+
block: {
|
|
613
|
+
type: "heading",
|
|
614
|
+
level,
|
|
615
|
+
children: parseInline(heading[2] ?? "")
|
|
616
|
+
},
|
|
617
|
+
next: index + 1
|
|
618
|
+
};
|
|
619
|
+
}
|
|
620
|
+
if (THEMATIC_BREAK_PATTERN.test(line)) return {
|
|
621
|
+
block: {
|
|
622
|
+
type: "thematicBreak"
|
|
623
|
+
},
|
|
624
|
+
next: index + 1
|
|
625
|
+
};
|
|
626
|
+
if (depth < MAX_BLOCK_DEPTH && QUOTE_PATTERN.test(line)) return parseQuote(lines, index, depth);
|
|
627
|
+
if (depth < MAX_BLOCK_DEPTH && LIST_ITEM_PATTERN.test(line)) return parseList(lines, index, depth);
|
|
628
|
+
return parseParagraph(lines, index, depth);
|
|
629
|
+
}
|
|
630
|
+
__name(parseBlockAt, "parseBlockAt");
|
|
631
|
+
function startsBlock(line, depth) {
|
|
632
|
+
return FENCE_PATTERN.test(line) || HEADING_PATTERN.test(line) || THEMATIC_BREAK_PATTERN.test(line) || depth < MAX_BLOCK_DEPTH && (QUOTE_PATTERN.test(line) || LIST_ITEM_PATTERN.test(line));
|
|
633
|
+
}
|
|
634
|
+
__name(startsBlock, "startsBlock");
|
|
635
|
+
function parseFence(lines, index) {
|
|
636
|
+
const opening = FENCE_PATTERN.exec(lines[index]);
|
|
637
|
+
const fence = opening[1];
|
|
638
|
+
const language = (opening[2] ?? "").toLowerCase().replace(/[^a-z0-9+#-]/g, "").slice(0, 30);
|
|
639
|
+
const content = [];
|
|
640
|
+
let cursor = index + 1;
|
|
641
|
+
while (cursor < lines.length) {
|
|
642
|
+
const line = lines[cursor];
|
|
643
|
+
const trimmed = line.trim();
|
|
644
|
+
if (trimmed.startsWith(fence[0]) && trimmed.length >= fence.length && /^(`+|~+)$/.test(trimmed)) {
|
|
645
|
+
cursor += 1;
|
|
646
|
+
break;
|
|
647
|
+
}
|
|
648
|
+
content.push(line);
|
|
649
|
+
cursor += 1;
|
|
650
|
+
}
|
|
651
|
+
const block = language ? {
|
|
652
|
+
type: "codeBlock",
|
|
653
|
+
language,
|
|
654
|
+
value: content.join("\n")
|
|
655
|
+
} : {
|
|
656
|
+
type: "codeBlock",
|
|
657
|
+
value: content.join("\n")
|
|
658
|
+
};
|
|
659
|
+
return {
|
|
660
|
+
block,
|
|
661
|
+
next: cursor
|
|
662
|
+
};
|
|
663
|
+
}
|
|
664
|
+
__name(parseFence, "parseFence");
|
|
665
|
+
function parseQuote(lines, index, depth) {
|
|
666
|
+
const inner = [];
|
|
667
|
+
let cursor = index;
|
|
668
|
+
while (cursor < lines.length) {
|
|
669
|
+
const match = QUOTE_PATTERN.exec(lines[cursor]);
|
|
670
|
+
if (!match) break;
|
|
671
|
+
inner.push(match[1] ?? "");
|
|
672
|
+
cursor += 1;
|
|
673
|
+
}
|
|
674
|
+
return {
|
|
675
|
+
block: {
|
|
676
|
+
type: "blockquote",
|
|
677
|
+
children: parseBlocks(inner, depth + 1)
|
|
678
|
+
},
|
|
679
|
+
next: cursor
|
|
680
|
+
};
|
|
681
|
+
}
|
|
682
|
+
__name(parseQuote, "parseQuote");
|
|
683
|
+
function parseParagraph(lines, index, depth) {
|
|
684
|
+
const content = [
|
|
685
|
+
lines[index].trimStart()
|
|
686
|
+
];
|
|
687
|
+
let cursor = index + 1;
|
|
688
|
+
while (cursor < lines.length) {
|
|
689
|
+
const line = lines[cursor];
|
|
690
|
+
if (!line.trim() || startsBlock(line, depth)) break;
|
|
691
|
+
content.push(line.trimStart());
|
|
692
|
+
cursor += 1;
|
|
693
|
+
}
|
|
694
|
+
return {
|
|
695
|
+
block: {
|
|
696
|
+
type: "paragraph",
|
|
697
|
+
children: parseInline(content.join("\n").trimEnd())
|
|
698
|
+
},
|
|
699
|
+
next: cursor
|
|
700
|
+
};
|
|
701
|
+
}
|
|
702
|
+
__name(parseParagraph, "parseParagraph");
|
|
703
|
+
function readMarker(line) {
|
|
704
|
+
const match = LIST_ITEM_PATTERN.exec(line);
|
|
705
|
+
if (!match) return void 0;
|
|
706
|
+
const indent = (match[1] ?? "").length;
|
|
707
|
+
const marker = match[2];
|
|
708
|
+
const spacing = (match[3] ?? "").length;
|
|
709
|
+
const ordered = /\d/.test(marker);
|
|
710
|
+
const contentIndent = indent + marker.length + (spacing === 0 || spacing > 4 ? 1 : spacing);
|
|
711
|
+
return {
|
|
712
|
+
ordered,
|
|
713
|
+
delimiter: ordered ? marker.slice(-1) : marker,
|
|
714
|
+
start: ordered ? Number.parseInt(marker, 10) : 1,
|
|
715
|
+
contentIndent,
|
|
716
|
+
text: match[4] ?? ""
|
|
717
|
+
};
|
|
718
|
+
}
|
|
719
|
+
__name(readMarker, "readMarker");
|
|
720
|
+
function indentOf(line) {
|
|
721
|
+
return line.length - line.trimStart().length;
|
|
722
|
+
}
|
|
723
|
+
__name(indentOf, "indentOf");
|
|
724
|
+
function parseList(lines, index, depth) {
|
|
725
|
+
const first = readMarker(lines[index]);
|
|
726
|
+
const items = [];
|
|
727
|
+
let tight = true;
|
|
728
|
+
let cursor = index;
|
|
729
|
+
while (cursor < lines.length) {
|
|
730
|
+
const marker = readMarker(lines[cursor]);
|
|
731
|
+
if (!marker || marker.ordered !== first.ordered || marker.delimiter !== first.delimiter) break;
|
|
732
|
+
const itemLines = [
|
|
733
|
+
marker.text
|
|
734
|
+
];
|
|
735
|
+
cursor += 1;
|
|
736
|
+
let sawBlank = false;
|
|
737
|
+
while (cursor < lines.length) {
|
|
738
|
+
const line = lines[cursor];
|
|
739
|
+
if (!line.trim()) {
|
|
740
|
+
sawBlank = true;
|
|
741
|
+
itemLines.push("");
|
|
742
|
+
cursor += 1;
|
|
743
|
+
continue;
|
|
744
|
+
}
|
|
745
|
+
if (indentOf(line) >= marker.contentIndent) {
|
|
746
|
+
itemLines.push(line.slice(marker.contentIndent));
|
|
747
|
+
cursor += 1;
|
|
748
|
+
continue;
|
|
749
|
+
}
|
|
750
|
+
if (!sawBlank && !startsBlock(line, depth)) {
|
|
751
|
+
itemLines.push(line.trimStart());
|
|
752
|
+
cursor += 1;
|
|
753
|
+
continue;
|
|
754
|
+
}
|
|
755
|
+
break;
|
|
756
|
+
}
|
|
757
|
+
while (itemLines.length > 0 && !itemLines[itemLines.length - 1].trim()) itemLines.pop();
|
|
758
|
+
if (itemLines.some((line) => !line.trim())) tight = false;
|
|
759
|
+
items.push(parseBlocks(itemLines, depth + 1));
|
|
760
|
+
const lookahead = lines[cursor];
|
|
761
|
+
if (sawBlank && lookahead !== void 0 && readMarker(lookahead)) tight = false;
|
|
762
|
+
}
|
|
763
|
+
const block = first.ordered ? {
|
|
764
|
+
type: "list",
|
|
765
|
+
ordered: true,
|
|
766
|
+
start: first.start,
|
|
767
|
+
tight,
|
|
768
|
+
items
|
|
769
|
+
} : {
|
|
770
|
+
type: "list",
|
|
771
|
+
ordered: false,
|
|
772
|
+
tight,
|
|
773
|
+
items
|
|
774
|
+
};
|
|
775
|
+
return {
|
|
776
|
+
block,
|
|
777
|
+
next: cursor
|
|
778
|
+
};
|
|
779
|
+
}
|
|
780
|
+
__name(parseList, "parseList");
|
|
781
|
+
|
|
782
|
+
// src/markdown/plainText.ts
|
|
783
|
+
function inlineToPlainText(nodes) {
|
|
784
|
+
return nodes.map((node) => {
|
|
785
|
+
switch (node.type) {
|
|
786
|
+
case "text":
|
|
787
|
+
case "inlineCode":
|
|
788
|
+
return node.value;
|
|
789
|
+
case "image":
|
|
790
|
+
return node.alt;
|
|
791
|
+
case "break":
|
|
792
|
+
return "\n";
|
|
793
|
+
default:
|
|
794
|
+
return inlineToPlainText(node.children);
|
|
795
|
+
}
|
|
796
|
+
}).join("");
|
|
797
|
+
}
|
|
798
|
+
__name(inlineToPlainText, "inlineToPlainText");
|
|
799
|
+
function blockToPlainText(block) {
|
|
800
|
+
switch (block.type) {
|
|
801
|
+
case "heading":
|
|
802
|
+
case "paragraph":
|
|
803
|
+
return inlineToPlainText(block.children);
|
|
804
|
+
case "codeBlock":
|
|
805
|
+
return block.value;
|
|
806
|
+
case "blockquote":
|
|
807
|
+
return block.children.map(blockToPlainText).join("\n\n");
|
|
808
|
+
case "list":
|
|
809
|
+
return block.items.map((item) => item.map(blockToPlainText).join("\n")).join("\n");
|
|
810
|
+
case "thematicBreak":
|
|
811
|
+
return "";
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
__name(blockToPlainText, "blockToPlainText");
|
|
815
|
+
function documentToPlainText(document) {
|
|
816
|
+
return document.children.map(blockToPlainText).filter((text) => text.length > 0).join("\n\n");
|
|
817
|
+
}
|
|
818
|
+
__name(documentToPlainText, "documentToPlainText");
|
|
819
|
+
function markdownToPlainText(markdown) {
|
|
820
|
+
return documentToPlainText(parseMarkdown(markdown));
|
|
821
|
+
}
|
|
822
|
+
__name(markdownToPlainText, "markdownToPlainText");
|
|
823
|
+
|
|
824
|
+
// src/readingTime.ts
|
|
825
|
+
var DEFAULT_WORDS_PER_MINUTE = 200;
|
|
826
|
+
function countWords(text) {
|
|
827
|
+
const words = text.trim().split(/\s+/);
|
|
828
|
+
return words[0] === "" ? 0 : words.length;
|
|
829
|
+
}
|
|
830
|
+
__name(countWords, "countWords");
|
|
831
|
+
function estimateReadingTimeMinutes(markdown, wordsPerMinute = DEFAULT_WORDS_PER_MINUTE) {
|
|
832
|
+
const words = countWords(markdownToPlainText(markdown));
|
|
833
|
+
return Math.max(1, Math.ceil(words / Math.max(1, wordsPerMinute)));
|
|
834
|
+
}
|
|
835
|
+
__name(estimateReadingTimeMinutes, "estimateReadingTimeMinutes");
|
|
836
|
+
function buildExcerpt(markdown, maxLength = 200) {
|
|
837
|
+
const text = markdownToPlainText(markdown).replace(/\s+/g, " ").trim();
|
|
838
|
+
if (text.length <= maxLength) return text;
|
|
839
|
+
const cut = text.slice(0, maxLength - 1);
|
|
840
|
+
const lastSpace = cut.lastIndexOf(" ");
|
|
841
|
+
return `${(lastSpace > maxLength / 2 ? cut.slice(0, lastSpace) : cut).replace(/[\s.,;:!?-]+$/, "")}\u2026`;
|
|
842
|
+
}
|
|
843
|
+
__name(buildExcerpt, "buildExcerpt");
|
|
844
|
+
|
|
845
|
+
// src/markdown/headingIds.ts
|
|
846
|
+
function createHeadingIdGenerator(prefix = "") {
|
|
847
|
+
const used = /* @__PURE__ */ new Map();
|
|
848
|
+
return (text) => {
|
|
849
|
+
const base = slugify(text) || "section";
|
|
850
|
+
const count = (used.get(base) ?? 0) + 1;
|
|
851
|
+
used.set(base, count);
|
|
852
|
+
return `${prefix}${count === 1 ? base : `${base}-${count}`}`;
|
|
853
|
+
};
|
|
854
|
+
}
|
|
855
|
+
__name(createHeadingIdGenerator, "createHeadingIdGenerator");
|
|
856
|
+
|
|
857
|
+
// src/markdown/renderHtml.ts
|
|
858
|
+
var DEFAULT_LINK_REL = "noopener noreferrer";
|
|
859
|
+
var ESCAPES = {
|
|
860
|
+
"&": "&",
|
|
861
|
+
"<": "<",
|
|
862
|
+
">": ">",
|
|
863
|
+
'"': """,
|
|
864
|
+
"'": "'"
|
|
865
|
+
};
|
|
866
|
+
function escapeHtml(value) {
|
|
867
|
+
return value.replace(/[&<>"']/g, (character) => ESCAPES[character] ?? character);
|
|
868
|
+
}
|
|
869
|
+
__name(escapeHtml, "escapeHtml");
|
|
870
|
+
function resolveHeadingLevel(level, offset = 0) {
|
|
871
|
+
return Math.min(6, Math.max(1, level + offset));
|
|
872
|
+
}
|
|
873
|
+
__name(resolveHeadingLevel, "resolveHeadingLevel");
|
|
874
|
+
function renderInline(nodes, context) {
|
|
875
|
+
return nodes.map((node) => renderInlineNode(node, context)).join("");
|
|
876
|
+
}
|
|
877
|
+
__name(renderInline, "renderInline");
|
|
878
|
+
function renderInlineNode(node, context) {
|
|
879
|
+
switch (node.type) {
|
|
880
|
+
case "text":
|
|
881
|
+
return escapeHtml(node.value);
|
|
882
|
+
case "strong":
|
|
883
|
+
return `<strong>${renderInline(node.children, context)}</strong>`;
|
|
884
|
+
case "emphasis":
|
|
885
|
+
return `<em>${renderInline(node.children, context)}</em>`;
|
|
886
|
+
case "strikethrough":
|
|
887
|
+
return `<del>${renderInline(node.children, context)}</del>`;
|
|
888
|
+
case "inlineCode":
|
|
889
|
+
return `<code>${escapeHtml(node.value)}</code>`;
|
|
890
|
+
case "break":
|
|
891
|
+
return "<br>";
|
|
892
|
+
case "image": {
|
|
893
|
+
const title = node.title ? ` title="${escapeHtml(node.title)}"` : "";
|
|
894
|
+
return `<img src="${escapeHtml(node.src)}" alt="${escapeHtml(node.alt)}"${title} loading="lazy" decoding="async">`;
|
|
895
|
+
}
|
|
896
|
+
case "link": {
|
|
897
|
+
const title = node.title ? ` title="${escapeHtml(node.title)}"` : "";
|
|
898
|
+
const newTab = (context.options.externalLinksInNewTab ?? true) && isAbsoluteHttpUrl(node.href);
|
|
899
|
+
const target = newTab ? ' target="_blank"' : "";
|
|
900
|
+
const rel = escapeHtml(context.options.linkRel ?? DEFAULT_LINK_REL);
|
|
901
|
+
return `<a href="${escapeHtml(node.href)}"${title}${target} rel="${rel}">${renderInline(node.children, context)}</a>`;
|
|
902
|
+
}
|
|
903
|
+
}
|
|
904
|
+
}
|
|
905
|
+
__name(renderInlineNode, "renderInlineNode");
|
|
906
|
+
function renderBlock(block, context, tight = false) {
|
|
907
|
+
switch (block.type) {
|
|
908
|
+
case "heading": {
|
|
909
|
+
const level = resolveHeadingLevel(block.level, context.options.headingLevelOffset);
|
|
910
|
+
const id = context.options.headingIds ?? true ? ` id="${escapeHtml(context.nextHeadingId(inlineToPlainText(block.children)))}"` : "";
|
|
911
|
+
return `<h${level}${id}>${renderInline(block.children, context)}</h${level}>`;
|
|
912
|
+
}
|
|
913
|
+
case "paragraph":
|
|
914
|
+
return tight ? renderInline(block.children, context) : `<p>${renderInline(block.children, context)}</p>`;
|
|
915
|
+
case "blockquote":
|
|
916
|
+
return `<blockquote>${block.children.map((child) => renderBlock(child, context)).join("")}</blockquote>`;
|
|
917
|
+
case "codeBlock": {
|
|
918
|
+
const className = block.language ? ` class="language-${escapeHtml(block.language)}"` : "";
|
|
919
|
+
return `<pre><code${className}>${escapeHtml(block.value)}</code></pre>`;
|
|
920
|
+
}
|
|
921
|
+
case "thematicBreak":
|
|
922
|
+
return "<hr>";
|
|
923
|
+
case "list": {
|
|
924
|
+
const tag = block.ordered ? "ol" : "ul";
|
|
925
|
+
const start = block.ordered && block.start !== void 0 && block.start !== 1 ? ` start="${block.start}"` : "";
|
|
926
|
+
const items = block.items.map((item) => `<li>${item.map((child) => renderBlock(child, context, block.tight)).join("")}</li>`).join("");
|
|
927
|
+
return `<${tag}${start}>${items}</${tag}>`;
|
|
928
|
+
}
|
|
929
|
+
}
|
|
930
|
+
}
|
|
931
|
+
__name(renderBlock, "renderBlock");
|
|
932
|
+
function renderMarkdownDocument(document, options = {}) {
|
|
933
|
+
const context = {
|
|
934
|
+
options,
|
|
935
|
+
nextHeadingId: createHeadingIdGenerator(options.headingIdPrefix)
|
|
936
|
+
};
|
|
937
|
+
return document.children.map((block) => renderBlock(block, context)).join("\n");
|
|
938
|
+
}
|
|
939
|
+
__name(renderMarkdownDocument, "renderMarkdownDocument");
|
|
940
|
+
function renderMarkdownToHtml(markdown, options = {}) {
|
|
941
|
+
return renderMarkdownDocument(parseMarkdown(markdown), options);
|
|
942
|
+
}
|
|
943
|
+
__name(renderMarkdownToHtml, "renderMarkdownToHtml");
|
|
944
|
+
|
|
945
|
+
// src/feed.ts
|
|
946
|
+
var DEFAULT_POST_PATH_TEMPLATE = "/blog/{slug}";
|
|
947
|
+
var XML_ESCAPES = {
|
|
948
|
+
"&": "&",
|
|
949
|
+
"<": "<",
|
|
950
|
+
">": ">",
|
|
951
|
+
'"': """,
|
|
952
|
+
"'": "'"
|
|
953
|
+
};
|
|
954
|
+
function escapeXml(value) {
|
|
955
|
+
return value.replace(/[\u0000-\u0008\u000b\u000c\u000e-\u001f\ufffe\uffff]/g, "").replace(/[&<>"']/g, (character) => XML_ESCAPES[character] ?? character);
|
|
956
|
+
}
|
|
957
|
+
__name(escapeXml, "escapeXml");
|
|
958
|
+
function buildPostUrl(params) {
|
|
959
|
+
const base = params.siteUrl.replace(/\/+$/, "");
|
|
960
|
+
const path = (params.postPathTemplate ?? DEFAULT_POST_PATH_TEMPLATE).replace("{slug}", encodeURIComponent(params.slug));
|
|
961
|
+
return `${base}${path.startsWith("/") ? path : `/${path}`}`;
|
|
962
|
+
}
|
|
963
|
+
__name(buildPostUrl, "buildPostUrl");
|
|
964
|
+
function toRfc822(iso) {
|
|
965
|
+
return new Date(iso).toUTCString();
|
|
966
|
+
}
|
|
967
|
+
__name(toRfc822, "toRfc822");
|
|
968
|
+
function rssItem(item) {
|
|
969
|
+
const parts = [
|
|
970
|
+
`<title>${escapeXml(item.title)}</title>`,
|
|
971
|
+
`<link>${escapeXml(item.url)}</link>`,
|
|
972
|
+
`<guid isPermaLink="true">${escapeXml(item.url)}</guid>`,
|
|
973
|
+
`<pubDate>${toRfc822(item.publishedAt)}</pubDate>`
|
|
974
|
+
];
|
|
975
|
+
if (item.summary) parts.push(`<description>${escapeXml(item.summary)}</description>`);
|
|
976
|
+
if (item.contentHtml) parts.push(`<content:encoded>${escapeXml(item.contentHtml)}</content:encoded>`);
|
|
977
|
+
if (item.authorName) parts.push(`<dc:creator>${escapeXml(item.authorName)}</dc:creator>`);
|
|
978
|
+
for (const category of item.categories ?? []) parts.push(`<category>${escapeXml(category)}</category>`);
|
|
979
|
+
if (item.imageUrl) parts.push(`<enclosure url="${escapeXml(item.imageUrl)}" type="image/*" length="0"/>`);
|
|
980
|
+
return `<item>${parts.join("")}</item>`;
|
|
981
|
+
}
|
|
982
|
+
__name(rssItem, "rssItem");
|
|
983
|
+
function buildRssFeed(params) {
|
|
984
|
+
const { channel } = params;
|
|
985
|
+
const head = [
|
|
986
|
+
`<title>${escapeXml(channel.title)}</title>`,
|
|
987
|
+
`<link>${escapeXml(channel.siteUrl)}</link>`,
|
|
988
|
+
`<description>${escapeXml(channel.description ?? channel.title)}</description>`
|
|
989
|
+
];
|
|
990
|
+
if (channel.language) head.push(`<language>${escapeXml(channel.language)}</language>`);
|
|
991
|
+
if (channel.feedUrl) head.push(`<atom:link href="${escapeXml(channel.feedUrl)}" rel="self" type="application/rss+xml"/>`);
|
|
992
|
+
const latest = params.items[0];
|
|
993
|
+
if (latest) head.push(`<lastBuildDate>${toRfc822(latest.updatedAt ?? latest.publishedAt)}</lastBuildDate>`);
|
|
994
|
+
return `<?xml version="1.0" encoding="UTF-8"?>
|
|
995
|
+
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel>${head.join("")}${params.items.map(rssItem).join("")}</channel></rss>
|
|
996
|
+
`;
|
|
997
|
+
}
|
|
998
|
+
__name(buildRssFeed, "buildRssFeed");
|
|
999
|
+
function atomEntry(item) {
|
|
1000
|
+
const parts = [
|
|
1001
|
+
`<title>${escapeXml(item.title)}</title>`,
|
|
1002
|
+
`<link href="${escapeXml(item.url)}"/>`,
|
|
1003
|
+
`<id>${escapeXml(item.url)}</id>`,
|
|
1004
|
+
`<published>${new Date(item.publishedAt).toISOString()}</published>`,
|
|
1005
|
+
`<updated>${new Date(item.updatedAt ?? item.publishedAt).toISOString()}</updated>`
|
|
1006
|
+
];
|
|
1007
|
+
if (item.summary) parts.push(`<summary>${escapeXml(item.summary)}</summary>`);
|
|
1008
|
+
if (item.contentHtml) parts.push(`<content type="html">${escapeXml(item.contentHtml)}</content>`);
|
|
1009
|
+
if (item.authorName) parts.push(`<author><name>${escapeXml(item.authorName)}</name></author>`);
|
|
1010
|
+
for (const category of item.categories ?? []) parts.push(`<category term="${escapeXml(category)}"/>`);
|
|
1011
|
+
return `<entry>${parts.join("")}</entry>`;
|
|
1012
|
+
}
|
|
1013
|
+
__name(atomEntry, "atomEntry");
|
|
1014
|
+
function buildAtomFeed(params) {
|
|
1015
|
+
const { channel } = params;
|
|
1016
|
+
const latest = params.items[0];
|
|
1017
|
+
const updated = latest ? new Date(latest.updatedAt ?? latest.publishedAt) : params.now ?? /* @__PURE__ */ new Date();
|
|
1018
|
+
const head = [
|
|
1019
|
+
`<title>${escapeXml(channel.title)}</title>`,
|
|
1020
|
+
`<id>${escapeXml(channel.feedUrl ?? channel.siteUrl)}</id>`,
|
|
1021
|
+
`<link href="${escapeXml(channel.siteUrl)}"/>`,
|
|
1022
|
+
`<updated>${updated.toISOString()}</updated>`
|
|
1023
|
+
];
|
|
1024
|
+
if (channel.feedUrl) head.push(`<link rel="self" href="${escapeXml(channel.feedUrl)}"/>`);
|
|
1025
|
+
if (channel.description) head.push(`<subtitle>${escapeXml(channel.description)}</subtitle>`);
|
|
1026
|
+
const language = channel.language ? ` xml:lang="${escapeXml(channel.language)}"` : "";
|
|
1027
|
+
return `<?xml version="1.0" encoding="UTF-8"?>
|
|
1028
|
+
<feed xmlns="http://www.w3.org/2005/Atom"${language}>${head.join("")}${params.items.map(atomEntry).join("")}</feed>
|
|
1029
|
+
`;
|
|
1030
|
+
}
|
|
1031
|
+
__name(buildAtomFeed, "buildAtomFeed");
|
|
1032
|
+
function buildSitemapXml(entries) {
|
|
1033
|
+
const urls = entries.map((entry) => `<url><loc>${escapeXml(entry.loc)}</loc>${entry.lastmod ? `<lastmod>${escapeXml(entry.lastmod)}</lastmod>` : ""}</url>`).join("");
|
|
1034
|
+
return `<?xml version="1.0" encoding="UTF-8"?>
|
|
1035
|
+
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">${urls}</urlset>
|
|
1036
|
+
`;
|
|
1037
|
+
}
|
|
1038
|
+
__name(buildSitemapXml, "buildSitemapXml");
|
|
1039
|
+
export {
|
|
1040
|
+
CONTENT_ERROR_CODES,
|
|
1041
|
+
ConfigMissingError,
|
|
1042
|
+
ContentError,
|
|
1043
|
+
DEFAULT_ADMIN_SCOPES,
|
|
1044
|
+
DEFAULT_LINK_REL,
|
|
1045
|
+
DEFAULT_POST_PATH_TEMPLATE,
|
|
1046
|
+
DEFAULT_PUBLIC_CACHE_CONTROL,
|
|
1047
|
+
DEFAULT_WORDS_PER_MINUTE,
|
|
1048
|
+
DuplicateSlugError,
|
|
1049
|
+
ImageStorageDisabledError,
|
|
1050
|
+
InvalidCoverImageError,
|
|
1051
|
+
InvalidSlugError,
|
|
1052
|
+
POST_EVENT,
|
|
1053
|
+
POST_LIMITS,
|
|
1054
|
+
POST_STATUS,
|
|
1055
|
+
POST_STATUSES,
|
|
1056
|
+
PostNotFoundError,
|
|
1057
|
+
SLUG_MAX_LENGTH,
|
|
1058
|
+
TAGS_MAX_COUNT,
|
|
1059
|
+
TAG_MAX_LENGTH,
|
|
1060
|
+
buildAtomFeed,
|
|
1061
|
+
buildExcerpt,
|
|
1062
|
+
buildPostUrl,
|
|
1063
|
+
buildRssFeed,
|
|
1064
|
+
buildSitemapXml,
|
|
1065
|
+
countWords,
|
|
1066
|
+
createHeadingIdGenerator,
|
|
1067
|
+
createPostSchema,
|
|
1068
|
+
documentToPlainText,
|
|
1069
|
+
escapeHtml,
|
|
1070
|
+
escapeXml,
|
|
1071
|
+
estimateReadingTimeMinutes,
|
|
1072
|
+
httpUrlSchema,
|
|
1073
|
+
inlineToPlainText,
|
|
1074
|
+
isAbsoluteHttpUrl,
|
|
1075
|
+
isPostStatus,
|
|
1076
|
+
isValidSlug,
|
|
1077
|
+
listPostsQuerySchema,
|
|
1078
|
+
listPublishedPostsQuerySchema,
|
|
1079
|
+
markdownToPlainText,
|
|
1080
|
+
normalizeTag,
|
|
1081
|
+
normalizeTags,
|
|
1082
|
+
parseInline,
|
|
1083
|
+
parseMarkdown,
|
|
1084
|
+
postFieldsSchema,
|
|
1085
|
+
publishPostSchema,
|
|
1086
|
+
renderMarkdownDocument,
|
|
1087
|
+
renderMarkdownToHtml,
|
|
1088
|
+
resolveHeadingLevel,
|
|
1089
|
+
sanitizeImageUrl,
|
|
1090
|
+
sanitizeLinkUrl,
|
|
1091
|
+
slugSchema,
|
|
1092
|
+
slugify,
|
|
1093
|
+
updatePostSchema,
|
|
1094
|
+
withSlugSuffix
|
|
1095
|
+
};
|
|
1096
|
+
//# sourceMappingURL=index.js.map
|