Elffuss BraidHub

kikocis/continue public

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