Elffuss BraidHub

kikocis/continue public

825624809c09 · 1.6 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 regex?: string | string[];
38 alwaysApply?: boolean;
39 } = {},
40): string {
41 const frontmatter: RuleFrontmatter = {};
42
43 if (options.globs) {
44 frontmatter.globs =
45 typeof options.globs === "string" ? options.globs.trim() : options.globs;
46 }
47
48 if (options.regex) {
49 frontmatter.regex =
50 typeof options.regex === "string" ? options.regex.trim() : options.regex;
51 }
52
53 if (options.description) {
54 frontmatter.description = options.description.trim();
55 }
56
57 if (options.alwaysApply !== undefined) {
58 frontmatter.alwaysApply = options.alwaysApply;
59 }
60
61 return createMarkdownWithFrontmatter(frontmatter, ruleContent);
62}