Forked from ceveyne/user-docs
Forked from ceveyne/user-docs
src / helpers / frontmatter.ts
/**
* frontmatter.ts — Playbook-compatible YAML frontmatter helpers.
*
* Extracted verbatim from playbook/src/tools/memorize.ts (slugify + buildFrontmatter).
* Shared by export_doc and memorize_doc.
*/
import path from "node:path";
export function slugify(title: string): string {
return title
.toLowerCase()
.normalize("NFD")
.replace(/[\u0300-\u036f]/g, "") // strip diacritics
.replace(/[^\w\s-]/g, "")
.trim()
.replace(/[\s_]+/g, "-")
.replace(/-+/g, "-")
.slice(0, 80);
}
export function buildFrontmatter(title: string, tags: string[], now: string, existingCreated?: string): string {
const created = existingCreated ?? now;
const tagList = tags.map((t) => `"${t}"`).join(", ");
return `---\ntitle: "${title}"\ntags: [${tagList}]\ncreated: "${created}"\nupdated: "${now}"\n---\n\n`;
}
/** SKILL.md frontmatter (name/description instead of title/tags) — Agent Skills–compatible. */
export function buildSkillFrontmatter(name: string, description: string, now: string, existingCreated?: string): string {
const created = existingCreated ?? now;
const esc = (s: string) => s.replace(/"/g, '\\"');
return `---\nname: "${esc(name)}"\ndescription: "${esc(description)}"\ncreated: "${created}"\nupdated: "${now}"\n---\n\n`;
}
/** Reject absolute paths, `..` traversal, and anything outside the supported .md/.txt extensions. */
export function sanitizeRelativeFile(file: string): string | null {
if (!file || path.isAbsolute(file)) return null;
const normalized = path.normalize(file);
if (normalized === "." || normalized.split(path.sep).some((part) => part === "..")) return null;
const ext = path.extname(normalized).toLowerCase();
if (ext !== ".md" && ext !== ".txt") return null;
return normalized;
}
src / helpers / frontmatter.ts
/**
* frontmatter.ts — Playbook-compatible YAML frontmatter helpers.
*
* Extracted verbatim from playbook/src/tools/memorize.ts (slugify + buildFrontmatter).
* Shared by export_doc and memorize_doc.
*/
import path from "node:path";
export function slugify(title: string): string {
return title
.toLowerCase()
.normalize("NFD")
.replace(/[\u0300-\u036f]/g, "") // strip diacritics
.replace(/[^\w\s-]/g, "")
.trim()
.replace(/[\s_]+/g, "-")
.replace(/-+/g, "-")
.slice(0, 80);
}
export function buildFrontmatter(title: string, tags: string[], now: string, existingCreated?: string): string {
const created = existingCreated ?? now;
const tagList = tags.map((t) => `"${t}"`).join(", ");
return `---\ntitle: "${title}"\ntags: [${tagList}]\ncreated: "${created}"\nupdated: "${now}"\n---\n\n`;
}
/** SKILL.md frontmatter (name/description instead of title/tags) — Agent Skills–compatible. */
export function buildSkillFrontmatter(name: string, description: string, now: string, existingCreated?: string): string {
const created = existingCreated ?? now;
const esc = (s: string) => s.replace(/"/g, '\\"');
return `---\nname: "${esc(name)}"\ndescription: "${esc(description)}"\ncreated: "${created}"\nupdated: "${now}"\n---\n\n`;
}
/** Reject absolute paths, `..` traversal, and anything outside the supported .md/.txt extensions. */
export function sanitizeRelativeFile(file: string): string | null {
if (!file || path.isAbsolute(file)) return null;
const normalized = path.normalize(file);
if (normalized === "." || normalized.split(path.sep).some((part) => part === "..")) return null;
const ext = path.extname(normalized).toLowerCase();
if (ext !== ".md" && ext !== ".txt") return null;
return normalized;
}