mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-03 03:03:24 -04:00
refactor(shared): dedupe frontmatter parsing
This commit is contained in:
43
src/shared/frontmatter.test.ts
Normal file
43
src/shared/frontmatter.test.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { describe, expect, test } from "vitest";
|
||||
import {
|
||||
getFrontmatterString,
|
||||
normalizeStringList,
|
||||
parseFrontmatterBool,
|
||||
resolveOpenClawManifestBlock,
|
||||
} from "./frontmatter.js";
|
||||
|
||||
describe("shared/frontmatter", () => {
|
||||
test("normalizeStringList handles strings and arrays", () => {
|
||||
expect(normalizeStringList("a, b,,c")).toEqual(["a", "b", "c"]);
|
||||
expect(normalizeStringList([" a ", "", "b"])).toEqual(["a", "b"]);
|
||||
expect(normalizeStringList(null)).toEqual([]);
|
||||
});
|
||||
|
||||
test("getFrontmatterString extracts strings only", () => {
|
||||
expect(getFrontmatterString({ a: "b" }, "a")).toBe("b");
|
||||
expect(getFrontmatterString({ a: 1 }, "a")).toBeUndefined();
|
||||
});
|
||||
|
||||
test("parseFrontmatterBool respects fallback", () => {
|
||||
expect(parseFrontmatterBool("true", false)).toBe(true);
|
||||
expect(parseFrontmatterBool("false", true)).toBe(false);
|
||||
expect(parseFrontmatterBool(undefined, true)).toBe(true);
|
||||
});
|
||||
|
||||
test("resolveOpenClawManifestBlock parses JSON5 metadata and picks openclaw block", () => {
|
||||
const frontmatter = {
|
||||
metadata: "{ openclaw: { foo: 1, bar: 'baz' } }",
|
||||
};
|
||||
expect(resolveOpenClawManifestBlock({ frontmatter })).toEqual({ foo: 1, bar: "baz" });
|
||||
});
|
||||
|
||||
test("resolveOpenClawManifestBlock returns undefined for invalid input", () => {
|
||||
expect(resolveOpenClawManifestBlock({ frontmatter: {} })).toBeUndefined();
|
||||
expect(
|
||||
resolveOpenClawManifestBlock({ frontmatter: { metadata: "not-json5" } }),
|
||||
).toBeUndefined();
|
||||
expect(
|
||||
resolveOpenClawManifestBlock({ frontmatter: { metadata: "{ nope: { a: 1 } }" } }),
|
||||
).toBeUndefined();
|
||||
});
|
||||
});
|
||||
60
src/shared/frontmatter.ts
Normal file
60
src/shared/frontmatter.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import JSON5 from "json5";
|
||||
import { LEGACY_MANIFEST_KEYS, MANIFEST_KEY } from "../compat/legacy-names.js";
|
||||
import { parseBooleanValue } from "../utils/boolean.js";
|
||||
|
||||
export function normalizeStringList(input: unknown): string[] {
|
||||
if (!input) {
|
||||
return [];
|
||||
}
|
||||
if (Array.isArray(input)) {
|
||||
return input.map((value) => String(value).trim()).filter(Boolean);
|
||||
}
|
||||
if (typeof input === "string") {
|
||||
return input
|
||||
.split(",")
|
||||
.map((value) => value.trim())
|
||||
.filter(Boolean);
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
export function getFrontmatterString(
|
||||
frontmatter: Record<string, unknown>,
|
||||
key: string,
|
||||
): string | undefined {
|
||||
const raw = frontmatter[key];
|
||||
return typeof raw === "string" ? raw : undefined;
|
||||
}
|
||||
|
||||
export function parseFrontmatterBool(value: string | undefined, fallback: boolean): boolean {
|
||||
const parsed = parseBooleanValue(value);
|
||||
return parsed === undefined ? fallback : parsed;
|
||||
}
|
||||
|
||||
export function resolveOpenClawManifestBlock(params: {
|
||||
frontmatter: Record<string, unknown>;
|
||||
key?: string;
|
||||
}): Record<string, unknown> | undefined {
|
||||
const raw = getFrontmatterString(params.frontmatter, params.key ?? "metadata");
|
||||
if (!raw) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
try {
|
||||
const parsed = JSON5.parse(raw);
|
||||
if (!parsed || typeof parsed !== "object") {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const manifestKeys = [MANIFEST_KEY, ...LEGACY_MANIFEST_KEYS];
|
||||
for (const key of manifestKeys) {
|
||||
const candidate = (parsed as Record<string, unknown>)[key];
|
||||
if (candidate && typeof candidate === "object") {
|
||||
return candidate as Record<string, unknown>;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user