@prunellejs/content 0.0.0-stage → 0.1.1
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 +54 -0
- package/package.json +24 -4
- package/src/app.ts +13 -0
- package/src/collection.ts +534 -0
- package/src/components.ts +524 -0
- package/src/frontmatter.ts +43 -0
- package/src/index.ts +169 -0
- package/src/markdown.ts +171 -0
- package/src/rss.ts +85 -0
- package/src/sanitize.ts +309 -0
- package/README.md +0 -3
|
@@ -0,0 +1,524 @@
|
|
|
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
|
+
const valStart = i
|
|
290
|
+
while (i < len && attrSrc[i] !== quote) {
|
|
291
|
+
if (attrSrc[i] === '\\') i++
|
|
292
|
+
i++
|
|
293
|
+
}
|
|
294
|
+
props[name] = attrSrc.slice(valStart, i)
|
|
295
|
+
i++
|
|
296
|
+
}
|
|
297
|
+
else if (ch === '{') {
|
|
298
|
+
let depth = 0
|
|
299
|
+
const exprStart = i + 1
|
|
300
|
+
let inStr: string | null = null
|
|
301
|
+
while (i < len) {
|
|
302
|
+
const c = attrSrc[i]!
|
|
303
|
+
if (inStr !== null) {
|
|
304
|
+
if (c === '\\') { i += 2; continue }
|
|
305
|
+
if (c === inStr) inStr = null
|
|
306
|
+
}
|
|
307
|
+
else {
|
|
308
|
+
if (c === '"' || c === '\'' || c === '`') inStr = c
|
|
309
|
+
else if (c === '{') depth++
|
|
310
|
+
else if (c === '}') {
|
|
311
|
+
depth--
|
|
312
|
+
if (depth === 0) break
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
i++
|
|
316
|
+
}
|
|
317
|
+
const expr = attrSrc.slice(exprStart, i).trim()
|
|
318
|
+
i++ // skip closing '}'
|
|
319
|
+
try {
|
|
320
|
+
props[name] = parseLiteralValue(expr)
|
|
321
|
+
}
|
|
322
|
+
catch {
|
|
323
|
+
// Reject/ignore arbitrary non-literal JS expressions
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
return props
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
// Find the end of an opening JSX tag (`>` or `/>`), respecting strings and balanced `{...}` expressions.
|
|
331
|
+
function findTagEnd(src: string, start: number): { end: number, selfClosing: boolean } | undefined {
|
|
332
|
+
let i = start
|
|
333
|
+
const len = src.length
|
|
334
|
+
let braceDepth = 0
|
|
335
|
+
let inStr: string | null = null
|
|
336
|
+
|
|
337
|
+
while (i < len) {
|
|
338
|
+
const c = src[i]!
|
|
339
|
+
if (inStr !== null) {
|
|
340
|
+
if (c === '\\') { i += 2; continue }
|
|
341
|
+
if (c === inStr) inStr = null
|
|
342
|
+
i++
|
|
343
|
+
continue
|
|
344
|
+
}
|
|
345
|
+
if (c === '"' || c === '\'' || c === '`') {
|
|
346
|
+
inStr = c
|
|
347
|
+
i++
|
|
348
|
+
continue
|
|
349
|
+
}
|
|
350
|
+
if (c === '{') {
|
|
351
|
+
braceDepth++
|
|
352
|
+
i++
|
|
353
|
+
continue
|
|
354
|
+
}
|
|
355
|
+
if (c === '}') {
|
|
356
|
+
braceDepth = Math.max(0, braceDepth - 1)
|
|
357
|
+
i++
|
|
358
|
+
continue
|
|
359
|
+
}
|
|
360
|
+
if (braceDepth === 0) {
|
|
361
|
+
if (c === '/' && src[i + 1] === '>')
|
|
362
|
+
return { end: i + 2, selfClosing: true }
|
|
363
|
+
if (c === '>')
|
|
364
|
+
return { end: i + 1, selfClosing: false }
|
|
365
|
+
}
|
|
366
|
+
i++
|
|
367
|
+
}
|
|
368
|
+
return undefined
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
// Returns true when the character at `idx` is escaped by an odd number of preceding backslashes.
|
|
372
|
+
function isBackslashEscaped(src: string, idx: number): boolean {
|
|
373
|
+
let slashes = 0
|
|
374
|
+
for (let k = idx - 1; k >= 0 && src[k] === '\\'; k--)
|
|
375
|
+
slashes++
|
|
376
|
+
return (slashes & 1) === 1
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
// Advances past a CommonMark inline code span starting at backtick offset `start`.
|
|
380
|
+
// Returns the offset right after the matching closing backtick run, or -1 if unclosed.
|
|
381
|
+
function matchInlineCodeSpan(src: string, start: number): number {
|
|
382
|
+
let ticks = 0
|
|
383
|
+
while (start + ticks < src.length && src[start + ticks] === '`')
|
|
384
|
+
ticks++
|
|
385
|
+
let pos = start + ticks
|
|
386
|
+
while (pos < src.length) {
|
|
387
|
+
if (src[pos] === '`') {
|
|
388
|
+
let run = 0
|
|
389
|
+
while (pos + run < src.length && src[pos + run] === '`')
|
|
390
|
+
run++
|
|
391
|
+
if (run === ticks)
|
|
392
|
+
return pos + run
|
|
393
|
+
pos += run
|
|
394
|
+
continue
|
|
395
|
+
}
|
|
396
|
+
pos++
|
|
397
|
+
}
|
|
398
|
+
return -1
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
function findMatchingCloseTag(md: string, tagName: string, start: number): number {
|
|
402
|
+
const closeTag = `</${tagName}>`
|
|
403
|
+
const len = md.length
|
|
404
|
+
let depth = 1
|
|
405
|
+
let pos = start
|
|
406
|
+
while (pos < len) {
|
|
407
|
+
const ch = md[pos]!
|
|
408
|
+
if (ch === '`' && !isBackslashEscaped(md, pos)) {
|
|
409
|
+
const spanEnd = matchInlineCodeSpan(md, pos)
|
|
410
|
+
if (spanEnd !== -1) {
|
|
411
|
+
pos = spanEnd
|
|
412
|
+
continue
|
|
413
|
+
}
|
|
414
|
+
while (pos < len && md[pos] === '`') pos++
|
|
415
|
+
continue
|
|
416
|
+
}
|
|
417
|
+
if (ch === '<') {
|
|
418
|
+
if (md.startsWith(closeTag, pos)) {
|
|
419
|
+
depth--
|
|
420
|
+
if (depth === 0)
|
|
421
|
+
return pos
|
|
422
|
+
pos += closeTag.length
|
|
423
|
+
continue
|
|
424
|
+
}
|
|
425
|
+
if (md.startsWith(`<${tagName}`, pos) && /[\s/>]/.test(md[pos + 1 + tagName.length] ?? '')) {
|
|
426
|
+
const innerEnd = findTagEnd(md, pos + 1 + tagName.length)
|
|
427
|
+
if (innerEnd !== undefined) {
|
|
428
|
+
if (!innerEnd.selfClosing)
|
|
429
|
+
depth++
|
|
430
|
+
pos = innerEnd.end
|
|
431
|
+
continue
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
pos++
|
|
436
|
+
}
|
|
437
|
+
return -1
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
export function expandComponents(
|
|
441
|
+
md: string,
|
|
442
|
+
components: Record<string, MarkdownComponent>,
|
|
443
|
+
options: RenderMarkdownOptions,
|
|
444
|
+
slots: string[],
|
|
445
|
+
slotPrefix: string,
|
|
446
|
+
renderMarkdown: (md: string, options: RenderMarkdownOptions) => { html: string },
|
|
447
|
+
): string {
|
|
448
|
+
const names = Object.keys(components)
|
|
449
|
+
if (names.length === 0)
|
|
450
|
+
return md
|
|
451
|
+
|
|
452
|
+
let out = ''
|
|
453
|
+
let i = 0
|
|
454
|
+
const len = md.length
|
|
455
|
+
|
|
456
|
+
while (i < len) {
|
|
457
|
+
const lt = md.indexOf('<', i)
|
|
458
|
+
const tick = md.indexOf('`', i)
|
|
459
|
+
|
|
460
|
+
// Skip CommonMark inline code spans so `<Component />` inside backticks is never expanded.
|
|
461
|
+
if (tick !== -1 && (lt === -1 || tick < lt)) {
|
|
462
|
+
if (!isBackslashEscaped(md, tick)) {
|
|
463
|
+
const spanEnd = matchInlineCodeSpan(md, tick)
|
|
464
|
+
if (spanEnd !== -1) {
|
|
465
|
+
out += md.slice(i, spanEnd)
|
|
466
|
+
i = spanEnd
|
|
467
|
+
continue
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
let endTick = tick + 1
|
|
471
|
+
while (endTick < len && md[endTick] === '`') endTick++
|
|
472
|
+
out += md.slice(i, endTick)
|
|
473
|
+
i = endTick
|
|
474
|
+
continue
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
if (lt === -1) {
|
|
478
|
+
out += md.slice(i)
|
|
479
|
+
break
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
const match = /^<([A-Z][\w]*)(?=[\s/>])/.exec(md.slice(lt))
|
|
483
|
+
if (!match || !(match[1]! in components)) {
|
|
484
|
+
out += md.slice(i, lt + 1)
|
|
485
|
+
i = lt + 1
|
|
486
|
+
continue
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
const tagName = match[1]!
|
|
490
|
+
const tagEnd = findTagEnd(md, lt + match[0].length)
|
|
491
|
+
if (!tagEnd) {
|
|
492
|
+
out += md.slice(i, lt + 1)
|
|
493
|
+
i = lt + 1
|
|
494
|
+
continue
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
out += md.slice(i, lt)
|
|
498
|
+
const attrSrc = md.slice(lt + match[0].length, tagEnd.selfClosing ? tagEnd.end - 2 : tagEnd.end - 1)
|
|
499
|
+
const props = parseComponentProps(attrSrc)
|
|
500
|
+
let afterIndex = tagEnd.end
|
|
501
|
+
|
|
502
|
+
if (!tagEnd.selfClosing) {
|
|
503
|
+
const closeTag = `</${tagName}>`
|
|
504
|
+
const closeIdx = findMatchingCloseTag(md, tagName, tagEnd.end)
|
|
505
|
+
if (closeIdx !== -1) {
|
|
506
|
+
const innerMd = md.slice(tagEnd.end, closeIdx).trim()
|
|
507
|
+
const innerRendered = renderMarkdown(innerMd, options)
|
|
508
|
+
props.children = raw(innerRendered.html.trim())
|
|
509
|
+
afterIndex = closeIdx + closeTag.length
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
if (options.locale !== undefined && props.locale === undefined)
|
|
514
|
+
props.locale = options.locale
|
|
515
|
+
|
|
516
|
+
const renderedHtml = ssrRaw(components[tagName]!(props as never))
|
|
517
|
+
const slotIdx = slots.length
|
|
518
|
+
slots.push(renderedHtml)
|
|
519
|
+
out += `\n\n<div data-pru-md-slot="${slotPrefix}${slotIdx}"></div>\n\n`
|
|
520
|
+
i = afterIndex
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
return out
|
|
524
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// YAML frontmatter parsing (via Bun.YAML) and source line lookup for diagnostics.
|
|
2
|
+
|
|
3
|
+
/** Parses full nested YAML frontmatter using Bun's native Zig `Bun.YAML.parse`. */
|
|
4
|
+
export function parseFrontmatter(source: string): { data: Record<string, unknown>, body: string } {
|
|
5
|
+
const normalized = source.replace(/\r\n/g, '\n')
|
|
6
|
+
if (!normalized.startsWith('---\n'))
|
|
7
|
+
return { data: {}, body: normalized }
|
|
8
|
+
|
|
9
|
+
const end = normalized.indexOf('\n---', 4)
|
|
10
|
+
if (end === -1)
|
|
11
|
+
return { data: {}, body: normalized }
|
|
12
|
+
|
|
13
|
+
const rawYaml = normalized.slice(4, end)
|
|
14
|
+
const restStart = normalized.indexOf('\n', end + 4)
|
|
15
|
+
const body = restStart === -1 ? '' : normalized.slice(restStart + 1)
|
|
16
|
+
|
|
17
|
+
const parsed = rawYaml.trim() === '' ? {} : Bun.YAML.parse(rawYaml)
|
|
18
|
+
const data = parsed && typeof parsed === 'object' && !Array.isArray(parsed)
|
|
19
|
+
? (parsed as Record<string, unknown>)
|
|
20
|
+
: {}
|
|
21
|
+
return { data, body }
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Finds the 1-based line number of a top-level frontmatter key for `prunelle check` diagnostics. */
|
|
25
|
+
export function findFrontmatterFieldLine(rawSource: string, fieldPath: string | undefined): number {
|
|
26
|
+
const normalized = rawSource.replace(/\r\n/g, '\n')
|
|
27
|
+
if (!normalized.startsWith('---\n'))
|
|
28
|
+
return 1
|
|
29
|
+
const end = normalized.indexOf('\n---', 4)
|
|
30
|
+
if (end === -1)
|
|
31
|
+
return 1
|
|
32
|
+
if (!fieldPath)
|
|
33
|
+
return 2
|
|
34
|
+
const topKey = fieldPath.split('.')[0] ?? fieldPath
|
|
35
|
+
const escaped = topKey.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
|
36
|
+
const keyRe = new RegExp(`^\\s*${escaped}\\s*:`)
|
|
37
|
+
const lines = normalized.slice(4, end).split('\n')
|
|
38
|
+
for (let i = 0; i < lines.length; i++) {
|
|
39
|
+
if (keyRe.test(lines[i]!))
|
|
40
|
+
return i + 2
|
|
41
|
+
}
|
|
42
|
+
return 2
|
|
43
|
+
}
|