frontmatter.mjs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // YAML-subset frontmatter parser for design-resources SKILL.md files; no dependencies.
  2. // Supports nested maps, "- " lists (scalars and maps), flow lists (also across lines), | and > block scalars,
  3. // quoted scalars and " #" comments. Anything else throws instead of being skipped.
  4. export function splitFrontmatter(text) {
  5. const match = text.replace(/^\uFEFF/, "").match(/^---\r?\n([\s\S]*?)\r?\n---(?:\r?\n|$)/);
  6. return match ? match[1] : null;
  7. }
  8. function parseScalar(raw) {
  9. let s = raw.trim();
  10. if (/^[&*!{|>]/.test(s)) throw new Error(`unsupported YAML construct: ${s}`);
  11. if (s.startsWith('"')) {
  12. if (!/^"(?:[^"\\]|\\.)*"$/.test(s)) throw new Error(`unsupported quoted scalar: ${s}`);
  13. return JSON.parse(s);
  14. }
  15. if (s.startsWith("'")) {
  16. if (!/^'(?:[^']|'')*'$/.test(s)) throw new Error(`unsupported quoted scalar: ${s}`);
  17. return s.slice(1, -1).replace(/''/g, "'");
  18. }
  19. s = s.replace(/(^|\s)#.*$/, "").trim();
  20. if (s.startsWith("[")) {
  21. if (!s.endsWith("]")) throw new Error(`unterminated flow list: ${s}`);
  22. const inner = s.slice(1, -1).trim();
  23. return inner === "" ? [] : inner.split(",").map(parseScalar);
  24. }
  25. if (s === "") return null;
  26. if (s === "true") return true;
  27. if (s === "false") return false;
  28. if (/^-?\d+(?:\.\d+)?$/.test(s)) return Number(s);
  29. return s;
  30. }
  31. export function parseFrontmatter(source) {
  32. const lines = [];
  33. for (const line of source.replace(/^\uFEFF/, "").split(/\r?\n/)) {
  34. const prev = lines[lines.length - 1];
  35. // Join a flow list that continues on the next lines.
  36. if (prev !== undefined && /:\s*\[[^\]]*$/.test(prev)) lines[lines.length - 1] = `${prev} ${line.trim()}`;
  37. else lines.push(line);
  38. }
  39. let i = 0;
  40. const indentOf = (line) => line.match(/^ */)[0].length;
  41. const blank = (line) => line.trim() === "" || line.trim().startsWith("#");
  42. const keyLine = /^([A-Za-z0-9_.-]+):(?:\s+(.*)|\s*)$/;
  43. function block(parentIndent) {
  44. while (i < lines.length && blank(lines[i])) i++;
  45. if (i >= lines.length || indentOf(lines[i]) <= parentIndent) return null;
  46. const indent = indentOf(lines[i]);
  47. return /^-(?:\s|$)/.test(lines[i].slice(indent)) ? list(indent) : map(indent);
  48. }
  49. function blockScalar(style, parentIndent) {
  50. const out = [];
  51. let indent = -1;
  52. while (i < lines.length) {
  53. const line = lines[i];
  54. if (line.trim() === "") {
  55. out.push("");
  56. i++;
  57. continue;
  58. }
  59. if (indentOf(line) <= parentIndent) break;
  60. if (indent < 0) indent = indentOf(line);
  61. out.push(line.slice(indent));
  62. i++;
  63. }
  64. while (out.length && out[out.length - 1] === "") out.pop();
  65. return style.startsWith("|") ? out.join("\n") : out.join(" ").replace(/ +/g, " ");
  66. }
  67. function value(rest, parentIndent) {
  68. const s = rest.trim();
  69. if (s === "" || s.startsWith("#")) return block(parentIndent);
  70. if (/^[|>][+-]?$/.test(s)) return blockScalar(s, parentIndent);
  71. return parseScalar(s);
  72. }
  73. function map(indent) {
  74. const out = {};
  75. while (i < lines.length) {
  76. const line = lines[i];
  77. if (blank(line)) {
  78. i++;
  79. continue;
  80. }
  81. const lineIndent = indentOf(line);
  82. if (lineIndent < indent) break;
  83. if (lineIndent > indent) throw new Error(`unexpected indent: ${line}`);
  84. const match = line.slice(lineIndent).match(keyLine);
  85. if (!match) throw new Error(`unsupported YAML line: ${line}`);
  86. i++;
  87. out[match[1]] = value(match[2] ?? "", indent);
  88. }
  89. return out;
  90. }
  91. function list(indent) {
  92. const out = [];
  93. while (i < lines.length) {
  94. const line = lines[i];
  95. if (blank(line)) {
  96. i++;
  97. continue;
  98. }
  99. const lineIndent = indentOf(line);
  100. if (lineIndent < indent || !/^-(?:\s|$)/.test(line.slice(lineIndent))) break;
  101. if (lineIndent > indent) throw new Error(`unexpected indent: ${line}`);
  102. const rest = line.slice(lineIndent + 1).replace(/^ /, "");
  103. if (keyLine.test(rest) && !/^["']/.test(rest)) {
  104. lines[i] = " ".repeat(lineIndent + 2) + rest;
  105. out.push(map(lineIndent + 2));
  106. } else {
  107. i++;
  108. out.push(value(rest, lineIndent));
  109. }
  110. }
  111. return out;
  112. }
  113. return map(0);
  114. }