refactor(oc-path): share frontmatter formatting (#120098)

This commit is contained in:
Vincent Koc
2026-08-26 05:45:20 +08:00
committed by GitHub
parent 5ac1b3c558
commit aaae1f0c8d
4 changed files with 13 additions and 31 deletions
+1 -10
View File
@@ -10,6 +10,7 @@
*/
import type { AstBlock, AstItem, FrontmatterEntry, MdAst } from "./ast.js";
import { formatFrontmatterValue } from "./frontmatter-format.js";
import { formatOcPath, type OcPath } from "./oc-path.js";
import { guardSentinel } from "./sentinel.js";
@@ -139,13 +140,3 @@ function finalize(ast: MdAst): MdEditResult {
}
return { ok: true, ast: { ...ast, raw: parts.join("\n") } };
}
function formatFrontmatterValue(value: string): string {
if (value.length === 0) {
return '""';
}
if (/[:#&*?|<>=!%@`,[\]{}\r\n]/.test(value)) {
return JSON.stringify(value);
}
return value;
}
+1 -11
View File
@@ -23,6 +23,7 @@
*/
import type { MdAst } from "./ast.js";
import { formatFrontmatterValue } from "./frontmatter-format.js";
import { guardSentinel } from "./sentinel.js";
/**
@@ -114,16 +115,5 @@ export function emitMd(ast: MdAst, opts: EmitOptions = {}): string {
return parts.join("\n");
}
function formatFrontmatterValue(value: string): string {
// Frontmatter is yaml-ish; quote values with structural chars.
if (value.length === 0) {
return '""';
}
if (/[:#&*?|<>=!%@`,[\]{}\r\n]/.test(value)) {
return JSON.stringify(value);
}
return value;
}
// Re-export the frontmatter type for convenience so tests don't need
// to import from ast.ts.
@@ -0,0 +1,10 @@
export function formatFrontmatterValue(value: string): string {
// Frontmatter is yaml-ish; quote values with structural chars.
if (value.length === 0) {
return '""';
}
if (/[:#&*?|<>=!%@`,[\]{}\r\n]/.test(value)) {
return JSON.stringify(value);
}
return value;
}
+1 -10
View File
@@ -19,6 +19,7 @@ import { expectDefined } from "openclaw/plugin-sdk/expect-runtime";
import { isMap, isScalar, isSeq, type Pair } from "yaml";
import type { MdAst } from "./ast.js";
import { setMdOcPath } from "./edit.js";
import { formatFrontmatterValue } from "./frontmatter-format.js";
import type { JsoncAst, JsoncEntry, JsoncValue } from "./jsonc/ast.js";
import { insertJsoncOcPath, setJsoncOcPath } from "./jsonc/edit.js";
import { resolveJsoncOcPath } from "./jsonc/resolve.js";
@@ -873,16 +874,6 @@ function rebuildMdRaw(ast: MdAst): MdAst {
return { ...ast, raw: parts.join("\n") };
}
function formatFrontmatterValue(value: string): string {
if (value.length === 0) {
return '""';
}
if (/[:#&*?|<>=!%@`,[\]{}\r\n]/.test(value)) {
return JSON.stringify(value);
}
return value;
}
function slugifyHeading(s: string): string {
return s
.toLowerCase()