Files
Rune-Spec/src/core/task-parser.ts

37 lines
973 B
TypeScript

import type { TaskItem } from "../types.ts";
import { CommandError } from "../cli/errors.ts";
export function parseTasks(content: string): TaskItem[] {
const tasks: TaskItem[] = [];
const lines = content.split(/\r?\n/);
for (const line of lines) {
const match = line.match(/^[\s]*- \[([ xX])\] (.*)$/);
if (match) {
tasks.push({
checked: match[1] !== " ",
text: match[2],
});
}
}
return tasks;
}
export class TaskFormatError extends CommandError {
constructor(message: string) {
super(message);
this.name = this.constructor.name;
}
}
export function validateTaskFormat(content: string): void {
const tasks = parseTasks(content);
if (tasks.length === 0) {
throw new TaskFormatError("task.md 必须包含至少一个 checkbox 项");
}
for (const task of tasks) {
if (task.text.trim() === "") {
throw new TaskFormatError("task.md 中每个 checkbox 项必须有非空描述");
}
}
}