@prunellejs/content 0.0.0-stage → 0.6.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/llms.txt +81 -0
- package/package.json +24 -4
- package/src/app.ts +15 -0
- package/src/collection.ts +68 -0
- package/src/components.ts +533 -0
- package/src/entry.ts +170 -0
- package/src/file-collection.ts +302 -0
- package/src/frontmatter.ts +43 -0
- package/src/index.ts +215 -0
- package/src/loader-collection.ts +263 -0
- package/src/markdown.ts +183 -0
- package/src/registry.ts +135 -0
- package/src/rss.ts +105 -0
- package/src/sanitize.ts +309 -0
- package/src/service.ts +88 -0
- package/src/tracking.ts +80 -0
- package/src/types.ts +198 -0
- package/README.md +0 -3
|
@@ -0,0 +1,533 @@
|
|
|
1
|
+
// MDX-lite: literal prop parsing and embedded component expansion for Markdown bodies.
|
|
2
|
+
|
|
3
|
+
import type { MarkdownComponent, RenderMarkdownOptions } from './index'
|
|
4
|
+
import { ssrRaw } from 'prunelle/internal/ssr'
|
|
5
|
+
import { raw } from 'prunelle/server'
|
|
6
|
+
|
|
7
|
+
// Safe recursive-descent JSON5-literal parser for embedded Markdown component props.
|
|
8
|
+
// Supports objects (unquoted/quoted keys, trailing commas), arrays (trailing commas),
|
|
9
|
+
// single/double-quoted strings, numbers (sign, float, exponent, hex), booleans, null, undefined.
|
|
10
|
+
// Rejects all arbitrary JS expressions (calls, operators, identifiers, templates, etc.).
|
|
11
|
+
function parseLiteralValue(src: string): unknown {
|
|
12
|
+
let i = 0
|
|
13
|
+
const len = src.length
|
|
14
|
+
|
|
15
|
+
function skipWsAndComments(): void {
|
|
16
|
+
while (i < len) {
|
|
17
|
+
const ch = src[i]!
|
|
18
|
+
if (ch === ' ' || ch === '\t' || ch === '\n' || ch === '\r') {
|
|
19
|
+
i++
|
|
20
|
+
continue
|
|
21
|
+
}
|
|
22
|
+
if (ch === '/' && src[i + 1] === '/') {
|
|
23
|
+
i += 2
|
|
24
|
+
while (i < len && src[i] !== '\n') i++
|
|
25
|
+
continue
|
|
26
|
+
}
|
|
27
|
+
if (ch === '/' && src[i + 1] === '*') {
|
|
28
|
+
i += 2
|
|
29
|
+
const end = src.indexOf('*/', i)
|
|
30
|
+
if (end === -1)
|
|
31
|
+
throw new SyntaxError('Unterminated block comment')
|
|
32
|
+
i = end + 2
|
|
33
|
+
continue
|
|
34
|
+
}
|
|
35
|
+
break
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function parseString(quote: string): string {
|
|
40
|
+
i++ // skip opening quote
|
|
41
|
+
let out = ''
|
|
42
|
+
while (i < len) {
|
|
43
|
+
const ch = src[i]!
|
|
44
|
+
if (ch === quote) {
|
|
45
|
+
i++
|
|
46
|
+
return out
|
|
47
|
+
}
|
|
48
|
+
if (ch === '\\') {
|
|
49
|
+
i++
|
|
50
|
+
if (i >= len)
|
|
51
|
+
throw new SyntaxError('Unterminated string escape')
|
|
52
|
+
const esc = src[i++]!
|
|
53
|
+
switch (esc) {
|
|
54
|
+
case 'n': out += '\n'; break
|
|
55
|
+
case 'r': out += '\r'; break
|
|
56
|
+
case 't': out += '\t'; break
|
|
57
|
+
case 'b': out += '\b'; break
|
|
58
|
+
case 'f': out += '\f'; break
|
|
59
|
+
case 'v': out += '\v'; break
|
|
60
|
+
case '0': out += '\0'; break
|
|
61
|
+
case '\n': break
|
|
62
|
+
case '\r': if (src[i] === '\n') i++; break
|
|
63
|
+
case 'x': {
|
|
64
|
+
const hex = src.slice(i, i + 2)
|
|
65
|
+
if (!/^[0-9a-fA-F]{2}$/.test(hex))
|
|
66
|
+
throw new SyntaxError('Invalid hex escape')
|
|
67
|
+
out += String.fromCharCode(Number.parseInt(hex, 16))
|
|
68
|
+
i += 2
|
|
69
|
+
break
|
|
70
|
+
}
|
|
71
|
+
case 'u': {
|
|
72
|
+
if (src[i] === '{') {
|
|
73
|
+
const close = src.indexOf('}', i + 1)
|
|
74
|
+
if (close === -1)
|
|
75
|
+
throw new SyntaxError('Unterminated unicode escape')
|
|
76
|
+
const hex = src.slice(i + 1, close)
|
|
77
|
+
if (!/^[0-9a-fA-F]+$/.test(hex))
|
|
78
|
+
throw new SyntaxError('Invalid unicode escape')
|
|
79
|
+
out += String.fromCodePoint(Number.parseInt(hex, 16))
|
|
80
|
+
i = close + 1
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
const hex = src.slice(i, i + 4)
|
|
84
|
+
if (!/^[0-9a-fA-F]{4}$/.test(hex))
|
|
85
|
+
throw new SyntaxError('Invalid unicode escape')
|
|
86
|
+
out += String.fromCharCode(Number.parseInt(hex, 16))
|
|
87
|
+
i += 4
|
|
88
|
+
}
|
|
89
|
+
break
|
|
90
|
+
}
|
|
91
|
+
default: out += esc; break
|
|
92
|
+
}
|
|
93
|
+
continue
|
|
94
|
+
}
|
|
95
|
+
if (ch === '\n' || ch === '\r')
|
|
96
|
+
throw new SyntaxError('Unescaped newline in string')
|
|
97
|
+
out += ch
|
|
98
|
+
i++
|
|
99
|
+
}
|
|
100
|
+
throw new SyntaxError('Unterminated string literal')
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function parseNumber(): number {
|
|
104
|
+
const start = i
|
|
105
|
+
if (src[i] === '+' || src[i] === '-')
|
|
106
|
+
i++
|
|
107
|
+
if (src[i] === '0' && (src[i + 1] === 'x' || src[i + 1] === 'X')) {
|
|
108
|
+
i += 2
|
|
109
|
+
const hexStart = i
|
|
110
|
+
while (i < len && /[0-9a-fA-F]/.test(src[i]!)) i++
|
|
111
|
+
if (i === hexStart)
|
|
112
|
+
throw new SyntaxError('Invalid hex number')
|
|
113
|
+
const sign = src[start] === '-' ? -1 : 1
|
|
114
|
+
return sign * Number.parseInt(src.slice(hexStart, i), 16)
|
|
115
|
+
}
|
|
116
|
+
let hasDigits = false
|
|
117
|
+
while (i < len && /[0-9]/.test(src[i]!)) {
|
|
118
|
+
hasDigits = true
|
|
119
|
+
i++
|
|
120
|
+
}
|
|
121
|
+
if (src[i] === '.') {
|
|
122
|
+
i++
|
|
123
|
+
while (i < len && /[0-9]/.test(src[i]!)) {
|
|
124
|
+
hasDigits = true
|
|
125
|
+
i++
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
if (!hasDigits)
|
|
129
|
+
throw new SyntaxError('Invalid number')
|
|
130
|
+
if (src[i] === 'e' || src[i] === 'E') {
|
|
131
|
+
i++
|
|
132
|
+
if (src[i] === '+' || src[i] === '-')
|
|
133
|
+
i++
|
|
134
|
+
const expStart = i
|
|
135
|
+
while (i < len && /[0-9]/.test(src[i]!)) i++
|
|
136
|
+
if (i === expStart)
|
|
137
|
+
throw new SyntaxError('Invalid exponent')
|
|
138
|
+
}
|
|
139
|
+
const num = Number(src.slice(start, i))
|
|
140
|
+
if (Number.isNaN(num))
|
|
141
|
+
throw new SyntaxError('Invalid number')
|
|
142
|
+
return num
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function parseArray(): unknown[] {
|
|
146
|
+
i++ // skip '['
|
|
147
|
+
const arr: unknown[] = []
|
|
148
|
+
while (true) {
|
|
149
|
+
skipWsAndComments()
|
|
150
|
+
if (i >= len)
|
|
151
|
+
throw new SyntaxError('Unterminated array')
|
|
152
|
+
if (src[i] === ']') {
|
|
153
|
+
i++
|
|
154
|
+
return arr
|
|
155
|
+
}
|
|
156
|
+
arr.push(parseValue())
|
|
157
|
+
skipWsAndComments()
|
|
158
|
+
if (src[i] === ',') {
|
|
159
|
+
i++
|
|
160
|
+
continue
|
|
161
|
+
}
|
|
162
|
+
if (src[i] === ']') {
|
|
163
|
+
i++
|
|
164
|
+
return arr
|
|
165
|
+
}
|
|
166
|
+
throw new SyntaxError('Expected "," or "]" in array')
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function parseObject(): Record<string, unknown> {
|
|
171
|
+
i++ // skip '{'
|
|
172
|
+
const obj: Record<string, unknown> = {}
|
|
173
|
+
while (true) {
|
|
174
|
+
skipWsAndComments()
|
|
175
|
+
if (i >= len)
|
|
176
|
+
throw new SyntaxError('Unterminated object')
|
|
177
|
+
if (src[i] === '}') {
|
|
178
|
+
i++
|
|
179
|
+
return obj
|
|
180
|
+
}
|
|
181
|
+
let key: string
|
|
182
|
+
const ch = src[i]!
|
|
183
|
+
if (ch === '"' || ch === '\'') {
|
|
184
|
+
key = parseString(ch)
|
|
185
|
+
}
|
|
186
|
+
else if (/[A-Za-z_$]/.test(ch)) {
|
|
187
|
+
const keyStart = i
|
|
188
|
+
while (i < len && /[A-Za-z0-9_$]/.test(src[i]!)) i++
|
|
189
|
+
key = src.slice(keyStart, i)
|
|
190
|
+
}
|
|
191
|
+
else {
|
|
192
|
+
throw new SyntaxError('Expected object key')
|
|
193
|
+
}
|
|
194
|
+
skipWsAndComments()
|
|
195
|
+
if (src[i] !== ':')
|
|
196
|
+
throw new SyntaxError('Expected ":" after object key')
|
|
197
|
+
i++
|
|
198
|
+
const val = parseValue()
|
|
199
|
+
if (key !== '__proto__' && key !== 'constructor' && key !== 'prototype') {
|
|
200
|
+
Object.defineProperty(obj, key, {
|
|
201
|
+
value: val,
|
|
202
|
+
enumerable: true,
|
|
203
|
+
writable: true,
|
|
204
|
+
configurable: true,
|
|
205
|
+
})
|
|
206
|
+
}
|
|
207
|
+
skipWsAndComments()
|
|
208
|
+
if (src[i] === ',') {
|
|
209
|
+
i++
|
|
210
|
+
continue
|
|
211
|
+
}
|
|
212
|
+
if (src[i] === '}') {
|
|
213
|
+
i++
|
|
214
|
+
return obj
|
|
215
|
+
}
|
|
216
|
+
throw new SyntaxError('Expected "," or "}" in object')
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function parseValue(): unknown {
|
|
221
|
+
skipWsAndComments()
|
|
222
|
+
if (i >= len)
|
|
223
|
+
throw new SyntaxError('Unexpected end of literal')
|
|
224
|
+
const ch = src[i]!
|
|
225
|
+
if (ch === '"' || ch === '\'')
|
|
226
|
+
return parseString(ch)
|
|
227
|
+
if (ch === '[')
|
|
228
|
+
return parseArray()
|
|
229
|
+
if (ch === '{')
|
|
230
|
+
return parseObject()
|
|
231
|
+
if (ch === '-' || ch === '+' || ch === '.' || (ch >= '0' && ch <= '9'))
|
|
232
|
+
return parseNumber()
|
|
233
|
+
if (src.startsWith('true', i) && !/[A-Za-z0-9_$]/.test(src[i + 4] ?? '')) {
|
|
234
|
+
i += 4
|
|
235
|
+
return true
|
|
236
|
+
}
|
|
237
|
+
if (src.startsWith('false', i) && !/[A-Za-z0-9_$]/.test(src[i + 5] ?? '')) {
|
|
238
|
+
i += 5
|
|
239
|
+
return false
|
|
240
|
+
}
|
|
241
|
+
if (src.startsWith('null', i) && !/[A-Za-z0-9_$]/.test(src[i + 4] ?? '')) {
|
|
242
|
+
i += 4
|
|
243
|
+
return null
|
|
244
|
+
}
|
|
245
|
+
if (src.startsWith('undefined', i) && !/[A-Za-z0-9_$]/.test(src[i + 9] ?? '')) {
|
|
246
|
+
i += 9
|
|
247
|
+
return undefined
|
|
248
|
+
}
|
|
249
|
+
throw new SyntaxError(`Unsupported non-literal expression at offset ${i}`)
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
const value = parseValue()
|
|
253
|
+
skipWsAndComments()
|
|
254
|
+
if (i !== len)
|
|
255
|
+
throw new SyntaxError(`Unexpected trailing token at offset ${i}`)
|
|
256
|
+
return value
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
// Parse JSX-style attributes inside embedded Markdown components: title="hi" count={42} rows={[...]} flag
|
|
260
|
+
function parseComponentProps(attrSrc: string): Record<string, unknown> {
|
|
261
|
+
const props: Record<string, unknown> = {}
|
|
262
|
+
let i = 0
|
|
263
|
+
const len = attrSrc.length
|
|
264
|
+
|
|
265
|
+
while (i < len) {
|
|
266
|
+
while (i < len && /\s/.test(attrSrc[i]!)) i++
|
|
267
|
+
if (i >= len) break
|
|
268
|
+
|
|
269
|
+
const nameStart = i
|
|
270
|
+
while (i < len && /[\w:-]/.test(attrSrc[i]!)) i++
|
|
271
|
+
const name = attrSrc.slice(nameStart, i)
|
|
272
|
+
if (!name) {
|
|
273
|
+
i++
|
|
274
|
+
continue
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
while (i < len && /\s/.test(attrSrc[i]!)) i++
|
|
278
|
+
if (attrSrc[i] !== '=') {
|
|
279
|
+
props[name] = true
|
|
280
|
+
continue
|
|
281
|
+
}
|
|
282
|
+
i++ // skip '='
|
|
283
|
+
while (i < len && /\s/.test(attrSrc[i]!)) i++
|
|
284
|
+
|
|
285
|
+
const ch = attrSrc[i]
|
|
286
|
+
if (ch === '"' || ch === '\'') {
|
|
287
|
+
const quote = ch
|
|
288
|
+
i++
|
|
289
|
+
let val = ''
|
|
290
|
+
while (i < len && attrSrc[i] !== quote) {
|
|
291
|
+
if (attrSrc[i] === '\\' && i + 1 < len) {
|
|
292
|
+
const next = attrSrc[i + 1]!
|
|
293
|
+
if (next === quote || next === '\\') {
|
|
294
|
+
val += next
|
|
295
|
+
i += 2
|
|
296
|
+
continue
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
val += attrSrc[i]!
|
|
300
|
+
i++
|
|
301
|
+
}
|
|
302
|
+
if (name !== '__proto__' && name !== 'constructor' && name !== 'prototype')
|
|
303
|
+
props[name] = val
|
|
304
|
+
if (i < len) i++
|
|
305
|
+
}
|
|
306
|
+
else if (ch === '{') {
|
|
307
|
+
let depth = 0
|
|
308
|
+
const exprStart = i + 1
|
|
309
|
+
let inStr: string | null = null
|
|
310
|
+
while (i < len) {
|
|
311
|
+
const c = attrSrc[i]!
|
|
312
|
+
if (inStr !== null) {
|
|
313
|
+
if (c === '\\') { i += 2; continue }
|
|
314
|
+
if (c === inStr) inStr = null
|
|
315
|
+
}
|
|
316
|
+
else {
|
|
317
|
+
if (c === '"' || c === '\'' || c === '`') inStr = c
|
|
318
|
+
else if (c === '{') depth++
|
|
319
|
+
else if (c === '}') {
|
|
320
|
+
depth--
|
|
321
|
+
if (depth === 0) break
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
i++
|
|
325
|
+
}
|
|
326
|
+
const expr = attrSrc.slice(exprStart, i).trim()
|
|
327
|
+
i++ // skip closing '}'
|
|
328
|
+
try {
|
|
329
|
+
props[name] = parseLiteralValue(expr)
|
|
330
|
+
}
|
|
331
|
+
catch {
|
|
332
|
+
// Reject/ignore arbitrary non-literal JS expressions
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
return props
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
// Find the end of an opening JSX tag (`>` or `/>`), respecting strings and balanced `{...}` expressions.
|
|
340
|
+
function findTagEnd(src: string, start: number): { end: number, selfClosing: boolean } | undefined {
|
|
341
|
+
let i = start
|
|
342
|
+
const len = src.length
|
|
343
|
+
let braceDepth = 0
|
|
344
|
+
let inStr: string | null = null
|
|
345
|
+
|
|
346
|
+
while (i < len) {
|
|
347
|
+
const c = src[i]!
|
|
348
|
+
if (inStr !== null) {
|
|
349
|
+
if (c === '\\') { i += 2; continue }
|
|
350
|
+
if (c === inStr) inStr = null
|
|
351
|
+
i++
|
|
352
|
+
continue
|
|
353
|
+
}
|
|
354
|
+
if (c === '"' || c === '\'' || c === '`') {
|
|
355
|
+
inStr = c
|
|
356
|
+
i++
|
|
357
|
+
continue
|
|
358
|
+
}
|
|
359
|
+
if (c === '{') {
|
|
360
|
+
braceDepth++
|
|
361
|
+
i++
|
|
362
|
+
continue
|
|
363
|
+
}
|
|
364
|
+
if (c === '}') {
|
|
365
|
+
braceDepth = Math.max(0, braceDepth - 1)
|
|
366
|
+
i++
|
|
367
|
+
continue
|
|
368
|
+
}
|
|
369
|
+
if (braceDepth === 0) {
|
|
370
|
+
if (c === '/' && src[i + 1] === '>')
|
|
371
|
+
return { end: i + 2, selfClosing: true }
|
|
372
|
+
if (c === '>')
|
|
373
|
+
return { end: i + 1, selfClosing: false }
|
|
374
|
+
}
|
|
375
|
+
i++
|
|
376
|
+
}
|
|
377
|
+
return undefined
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
// Returns true when the character at `idx` is escaped by an odd number of preceding backslashes.
|
|
381
|
+
function isBackslashEscaped(src: string, idx: number): boolean {
|
|
382
|
+
let slashes = 0
|
|
383
|
+
for (let k = idx - 1; k >= 0 && src[k] === '\\'; k--)
|
|
384
|
+
slashes++
|
|
385
|
+
return (slashes & 1) === 1
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
// Advances past a CommonMark inline code span starting at backtick offset `start`.
|
|
389
|
+
// Returns the offset right after the matching closing backtick run, or -1 if unclosed.
|
|
390
|
+
function matchInlineCodeSpan(src: string, start: number): number {
|
|
391
|
+
let ticks = 0
|
|
392
|
+
while (start + ticks < src.length && src[start + ticks] === '`')
|
|
393
|
+
ticks++
|
|
394
|
+
let pos = start + ticks
|
|
395
|
+
while (pos < src.length) {
|
|
396
|
+
if (src[pos] === '`') {
|
|
397
|
+
let run = 0
|
|
398
|
+
while (pos + run < src.length && src[pos + run] === '`')
|
|
399
|
+
run++
|
|
400
|
+
if (run === ticks)
|
|
401
|
+
return pos + run
|
|
402
|
+
pos += run
|
|
403
|
+
continue
|
|
404
|
+
}
|
|
405
|
+
pos++
|
|
406
|
+
}
|
|
407
|
+
return -1
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
function findMatchingCloseTag(md: string, tagName: string, start: number): number {
|
|
411
|
+
const closeTag = `</${tagName}>`
|
|
412
|
+
const len = md.length
|
|
413
|
+
let depth = 1
|
|
414
|
+
let pos = start
|
|
415
|
+
while (pos < len) {
|
|
416
|
+
const ch = md[pos]!
|
|
417
|
+
if (ch === '`' && !isBackslashEscaped(md, pos)) {
|
|
418
|
+
const spanEnd = matchInlineCodeSpan(md, pos)
|
|
419
|
+
if (spanEnd !== -1) {
|
|
420
|
+
pos = spanEnd
|
|
421
|
+
continue
|
|
422
|
+
}
|
|
423
|
+
while (pos < len && md[pos] === '`') pos++
|
|
424
|
+
continue
|
|
425
|
+
}
|
|
426
|
+
if (ch === '<') {
|
|
427
|
+
if (md.startsWith(closeTag, pos)) {
|
|
428
|
+
depth--
|
|
429
|
+
if (depth === 0)
|
|
430
|
+
return pos
|
|
431
|
+
pos += closeTag.length
|
|
432
|
+
continue
|
|
433
|
+
}
|
|
434
|
+
if (md.startsWith(`<${tagName}`, pos) && /[\s/>]/.test(md[pos + 1 + tagName.length] ?? '')) {
|
|
435
|
+
const innerEnd = findTagEnd(md, pos + 1 + tagName.length)
|
|
436
|
+
if (innerEnd !== undefined) {
|
|
437
|
+
if (!innerEnd.selfClosing)
|
|
438
|
+
depth++
|
|
439
|
+
pos = innerEnd.end
|
|
440
|
+
continue
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
pos++
|
|
445
|
+
}
|
|
446
|
+
return -1
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
export function expandComponents(
|
|
450
|
+
md: string,
|
|
451
|
+
components: Record<string, MarkdownComponent>,
|
|
452
|
+
options: RenderMarkdownOptions,
|
|
453
|
+
slots: string[],
|
|
454
|
+
slotPrefix: string,
|
|
455
|
+
renderMarkdown: (md: string, options: RenderMarkdownOptions) => { html: string },
|
|
456
|
+
): string {
|
|
457
|
+
const names = Object.keys(components)
|
|
458
|
+
if (names.length === 0)
|
|
459
|
+
return md
|
|
460
|
+
|
|
461
|
+
let out = ''
|
|
462
|
+
let i = 0
|
|
463
|
+
const len = md.length
|
|
464
|
+
|
|
465
|
+
while (i < len) {
|
|
466
|
+
const lt = md.indexOf('<', i)
|
|
467
|
+
const tick = md.indexOf('`', i)
|
|
468
|
+
|
|
469
|
+
// Skip CommonMark inline code spans so `<Component />` inside backticks is never expanded.
|
|
470
|
+
if (tick !== -1 && (lt === -1 || tick < lt)) {
|
|
471
|
+
if (!isBackslashEscaped(md, tick)) {
|
|
472
|
+
const spanEnd = matchInlineCodeSpan(md, tick)
|
|
473
|
+
if (spanEnd !== -1) {
|
|
474
|
+
out += md.slice(i, spanEnd)
|
|
475
|
+
i = spanEnd
|
|
476
|
+
continue
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
let endTick = tick + 1
|
|
480
|
+
while (endTick < len && md[endTick] === '`') endTick++
|
|
481
|
+
out += md.slice(i, endTick)
|
|
482
|
+
i = endTick
|
|
483
|
+
continue
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
if (lt === -1) {
|
|
487
|
+
out += md.slice(i)
|
|
488
|
+
break
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
const match = /^<([A-Z][\w]*)(?=[\s/>])/.exec(md.slice(lt))
|
|
492
|
+
if (!match || !Object.hasOwn(components, match[1]!)) {
|
|
493
|
+
out += md.slice(i, lt + 1)
|
|
494
|
+
i = lt + 1
|
|
495
|
+
continue
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
const tagName = match[1]!
|
|
499
|
+
const tagEnd = findTagEnd(md, lt + match[0].length)
|
|
500
|
+
if (!tagEnd) {
|
|
501
|
+
out += md.slice(i, lt + 1)
|
|
502
|
+
i = lt + 1
|
|
503
|
+
continue
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
out += md.slice(i, lt)
|
|
507
|
+
const attrSrc = md.slice(lt + match[0].length, tagEnd.selfClosing ? tagEnd.end - 2 : tagEnd.end - 1)
|
|
508
|
+
const props = parseComponentProps(attrSrc)
|
|
509
|
+
let afterIndex = tagEnd.end
|
|
510
|
+
|
|
511
|
+
if (!tagEnd.selfClosing) {
|
|
512
|
+
const closeTag = `</${tagName}>`
|
|
513
|
+
const closeIdx = findMatchingCloseTag(md, tagName, tagEnd.end)
|
|
514
|
+
if (closeIdx !== -1) {
|
|
515
|
+
const innerMd = md.slice(tagEnd.end, closeIdx).trim()
|
|
516
|
+
const innerRendered = renderMarkdown(innerMd, options)
|
|
517
|
+
props.children = raw(innerRendered.html.trim())
|
|
518
|
+
afterIndex = closeIdx + closeTag.length
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
if (options.locale !== undefined && props.locale === undefined)
|
|
523
|
+
props.locale = options.locale
|
|
524
|
+
|
|
525
|
+
const renderedHtml = ssrRaw(components[tagName]!(props as never))
|
|
526
|
+
const slotIdx = slots.length
|
|
527
|
+
slots.push(renderedHtml)
|
|
528
|
+
out += `\n\n<div data-pru-md-slot="${slotPrefix}${slotIdx}"></div>\n\n`
|
|
529
|
+
i = afterIndex
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
return out
|
|
533
|
+
}
|
package/src/entry.ts
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
// Shared entry construction, lazy Markdown render-config resolution, sorting, and filtering helpers.
|
|
2
|
+
|
|
3
|
+
import type {
|
|
4
|
+
BaseCollectionOptions,
|
|
5
|
+
CollectionRenderConfig,
|
|
6
|
+
ContentEntry,
|
|
7
|
+
RenderedMarkdown,
|
|
8
|
+
RenderMarkdownOptions,
|
|
9
|
+
} from './types'
|
|
10
|
+
import { currentApp } from './app'
|
|
11
|
+
import { renderMarkdown } from './markdown'
|
|
12
|
+
import { shouldWrapCmsBody, wrapCmsBodyHtml } from './tracking'
|
|
13
|
+
|
|
14
|
+
export interface RenderConfigLoader {
|
|
15
|
+
ensureSync: () => RenderMarkdownOptions
|
|
16
|
+
ensureAsync: () => Promise<RenderMarkdownOptions>
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export type RenderConfigOptions = Pick<
|
|
20
|
+
BaseCollectionOptions<never>,
|
|
21
|
+
'highlight' | 'components' | 'headingLinks' | 'resolveImage' | 'trusted' | 'render'
|
|
22
|
+
>
|
|
23
|
+
|
|
24
|
+
export function createRenderConfigLoader(
|
|
25
|
+
label: string,
|
|
26
|
+
options: RenderConfigOptions,
|
|
27
|
+
): RenderConfigLoader {
|
|
28
|
+
const baseRenderOpts: RenderMarkdownOptions = {
|
|
29
|
+
highlight: options.highlight,
|
|
30
|
+
components: options.components,
|
|
31
|
+
headingLinks: options.headingLinks,
|
|
32
|
+
resolveImage: options.resolveImage,
|
|
33
|
+
trusted: options.trusted,
|
|
34
|
+
}
|
|
35
|
+
let resolvedRenderOpts: RenderMarkdownOptions | undefined = options.render === undefined ? baseRenderOpts : undefined
|
|
36
|
+
let pendingRenderOpts: Promise<RenderMarkdownOptions> | undefined
|
|
37
|
+
|
|
38
|
+
function mergeRenderConfig(cfg: CollectionRenderConfig): RenderMarkdownOptions {
|
|
39
|
+
return {
|
|
40
|
+
highlight: cfg.highlight ?? baseRenderOpts.highlight,
|
|
41
|
+
components: cfg.components ?? baseRenderOpts.components,
|
|
42
|
+
headingLinks: cfg.headingLinks ?? baseRenderOpts.headingLinks,
|
|
43
|
+
resolveImage: cfg.resolveImage !== undefined ? cfg.resolveImage : baseRenderOpts.resolveImage,
|
|
44
|
+
trusted: cfg.trusted !== undefined ? cfg.trusted : baseRenderOpts.trusted,
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function ensureSync(): RenderMarkdownOptions {
|
|
49
|
+
if (resolvedRenderOpts !== undefined)
|
|
50
|
+
return resolvedRenderOpts
|
|
51
|
+
const loaded = options.render!()
|
|
52
|
+
if (loaded instanceof Promise) {
|
|
53
|
+
pendingRenderOpts ??= loaded.then((cfg) => {
|
|
54
|
+
resolvedRenderOpts = mergeRenderConfig(cfg)
|
|
55
|
+
return resolvedRenderOpts
|
|
56
|
+
}).catch((err) => {
|
|
57
|
+
pendingRenderOpts = undefined
|
|
58
|
+
throw err
|
|
59
|
+
})
|
|
60
|
+
throw new Error(
|
|
61
|
+
`@prunellejs/content: collection "${label}" uses an async "render" loader; call \`await entry.render()\` (e.g. in \`load()\`) before reading \`entry.html\` or \`entry.headings\`.`,
|
|
62
|
+
)
|
|
63
|
+
}
|
|
64
|
+
resolvedRenderOpts = mergeRenderConfig(loaded)
|
|
65
|
+
return resolvedRenderOpts
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
async function ensureAsync(): Promise<RenderMarkdownOptions> {
|
|
69
|
+
if (resolvedRenderOpts !== undefined)
|
|
70
|
+
return resolvedRenderOpts
|
|
71
|
+
pendingRenderOpts ??= Promise.resolve(options.render!()).then((cfg) => {
|
|
72
|
+
resolvedRenderOpts = mergeRenderConfig(cfg)
|
|
73
|
+
return resolvedRenderOpts
|
|
74
|
+
}).catch((err) => {
|
|
75
|
+
pendingRenderOpts = undefined
|
|
76
|
+
throw err
|
|
77
|
+
})
|
|
78
|
+
return pendingRenderOpts
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return { ensureSync, ensureAsync }
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export interface BuildContentEntryParams<TData> {
|
|
85
|
+
slug: string
|
|
86
|
+
locale?: string | undefined
|
|
87
|
+
file: string
|
|
88
|
+
data: TData
|
|
89
|
+
body: string
|
|
90
|
+
renderLoader: RenderConfigLoader
|
|
91
|
+
/** Set for file-backed collections so dev inline CMS can anchor rendered Markdown bodies. */
|
|
92
|
+
collectionName?: string | undefined
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function buildContentEntry<TData>(params: BuildContentEntryParams<TData>): ContentEntry<TData> {
|
|
96
|
+
const { slug, locale, file, data, body, renderLoader, collectionName } = params
|
|
97
|
+
const wordCount = body.trim().split(/\s+/).filter(Boolean).length
|
|
98
|
+
const readingMinutes = Math.max(1, Math.ceil(wordCount / 220))
|
|
99
|
+
let renderedCache: RenderedMarkdown | undefined
|
|
100
|
+
|
|
101
|
+
const getRenderedSync = (): RenderedMarkdown => {
|
|
102
|
+
renderedCache ??= renderMarkdown(body, { ...renderLoader.ensureSync(), locale, file })
|
|
103
|
+
return renderedCache
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const renderAsync = async (): Promise<RenderedMarkdown> => {
|
|
107
|
+
if (renderedCache === undefined) {
|
|
108
|
+
const opts = await renderLoader.ensureAsync()
|
|
109
|
+
renderedCache ??= renderMarkdown(body, { ...opts, locale, file })
|
|
110
|
+
}
|
|
111
|
+
if (shouldWrapCmsBody(collectionName)) {
|
|
112
|
+
return {
|
|
113
|
+
...renderedCache,
|
|
114
|
+
html: wrapCmsBodyHtml(collectionName, slug, locale, renderedCache.html),
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return renderedCache
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return {
|
|
121
|
+
slug,
|
|
122
|
+
...(locale !== undefined ? { locale } : {}),
|
|
123
|
+
file,
|
|
124
|
+
data,
|
|
125
|
+
body,
|
|
126
|
+
get html() {
|
|
127
|
+
const raw = getRenderedSync().html
|
|
128
|
+
return shouldWrapCmsBody(collectionName)
|
|
129
|
+
? wrapCmsBodyHtml(collectionName, slug, locale, raw)
|
|
130
|
+
: raw
|
|
131
|
+
},
|
|
132
|
+
get headings() {
|
|
133
|
+
return getRenderedSync().headings
|
|
134
|
+
},
|
|
135
|
+
wordCount,
|
|
136
|
+
readingMinutes,
|
|
137
|
+
render: renderAsync,
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export function isDevMode(): boolean {
|
|
142
|
+
return currentApp.dev() ?? (process.env.PRUNELLE_DEV === '1')
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export function isDraft(data: unknown): boolean {
|
|
146
|
+
return data !== null && typeof data === 'object' && (data as Record<string, unknown>).draft === true
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export function defaultSort<T>(a: ContentEntry<T>, b: ContentEntry<T>): number {
|
|
150
|
+
const ad = a.data !== null && typeof a.data === 'object' ? (a.data as Record<string, unknown>).date : undefined
|
|
151
|
+
const bd = b.data !== null && typeof b.data === 'object' ? (b.data as Record<string, unknown>).date : undefined
|
|
152
|
+
const at = ad instanceof Date ? ad.getTime() : (typeof ad === 'string' || typeof ad === 'number' ? Date.parse(String(ad)) : Number.NaN)
|
|
153
|
+
const bt = bd instanceof Date ? bd.getTime() : (typeof bd === 'string' || typeof bd === 'number' ? Date.parse(String(bd)) : Number.NaN)
|
|
154
|
+
if (!Number.isNaN(at) && !Number.isNaN(bt) && at !== bt)
|
|
155
|
+
return bt - at
|
|
156
|
+
return a.slug.localeCompare(b.slug)
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export function filterByTag<T>(entries: readonly ContentEntry<T>[], tag: string): ContentEntry<T>[] {
|
|
160
|
+
const needle = tag.toLowerCase()
|
|
161
|
+
return entries.filter((e) => {
|
|
162
|
+
const tags = e.data !== null && typeof e.data === 'object' ? (e.data as Record<string, unknown>).tags : undefined
|
|
163
|
+
return Array.isArray(tags) && tags.some(t => String(t).toLowerCase() === needle)
|
|
164
|
+
})
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export function formatPaths<T>(entries: readonly ContentEntry<T>[], prefix = ''): string[] {
|
|
168
|
+
const base = prefix.replace(/\/$/, '')
|
|
169
|
+
return entries.map(e => `${base}/${e.slug}`)
|
|
170
|
+
}
|