singularity-forge/packages/coding-agent/src/utils/frontmatter.ts
Mikael Hugo 6725a55591 feat(web): add error boundaries, expand test coverage, add README
- Add class-based ErrorBoundary component wrapping all 7 main views
  inside WorkspaceChrome; fallback shows view name, error, reload button
- Add 30 new unit tests (boot null-project path × 9, onboarding
  pure-function logic × 21); all 43 web/lib tests pass
- Add web/README.md: architecture, auth flow, 7 views, dev setup,
  API route pattern, test instructions

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-05-10 11:24:40 +02:00

45 lines
1.1 KiB
TypeScript

import { parse } from "yaml";
type ParsedFrontmatter<T extends Record<string, unknown>> = {
frontmatter: T;
body: string;
};
const normalizeNewlines = (value: string): string =>
value.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
const extractFrontmatter = (
content: string,
): { yamlString: string | null; body: string } => {
const normalized = normalizeNewlines(content);
if (!normalized.startsWith("---")) {
return { yamlString: null, body: normalized };
}
const endIndex = normalized.indexOf("\n---", 3);
if (endIndex === -1) {
return { yamlString: null, body: normalized };
}
return {
yamlString: normalized.slice(4, endIndex),
body: normalized.slice(endIndex + 4).trim(),
};
};
export const parseFrontmatter = <
T extends Record<string, unknown> = Record<string, unknown>,
>(
content: string,
): ParsedFrontmatter<T> => {
const { yamlString, body } = extractFrontmatter(content);
if (!yamlString) {
return { frontmatter: {} as T, body };
}
const parsed = parse(yamlString);
return { frontmatter: (parsed ?? {}) as T, body };
};
export const stripFrontmatter = (content: string): string =>
parseFrontmatter(content).body;