Elffuss BraidHub

kikocis/continue public

d0cd3fef8227 · 1.4 KiBRawPermalink
1import * as YAML from "yaml";
2import { RuleFrontmatter } from "./markdownToRule.js";
3
4export const RULE_FILE_EXTENSION = "md";
5
6/**
7 * Sanitizes a rule name for use in filenames (removes special chars, replaces spaces with dashes)
8 */
9export function sanitizeRuleName(name: string): string {
10 return name
11 .toLowerCase()
12 .replace(/[^a-z0-9\s-]/g, "")
13 .replace(/\s+/g, "-")
14 .replace(/^-+|-+$/g, ""); // Remove leading/trailing dashes
15}
16
17/**
18 * Creates markdown content with YAML frontmatter in the format expected by parseMarkdownRule
19 */
20export function createMarkdownWithFrontmatter(
21 frontmatter: RuleFrontmatter,
22 markdown: string,
23): string {
24 const frontmatterStr = YAML.stringify(frontmatter).trim();
25 return `---\n${frontmatterStr}\n---\n\n${markdown}`;
26}
27
28/**
29 * Creates a rule markdown file content from rule components
30 */
31export function createRuleMarkdown(
32 name: string,
33 ruleContent: string,
34 options: {
35 description?: string;
36 globs?: string | string[];
37 alwaysApply?: boolean;
38 } = {},
39): string {
40 const frontmatter: RuleFrontmatter = {};
41
42 if (options.globs) {
43 frontmatter.globs =
44 typeof options.globs === "string" ? options.globs.trim() : options.globs;
45 }
46
47 if (options.description) {
48 frontmatter.description = options.description.trim();
49 }
50
51 if (options.alwaysApply !== undefined) {
52 frontmatter.alwaysApply = options.alwaysApply;
53 }
54
55 return createMarkdownWithFrontmatter(frontmatter, ruleContent);
56}