@equationalapplications/core-okf 9.20.1 → 9.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -3,7 +3,7 @@
3
3
  // src/frontmatter.ts
4
4
  var RESERVED_LITERALS = /* @__PURE__ */ new Set(["true", "false", "yes", "no", "on", "off", "null", "~", ""]);
5
5
  function looksLikeNumber(value) {
6
- return /^[+-]?(\d+\.?\d*|\.\d+)([eE][+-]?\d+)?$/.test(value);
6
+ return /^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?$/.test(value);
7
7
  }
8
8
  function isIso8601Timestamp(value) {
9
9
  return /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:\d{2}|[+-]\d{4}|[+-]\d{2})$/.test(value);
@@ -87,7 +87,7 @@ function serializeFlowSequenceValue(arr, depth = 0) {
87
87
  });
88
88
  return `[ ${parts.join(", ")} ]`;
89
89
  }
90
- function serializeFrontmatter(fm) {
90
+ function serializeFrontmatter(fm, options) {
91
91
  const lines = ["---"];
92
92
  for (const [key, value] of Object.entries(fm)) {
93
93
  if (value === void 0) continue;
@@ -108,6 +108,9 @@ function serializeFrontmatter(fm) {
108
108
  lines.push(`${serializeKey(key)}: ${serializeValue(value)}`);
109
109
  }
110
110
  }
111
+ for (const block of options?.rawBlocks ?? []) {
112
+ lines.push(block);
113
+ }
111
114
  lines.push("---");
112
115
  return lines.join("\n") + "\n";
113
116
  }
@@ -213,12 +216,38 @@ function matchFrontmatterKeyValue(line) {
213
216
  const tail = line.slice(key.length + 1);
214
217
  return { key, value: tail.trimStart(), hasValue: tail.trim().length > 0 };
215
218
  }
216
- function hasUnquotedAnchorOrAlias(text) {
219
+ function matchBlockKeyValue(line) {
220
+ const kv = matchFrontmatterKeyValue(line);
221
+ if (!kv) return null;
222
+ const tail = line.slice(kv.key.length + 1);
223
+ if (tail.length > 0 && tail[0] !== " " && tail[0] !== " ") return null;
224
+ return kv;
225
+ }
226
+ function indentWidth(line) {
227
+ let n = 0;
228
+ while (n < line.length && (line[n] === " " || line[n] === " ")) n++;
229
+ return n;
230
+ }
231
+ function quoteOpensAtNodeStart(text, i) {
232
+ if (i === 0) return true;
233
+ let j = i - 1;
234
+ while (j >= 0 && (text[j] === " " || text[j] === " ")) j--;
235
+ if (j < 0) return true;
236
+ const c = text[j];
237
+ if (c === "{" || c === "[" || c === "," || c === ":") return true;
238
+ if (c === "-") {
239
+ if (j === 0) return true;
240
+ const d = text[j - 1];
241
+ return d === " " || d === " " || d === "{" || d === "[" || d === "," || d === ":";
242
+ }
243
+ return false;
244
+ }
245
+ function hasNodeStartAnchorOrAlias(text) {
217
246
  let quote = null;
218
247
  for (let i = 0; i < text.length; i++) {
219
248
  const ch = text[i];
220
249
  if (quote) {
221
- if (ch === "\\" && i + 1 < text.length) {
250
+ if (quote === '"' && ch === "\\" && i + 1 < text.length) {
222
251
  i++;
223
252
  continue;
224
253
  }
@@ -226,13 +255,116 @@ function hasUnquotedAnchorOrAlias(text) {
226
255
  continue;
227
256
  }
228
257
  if (ch === '"' || ch === "'") {
229
- quote = ch;
258
+ if (quoteOpensAtNodeStart(text, i)) quote = ch;
259
+ continue;
260
+ }
261
+ if (ch !== "&" && ch !== "*") continue;
262
+ let j = i - 1;
263
+ while (j >= 0 && (text[j] === " " || text[j] === " ")) j--;
264
+ if (j < 0) return true;
265
+ const c = text[j];
266
+ if (c === "{" || c === "[" || c === "," || c === ":") return true;
267
+ if (c === "-") {
268
+ if (j === 0) return true;
269
+ const d = text[j - 1];
270
+ if (d === " " || d === " " || d === "{" || d === "[" || d === "," || d === ":") return true;
271
+ }
272
+ }
273
+ return false;
274
+ }
275
+ function isFullLineComment(line) {
276
+ return /^[ \t]*#/.test(line);
277
+ }
278
+ function stripInlineComment(tail) {
279
+ let start = 0;
280
+ while (start < tail.length && (tail[start] === " " || tail[start] === " ")) start++;
281
+ if (start >= tail.length) return "";
282
+ let quote = null;
283
+ for (let i = start; i < tail.length; i++) {
284
+ const ch = tail[i];
285
+ if (quote) {
286
+ if (ch === "\\" && quote === '"' && i + 1 < tail.length) {
287
+ i++;
288
+ continue;
289
+ }
290
+ if (ch === quote) {
291
+ if (quote === "'" && tail[i + 1] === "'") {
292
+ i++;
293
+ continue;
294
+ }
295
+ quote = null;
296
+ }
230
297
  continue;
231
298
  }
232
- if (ch === "&" || ch === "*") return true;
299
+ if (ch === "#" && i > 0 && (tail[i - 1] === " " || tail[i - 1] === " ")) {
300
+ return tail.slice(0, i).trim();
301
+ }
302
+ if (ch === '"' || ch === "'") {
303
+ let k = i - 1;
304
+ while (k >= start && (tail[k] === " " || tail[k] === " ")) k--;
305
+ const nodeStart = i === start || k < start || (tail[k] === "{" || tail[k] === "[" || tail[k] === "," || tail[k] === ":" || tail[k] === "-");
306
+ if (nodeStart) quote = ch;
307
+ }
308
+ }
309
+ return tail.trim();
310
+ }
311
+ function quoteCloses(text) {
312
+ const q = text[0];
313
+ for (let i = 1; i < text.length; i++) {
314
+ if (q === '"' && text[i] === "\\") {
315
+ i++;
316
+ continue;
317
+ }
318
+ if (text[i] === q) {
319
+ if (q === "'" && text[i + 1] === "'") {
320
+ i++;
321
+ continue;
322
+ }
323
+ return true;
324
+ }
233
325
  }
234
326
  return false;
235
327
  }
328
+ function flowIsOpen(text) {
329
+ let quote = null;
330
+ let brace = 0;
331
+ let bracket = 0;
332
+ for (let i = 0; i < text.length; i++) {
333
+ const ch = text[i];
334
+ if (quote) {
335
+ if (quote === '"' && ch === "\\" && i + 1 < text.length) {
336
+ i++;
337
+ continue;
338
+ }
339
+ if (ch === quote) quote = null;
340
+ continue;
341
+ }
342
+ if ((ch === '"' || ch === "'") && quoteOpensAtNodeStart(text, i)) {
343
+ quote = ch;
344
+ continue;
345
+ }
346
+ if (ch === "{") brace++;
347
+ else if (ch === "}") brace--;
348
+ else if (ch === "[") bracket++;
349
+ else if (ch === "]") bracket--;
350
+ }
351
+ return brace !== 0 || bracket !== 0 || quote !== null;
352
+ }
353
+ function captureRawLines(lines, start, closingIndex, includeColumn0Items) {
354
+ const captured = [];
355
+ let j = start;
356
+ while (j < closingIndex) {
357
+ const l = lines[j];
358
+ if (l.trim() === "" || /^[ \t]/.test(l) || includeColumn0Items && (/^-\s/.test(l) || isFullLineComment(l))) {
359
+ captured.push(l);
360
+ j++;
361
+ continue;
362
+ }
363
+ break;
364
+ }
365
+ while (captured.length > 0 && captured[captured.length - 1].trim() === "") captured.pop();
366
+ return { captured, next: j };
367
+ }
236
368
  function splitFlowEntries(inner) {
237
369
  const entries = [];
238
370
  let buf = "";
@@ -244,7 +376,7 @@ function splitFlowEntries(inner) {
244
376
  const ch = inner[i];
245
377
  if (quote) {
246
378
  buf += ch;
247
- if (ch === "\\" && i + 1 < inner.length) {
379
+ if (quote === '"' && ch === "\\" && i + 1 < inner.length) {
248
380
  buf += inner[i + 1];
249
381
  i += 2;
250
382
  continue;
@@ -276,61 +408,176 @@ function splitFlowEntries(inner) {
276
408
  if (depthBrace !== 0 || depthBracket !== 0 || quote !== null) return null;
277
409
  return entries;
278
410
  }
279
- function parseFlowMapping(text, depth = 0) {
411
+ function blockIssueReason(reason) {
412
+ return reason === "too_deep" ? "nested_too_deep" : reason;
413
+ }
414
+ function parseFlowMappingE(text, depth = 0) {
280
415
  const trimmed = text.trim();
281
- if (!trimmed.startsWith("{") || !trimmed.endsWith("}")) return null;
282
- if (hasUnquotedAnchorOrAlias(trimmed)) return null;
416
+ if (!trimmed.startsWith("{") || !trimmed.endsWith("}")) return { ok: false, reason: "malformed" };
417
+ if (hasNodeStartAnchorOrAlias(trimmed)) return { ok: false, reason: "anchor_or_alias" };
283
418
  const inner = trimmed.slice(1, -1).trim();
284
- if (inner.length === 0) return {};
419
+ if (inner.length === 0) return { ok: true, value: {} };
285
420
  const entries = splitFlowEntries(inner);
286
- if (!entries) return null;
421
+ if (!entries) return { ok: false, reason: "malformed" };
287
422
  const out = {};
288
423
  for (const entry of entries) {
289
424
  const kv = matchFrontmatterKeyValue(entry);
290
- if (!kv || !kv.hasValue) return null;
425
+ if (!kv || !kv.hasValue) return { ok: false, reason: "malformed" };
291
426
  const v = kv.value.trim();
292
427
  if (v.startsWith("{") || v.startsWith("[")) {
293
- if (depth >= 1) return null;
294
- const nested = v.startsWith("{") ? parseFlowMapping(v, depth + 1) : parseFlowSequence(v, depth + 1);
295
- if (nested === null) return null;
296
- out[parseKey(kv.key)] = nested;
428
+ if (depth >= 1) return { ok: false, reason: "too_deep" };
429
+ const nested = v.startsWith("{") ? parseFlowMappingE(v, depth + 1) : parseFlowSequenceE(v, depth + 1);
430
+ if (!nested.ok) return nested;
431
+ const fk = parseKey(kv.key);
432
+ if (fk !== "__proto__") out[fk] = nested.value;
297
433
  continue;
298
434
  }
299
435
  out[parseKey(kv.key)] = parseScalarValue(v);
300
436
  }
301
- return out;
437
+ return { ok: true, value: out };
302
438
  }
303
- function parseFlowSequence(text, depth = 0) {
439
+ function parseFlowSequenceE(text, depth = 0) {
304
440
  const trimmed = text.trim();
305
- if (!trimmed.startsWith("[") || !trimmed.endsWith("]")) return null;
306
- if (hasUnquotedAnchorOrAlias(trimmed)) return null;
441
+ if (!trimmed.startsWith("[") || !trimmed.endsWith("]")) return { ok: false, reason: "malformed" };
442
+ if (hasNodeStartAnchorOrAlias(trimmed)) return { ok: false, reason: "anchor_or_alias" };
307
443
  const inner = trimmed.slice(1, -1).trim();
308
- if (inner.length === 0) return [];
444
+ if (inner.length === 0) return { ok: true, value: [] };
309
445
  const entries = splitFlowEntries(inner);
310
- if (!entries) return null;
446
+ if (!entries) return { ok: false, reason: "malformed" };
311
447
  const out = [];
312
448
  for (const entry of entries) {
313
449
  const v = entry.trim();
314
450
  if (v.startsWith("{")) {
315
- const nested = parseFlowMapping(v, depth);
316
- if (nested === null) return null;
317
- out.push(nested);
451
+ const nested = parseFlowMappingE(v, depth);
452
+ if (!nested.ok) return nested;
453
+ out.push(nested.value);
318
454
  continue;
319
455
  }
320
456
  if (v.startsWith("[")) {
321
- if (depth >= 1) return null;
322
- const nested = parseFlowSequence(v, depth + 1);
323
- if (nested === null) return null;
324
- out.push(nested);
457
+ if (depth >= 1) return { ok: false, reason: "too_deep" };
458
+ const nested = parseFlowSequenceE(v, depth + 1);
459
+ if (!nested.ok) return nested;
460
+ out.push(nested.value);
325
461
  continue;
326
462
  }
327
463
  out.push(parseScalarValue(v));
328
464
  }
329
- return out;
465
+ return { ok: true, value: out };
466
+ }
467
+ function parseFlowMapping(text, depth = 0) {
468
+ const res = parseFlowMappingE(text, depth);
469
+ return res.ok ? res.value : null;
470
+ }
471
+ function parseFlowSequence(text, depth = 0) {
472
+ const res = parseFlowSequenceE(text, depth);
473
+ return res.ok ? res.value : null;
474
+ }
475
+ function parseBlockLines(blockLines) {
476
+ if (/^[|>][-+0-9]*$/.test(blockLines[0].trim())) {
477
+ return { ok: false, reason: "block_scalar" };
478
+ }
479
+ return /^-\s/.test(blockLines[0].trimStart()) ? parseBlockList(blockLines) : parseBlockMapping(blockLines);
480
+ }
481
+ function setBlockMappingEntry(out, kv, content) {
482
+ const key = parseKey(kv.key);
483
+ const valueText = stripInlineComment(content.slice(kv.key.length + 1));
484
+ if (valueText.length === 0) {
485
+ out[key] = [];
486
+ return { ok: true };
487
+ }
488
+ if (/^[|>][-+0-9]*$/.test(valueText)) return { ok: false, reason: "block_scalar" };
489
+ if ((valueText[0] === '"' || valueText[0] === "'") && !quoteCloses(valueText)) {
490
+ return { ok: false, reason: "multiline_scalar" };
491
+ }
492
+ if (hasNodeStartAnchorOrAlias(valueText)) return { ok: false, reason: "anchor_or_alias" };
493
+ if (valueText.startsWith("{") || valueText.startsWith("[")) {
494
+ const flow = valueText.startsWith("{") ? parseFlowMappingE(valueText, 1) : parseFlowSequenceE(valueText, 1);
495
+ if (!flow.ok) return { ok: false, reason: blockIssueReason(flow.reason) };
496
+ if (key !== "__proto__") out[key] = flow.value;
497
+ return { ok: true };
498
+ }
499
+ out[key] = parseScalarValue(valueText);
500
+ return { ok: true };
501
+ }
502
+ function parseBlockMapping(blockLines) {
503
+ const indent = indentWidth(blockLines[0]);
504
+ const out = {};
505
+ for (const line of blockLines) {
506
+ const content = line.trimStart();
507
+ if (indentWidth(line) !== indent || /^-\s/.test(content)) {
508
+ return { ok: false, reason: "nested_too_deep" };
509
+ }
510
+ if (content[0] === "{" || content[0] === "[") return { ok: false, reason: "nested_too_deep" };
511
+ if (hasNodeStartAnchorOrAlias(content)) return { ok: false, reason: "anchor_or_alias" };
512
+ const kv = matchBlockKeyValue(content);
513
+ if (!kv) return { ok: false, reason: "nested_too_deep" };
514
+ const stored = setBlockMappingEntry(out, kv, content);
515
+ if (!stored.ok) return stored;
516
+ }
517
+ return { ok: true, value: out };
518
+ }
519
+ function parseBlockList(blockLines) {
520
+ const dashIndent = indentWidth(blockLines[0]);
521
+ const out = [];
522
+ let idx = 0;
523
+ while (idx < blockLines.length) {
524
+ const line = blockLines[idx];
525
+ const content = line.trimStart();
526
+ if (indentWidth(line) !== dashIndent || !/^-\s/.test(content)) {
527
+ return { ok: false, reason: "nested_too_deep" };
528
+ }
529
+ const afterDash = line.slice(dashIndent + 1);
530
+ const itemWs = afterDash.length - afterDash.trimStart().length;
531
+ const itemText = stripInlineComment(` ${afterDash.trimStart()}`).trim();
532
+ if (hasNodeStartAnchorOrAlias(itemText)) return { ok: false, reason: "anchor_or_alias" };
533
+ if ((itemText[0] === '"' || itemText[0] === "'") && !quoteCloses(itemText)) {
534
+ return { ok: false, reason: "multiline_scalar" };
535
+ }
536
+ if (itemText.startsWith("{") || itemText.startsWith("[")) {
537
+ const flow = itemText.startsWith("{") ? parseFlowMappingE(itemText, 0) : parseFlowSequenceE(itemText, 1);
538
+ if (!flow.ok) return { ok: false, reason: blockIssueReason(flow.reason) };
539
+ out.push(flow.value);
540
+ idx++;
541
+ continue;
542
+ }
543
+ const kv = matchBlockKeyValue(itemText);
544
+ if (!kv) {
545
+ out.push(parseScalarValue(itemText));
546
+ idx++;
547
+ continue;
548
+ }
549
+ const item = {};
550
+ const contIndent = dashIndent + 1 + itemWs;
551
+ const started = setBlockMappingEntry(item, kv, itemText);
552
+ if (!started.ok) return started;
553
+ idx++;
554
+ while (idx < blockLines.length) {
555
+ const cont = blockLines[idx];
556
+ const ci = indentWidth(cont);
557
+ if (ci === dashIndent && /^-\s/.test(cont.trimStart())) break;
558
+ if (ci !== contIndent) return { ok: false, reason: "nested_too_deep" };
559
+ const cc = cont.trimStart();
560
+ if (/^-\s/.test(cc)) return { ok: false, reason: "nested_too_deep" };
561
+ if (cc[0] === "{" || cc[0] === "[") return { ok: false, reason: "nested_too_deep" };
562
+ if (hasNodeStartAnchorOrAlias(cc)) return { ok: false, reason: "anchor_or_alias" };
563
+ const ckv = matchBlockKeyValue(cc);
564
+ if (!ckv) return { ok: false, reason: "nested_too_deep" };
565
+ const stored = setBlockMappingEntry(item, ckv, cc);
566
+ if (!stored.ok) return stored;
567
+ idx++;
568
+ }
569
+ out.push(item);
570
+ }
571
+ return { ok: true, value: out };
330
572
  }
331
573
  function parseFrontmatter(content) {
332
574
  const lines = content.split(/\r?\n/);
333
- const fallback = { frontmatter: { type: "" }, rest: content };
575
+ const fallback = {
576
+ frontmatter: { type: "" },
577
+ rest: content,
578
+ raw: {},
579
+ issues: []
580
+ };
334
581
  if (lines[0]?.trim() !== "---") return fallback;
335
582
  let closingIndex = -1;
336
583
  for (let i2 = 1; i2 < lines.length; i2++) {
@@ -341,78 +588,158 @@ function parseFrontmatter(content) {
341
588
  }
342
589
  if (closingIndex === -1) return fallback;
343
590
  const frontmatter = {};
591
+ const raw = {};
592
+ const issues = [];
593
+ const capturedLinesHaveAnchorOrAlias = (allLines) => {
594
+ for (const l of allLines) {
595
+ if (l.trim() === "" || isFullLineComment(l)) continue;
596
+ let i2 = 0;
597
+ while (i2 < l.length && (l[i2] === " " || l[i2] === " ")) i2++;
598
+ let colon = -1;
599
+ if (l[i2] === '"' || l[i2] === "'") {
600
+ let close = -1;
601
+ for (let s = i2 + 1; s < l.length; s++) {
602
+ if (l[i2] === '"' && l[s] === "\\") {
603
+ s++;
604
+ continue;
605
+ }
606
+ if (l[s] === l[i2]) {
607
+ close = s;
608
+ break;
609
+ }
610
+ }
611
+ if (close !== -1 && l[close + 1] === ":") {
612
+ colon = close + 1;
613
+ if (hasNodeStartAnchorOrAlias(l.slice(i2, close))) return true;
614
+ }
615
+ } else {
616
+ let j = i2;
617
+ while (j < l.length && l[j] !== ":" && l[j] !== "#") j++;
618
+ if (j < l.length && l[j] === ":" && j > i2) {
619
+ colon = j;
620
+ if (hasNodeStartAnchorOrAlias(l.slice(i2, j))) return true;
621
+ }
622
+ }
623
+ const tail = colon !== -1 ? l.slice(colon + 1) : l.trimStart();
624
+ if (hasNodeStartAnchorOrAlias(tail)) return true;
625
+ }
626
+ return false;
627
+ };
628
+ const handBack = (key, line, captured, opts) => {
629
+ if (!opts?.allowAnchors && capturedLinesHaveAnchorOrAlias([line, ...captured])) {
630
+ issues.push({ key, reason: "anchor_or_alias" });
631
+ return false;
632
+ }
633
+ raw[key] = [line, ...captured].join("\n");
634
+ return true;
635
+ };
344
636
  let i = 1;
345
637
  while (i < closingIndex) {
346
638
  const line = lines[i];
347
- const kv = matchFrontmatterKeyValue(line);
348
- if (!kv) {
639
+ if (line.trim() === "" || isFullLineComment(line) || /^[ \t]/.test(line)) {
349
640
  i++;
350
641
  continue;
351
642
  }
352
- if (kv.value.trim() === "[]") {
353
- frontmatter[parseKey(kv.key)] = [];
643
+ if (line[0] === "&" || line[0] === "*") {
644
+ const guess = matchFrontmatterKeyValue(line);
645
+ issues.push({ key: guess ? parseKey(guess.key) : line.trim(), reason: "anchor_or_alias" });
354
646
  i++;
355
647
  continue;
356
648
  }
357
- if (!kv.hasValue) {
358
- const key = parseKey(kv.key);
359
- const items = [];
649
+ const kv = matchFrontmatterKeyValue(line);
650
+ if (!kv) {
360
651
  i++;
361
- while (i < closingIndex && /^\s*-\s/.test(lines[i])) {
362
- items.push(parseScalarValue(lines[i].replace(/^\s*-\s/, "")));
363
- i++;
364
- }
365
- frontmatter[key] = items;
366
652
  continue;
367
653
  }
368
- const valueRaw = kv.value.trim();
369
- if (valueRaw.startsWith("{")) {
370
- const flowObj = parseFlowMapping(valueRaw);
371
- if (flowObj !== null) {
372
- frontmatter[parseKey(kv.key)] = flowObj;
373
- i++;
374
- continue;
654
+ const key = parseKey(kv.key);
655
+ const valueText = stripInlineComment(line.slice(kv.key.length + 1));
656
+ if (valueText.length === 0) {
657
+ const region2 = captureRawLines(lines, i + 1, closingIndex, true);
658
+ const blockLines = region2.captured.filter((l) => l.trim() !== "" && !isFullLineComment(l));
659
+ if (blockLines.length === 0) {
660
+ frontmatter[key] = [];
661
+ } else {
662
+ const block = parseBlockLines(blockLines);
663
+ if (block.ok) {
664
+ if (key !== "__proto__") frontmatter[key] = block.value;
665
+ } else if (block.reason === "anchor_or_alias") {
666
+ issues.push({ key, reason: "anchor_or_alias" });
667
+ } else {
668
+ if (handBack(key, line, region2.captured)) issues.push({ key, reason: block.reason });
669
+ }
375
670
  }
376
- frontmatter[parseKey(kv.key)] = null;
671
+ i = region2.next;
672
+ continue;
673
+ }
674
+ if (hasNodeStartAnchorOrAlias(valueText)) {
675
+ issues.push({ key, reason: "anchor_or_alias" });
377
676
  i++;
378
677
  continue;
379
678
  }
380
- if (valueRaw.startsWith("[")) {
381
- if (valueRaw === "[]") {
382
- frontmatter[parseKey(kv.key)] = [];
679
+ if (/^[|>][-+0-9]*$/.test(valueText)) {
680
+ const region2 = captureRawLines(lines, i + 1, closingIndex, false);
681
+ handBack(key, line, region2.captured, { allowAnchors: true });
682
+ issues.push({ key, reason: "block_scalar" });
683
+ i = region2.next;
684
+ continue;
685
+ }
686
+ const region = captureRawLines(lines, i + 1, closingIndex, false);
687
+ const hasContinuation = region.captured.some((l) => l.trim() !== "" && !isFullLineComment(l));
688
+ if (valueText.startsWith("{") || valueText.startsWith("[")) {
689
+ const flow = valueText.startsWith("{") ? parseFlowMappingE(valueText) : parseFlowSequenceE(valueText);
690
+ if (flow.ok) {
691
+ if (hasContinuation) {
692
+ if (handBack(key, line, region.captured)) issues.push({ key, reason: "multiline_scalar" });
693
+ } else {
694
+ if (key !== "__proto__") frontmatter[key] = flow.value;
695
+ }
696
+ i = region.next;
697
+ continue;
698
+ }
699
+ if (flow.reason === "anchor_or_alias") {
700
+ issues.push({ key, reason: "anchor_or_alias" });
383
701
  i++;
384
702
  continue;
385
703
  }
386
- const flowSeq = parseFlowSequence(valueRaw);
387
- if (flowSeq !== null) {
388
- frontmatter[parseKey(kv.key)] = flowSeq;
704
+ if (flowIsOpen(valueText) && region.captured.length === 0) {
705
+ issues.push({ key, reason: "malformed" });
389
706
  i++;
390
707
  continue;
391
708
  }
392
- frontmatter[parseKey(kv.key)] = null;
393
- i++;
709
+ if (handBack(key, line, region.captured)) issues.push({ key, reason: "malformed" });
710
+ i = region.next;
394
711
  continue;
395
712
  }
396
- frontmatter[parseKey(kv.key)] = parseScalarValue(kv.value);
397
- i++;
713
+ const quoteOpen = (valueText[0] === '"' || valueText[0] === "'") && !quoteCloses(valueText);
714
+ const value = parseScalarValue(valueText);
715
+ if (quoteOpen || hasContinuation) {
716
+ if (handBack(key, line, region.captured)) issues.push({ key, reason: "multiline_scalar" });
717
+ i = region.next;
718
+ continue;
719
+ }
720
+ frontmatter[key] = value;
721
+ if (valueText[0] !== '"' && valueText[0] !== "'" && serializeValue(value) !== valueText) {
722
+ raw[key] = line;
723
+ }
724
+ i = region.next;
398
725
  }
399
726
  if (typeof frontmatter["type"] !== "string") {
400
727
  frontmatter["type"] = "";
401
728
  }
402
729
  const restLines = lines.slice(closingIndex + 1);
403
730
  const rest = restLines.length > 0 ? restLines.join("\n") : "";
404
- return { frontmatter, rest };
731
+ return { frontmatter, rest, raw, issues };
405
732
  }
406
733
 
407
734
  // src/concept.ts
408
- function buildConceptDocument(fm, body) {
409
- return `${serializeFrontmatter(fm)}
735
+ function buildConceptDocument(fm, body, options) {
736
+ return `${serializeFrontmatter(fm, options)}
410
737
  ${body}`;
411
738
  }
412
739
  function parseConcept(content) {
413
- const { frontmatter, rest } = parseFrontmatter(content);
740
+ const { frontmatter, rest, raw, issues } = parseFrontmatter(content);
414
741
  const body = rest.startsWith("\n") ? rest.slice(1) : rest;
415
- return { frontmatter, body };
742
+ return { frontmatter, body, raw, issues };
416
743
  }
417
744
 
418
745
  // src/index-md.ts
@@ -438,21 +765,97 @@ function renderSections(sections) {
438
765
  function buildIndexMd(sections) {
439
766
  return renderSections(sections);
440
767
  }
768
+ var RAW_SLOT_KEY = "x-llm-wiki-raw";
441
769
  function buildRootIndexMd(okfVersion, sections, options) {
442
- const lines = ["---", `okf_version: ${serializeScalarString(okfVersion)}`];
443
- if (options?.profile) {
444
- lines.push(`profile: ${serializeScalarString(options.profile)}`);
770
+ const metadata = {};
771
+ const OWNED_RAW_KEYS = /* @__PURE__ */ new Set(["okf_version", "profile", RAW_SLOT_KEY]);
772
+ const rawBlockKeyOf = (lines) => {
773
+ const first = lines.slice(0, lines.indexOf("\n") === -1 ? lines.length : lines.indexOf("\n"));
774
+ const m = first.match(/^[ \t]*(?:"([^"]*)"|'([^']*)'|([^:#\s][^:]*)):/);
775
+ if (!m) return void 0;
776
+ return parseKey(m[1] ?? m[2] ?? m[3] ?? "");
777
+ };
778
+ const isOwnedRawBlock = (lines) => {
779
+ const key = rawBlockKeyOf(lines);
780
+ return key !== void 0 && OWNED_RAW_KEYS.has(key);
781
+ };
782
+ const rawBlocks = (options?.rawBlocks ?? []).filter((b) => !isOwnedRawBlock(b));
783
+ for (const [key, value] of Object.entries(options?.bundleMetadata ?? {})) {
784
+ if (key === "okf_version" || key === "profile") continue;
785
+ if (key === RAW_SLOT_KEY) {
786
+ if (options?.profile === "llm-wiki/3" && value !== null && typeof value === "object" && !Array.isArray(value)) {
787
+ for (const lines of Object.values(value)) {
788
+ if (typeof lines === "string" && lines.length > 0 && !isOwnedRawBlock(lines)) rawBlocks.push(lines);
789
+ }
790
+ }
791
+ continue;
792
+ }
793
+ metadata[key] = value;
445
794
  }
446
- lines.push("---", "");
447
- return `${lines.join("\n")}
795
+ const claimedByRaw = /* @__PURE__ */ new Set();
796
+ for (const b of rawBlocks) {
797
+ const k = rawBlockKeyOf(b);
798
+ if (k !== void 0) claimedByRaw.add(k);
799
+ }
800
+ for (const k of claimedByRaw) delete metadata[k];
801
+ const fm = {
802
+ okf_version: okfVersion,
803
+ ...options?.profile ? { profile: options.profile } : {},
804
+ ...metadata
805
+ };
806
+ const seenRawKeys = /* @__PURE__ */ new Set();
807
+ const uniqueRawBlocks = [];
808
+ for (const b of rawBlocks) {
809
+ const k = rawBlockKeyOf(b);
810
+ if (k !== void 0 && seenRawKeys.has(k)) continue;
811
+ if (k !== void 0) seenRawKeys.add(k);
812
+ uniqueRawBlocks.push(b);
813
+ }
814
+ return `${serializeFrontmatter(fm, { rawBlocks: uniqueRawBlocks })}
448
815
  ${renderSections(sections)}`;
449
816
  }
450
817
  function parseRootIndexMd(content) {
451
- const { frontmatter } = parseFrontmatter(content);
818
+ const { frontmatter, raw, issues } = parseFrontmatter(content);
819
+ const metadata = {};
820
+ let literalReservedRaw = false;
821
+ for (const [key, value] of Object.entries(frontmatter)) {
822
+ if (value === void 0) continue;
823
+ if (key === "okf_version" || key === "profile") continue;
824
+ if (key === RAW_SLOT_KEY) {
825
+ literalReservedRaw = true;
826
+ continue;
827
+ }
828
+ if (key === "type" && value === "") continue;
829
+ metadata[key] = value;
830
+ }
831
+ if (literalReservedRaw) issues.push({ key: RAW_SLOT_KEY, reason: "reserved_key" });
832
+ const builderOwnedRaw = Object.fromEntries(
833
+ Object.entries(raw).filter(
834
+ ([key]) => key !== "okf_version" && key !== "profile" && key !== RAW_SLOT_KEY
835
+ )
836
+ );
837
+ if (Object.keys(builderOwnedRaw).length > 0) {
838
+ metadata[RAW_SLOT_KEY] = { ...builderOwnedRaw };
839
+ }
452
840
  const okf = frontmatter.okf_version;
841
+ let okfVersion;
842
+ if (typeof okf === "string") okfVersion = okf;
843
+ else {
844
+ const rawLine = raw["okf_version"];
845
+ let rawText = rawLine != null ? rawLine.slice(rawLine.indexOf(":") + 1).trim() : void 0;
846
+ if (rawText != null) {
847
+ const hash = rawText.indexOf(" #");
848
+ if (hash !== -1) rawText = rawText.slice(0, hash).trim();
849
+ }
850
+ if (rawText != null && /^\d+\.\d+$/.test(rawText)) okfVersion = rawText;
851
+ else if (typeof okf === "number" && Number.isFinite(okf)) okfVersion = String(okf);
852
+ }
453
853
  return {
454
- okf_version: typeof okf === "string" ? okf : typeof okf === "number" && Number.isFinite(okf) ? String(okf) : void 0,
455
- profile: typeof frontmatter.profile === "string" ? frontmatter.profile : void 0
854
+ okf_version: okfVersion,
855
+ profile: typeof frontmatter.profile === "string" ? frontmatter.profile : void 0,
856
+ metadata,
857
+ raw,
858
+ issues
456
859
  };
457
860
  }
458
861
 
@@ -885,34 +1288,96 @@ function serializeFootnotes(footnotes) {
885
1288
  }).join("\n");
886
1289
  }
887
1290
 
888
- // src/v02-helpers.ts
889
- function deriveTrustTier(verified) {
890
- if (!verified || verified.length === 0) return "unverified";
891
- for (const v of verified) {
892
- if (typeof v?.by === "string" && v.by.startsWith("human:")) return "human-reviewed";
1291
+ // src/v03-helpers.ts
1292
+ var DATE_ONLY = /^(\d{4})-(\d{2})-(\d{2})$/;
1293
+ var DATE_TIME = /^(\d{4})-(\d{2})-(\d{2})[Tt](\d{2}):(\d{2}):(\d{2})(\.\d+)?([Zz]|[+-]\d{2}:\d{2})$/;
1294
+ function epochFromDateTime(dt) {
1295
+ const year = Number(dt[1]);
1296
+ const month = Number(dt[2]);
1297
+ const day = Number(dt[3]);
1298
+ const hour = Number(dt[4]);
1299
+ const minute = Number(dt[5]);
1300
+ const second = Number(dt[6]);
1301
+ const frac = dt[7];
1302
+ const fracMs = frac ? Number(frac.slice(1, 4).padEnd(3, "0")) : 0;
1303
+ let ms = Date.UTC(year, month - 1, day, hour, minute, second, fracMs);
1304
+ const offset = dt[8];
1305
+ if (offset[0] === "+" || offset[0] === "-") {
1306
+ const sign = offset[0] === "-" ? -1 : 1;
1307
+ const oh = Number(offset.slice(1, 3));
1308
+ const om = Number(offset.slice(4, 6));
1309
+ ms -= sign * (oh * 60 + om) * 6e4;
893
1310
  }
894
- return "machine-confirmed";
1311
+ return ms;
895
1312
  }
896
- function parseStaleAfter(staleAfter) {
897
- if (staleAfter == null) return null;
898
- if (typeof staleAfter === "number") return Number.isFinite(staleAfter) ? staleAfter : null;
899
- const match = /^(\d{4})-(\d{2})-(\d{2})$/.exec(staleAfter);
900
- if (!match) return null;
901
- const year = Number(match[1]);
902
- const month = Number(match[2]);
903
- const day = Number(match[3]);
1313
+ function utcDayValid(year, month, day) {
904
1314
  const ts = Date.UTC(year, month - 1, day);
905
- if (!Number.isFinite(ts)) return null;
906
1315
  const d = new Date(ts);
907
- if (d.getUTCFullYear() !== year || d.getUTCMonth() + 1 !== month || d.getUTCDate() !== day) {
908
- return null;
1316
+ return d.getUTCFullYear() === year && d.getUTCMonth() + 1 === month && d.getUTCDate() === day;
1317
+ }
1318
+ function parseOkfTimestamp(value) {
1319
+ if (typeof value === "number") return Number.isFinite(value) ? value : null;
1320
+ if (typeof value !== "string") return null;
1321
+ const s = value.trim();
1322
+ if (s.length === 0) return null;
1323
+ const dateOnly = DATE_ONLY.exec(s);
1324
+ if (dateOnly) {
1325
+ const year2 = Number(dateOnly[1]);
1326
+ const month2 = Number(dateOnly[2]);
1327
+ const day2 = Number(dateOnly[3]);
1328
+ if (!utcDayValid(year2, month2, day2)) return null;
1329
+ return Date.UTC(year2, month2 - 1, day2);
1330
+ }
1331
+ const dt = DATE_TIME.exec(s);
1332
+ if (!dt) return null;
1333
+ const year = Number(dt[1]);
1334
+ const month = Number(dt[2]);
1335
+ const day = Number(dt[3]);
1336
+ const hour = Number(dt[4]);
1337
+ const minute = Number(dt[5]);
1338
+ const second = Number(dt[6]);
1339
+ if (!utcDayValid(year, month, day)) return null;
1340
+ if (hour > 23 || minute > 59 || second > 60) return null;
1341
+ const offset = dt[8];
1342
+ if (offset[0] !== "Z" && offset[0] !== "z") {
1343
+ const oh = Number(offset.slice(1, 3));
1344
+ const om = Number(offset.slice(4, 6));
1345
+ if (oh > 23 || om > 59) return null;
1346
+ }
1347
+ return epochFromDateTime(dt);
1348
+ }
1349
+
1350
+ // src/v02-helpers.ts
1351
+ function deriveTrustTier(verified, generatedAt) {
1352
+ const events = !verified ? [] : Array.isArray(verified) ? verified : [verified];
1353
+ if (events.length === 0) return "unverified";
1354
+ const restricted = generatedAt !== void 0 && generatedAt !== null;
1355
+ const cutoff = restricted ? parseOkfTimestamp(generatedAt) : null;
1356
+ const isCurrent = (v) => {
1357
+ if (!restricted) return true;
1358
+ const at = parseOkfTimestamp(v.at);
1359
+ if (at == null) return false;
1360
+ return cutoff != null && at >= cutoff;
1361
+ };
1362
+ const current = events.filter(isCurrent);
1363
+ if (current.length === 0) return "unverified";
1364
+ for (const v of current) {
1365
+ if (typeof v?.by === "string" && v.by.startsWith("human:")) {
1366
+ return "human-reviewed";
1367
+ }
909
1368
  }
910
- return ts;
1369
+ return "machine-confirmed";
911
1370
  }
912
1371
  function isStaleAfter(staleAfter, now) {
913
- const cutoff = parseStaleAfter(staleAfter);
914
- if (cutoff == null) return false;
915
- return now >= cutoff;
1372
+ if (staleAfter == null) return false;
1373
+ if (typeof staleAfter === "number") {
1374
+ return Number.isFinite(staleAfter) ? now >= staleAfter : true;
1375
+ }
1376
+ if (typeof staleAfter !== "string") return true;
1377
+ if (staleAfter.trim().length === 0) return false;
1378
+ const cutoff = parseOkfTimestamp(staleAfter);
1379
+ if (cutoff != null) return now >= cutoff;
1380
+ return true;
916
1381
  }
917
1382
  function parseVerifiedFlexible(value) {
918
1383
  if (value == null) return [];
@@ -926,12 +1391,28 @@ function parseVerifiedFlexible(value) {
926
1391
  }
927
1392
  return [];
928
1393
  }
1394
+ function parseSourcesFlexible(value) {
1395
+ const isValidSource = (v) => !!v && typeof v === "object" && !Array.isArray(v) && typeof v.resource === "string" && v.resource !== "";
1396
+ if (value == null) return { sources: [], ignored: 0 };
1397
+ if (Array.isArray(value)) {
1398
+ const sources = [];
1399
+ let ignored = 0;
1400
+ for (const entry of value) {
1401
+ if (isValidSource(entry)) sources.push(entry);
1402
+ else ignored += 1;
1403
+ }
1404
+ return { sources, ignored };
1405
+ }
1406
+ if (isValidSource(value)) return { sources: [value], ignored: 0 };
1407
+ if (typeof value === "object") return { sources: [], ignored: 1 };
1408
+ return { sources: [], ignored: 0 };
1409
+ }
929
1410
  function formatVerifiedJson(entries) {
930
1411
  const sorted = [...entries].sort((a, b) => {
931
- const ta = Date.parse(a.at);
932
- const tb = Date.parse(b.at);
933
- const fa = Number.isFinite(ta);
934
- const fb = Number.isFinite(tb);
1412
+ const ta = parseOkfTimestamp(a.at);
1413
+ const tb = parseOkfTimestamp(b.at);
1414
+ const fa = ta != null;
1415
+ const fb = tb != null;
935
1416
  if (fa && fb) return ta - tb;
936
1417
  if (fa && !fb) return -1;
937
1418
  if (!fa && fb) return 1;
@@ -950,8 +1431,8 @@ function latestVerified(entries, _now) {
950
1431
  let best = null;
951
1432
  let bestAt = -Infinity;
952
1433
  for (const e of entries) {
953
- const ts = Date.parse(e.at);
954
- if (Number.isFinite(ts) && ts > bestAt) {
1434
+ const ts = parseOkfTimestamp(e.at);
1435
+ if (ts != null && ts > bestAt) {
955
1436
  best = e;
956
1437
  bestAt = ts;
957
1438
  }
@@ -1045,7 +1526,9 @@ exports.parseFlowMapping = parseFlowMapping;
1045
1526
  exports.parseFlowSequence = parseFlowSequence;
1046
1527
  exports.parseFrontmatter = parseFrontmatter;
1047
1528
  exports.parseLogMd = parseLogMd;
1529
+ exports.parseOkfTimestamp = parseOkfTimestamp;
1048
1530
  exports.parseRootIndexMd = parseRootIndexMd;
1531
+ exports.parseSourcesFlexible = parseSourcesFlexible;
1049
1532
  exports.parseVerifiedFlexible = parseVerifiedFlexible;
1050
1533
  exports.serializeActorString = serializeActorString;
1051
1534
  exports.serializeFootnotes = serializeFootnotes;