singularity-forge/packages/native/src/forge-parser/index.ts
2026-05-05 14:46:18 +02:00

142 lines
3.7 KiB
TypeScript

/**
* SF file parser — native Rust implementation.
*
* Parses `.sf/` directory markdown files containing YAML-like frontmatter
* and structured sections. Replaces the JS regex-based parser for
* performance-critical batch operations.
*/
import { native } from "../native.js";
import type {
BatchParseResult,
FrontmatterResult,
JsonlParseResult,
NativePlan,
NativeRoadmap,
NativeSummary,
SectionResult,
SfTreeEntry,
} from "./types.js";
export type {
BatchParseResult,
FrontmatterResult,
JsonlParseResult,
NativeBoundaryMapEntry,
NativeFileModified,
NativePlan,
NativeRoadmap,
NativeRoadmapSlice,
NativeSummary,
NativeSummaryFrontmatter,
NativeSummaryRequires,
NativeTaskEntry,
ParsedSfFile,
SectionResult,
SfTreeEntry,
} from "./types.js";
/**
* Parse YAML-like frontmatter from markdown content.
*
* Returns `{ metadata, body }` where `metadata` is a JSON string
* of the parsed frontmatter key-value pairs. Parse it with `JSON.parse()`.
*/
export function parseFrontmatter(content: string): FrontmatterResult {
return (
native as Record<string, (...args: unknown[]) => unknown>
).parseFrontmatter(content) as FrontmatterResult;
}
/**
* Extract a section from markdown content by heading name.
*
* @param content Markdown content to search.
* @param heading Heading text to match (without the `#` prefix).
* @param level Heading level (default 2 for `##`).
*/
export function extractSection(
content: string,
heading: string,
level?: number,
): SectionResult {
return (
native as Record<string, (...args: unknown[]) => unknown>
).extractSection(content, heading, level) as SectionResult;
}
/**
* Extract all sections at a given heading level.
*
* Returns a JSON string mapping heading names to their content.
* Parse with `JSON.parse()`.
*/
export function extractAllSections(content: string, level?: number): string {
return (
native as Record<string, (...args: unknown[]) => unknown>
).extractAllSections(content, level) as string;
}
/**
* Batch-parse all `.md` files in a `.sf/` directory tree.
*
* Reads and parses all markdown files under the given directory.
* Each file gets frontmatter parsing and section extraction.
*/
export function batchParseSfFiles(directory: string): BatchParseResult {
return (
native as Record<string, (...args: unknown[]) => unknown>
).batchParseSfFiles(directory) as BatchParseResult;
}
/**
* Parse a roadmap file's content into structured data.
*
* Extracts title, vision, success criteria, slices (with risk/depends),
* and boundary map entries.
*/
export function parseRoadmapFile(content: string): NativeRoadmap {
return (
native as Record<string, (...args: unknown[]) => unknown>
).parseRoadmapFile(content) as NativeRoadmap;
}
/**
* Scan a `.sf/` directory tree.
*/
export function scanSfTree(directory: string): SfTreeEntry[] {
return (native as Record<string, (...args: unknown[]) => unknown>).scanSfTree(
directory,
) as SfTreeEntry[];
}
/**
* Parse the tail of a JSONL file without loading the full file into JS memory.
*/
export function parseJsonlTail(
filePath: string,
maxBytes?: number,
maxEntries?: number,
): JsonlParseResult {
return (
native as Record<string, (...args: unknown[]) => unknown>
).parseJsonlTail(filePath, maxBytes, maxEntries) as JsonlParseResult;
}
/**
* Parse a task plan markdown file into structured data.
*/
export function parsePlanFile(content: string): NativePlan {
return (
native as Record<string, (...args: unknown[]) => unknown>
).parsePlanFile(content) as NativePlan;
}
/**
* Parse a summary markdown file into structured data.
*/
export function parseSummaryFile(content: string): NativeSummary {
return (
native as Record<string, (...args: unknown[]) => unknown>
).parseSummaryFile(content) as NativeSummary;
}