openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
198 lines (197 loc) • 6.42 kB
JavaScript
import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs";
import JSON5 from "json5";
import { isAlias, isMap, isNode, isScalar, parseDocument } from "yaml";
//#region packages/markdown-core/src/frontmatter.ts
function stripQuotes(e) {
const t = e.at(0);
return (t === "\"" || t === "'") && e.at(-1) === t ? e.slice(1, -1) : e;
}
function coerceYamlFrontmatterValue(e) {
if (e === null || e === void 0) return;
if (typeof e === "string" || typeof e === "number" || typeof e === "boolean") return String(e).trim();
if (typeof e === "object") try {
return JSON.stringify(e);
} catch {
return;
}
}
function parseLineFrontmatter(e) {
const t = {};
const n = e.split("\n");
for (let e = 0; e < n.length; e += 1) {
const r = n.at(e)?.match(/^([\w-]+):\s*(.*)$/);
const i = r?.[1];
const a = r?.[2];
if (!i || a === void 0) continue;
let o = a.trim();
if (!o && /^[ \t]/.test(n.at(e + 1) ?? "")) {
const t = [];
while (e + 1 < n.length) {
const r = n.at(e + 1);
if (r === void 0 || r && !/^[ \t]/.test(r)) break;
t.push(r);
e += 1;
}
o = t.join("\n").trim();
} else o = stripQuotes(o);
if (o) t[i] = o;
}
return t;
}
const FREEFORM_TEXT_FIELDS = /* @__PURE__ */ new Set([
"description",
"read_when",
"summary"
]);
function normalizeFreeformFieldAtError(e, t) {
const n = t ?? parseDocument(e, {
schema: "core",
prettyErrors: false
});
if (!isMap(n.contents)) return e;
const r = n.errors.find((e) => e.pos?.[0] !== void 0);
const i = n.contents.items.find((e) => isScalar(e.key) && e.key.value === "description" && isAlias(e.value));
const a = r?.pos?.[0] ?? (isNode(i?.key) ? i.key.range?.[0] : void 0);
if (a === void 0) return e;
const o = e.lastIndexOf("\n", a) + 1;
const s = e.indexOf("\n", a);
const c = s === -1 ? e.length : s;
const l = e.slice(o, c).match(/^(?:([\w-]+)|"([\w-]+)"|'([\w-]+)'):\s*(.*)$/);
const u = l?.[1] ?? l?.[2] ?? l?.[3];
const d = l?.[4]?.trim();
const f = n.contents.items.some((e) => isScalar(e.key) && e.key.value === u && e.key.range?.[0] === o);
const p = r?.code === "BLOCK_AS_IMPLICIT_KEY" && d?.includes(": ");
if (!u || !d || !FREEFORM_TEXT_FIELDS.has(u) || !f || u !== "description" && !p || /^[|>](?:[1-9][+-]?|[+-][1-9]?)?$/.test(d)) return e;
const m = `${u}: ${JSON.stringify(stripQuotes(d))}`;
return `${e.slice(0, o)}${m}${e.slice(c)}`;
}
function parseYamlFrontmatterOnce(e, t) {
let n;
const failed = (e) => ({
document: n,
result: {
frontmatter: t,
issues: e
}
});
try {
n = parseDocument(e, {
schema: "core",
prettyErrors: false
});
if (n.errors.length > 0 || !isMap(n.contents)) return failed(n.errors.length > 0 ? n.errors.map((e) => ({
code: e.code ?? e.name,
message: e.message
})) : [{
code: "INVALID_ROOT",
message: "frontmatter must be a YAML mapping"
}]);
const r = n.toJS();
if (!isRecord(r)) return failed([{
code: "INVALID_ROOT",
message: "frontmatter must be a YAML mapping"
}]);
const i = /* @__PURE__ */ new Map();
for (const t of n.contents.items) {
if (!isNode(t.key)) continue;
const n = t.key.range?.[0];
if (n === void 0) continue;
const r = e.indexOf("\n", n);
const a = e.slice(n, r === -1 ? e.length : r);
const o = a.match(/^([\w-]+):\s*(.*)$/);
const s = isNode(t.value) ? t.value.range?.[1] : void 0;
if (o?.[1] && o[2]?.includes(":") && s !== void 0 && (r === -1 || s <= r)) {
const t = n + a.length - o[2].length;
const r = e.slice(t, s).trim();
try {
JSON5.parse(r);
i.set(o[1], r);
} catch {}
}
}
const a = {};
for (const [e, n] of Object.entries(r)) {
const r = e.trim();
const o = r ? coerceYamlFrontmatterValue(n) : void 0;
if (o === void 0) continue;
const s = i.get(r);
a[r] = typeof n === "object" && s !== void 0 && Object.hasOwn(t, r) ? s : o;
}
for (const [e, n] of Object.entries(t)) if (!Object.hasOwn(a, e) && !/^#(?:\s|$)/.test(n)) a[e] = n;
return {
document: n,
result: {
frontmatter: a,
issues: []
}
};
} catch (e) {
return failed([{
code: "YAML_EXCEPTION",
message: e instanceof Error ? e.message : String(e)
}]);
}
}
function parseYamlFrontmatter(e) {
const t = parseLineFrontmatter(e);
let n = parseYamlFrontmatterOnce(e, t);
let r = e;
for (let e = 0; e < FREEFORM_TEXT_FIELDS.size && n.result.issues.length > 0; e += 1) {
const e = normalizeFreeformFieldAtError(r, n.document);
if (e === r) break;
r = e;
n = parseYamlFrontmatterOnce(r, t);
}
return n.result;
}
function normalizeFrontmatterContent(e) {
return e.replace(/^\uFEFF/, "").replace(/\r\n/g, "\n").replace(/\r/g, "\n");
}
const FRONTMATTER_CLOSING_DELIMITER = /(?:^|\n)---[^\S\n]*(?:\n|(?![\s\S]))/;
const FRONTMATTER_OPENING_DELIMITER = /^---[^\S\n]*\n/;
function extractFrontmatterBlockFromNormalized(e) {
const t = FRONTMATTER_OPENING_DELIMITER.exec(e);
if (!t) return;
const n = t[0].length;
const r = e.slice(n);
const i = FRONTMATTER_CLOSING_DELIMITER.exec(r);
if (!i) return;
return {
block: r.slice(0, i.index),
body: r.slice(i.index + i[0].length)
};
}
/** Splits a complete leading YAML frontmatter block from its Markdown body. */
function extractFrontmatterBlock(e) {
return extractFrontmatterBlockFromNormalized(normalizeFrontmatterContent(e));
}
/** Removes a leading YAML frontmatter block and returns the remaining Markdown body. */
function stripFrontmatterBlock(e) {
const t = normalizeFrontmatterContent(e);
return (extractFrontmatterBlockFromNormalized(t)?.body ?? t).trim();
}
/** Parses leading YAML frontmatter into string values used by skill and metadata loaders. */
function parseFrontmatterBlock(e) {
return parseFrontmatterBlockResult(e).frontmatter;
}
/** Parses frontmatter once while retaining recoverable YAML parser issues for owning loaders. */
function parseFrontmatterBlockResult(e) {
const t = normalizeFrontmatterContent(e);
const n = extractFrontmatterBlockFromNormalized(t)?.block;
if (n !== void 0) return n ? parseYamlFrontmatter(n) : {
frontmatter: {},
issues: []
};
return FRONTMATTER_OPENING_DELIMITER.test(t) ? {
frontmatter: {},
issues: [{
code: "UNTERMINATED_FRONTMATTER",
message: "missing closing --- delimiter"
}]
} : {
frontmatter: {},
issues: []
};
}
//#endregion
export { stripFrontmatterBlock as i, parseFrontmatterBlock as n, parseFrontmatterBlockResult as r, extractFrontmatterBlock as t };