@prunellejs/content 0.7.2 → 0.7.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prunellejs/content",
3
- "version": "0.7.2",
3
+ "version": "0.7.3",
4
4
  "description": "Zero-dependency Bun.markdown collections, YAML frontmatter, TOC, and RSS plugin for Prunelle",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -21,6 +21,6 @@
21
21
  "directory": "packages/plugin-content"
22
22
  },
23
23
  "peerDependencies": {
24
- "prunelle": "^0.7.2"
24
+ "prunelle": "^0.7.3"
25
25
  }
26
26
  }
package/src/components.ts CHANGED
@@ -343,7 +343,7 @@ function parseComponentProps(attrSrc: string, tag: string, file: string | undefi
343
343
 
344
344
  // What one expansion pass has learnt about the source, so no scan is run twice over the same ground.
345
345
  export interface ScanMemo {
346
- // Offsets a failed tag-end scan crossed outside any string or brace: a scan reaching one fails the same way.
346
+ // Per-offset 4-bit mask of failed tag-end states at braceDepth === 0: bit 0 none, bit 1 `"`, bit 2 `'`, bit 3 `` ` ``.
347
347
  failedAt?: Uint8Array
348
348
  // Per tag name, the offset of the matching close tag (-1 for none), keyed by the end of the opening tag.
349
349
  closes: Map<string, Map<number, number>>
@@ -355,6 +355,14 @@ export function createScanMemo(): ScanMemo {
355
355
 
356
356
  const MAX_BRACE_DEPTH = 32
357
357
 
358
+ function strStateBit(inStr: string | null): number {
359
+ if (inStr === null)
360
+ return 1
361
+ if (inStr === '"')
362
+ return 2
363
+ return inStr === '\'' ? 4 : 8
364
+ }
365
+
358
366
  // Find the end of an opening JSX tag (`>` or `/>`), respecting strings and balanced `{...}` expressions.
359
367
  function findTagEnd(src: string, start: number, memo: ScanMemo): { end: number, selfClosing: boolean } | undefined {
360
368
  let i = start
@@ -364,17 +372,20 @@ function findTagEnd(src: string, start: number, memo: ScanMemo): { end: number,
364
372
  const crossed: number[] = []
365
373
  const fail = (): undefined => {
366
374
  const failedAt = memo.failedAt ??= new Uint8Array(len)
367
- for (const at of crossed)
368
- failedAt[at] = 1
375
+ for (let k = 0; k < crossed.length; k += 2) {
376
+ const at = crossed[k]!
377
+ failedAt[at] = (failedAt[at] ?? 0) | crossed[k + 1]!
378
+ }
369
379
  return undefined
370
380
  }
371
381
 
372
382
  while (i < len) {
373
383
  const c = src[i]!
374
- if (inStr === null && braceDepth === 0) {
375
- if (memo.failedAt?.[i] === 1)
384
+ if (braceDepth === 0) {
385
+ const bit = strStateBit(inStr)
386
+ if (((memo.failedAt?.[i] ?? 0) & bit) !== 0)
376
387
  return fail()
377
- crossed.push(i)
388
+ crossed.push(i, bit)
378
389
  }
379
390
  if (inStr !== null) {
380
391
  if (c === '\\') { i += 2; continue }
@@ -2,6 +2,12 @@
2
2
 
3
3
  const CLOSING_LINE_RE = /^---[ \t]*$/
4
4
 
5
+ // Strips a leading UTF-8 BOM and normalizes CRLF to LF before frontmatter splitting.
6
+ function normalizeSource(source: string): string {
7
+ const withoutBom = source.charCodeAt(0) === 0xFEFF ? source.slice(1) : source
8
+ return withoutBom.replace(/\r\n/g, '\n')
9
+ }
10
+
5
11
  // The YAML between the opening `---` line and the first line that is exactly `---` (the empty block included), and where the body starts.
6
12
  function splitFrontmatter(normalized: string): { yaml: string, bodyStart: number } | undefined {
7
13
  if (!normalized.startsWith('---\n'))
@@ -21,7 +27,7 @@ function splitFrontmatter(normalized: string): { yaml: string, bodyStart: number
21
27
 
22
28
  /** Parses full nested YAML frontmatter using Bun's native Zig `Bun.YAML.parse`. */
23
29
  export function parseFrontmatter(source: string): { data: Record<string, unknown>, body: string } {
24
- const normalized = source.replace(/\r\n/g, '\n')
30
+ const normalized = normalizeSource(source)
25
31
  const split = splitFrontmatter(normalized)
26
32
  if (split === undefined)
27
33
  return { data: {}, body: normalized }
@@ -38,7 +44,7 @@ export function parseFrontmatter(source: string): { data: Record<string, unknown
38
44
 
39
45
  /** Finds the 1-based line number of a top-level frontmatter key for `prunelle check` diagnostics. */
40
46
  export function findFrontmatterFieldLine(rawSource: string, fieldPath: string | undefined): number {
41
- const normalized = rawSource.replace(/\r\n/g, '\n')
47
+ const normalized = normalizeSource(rawSource)
42
48
  const split = splitFrontmatter(normalized)
43
49
  if (split === undefined)
44
50
  return 1