Changesets · Branches · Tags · Compare
| Changeset | 681f4f45131eff784362b0007242e7b5dc38cff2c5aea1fc4105b046116f5d34 |
|---|---|
| Author | Nate Sesti <33237525+sestinj@users.noreply.github.com> · 2025-06-16 20:35 -07:00 |
| Committer | GitHub <noreply@github.com> · 2025-06-16 20:35 -07:00 |
| Parents | c3e8fbcfb752, 20f1ec866261 |
30 files changed
deleted core/config/markdown/createMarkdownRule.test.ts +0 −157
| @@ -1,157 +0,0 @@ | ||
| 1 | -import { | |
| 2 | - createMarkdownWithFrontmatter, | |
| 3 | - createRuleFilePath, | |
| 4 | - createRuleMarkdown, | |
| 5 | - sanitizeRuleName, | |
| 6 | -} from "./createMarkdownRule"; | |
| 7 | -import { parseMarkdownRule } from "./parseMarkdownRule"; | |
| 8 | - | |
| 9 | -describe("sanitizeRuleName", () => { | |
| 10 | - it("should sanitize rule names for filenames", () => { | |
| 11 | - expect(sanitizeRuleName("My Test Rule")).toBe("my-test-rule"); | |
| 12 | - expect(sanitizeRuleName("Rule with @#$% chars")).toBe("rule-with-chars"); | |
| 13 | - expect(sanitizeRuleName("Multiple spaces")).toBe("multiple-spaces"); | |
| 14 | - expect(sanitizeRuleName("UPPERCASE-rule")).toBe("uppercase-rule"); | |
| 15 | - expect(sanitizeRuleName("already-sanitized")).toBe("already-sanitized"); | |
| 16 | - }); | |
| 17 | - | |
| 18 | - it("should handle empty and edge case inputs", () => { | |
| 19 | - expect(sanitizeRuleName("")).toBe(""); | |
| 20 | - expect(sanitizeRuleName(" ")).toBe(""); | |
| 21 | - expect(sanitizeRuleName("123")).toBe("123"); | |
| 22 | - expect(sanitizeRuleName("rule-with-numbers-123")).toBe( | |
| 23 | - "rule-with-numbers-123", | |
| 24 | - ); | |
| 25 | - }); | |
| 26 | -}); | |
| 27 | - | |
| 28 | -describe("createRuleFilePath", () => { | |
| 29 | - it("should create correct rule file path", () => { | |
| 30 | - const result = createRuleFilePath("/workspace", "My Test Rule"); | |
| 31 | - expect(result).toBe("/workspace/.continue/rules/my-test-rule.md"); | |
| 32 | - }); | |
| 33 | - | |
| 34 | - it("should handle special characters in rule name", () => { | |
| 35 | - const result = createRuleFilePath("/home/user", "Rule with @#$% chars"); | |
| 36 | - expect(result).toBe("/home/user/.continue/rules/rule-with-chars.md"); | |
| 37 | - }); | |
| 38 | - | |
| 39 | - it("should handle edge case rule names", () => { | |
| 40 | - const result = createRuleFilePath("/test", " Multiple Spaces "); | |
| 41 | - expect(result).toBe("/test/.continue/rules/multiple-spaces.md"); | |
| 42 | - }); | |
| 43 | -}); | |
| 44 | - | |
| 45 | -describe("createMarkdownWithFrontmatter", () => { | |
| 46 | - it("should create properly formatted markdown with frontmatter", () => { | |
| 47 | - const frontmatter = { | |
| 48 | - name: "Test Rule", | |
| 49 | - description: "A test rule", | |
| 50 | - globs: "*.ts", | |
| 51 | - }; | |
| 52 | - const markdown = "# Test Rule\n\nThis is a test rule."; | |
| 53 | - | |
| 54 | - const result = createMarkdownWithFrontmatter(frontmatter, markdown); | |
| 55 | - | |
| 56 | - // The exact quote style doesn't matter as long as it parses correctly | |
| 57 | - expect(result).toContain("name: Test Rule"); | |
| 58 | - expect(result).toContain("description: A test rule"); | |
| 59 | - expect(result).toContain("globs:"); | |
| 60 | - expect(result).toContain("*.ts"); | |
| 61 | - expect(result).toContain("---\n\n# Test Rule\n\nThis is a test rule."); | |
| 62 | - }); | |
| 63 | - | |
| 64 | - it("should handle empty frontmatter", () => { | |
| 65 | - const frontmatter = {}; | |
| 66 | - const markdown = "# Simple Rule\n\nJust markdown content."; | |
| 67 | - | |
| 68 | - const result = createMarkdownWithFrontmatter(frontmatter, markdown); | |
| 69 | - | |
| 70 | - const expected = `--- | |
| 71 | -{} | |
| 72 | ---- | |
| 73 | - | |
| 74 | -# Simple Rule | |
| 75 | - | |
| 76 | -Just markdown content.`; | |
| 77 | - | |
| 78 | - expect(result).toBe(expected); | |
| 79 | - }); | |
| 80 | - | |
| 81 | - it("should create content that can be parsed back correctly", () => { | |
| 82 | - const originalFrontmatter = { | |
| 83 | - name: "Roundtrip Test", | |
| 84 | - description: "Testing roundtrip parsing", | |
| 85 | - globs: ["*.js", "*.ts"], | |
| 86 | - alwaysApply: true, | |
| 87 | - }; | |
| 88 | - const originalMarkdown = | |
| 89 | - "# Roundtrip Test\n\nThis should parse back correctly."; | |
| 90 | - | |
| 91 | - const created = createMarkdownWithFrontmatter( | |
| 92 | - originalFrontmatter, | |
| 93 | - originalMarkdown, | |
| 94 | - ); | |
| 95 | - const parsed = parseMarkdownRule(created); | |
| 96 | - | |
| 97 | - expect(parsed.frontmatter).toEqual(originalFrontmatter); | |
| 98 | - expect(parsed.markdown).toBe(originalMarkdown); | |
| 99 | - }); | |
| 100 | -}); | |
| 101 | - | |
| 102 | -describe("createRuleMarkdown", () => { | |
| 103 | - it("should create rule markdown with all options", () => { | |
| 104 | - const result = createRuleMarkdown("Test Rule", "This is the rule content", { | |
| 105 | - description: "Test description", | |
| 106 | - globs: ["*.ts", "*.js"], | |
| 107 | - alwaysApply: true, | |
| 108 | - }); | |
| 109 | - | |
| 110 | - const parsed = parseMarkdownRule(result); | |
| 111 | - | |
| 112 | - expect(parsed.frontmatter.description).toBe("Test description"); | |
| 113 | - expect(parsed.frontmatter.globs).toEqual(["*.ts", "*.js"]); | |
| 114 | - expect(parsed.frontmatter.alwaysApply).toBe(true); | |
| 115 | - expect(parsed.markdown).toBe("# Test Rule\n\nThis is the rule content"); | |
| 116 | - }); | |
| 117 | - | |
| 118 | - it("should create rule markdown with minimal options", () => { | |
| 119 | - const result = createRuleMarkdown("Simple Rule", "Simple content"); | |
| 120 | - | |
| 121 | - const parsed = parseMarkdownRule(result); | |
| 122 | - | |
| 123 | - expect(parsed.frontmatter.description).toBeUndefined(); | |
| 124 | - expect(parsed.frontmatter.globs).toBeUndefined(); | |
| 125 | - expect(parsed.frontmatter.alwaysApply).toBeUndefined(); | |
| 126 | - expect(parsed.markdown).toBe("# Simple Rule\n\nSimple content"); | |
| 127 | - }); | |
| 128 | - | |
| 129 | - it("should handle string globs", () => { | |
| 130 | - const result = createRuleMarkdown("String Glob Rule", "Content", { | |
| 131 | - globs: "*.py", | |
| 132 | - }); | |
| 133 | - | |
| 134 | - const parsed = parseMarkdownRule(result); | |
| 135 | - expect(parsed.frontmatter.globs).toBe("*.py"); | |
| 136 | - }); | |
| 137 | - | |
| 138 | - it("should trim description and globs", () => { | |
| 139 | - const result = createRuleMarkdown("Trim Test", "Content", { | |
| 140 | - description: " spaced description ", | |
| 141 | - globs: " *.ts ", | |
| 142 | - }); | |
| 143 | - | |
| 144 | - const parsed = parseMarkdownRule(result); | |
| 145 | - expect(parsed.frontmatter.description).toBe("spaced description"); | |
| 146 | - expect(parsed.frontmatter.globs).toBe("*.ts"); | |
| 147 | - }); | |
| 148 | - | |
| 149 | - it("should handle alwaysApply false explicitly", () => { | |
| 150 | - const result = createRuleMarkdown("Always Apply False", "Content", { | |
| 151 | - alwaysApply: false, | |
| 152 | - }); | |
| 153 | - | |
| 154 | - const parsed = parseMarkdownRule(result); | |
| 155 | - expect(parsed.frontmatter.alwaysApply).toBe(false); | |
| 156 | - }); | |
| 157 | -}); |
deleted core/config/markdown/createMarkdownRule.ts +0 −75
| @@ -1,75 +0,0 @@ | ||
| 1 | -import * as YAML from "yaml"; | |
| 2 | -import { joinPathsToUri } from "../../util/uri"; | |
| 3 | -import { RuleFrontmatter } from "./parseMarkdownRule"; | |
| 4 | - | |
| 5 | -export const RULE_FILE_EXTENSION = "md"; | |
| 6 | - | |
| 7 | -/** | |
| 8 | - * Sanitizes a rule name for use in filenames (removes special chars, replaces spaces with dashes) | |
| 9 | - */ | |
| 10 | -export function sanitizeRuleName(name: string): string { | |
| 11 | - return name | |
| 12 | - .toLowerCase() | |
| 13 | - .replace(/[^a-z0-9\s-]/g, "") | |
| 14 | - .replace(/\s+/g, "-") | |
| 15 | - .replace(/^-+|-+$/g, ""); // Remove leading/trailing dashes | |
| 16 | -} | |
| 17 | - | |
| 18 | -/** | |
| 19 | - * Creates the file path for a rule in the workspace .continue/rules directory | |
| 20 | - */ | |
| 21 | -export function createRuleFilePath( | |
| 22 | - workspaceDir: string, | |
| 23 | - ruleName: string, | |
| 24 | -): string { | |
| 25 | - const safeRuleName = sanitizeRuleName(ruleName); | |
| 26 | - return joinPathsToUri( | |
| 27 | - workspaceDir, | |
| 28 | - ".continue", | |
| 29 | - "rules", | |
| 30 | - `${safeRuleName}.${RULE_FILE_EXTENSION}`, | |
| 31 | - ); | |
| 32 | -} | |
| 33 | - | |
| 34 | -/** | |
| 35 | - * Creates markdown content with YAML frontmatter in the format expected by parseMarkdownRule | |
| 36 | - */ | |
| 37 | -export function createMarkdownWithFrontmatter( | |
| 38 | - frontmatter: RuleFrontmatter, | |
| 39 | - markdown: string, | |
| 40 | -): string { | |
| 41 | - const frontmatterStr = YAML.stringify(frontmatter).trim(); | |
| 42 | - return `---\n${frontmatterStr}\n---\n\n${markdown}`; | |
| 43 | -} | |
| 44 | - | |
| 45 | -/** | |
| 46 | - * Creates a rule markdown file content from rule components | |
| 47 | - */ | |
| 48 | -export function createRuleMarkdown( | |
| 49 | - name: string, | |
| 50 | - ruleContent: string, | |
| 51 | - options: { | |
| 52 | - description?: string; | |
| 53 | - globs?: string | string[]; | |
| 54 | - alwaysApply?: boolean; | |
| 55 | - } = {}, | |
| 56 | -): string { | |
| 57 | - const frontmatter: RuleFrontmatter = {}; | |
| 58 | - | |
| 59 | - if (options.globs) { | |
| 60 | - frontmatter.globs = | |
| 61 | - typeof options.globs === "string" ? options.globs.trim() : options.globs; | |
| 62 | - } | |
| 63 | - | |
| 64 | - if (options.description) { | |
| 65 | - frontmatter.description = options.description.trim(); | |
| 66 | - } | |
| 67 | - | |
| 68 | - if (options.alwaysApply !== undefined) { | |
| 69 | - frontmatter.alwaysApply = options.alwaysApply; | |
| 70 | - } | |
| 71 | - | |
| 72 | - const markdownBody = `# ${name}\n\n${ruleContent}`; | |
| 73 | - | |
| 74 | - return createMarkdownWithFrontmatter(frontmatter, markdownBody); | |
| 75 | -} |
modified core/config/markdown/index.ts +0 −2
| @@ -1,3 +1,1 @@ | ||
| 1 | -export * from "./createMarkdownRule"; | |
| 2 | 1 | export * from "./loadMarkdownRules"; |
| 3 | -export * from "./parseMarkdownRule"; |
modified core/config/markdown/loadCodebaseRules.ts +6 −4
| @@ -1,9 +1,11 @@ | ||
| 1 | -import { ConfigValidationError } from "@continuedev/config-yaml"; | |
| 1 | +import { | |
| 2 | + ConfigValidationError, | |
| 3 | + markdownToRule, | |
| 4 | +} from "@continuedev/config-yaml"; | |
| 2 | 5 | import { IDE, RuleWithSource } from "../.."; |
| 3 | 6 | import { walkDirs } from "../../indexing/walkDir"; |
| 4 | 7 | import { RULES_MARKDOWN_FILENAME } from "../../llm/rules/constants"; |
| 5 | 8 | import { getUriPathBasename } from "../../util/uri"; |
| 6 | -import { convertMarkdownRuleToContinueRule } from "./parseMarkdownRule"; | |
| 7 | 9 | |
| 8 | 10 | /** |
| 9 | 11 | * Loads rules from rules.md files colocated in the codebase |
| @@ -29,9 +31,9 @@ | ||
| 29 | 31 | for (const filePath of rulesMdFiles) { |
| 30 | 32 | try { |
| 31 | 33 | const content = await ide.readFile(filePath); |
| 32 | - const rule = convertMarkdownRuleToContinueRule(filePath, content); | |
| 34 | + const rule = markdownToRule(content, { uriType: "file", filePath }); | |
| 33 | 35 | |
| 34 | - rules.push(rule); | |
| 36 | + rules.push({ ...rule, source: "rules-block" }); | |
| 35 | 37 | } catch (e) { |
| 36 | 38 | errors.push({ |
| 37 | 39 | fatal: false, |
modified core/config/markdown/loadCodebaseRules.vitest.ts +8 −8
| @@ -1,16 +1,16 @@ | ||
| 1 | +import { markdownToRule } from "@continuedev/config-yaml"; | |
| 1 | 2 | import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | 3 | import { IDE } from "../.."; |
| 3 | 4 | import { walkDirs } from "../../indexing/walkDir"; |
| 4 | 5 | import { loadCodebaseRules } from "./loadCodebaseRules"; |
| 5 | -import { convertMarkdownRuleToContinueRule } from "./parseMarkdownRule"; | |
| 6 | 6 | |
| 7 | 7 | // Mock dependencies |
| 8 | 8 | vi.mock("../../indexing/walkDir", () => ({ |
| 9 | 9 | walkDirs: vi.fn(), |
| 10 | 10 | })); |
| 11 | 11 | |
| 12 | -vi.mock("./parseMarkdownRule", () => ({ | |
| 13 | - convertMarkdownRuleToContinueRule: vi.fn(), | |
| 12 | +vi.mock("@continuedev/config-yaml", () => ({ | |
| 13 | + markdownToRule: vi.fn(), | |
| 14 | 14 | })); |
| 15 | 15 | |
| 16 | 16 | describe("loadCodebaseRules", () => { |
| @@ -82,10 +82,10 @@ | ||
| 82 | 82 | return Promise.resolve(mockRuleContent[path] || ""); |
| 83 | 83 | }); |
| 84 | 84 | |
| 85 | - // Mock convertMarkdownRuleToContinueRule to return converted rules | |
| 86 | - (convertMarkdownRuleToContinueRule as any).mockImplementation( | |
| 87 | - (path: string, content: string) => { | |
| 88 | - return mockConvertedRules[path]; | |
| 85 | + // Mock markdownToRule to return converted rules | |
| 86 | + (markdownToRule as any).mockImplementation( | |
| 87 | + (content: string, options: any) => { | |
| 88 | + return mockConvertedRules[options.filePath]; | |
| 89 | 89 | }, |
| 90 | 90 | ); |
| 91 | 91 | }); |
| @@ -107,7 +107,7 @@ | ||
| 107 | 107 | expect(mockIde.readFile).toHaveBeenCalledWith(".continue/rules.md"); |
| 108 | 108 | |
| 109 | 109 | // Should convert all rules |
| 110 | - expect(convertMarkdownRuleToContinueRule).toHaveBeenCalledTimes(4); | |
| 110 | + expect(markdownToRule).toHaveBeenCalledTimes(4); | |
| 111 | 111 | |
| 112 | 112 | // Should return all rules |
| 113 | 113 | expect(rules).toHaveLength(4); |
modified core/config/markdown/loadMarkdownRules.ts +9 −4
| @@ -1,7 +1,9 @@ | ||
| 1 | -import { ConfigValidationError } from "@continuedev/config-yaml"; | |
| 1 | +import { | |
| 2 | + ConfigValidationError, | |
| 3 | + markdownToRule, | |
| 4 | +} from "@continuedev/config-yaml"; | |
| 2 | 5 | import { IDE, RuleWithSource } from "../.."; |
| 3 | 6 | import { getAllDotContinueDefinitionFiles } from "../loadLocalAssistants"; |
| 4 | -import { convertMarkdownRuleToContinueRule } from "./parseMarkdownRule"; | |
| 5 | 7 | |
| 6 | 8 | /** |
| 7 | 9 | * Loads rules from markdown files in the .continue/rules directory |
| @@ -27,8 +29,11 @@ | ||
| 27 | 29 | // Process each markdown file |
| 28 | 30 | for (const file of mdFiles) { |
| 29 | 31 | try { |
| 30 | - const rule = convertMarkdownRuleToContinueRule(file.path, file.content); | |
| 31 | - rules.push(rule); | |
| 32 | + const rule = markdownToRule(file.content, { | |
| 33 | + uriType: "file", | |
| 34 | + filePath: file.path, | |
| 35 | + }); | |
| 36 | + rules.push({ ...rule, source: "rules-block" }); | |
| 32 | 37 | } catch (e) { |
| 33 | 38 | errors.push({ |
| 34 | 39 | fatal: false, |
deleted core/config/markdown/parseMarkdownRule.test.ts +0 −192
| @@ -1,192 +0,0 @@ | ||
| 1 | -import { | |
| 2 | - convertMarkdownRuleToContinueRule, | |
| 3 | - parseMarkdownRule, | |
| 4 | -} from "./parseMarkdownRule"; | |
| 5 | - | |
| 6 | -describe("parseMarkdownRule", () => { | |
| 7 | - it("should correctly parse markdown with YAML frontmatter", () => { | |
| 8 | - const content = `--- | |
| 9 | -globs: "**/test/**/*.kt" | |
| 10 | ---- | |
| 11 | - | |
| 12 | -# Test Rule | |
| 13 | - | |
| 14 | -This is a test rule.`; | |
| 15 | - | |
| 16 | - const result = parseMarkdownRule(content); | |
| 17 | - expect(result.frontmatter).toEqual({ globs: "**/test/**/*.kt" }); | |
| 18 | - expect(result.markdown).toBe("# Test Rule\n\nThis is a test rule."); | |
| 19 | - }); | |
| 20 | - | |
| 21 | - it("should handle missing frontmatter", () => { | |
| 22 | - const content = `# Test Rule | |
| 23 | - | |
| 24 | -This is a test rule without frontmatter.`; | |
| 25 | - | |
| 26 | - const result = parseMarkdownRule(content); | |
| 27 | - expect(result.frontmatter).toEqual({}); | |
| 28 | - expect(result.markdown).toBe(content); | |
| 29 | - }); | |
| 30 | - | |
| 31 | - it("should handle empty frontmatter", () => { | |
| 32 | - const content = `--- | |
| 33 | ---- | |
| 34 | - | |
| 35 | -# Test Rule | |
| 36 | - | |
| 37 | -This is a test rule with empty frontmatter.`; | |
| 38 | - | |
| 39 | - const result = parseMarkdownRule(content); | |
| 40 | - | |
| 41 | - // Log exact strings for debugging | |
| 42 | - console.log("Actual:", JSON.stringify(result.markdown)); | |
| 43 | - console.log( | |
| 44 | - "Expected:", | |
| 45 | - JSON.stringify( | |
| 46 | - "# Test Rule\n\nThis is a test rule with empty frontmatter.", | |
| 47 | - ), | |
| 48 | - ); | |
| 49 | - | |
| 50 | - expect(result.frontmatter).toEqual({}); | |
| 51 | - expect(result.markdown).toBe( | |
| 52 | - "# Test Rule\n\nThis is a test rule with empty frontmatter.", | |
| 53 | - ); | |
| 54 | - }); | |
| 55 | - | |
| 56 | - it("should handle frontmatter with whitespace", () => { | |
| 57 | - const content = `--- | |
| 58 | -globs: "**/test/**/*.kt" | |
| 59 | ---- | |
| 60 | - | |
| 61 | -# Test Rule | |
| 62 | - | |
| 63 | -This is a test rule.`; | |
| 64 | - | |
| 65 | - const result = parseMarkdownRule(content); | |
| 66 | - expect(result.frontmatter).toEqual({ globs: "**/test/**/*.kt" }); | |
| 67 | - expect(result.markdown).toBe("# Test Rule\n\nThis is a test rule."); | |
| 68 | - }); | |
| 69 | - | |
| 70 | - it("should handle Windows line endings (CRLF)", () => { | |
| 71 | - // Using \r\n for CRLF line endings | |
| 72 | - const content = `---\r | |
| 73 | -globs: "**/test/**/*.kt"\r | |
| 74 | ----\r | |
| 75 | -\r | |
| 76 | -# Test Rule\r | |
| 77 | -\r | |
| 78 | -This is a test rule.`; | |
| 79 | - | |
| 80 | - const result = parseMarkdownRule(content); | |
| 81 | - expect(result.frontmatter).toEqual({ globs: "**/test/**/*.kt" }); | |
| 82 | - // The result should be normalized to \n | |
| 83 | - expect(result.markdown).toBe("# Test Rule\n\nThis is a test rule."); | |
| 84 | - }); | |
| 85 | - | |
| 86 | - it("should handle malformed frontmatter", () => { | |
| 87 | - const content = `--- | |
| 88 | -globs: - "**/test/**/*.kt" | |
| 89 | -invalid: yaml: content | |
| 90 | ---- | |
| 91 | - | |
| 92 | -# Test Rule | |
| 93 | - | |
| 94 | -This is a test rule.`; | |
| 95 | - | |
| 96 | - // Should treat as only markdown when frontmatter is malformed | |
| 97 | - const result = parseMarkdownRule(content); | |
| 98 | - expect(result.frontmatter).toEqual({}); | |
| 99 | - expect(result.markdown).toBe(content); | |
| 100 | - }); | |
| 101 | -}); | |
| 102 | - | |
| 103 | -describe("convertMarkdownRuleToContinueRule", () => { | |
| 104 | - it("should convert markdown with frontmatter to a rule", () => { | |
| 105 | - const content = `--- | |
| 106 | -globs: "**/test/**/*.kt" | |
| 107 | -name: Custom Name | |
| 108 | ---- | |
| 109 | - | |
| 110 | -# Test Rule | |
| 111 | - | |
| 112 | -This is a test rule.`; | |
| 113 | - | |
| 114 | - const result = convertMarkdownRuleToContinueRule( | |
| 115 | - "/path/to/rule.md", | |
| 116 | - content, | |
| 117 | - ); | |
| 118 | - expect(result.rule).toBe("# Test Rule\n\nThis is a test rule."); | |
| 119 | - expect(result.globs).toBe("**/test/**/*.kt"); | |
| 120 | - expect(result.name).toBe("Custom Name"); | |
| 121 | - expect(result.source).toBe("rules-block"); | |
| 122 | - expect(result.ruleFile).toBe("/path/to/rule.md"); | |
| 123 | - }); | |
| 124 | - | |
| 125 | - it("should use the first heading as name if not in frontmatter", () => { | |
| 126 | - const content = `--- | |
| 127 | -globs: "**/test/**/*.kt" | |
| 128 | ---- | |
| 129 | - | |
| 130 | -# Test Rule Title | |
| 131 | - | |
| 132 | -This is a test rule.`; | |
| 133 | - | |
| 134 | - const result = convertMarkdownRuleToContinueRule( | |
| 135 | - "/path/to/rule.md", | |
| 136 | - content, | |
| 137 | - ); | |
| 138 | - expect(result.name).toBe("Test Rule Title"); | |
| 139 | - }); | |
| 140 | - | |
| 141 | - it("should use filename as name if no heading or frontmatter name", () => { | |
| 142 | - const content = `--- | |
| 143 | -globs: "**/test/**/*.kt" | |
| 144 | ---- | |
| 145 | - | |
| 146 | -This is a test rule without a heading.`; | |
| 147 | - | |
| 148 | - const result = convertMarkdownRuleToContinueRule( | |
| 149 | - "/path/to/custom-rule.md", | |
| 150 | - content, | |
| 151 | - ); | |
| 152 | - expect(result.name).toBe("custom-rule"); | |
| 153 | - }); | |
| 154 | - | |
| 155 | - it("should include description from frontmatter", () => { | |
| 156 | - const content = `--- | |
| 157 | -globs: "**/test/**/*.kt" | |
| 158 | -name: Test Rule | |
| 159 | -description: This is a rule description from frontmatter | |
| 160 | ---- | |
| 161 | - | |
| 162 | -# Test Rule | |
| 163 | - | |
| 164 | -This is the content of the rule.`; | |
| 165 | - | |
| 166 | - const result = convertMarkdownRuleToContinueRule( | |
| 167 | - "/path/to/rule.md", | |
| 168 | - content, | |
| 169 | - ); | |
| 170 | - expect(result.description).toBe( | |
| 171 | - "This is a rule description from frontmatter", | |
| 172 | - ); | |
| 173 | - }); | |
| 174 | - | |
| 175 | - it("should include `alwaysApply` from frontmatter", () => { | |
| 176 | - const content = `--- | |
| 177 | -globs: "**/test/**/*.kt" | |
| 178 | -name: Test Rule | |
| 179 | -alwaysApply: false | |
| 180 | ---- | |
| 181 | - | |
| 182 | -# Test Rule | |
| 183 | - | |
| 184 | -This is a rule with alwaysApply explicitly set to false.`; | |
| 185 | - | |
| 186 | - const result = convertMarkdownRuleToContinueRule( | |
| 187 | - "/path/to/rule.md", | |
| 188 | - content, | |
| 189 | - ); | |
| 190 | - expect(result.alwaysApply).toBe(false); | |
| 191 | - }); | |
| 192 | -}); |
deleted core/config/markdown/parseMarkdownRule.ts +0 −77
| @@ -1,77 +0,0 @@ | ||
| 1 | -import { basename } from "path"; | |
| 2 | -import * as YAML from "yaml"; | |
| 3 | -import { RuleWithSource } from "../.."; | |
| 4 | - | |
| 5 | -export interface RuleFrontmatter { | |
| 6 | - globs?: RuleWithSource["globs"]; | |
| 7 | - name?: RuleWithSource["name"]; | |
| 8 | - description?: RuleWithSource["description"]; | |
| 9 | - alwaysApply?: RuleWithSource["alwaysApply"]; | |
| 10 | -} | |
| 11 | - | |
| 12 | -/** | |
| 13 | - * Parses markdown content with YAML frontmatter | |
| 14 | - */ | |
| 15 | -export function parseMarkdownRule(content: string): { | |
| 16 | - frontmatter: RuleFrontmatter; | |
| 17 | - markdown: string; | |
| 18 | -} { | |
| 19 | - // Normalize line endings to \n | |
| 20 | - const normalizedContent = content.replace(/\r\n/g, "\n"); | |
| 21 | - | |
| 22 | - // More reliable frontmatter detection | |
| 23 | - const parts = normalizedContent.split(/^---\s*$/m); | |
| 24 | - | |
| 25 | - // If we have at least 3 parts (before ---, frontmatter, after ---), we have frontmatter | |
| 26 | - if (parts.length >= 3) { | |
| 27 | - const frontmatterStr = parts[1]; | |
| 28 | - // Join the remaining parts back together (in case there are more --- in the markdown) | |
| 29 | - const markdownContent = parts.slice(2).join("---"); | |
| 30 | - | |
| 31 | - try { | |
| 32 | - // Parse YAML frontmatter | |
| 33 | - const frontmatter = YAML.parse(frontmatterStr) || {}; // Handle empty frontmatter | |
| 34 | - return { frontmatter, markdown: markdownContent.trim() }; | |
| 35 | - } catch (e) { | |
| 36 | - // Error parsing frontmatter, treat as markdown only | |
| 37 | - console.warn("Error parsing markdown frontmatter:", e); | |
| 38 | - return { frontmatter: {}, markdown: normalizedContent }; | |
| 39 | - } | |
| 40 | - } | |
| 41 | - | |
| 42 | - // No frontmatter found | |
| 43 | - return { frontmatter: {}, markdown: normalizedContent }; | |
| 44 | -} | |
| 45 | - | |
| 46 | -/** | |
| 47 | - * Converts a markdown file with YAML frontmatter to a RuleWithSource object | |
| 48 | - */ | |
| 49 | -export function convertMarkdownRuleToContinueRule( | |
| 50 | - path: string, | |
| 51 | - content: string, | |
| 52 | -): RuleWithSource { | |
| 53 | - const { frontmatter, markdown } = parseMarkdownRule(content); | |
| 54 | - | |
| 55 | - // Try to extract title from first heading if no name in frontmatter | |
| 56 | - let name = frontmatter.name; | |
| 57 | - if (!name) { | |
| 58 | - // Look for a heading in the markdown | |
| 59 | - const headingMatch = markdown.match(/^#\s+(.+)$/m); | |
| 60 | - if (headingMatch) { | |
| 61 | - name = headingMatch[1].trim(); | |
| 62 | - } else { | |
| 63 | - // Fall back to filename | |
| 64 | - name = basename(path).replace(/\.md$/, ""); | |
| 65 | - } | |
| 66 | - } | |
| 67 | - | |
| 68 | - return { | |
| 69 | - name, | |
| 70 | - rule: markdown, | |
| 71 | - globs: frontmatter.globs, | |
| 72 | - description: frontmatter.description, | |
| 73 | - alwaysApply: frontmatter.alwaysApply, | |
| 74 | - source: "rules-block", | |
| 75 | - ruleFile: path, | |
| 76 | - }; | |
| 77 | -} |
added core/config/markdown/utils.ts +21 −0
| @@ -0,0 +1,21 @@ | ||
| 1 | +import { | |
| 2 | + RULE_FILE_EXTENSION, | |
| 3 | + sanitizeRuleName, | |
| 4 | +} from "@continuedev/config-yaml"; | |
| 5 | +import { joinPathsToUri } from "../../util/uri"; | |
| 6 | + | |
| 7 | +/** | |
| 8 | + * Creates the file path for a rule in the workspace .continue/rules directory | |
| 9 | + */ | |
| 10 | +export function createRuleFilePath( | |
| 11 | + workspaceDir: string, | |
| 12 | + ruleName: string, | |
| 13 | +): string { | |
| 14 | + const safeRuleName = sanitizeRuleName(ruleName); | |
| 15 | + return joinPathsToUri( | |
| 16 | + workspaceDir, | |
| 17 | + ".continue", | |
| 18 | + "rules", | |
| 19 | + `${safeRuleName}.${RULE_FILE_EXTENSION}`, | |
| 20 | + ); | |
| 21 | +} |
added core/config/markdown/utils.vitest.ts +19 −0
| @@ -0,0 +1,19 @@ | ||
| 1 | +import { describe, expect, it } from "vitest"; | |
| 2 | +import { createRuleFilePath } from "./utils"; | |
| 3 | + | |
| 4 | +describe("createRuleFilePath", () => { | |
| 5 | + it("should create correct rule file path", () => { | |
| 6 | + const result = createRuleFilePath("/workspace", "My Test Rule"); | |
| 7 | + expect(result).toBe("/workspace/.continue/rules/my-test-rule.md"); | |
| 8 | + }); | |
| 9 | + | |
| 10 | + it("should handle special characters in rule name", () => { | |
| 11 | + const result = createRuleFilePath("/home/user", "Rule with @#$% chars"); | |
| 12 | + expect(result).toBe("/home/user/.continue/rules/rule-with-chars.md"); | |
| 13 | + }); | |
| 14 | + | |
| 15 | + it("should handle edge case rule names", () => { | |
| 16 | + const result = createRuleFilePath("/test", " Multiple Spaces "); | |
| 17 | + expect(result).toBe("/test/.continue/rules/multiple-spaces.md"); | |
| 18 | + }); | |
| 19 | +}); |
modified core/config/workspace/workspaceBlocks.test.ts +1 −3
| @@ -1,12 +1,10 @@ | ||
| 1 | -import { BlockType } from "@continuedev/config-yaml"; | |
| 1 | +import { BlockType, RULE_FILE_EXTENSION } from "@continuedev/config-yaml"; | |
| 2 | 2 | import { describe, expect, test } from "@jest/globals"; |
| 3 | -import { RULE_FILE_EXTENSION } from "../markdown"; | |
| 4 | 3 | import { findAvailableFilename, getFileContent } from "./workspaceBlocks"; |
| 5 | 4 | |
| 6 | 5 | describe("getFileContent", () => { |
| 7 | 6 | test("returns markdown content for rules block type", () => { |
| 8 | 7 | const result = getFileContent("rules"); |
| 9 | - expect(result).toContain("# New Rule"); | |
| 10 | 8 | expect(result).toContain("Your rule content"); |
| 11 | 9 | expect(result).toContain("A description of your rule"); |
| 12 | 10 | }); |
modified core/config/workspace/workspaceBlocks.ts +6 −2
| @@ -1,8 +1,12 @@ | ||
| 1 | -import { BlockType, ConfigYaml } from "@continuedev/config-yaml"; | |
| 1 | +import { | |
| 2 | + BlockType, | |
| 3 | + ConfigYaml, | |
| 4 | + createRuleMarkdown, | |
| 5 | + RULE_FILE_EXTENSION, | |
| 6 | +} from "@continuedev/config-yaml"; | |
| 2 | 7 | import * as YAML from "yaml"; |
| 3 | 8 | import { IDE } from "../.."; |
| 4 | 9 | import { joinPathsToUri } from "../../util/uri"; |
| 5 | -import { RULE_FILE_EXTENSION, createRuleMarkdown } from "../markdown"; | |
| 6 | 10 | |
| 7 | 11 | const BLOCK_TYPE_CONFIG: Record< |
| 8 | 12 | BlockType, |
modified core/config/yaml/loadYaml.ts +1 −2
| @@ -134,6 +134,7 @@ | ||
| 134 | 134 | rootPath, |
| 135 | 135 | }), |
| 136 | 136 | { |
| 137 | + renderSecrets: true, | |
| 137 | 138 | currentUserSlug: "", |
| 138 | 139 | onPremProxyUrl: null, |
| 139 | 140 | orgScopeId, |
| @@ -142,9 +143,7 @@ | ||
| 142 | 143 | controlPlaneClient, |
| 143 | 144 | ide, |
| 144 | 145 | ), |
| 145 | - renderSecrets: true, | |
| 146 | 146 | injectBlocks: allLocalBlocks, |
| 147 | - asConfigResult: true, | |
| 148 | 147 | }, |
| 149 | 148 | ); |
| 150 | 149 | config = unrollResult.config; |
modified core/tools/implementations/createRuleBlock.test.ts +1 −4
| @@ -1,5 +1,5 @@ | ||
| 1 | +import { parseMarkdownRule } from "@continuedev/config-yaml"; | |
| 1 | 2 | import { jest } from "@jest/globals"; |
| 2 | -import { parseMarkdownRule } from "../../config/markdown"; | |
| 3 | 3 | import { createRuleBlockImpl } from "./createRuleBlock"; |
| 4 | 4 | |
| 5 | 5 | const mockIde = { |
| @@ -40,7 +40,6 @@ | ||
| 40 | 40 | globs: "**/*.{ts,tsx}", |
| 41 | 41 | }); |
| 42 | 42 | |
| 43 | - expect(markdown).toContain("# TypeScript Rule"); | |
| 44 | 43 | expect(markdown).toContain("Use interfaces for object shapes"); |
| 45 | 44 | }); |
| 46 | 45 | |
| @@ -76,7 +75,6 @@ | ||
| 76 | 75 | description: "This is a detailed explanation of the rule", |
| 77 | 76 | }); |
| 78 | 77 | |
| 79 | - expect(markdown).toContain("# Description Test"); | |
| 80 | 78 | expect(markdown).toContain("This is the rule content"); |
| 81 | 79 | }); |
| 82 | 80 | |
| @@ -100,7 +98,6 @@ | ||
| 100 | 98 | globs: "**/*.js", |
| 101 | 99 | }); |
| 102 | 100 | |
| 103 | - expect(markdown).toContain("# Complete Rule"); | |
| 104 | 101 | expect(markdown).toContain("Follow this standard"); |
| 105 | 102 | }); |
| 106 | 103 |
modified core/tools/implementations/createRuleBlock.ts +2 −1
| @@ -1,6 +1,7 @@ | ||
| 1 | +import { createRuleMarkdown } from "@continuedev/config-yaml"; | |
| 1 | 2 | import { ToolImpl } from "."; |
| 2 | 3 | import { RuleWithSource } from "../.."; |
| 3 | -import { createRuleFilePath, createRuleMarkdown } from "../../config/markdown"; | |
| 4 | +import { createRuleFilePath } from "../../config/markdown/utils"; | |
| 4 | 5 | |
| 5 | 6 | export type CreateRuleBlockArgs = Pick< |
| 6 | 7 | Required<RuleWithSource>, |
modified core/tools/implementations/requestRule.ts +3 −7
| @@ -1,5 +1,4 @@ | ||
| 1 | 1 | import { ToolImpl } from "."; |
| 2 | -import { parseMarkdownRule } from "../../config/markdown/parseMarkdownRule"; | |
| 3 | 2 | |
| 4 | 3 | export const requestRuleImpl: ToolImpl = async (args, extras) => { |
| 5 | 4 | // Find the rule by name in the config |
| @@ -11,14 +10,11 @@ | ||
| 11 | 10 | ); |
| 12 | 11 | } |
| 13 | 12 | |
| 14 | - const fileContent = await extras.ide.readFile(rule.ruleFile); | |
| 15 | - const { markdown, frontmatter } = parseMarkdownRule(fileContent); | |
| 16 | - | |
| 17 | 13 | return [ |
| 18 | 14 | { |
| 19 | - name: frontmatter.name ?? "", | |
| 20 | - description: frontmatter.description ?? "", | |
| 21 | - content: markdown, | |
| 15 | + name: rule.name ?? "", | |
| 16 | + description: rule.description ?? "", | |
| 17 | + content: rule.rule, | |
| 22 | 18 | uri: { |
| 23 | 19 | type: "file", |
| 24 | 20 | value: rule.ruleFile, |
modified gui/package-lock.json +0 −0
Not shown: the file is larger than the hub shows, or this page holds as much as it shows. View the file.
modified packages/config-yaml/package-lock.json +2 −2
| @@ -1,12 +1,12 @@ | ||
| 1 | 1 | { |
| 2 | 2 | "name": "@continuedev/config-yaml", |
| 3 | - "version": "1.0.88", | |
| 3 | + "version": "1.0.92", | |
| 4 | 4 | "lockfileVersion": 3, |
| 5 | 5 | "requires": true, |
| 6 | 6 | "packages": { |
| 7 | 7 | "": { |
| 8 | 8 | "name": "@continuedev/config-yaml", |
| 9 | - "version": "1.0.88", | |
| 9 | + "version": "1.0.92", | |
| 10 | 10 | "license": "Apache-2.0", |
| 11 | 11 | "dependencies": { |
| 12 | 12 | "@continuedev/config-types": "^1.0.14", |
modified packages/config-yaml/package.json +8 −1
| @@ -1,10 +1,17 @@ | ||
| 1 | 1 | { |
| 2 | 2 | "name": "@continuedev/config-yaml", |
| 3 | - "version": "1.0.88", | |
| 3 | + "version": "1.0.92", | |
| 4 | 4 | "description": "", |
| 5 | 5 | "main": "dist/index.js", |
| 6 | 6 | "types": "dist/index.d.ts", |
| 7 | 7 | "type": "module", |
| 8 | + "exports": { | |
| 9 | + ".": { | |
| 10 | + "browser": "./dist/browser.js", | |
| 11 | + "node": "./dist/index.js", | |
| 12 | + "default": "./dist/index.js" | |
| 13 | + } | |
| 14 | + }, | |
| 8 | 15 | "bin": { |
| 9 | 16 | "@continuedev/config-yaml": "./dist/cli.js" |
| 10 | 17 | }, |
modified packages/config-yaml/src/__tests__/index.test.ts +16 −12
| @@ -119,7 +119,7 @@ | ||
| 119 | 119 | }, |
| 120 | 120 | ); |
| 121 | 121 | |
| 122 | - expect(unrolledConfig.rules?.[0]).toBeNull(); | |
| 122 | + expect(unrolledConfig.config?.rules?.[0]).toBeNull(); | |
| 123 | 123 | }); |
| 124 | 124 | |
| 125 | 125 | it("should correctly unroll assistant", async () => { |
| @@ -142,19 +142,21 @@ | ||
| 142 | 142 | }, |
| 143 | 143 | ); |
| 144 | 144 | |
| 145 | + const config = unrolledConfig.config; | |
| 146 | + | |
| 145 | 147 | // Test that packages were correctly unrolled and params replaced |
| 146 | - expect(unrolledConfig.models?.length).toBe(4); | |
| 148 | + expect(config?.models?.length).toBe(4); | |
| 147 | 149 | |
| 148 | - const openAiModel = unrolledConfig.models?.[0]!; | |
| 150 | + const openAiModel = config?.models?.[0]!; | |
| 149 | 151 | expect(openAiModel.apiKey).toBe("sk-123"); |
| 150 | 152 | |
| 151 | - const geminiModel = unrolledConfig.models?.[1]!; | |
| 153 | + const geminiModel = config?.models?.[1]!; | |
| 152 | 154 | expect(geminiModel.provider).toBe("continue-proxy"); |
| 153 | 155 | expect(geminiModel.apiKey).toBeUndefined(); |
| 154 | 156 | const geminiSecretLocation = "organization:test-org/GEMINI_API_KEY"; |
| 155 | 157 | expect((geminiModel as any).apiKeyLocation).toBe(geminiSecretLocation); |
| 156 | 158 | |
| 157 | - const anthropicModel = unrolledConfig.models?.[2]!; | |
| 159 | + const anthropicModel = config?.models?.[2]!; | |
| 158 | 160 | expect(anthropicModel.provider).toBe("continue-proxy"); |
| 159 | 161 | expect(anthropicModel.apiKey).toBeUndefined(); |
| 160 | 162 | const anthropicSecretLocation = |
| @@ -163,15 +165,15 @@ | ||
| 163 | 165 | anthropicSecretLocation, |
| 164 | 166 | ); |
| 165 | 167 | |
| 166 | - const proxyOllamaModel = unrolledConfig.models?.[3]!; | |
| 168 | + const proxyOllamaModel = config?.models?.[3]!; | |
| 167 | 169 | expect(proxyOllamaModel.provider).toBe("ollama"); |
| 168 | 170 | expect(proxyOllamaModel.defaultCompletionOptions?.stream).toBe(false); |
| 169 | 171 | |
| 170 | - expect(unrolledConfig.rules?.length).toBe(2); | |
| 171 | - expect(unrolledConfig.docs?.[0]?.startUrl).toBe( | |
| 172 | + expect(config?.rules?.length).toBe(2); | |
| 173 | + expect(config?.docs?.[0]?.startUrl).toBe( | |
| 172 | 174 | "https://docs.python.org/release/3.13.1", |
| 173 | 175 | ); |
| 174 | - expect(unrolledConfig.docs?.[0]?.rootUrl).toBe( | |
| 176 | + expect(config?.docs?.[0]?.rootUrl).toBe( | |
| 175 | 177 | "https://docs.python.org/release/3.13.1", |
| 176 | 178 | ); |
| 177 | 179 | |
| @@ -242,16 +244,18 @@ | ||
| 242 | 244 | }, |
| 243 | 245 | ); |
| 244 | 246 | |
| 247 | + const config = unrolledConfig.config; | |
| 248 | + | |
| 245 | 249 | // The original rules array should have two items |
| 246 | - expect(unrolledConfig.rules?.length).toBe(3); // Now 3 with the injected block | |
| 250 | + expect(config?.rules?.length).toBe(3); // Now 3 with the injected block | |
| 247 | 251 | |
| 248 | 252 | // Check the original doc is still there |
| 249 | - expect(unrolledConfig.docs?.[0]?.startUrl).toBe( | |
| 253 | + expect(config?.docs?.[0]?.startUrl).toBe( | |
| 250 | 254 | "https://docs.python.org/release/3.13.1", |
| 251 | 255 | ); |
| 252 | 256 | |
| 253 | 257 | // Check the injected doc block was added |
| 254 | - expect(unrolledConfig.rules?.[2]).toBe("Be kind"); | |
| 258 | + expect(config?.rules?.[2]).toBe("Be kind"); | |
| 255 | 259 | }); |
| 256 | 260 | |
| 257 | 261 | it.skip("should prioritize org over user / package secrets", () => {}); |
added packages/config-yaml/src/browser.ts +18 −0
| @@ -0,0 +1,18 @@ | ||
| 1 | +// Browser-compatible exports (excludes RegistryClient which uses Node.js APIs) | |
| 2 | +export * from "./converter.js"; | |
| 3 | +export * from "./interfaces/index.js"; | |
| 4 | +export * from "./interfaces/SecretResult.js"; | |
| 5 | +export * from "./interfaces/slugs.js"; | |
| 6 | +export * from "./load/clientRender.js"; | |
| 7 | +export * from "./load/getBlockType.js"; | |
| 8 | +export * from "./load/merge.js"; | |
| 9 | +export * from "./load/proxySecretResolution.js"; | |
| 10 | +export * from "./load/typeGuards.js"; | |
| 11 | +export * from "./load/unroll.js"; | |
| 12 | +export * from "./markdown/index.js"; | |
| 13 | +export * from "./modelName.js"; | |
| 14 | +// Note: registryClient.js is excluded because it uses Node.js fs/path APIs | |
| 15 | +export * from "./schemas/data/index.js"; | |
| 16 | +export * from "./schemas/index.js"; | |
| 17 | +export * from "./schemas/models.js"; | |
| 18 | +export * from "./validation.js"; |
modified packages/config-yaml/src/index.ts +1 −15
| @@ -1,16 +1,2 @@ | ||
| 1 | -export * from "./converter.js"; | |
| 2 | -export * from "./interfaces/index.js"; | |
| 3 | -export * from "./interfaces/SecretResult.js"; | |
| 4 | -export * from "./interfaces/slugs.js"; | |
| 5 | -export * from "./load/clientRender.js"; | |
| 6 | -export * from "./load/getBlockType.js"; | |
| 7 | -export * from "./load/merge.js"; | |
| 8 | -export * from "./load/proxySecretResolution.js"; | |
| 9 | -export * from "./load/typeGuards.js"; | |
| 10 | -export * from "./load/unroll.js"; | |
| 11 | -export * from "./modelName.js"; | |
| 1 | +export * from "./browser.js"; | |
| 12 | 2 | export * from "./registryClient.js"; |
| 13 | -export * from "./schemas/data/index.js"; | |
| 14 | -export * from "./schemas/index.js"; | |
| 15 | -export * from "./schemas/models.js"; | |
| 16 | -export * from "./validation.js"; |
modified packages/config-yaml/src/interfaces/slugs.ts +9 −0
| @@ -25,6 +25,15 @@ | ||
| 25 | 25 | |
| 26 | 26 | export type PackageIdentifier = FullSlugIdentifier | FileIdentifier; |
| 27 | 27 | |
| 28 | +export function packageIdentifierToDisplayName(id: PackageIdentifier): string { | |
| 29 | + switch (id.uriType) { | |
| 30 | + case "file": | |
| 31 | + return id.filePath; | |
| 32 | + case "slug": | |
| 33 | + return id.fullSlug.packageSlug; | |
| 34 | + } | |
| 35 | +} | |
| 36 | + | |
| 28 | 37 | export function encodePackageIdentifier(identifier: PackageIdentifier): string { |
| 29 | 38 | switch (identifier.uriType) { |
| 30 | 39 | case "slug": |
modified packages/config-yaml/src/load/unroll.ts +87 −56
| @@ -9,6 +9,7 @@ | ||
| 9 | 9 | FQSN, |
| 10 | 10 | PackageIdentifier, |
| 11 | 11 | } from "../interfaces/slugs.js"; |
| 12 | +import { markdownToRule } from "../markdown/index.js"; | |
| 12 | 13 | import { |
| 13 | 14 | AssistantUnrolled, |
| 14 | 15 | assistantUnrolledSchema, |
| @@ -195,7 +196,6 @@ | ||
| 195 | 196 | export interface BaseUnrollAssistantOptions { |
| 196 | 197 | renderSecrets: boolean; |
| 197 | 198 | injectBlocks?: PackageIdentifier[]; |
| 198 | - asConfigResult?: true; | |
| 199 | 199 | } |
| 200 | 200 | |
| 201 | 201 | export interface DoNotRenderSecretsUnrollAssistantOptions |
| @@ -217,24 +217,11 @@ | ||
| 217 | 217 | | DoNotRenderSecretsUnrollAssistantOptions |
| 218 | 218 | | RenderSecretsUnrollAssistantOptions; |
| 219 | 219 | |
| 220 | -// Overload to satisfy existing consumers of unrollAssistant. | |
| 221 | -export async function unrollAssistant( | |
| 222 | - id: PackageIdentifier, | |
| 223 | - registry: Registry, | |
| 224 | - options: UnrollAssistantOptions & { asConfigResult: true }, | |
| 225 | -): Promise<ConfigResult<AssistantUnrolled>>; | |
| 226 | - | |
| 227 | -export async function unrollAssistant( | |
| 228 | - id: PackageIdentifier, | |
| 229 | - registry: Registry, | |
| 230 | - options: UnrollAssistantOptions, | |
| 231 | -): Promise<AssistantUnrolled>; | |
| 232 | - | |
| 233 | 220 | export async function unrollAssistant( |
| 234 | 221 | id: PackageIdentifier, |
| 235 | 222 | registry: Registry, |
| 236 | 223 | options: UnrollAssistantOptions, |
| 237 | -): Promise<AssistantUnrolled | ConfigResult<AssistantUnrolled>> { | |
| 224 | +): Promise<ConfigResult<AssistantUnrolled>> { | |
| 238 | 225 | // Request the content from the registry |
| 239 | 226 | const rawContent = await registry.getContent(id); |
| 240 | 227 | |
| @@ -265,24 +252,19 @@ | ||
| 265 | 252 | rawYaml: string, |
| 266 | 253 | registry: Registry, |
| 267 | 254 | options: UnrollAssistantOptions, |
| 268 | -): Promise<AssistantUnrolled | ConfigResult<AssistantUnrolled>> { | |
| 255 | +): Promise<ConfigResult<AssistantUnrolled>> { | |
| 269 | 256 | // Parse string to Zod-validated YAML |
| 270 | 257 | let parsedYaml = parseConfigYaml(rawYaml); |
| 271 | 258 | |
| 272 | 259 | // Unroll blocks and convert their secrets to FQSNs |
| 273 | - const unrolledAssistant = await unrollBlocks( | |
| 274 | - parsedYaml, | |
| 275 | - registry, | |
| 276 | - options.injectBlocks, | |
| 277 | - options.asConfigResult ?? false, | |
| 278 | - ); | |
| 260 | + const { | |
| 261 | + config: unrolledAssistant, | |
| 262 | + configLoadInterrupted, | |
| 263 | + errors, | |
| 264 | + } = await unrollBlocks(parsedYaml, registry, options.injectBlocks); | |
| 279 | 265 | |
| 280 | 266 | // Back to a string so we can fill in template variables |
| 281 | - const rawUnrolledYaml = options.asConfigResult | |
| 282 | - ? YAML.stringify( | |
| 283 | - (unrolledAssistant as ConfigResult<AssistantUnrolled>).config, | |
| 284 | - ) | |
| 285 | - : YAML.stringify(unrolledAssistant); | |
| 267 | + const rawUnrolledYaml = YAML.stringify(unrolledAssistant); | |
| 286 | 268 | |
| 287 | 269 | // Convert all of the template variables to FQSNs |
| 288 | 270 | // Secrets from the block will have the assistant slug prepended to the FQSN |
| @@ -291,7 +273,11 @@ | ||
| 291 | 273 | }); |
| 292 | 274 | |
| 293 | 275 | if (!options.renderSecrets) { |
| 294 | - return parseAssistantUnrolled(templatedYaml); | |
| 276 | + return { | |
| 277 | + config: parseAssistantUnrolled(templatedYaml), | |
| 278 | + errors: [], | |
| 279 | + configLoadInterrupted: false, | |
| 280 | + }; | |
| 295 | 281 | } |
| 296 | 282 | |
| 297 | 283 | // Render secret values/locations for client |
| @@ -312,25 +298,18 @@ | ||
| 312 | 298 | options.onPremProxyUrl, |
| 313 | 299 | ); |
| 314 | 300 | |
| 315 | - if (options.asConfigResult) { | |
| 316 | - return { | |
| 317 | - config: finalConfig, | |
| 318 | - errors: (unrolledAssistant as ConfigResult<AssistantUnrolled>).errors, | |
| 319 | - configLoadInterrupted: ( | |
| 320 | - unrolledAssistant as ConfigResult<AssistantUnrolled> | |
| 321 | - ).configLoadInterrupted, | |
| 322 | - }; | |
| 323 | - } | |
| 324 | - | |
| 325 | - return finalConfig; | |
| 301 | + return { | |
| 302 | + config: finalConfig, | |
| 303 | + errors, | |
| 304 | + configLoadInterrupted, | |
| 305 | + }; | |
| 326 | 306 | } |
| 327 | 307 | |
| 328 | 308 | export async function unrollBlocks( |
| 329 | 309 | assistant: ConfigYaml, |
| 330 | 310 | registry: Registry, |
| 331 | 311 | injectBlocks: PackageIdentifier[] | undefined, |
| 332 | - asConfigError: boolean, | |
| 333 | -): Promise<AssistantUnrolled | ConfigResult<AssistantUnrolled>> { | |
| 312 | +): Promise<ConfigResult<AssistantUnrolled>> { | |
| 334 | 313 | const errors: ConfigValidationError[] = []; |
| 335 | 314 | |
| 336 | 315 | const unrolledAssistant: AssistantUnrolled = { |
| @@ -432,7 +411,10 @@ | ||
| 432 | 411 | for (const injectBlock of injectBlocks ?? []) { |
| 433 | 412 | try { |
| 434 | 413 | const blockConfigYaml = await registry.getContent(injectBlock); |
| 435 | - const parsedBlock = parseConfigYaml(blockConfigYaml); | |
| 414 | + const parsedBlock = parseMarkdownRuleOrConfigYaml( | |
| 415 | + blockConfigYaml, | |
| 416 | + injectBlock, | |
| 417 | + ); | |
| 436 | 418 | const blockType = getBlockType(parsedBlock); |
| 437 | 419 | const resolvedBlock = await resolveBlock( |
| 438 | 420 | injectBlock, |
| @@ -466,20 +448,16 @@ | ||
| 466 | 448 | } |
| 467 | 449 | } |
| 468 | 450 | |
| 469 | - if (asConfigError) { | |
| 470 | - const configResult: ConfigResult<AssistantUnrolled> = { | |
| 471 | - config: undefined, | |
| 472 | - errors: undefined, | |
| 473 | - configLoadInterrupted: false, | |
| 474 | - }; | |
| 475 | - configResult.config = unrolledAssistant; | |
| 476 | - if (errors.length > 0) { | |
| 477 | - configResult.errors = errors; | |
| 478 | - } | |
| 479 | - return configResult; | |
| 451 | + const configResult: ConfigResult<AssistantUnrolled> = { | |
| 452 | + config: undefined, | |
| 453 | + errors: undefined, | |
| 454 | + configLoadInterrupted: false, | |
| 455 | + }; | |
| 456 | + configResult.config = unrolledAssistant; | |
| 457 | + if (errors.length > 0) { | |
| 458 | + configResult.errors = errors; | |
| 480 | 459 | } |
| 481 | - | |
| 482 | - return unrolledAssistant; | |
| 460 | + return configResult; | |
| 483 | 461 | } |
| 484 | 462 | |
| 485 | 463 | export async function resolveBlock( |
| @@ -503,7 +481,60 @@ | ||
| 503 | 481 | secrets: extractFQSNMap(rawYaml, [id]), |
| 504 | 482 | }); |
| 505 | 483 | |
| 506 | - const parsedYaml = parseBlock(templatedYaml); | |
| 484 | + return parseMarkdownRuleOrAssistantUnrolled(templatedYaml, id); | |
| 485 | +} | |
| 486 | + | |
| 487 | +function parseMarkdownRuleOrAssistantUnrolled( | |
| 488 | + content: string, | |
| 489 | + id: PackageIdentifier, | |
| 490 | +): AssistantUnrolled { | |
| 491 | + // Try to parse as YAML first, then as markdown rule if that fails | |
| 492 | + let parsedYaml: AssistantUnrolled; | |
| 493 | + try { | |
| 494 | + parsedYaml = parseBlock(content); | |
| 495 | + } catch (yamlError) { | |
| 496 | + // If YAML parsing fails, try parsing as markdown rule | |
| 497 | + try { | |
| 498 | + const rule = markdownToRule(content, id); | |
| 499 | + // Convert the rule object to the expected format | |
| 500 | + parsedYaml = { | |
| 501 | + name: rule.name, | |
| 502 | + version: "1.0.0", | |
| 503 | + rules: [rule], | |
| 504 | + }; | |
| 505 | + } catch (markdownError) { | |
| 506 | + // If both fail, throw the original YAML error | |
| 507 | + throw yamlError; | |
| 508 | + } | |
| 509 | + } | |
| 510 | + | |
| 511 | + return parsedYaml; | |
| 512 | +} | |
| 513 | + | |
| 514 | +function parseMarkdownRuleOrConfigYaml( | |
| 515 | + content: string, | |
| 516 | + id: PackageIdentifier, | |
| 517 | +): ConfigYaml { | |
| 518 | + // Try to parse as YAML first, then as markdown rule if that fails | |
| 519 | + let parsedYaml: ConfigYaml; | |
| 520 | + try { | |
| 521 | + parsedYaml = parseConfigYaml(content); | |
| 522 | + } catch (yamlError) { | |
| 523 | + // If YAML parsing fails, try parsing as markdown rule | |
| 524 | + try { | |
| 525 | + const rule = markdownToRule(content, id); | |
| 526 | + // Convert the rule object to the expected format | |
| 527 | + parsedYaml = { | |
| 528 | + name: rule.name, | |
| 529 | + version: "1.0.0", | |
| 530 | + rules: [rule], | |
| 531 | + }; | |
| 532 | + } catch (markdownError) { | |
| 533 | + // If both fail, throw the original YAML error | |
| 534 | + throw yamlError; | |
| 535 | + } | |
| 536 | + } | |
| 537 | + | |
| 507 | 538 | return parsedYaml; |
| 508 | 539 | } |
| 509 | 540 |
added packages/config-yaml/src/markdown/createMarkdownRule.test.ts +149 −0
| @@ -0,0 +1,149 @@ | ||
| 1 | +import { PackageIdentifier } from "../browser.js"; | |
| 2 | +import { | |
| 3 | + createMarkdownWithFrontmatter, | |
| 4 | + createRuleMarkdown, | |
| 5 | + sanitizeRuleName, | |
| 6 | +} from "./createMarkdownRule.js"; | |
| 7 | +import { markdownToRule } from "./markdownToRule.js"; | |
| 8 | + | |
| 9 | +// Mock package identifier for testing | |
| 10 | +const mockPackageId: PackageIdentifier = { | |
| 11 | + uriType: "file", | |
| 12 | + filePath: "/path/to/file", | |
| 13 | +}; | |
| 14 | + | |
| 15 | +describe("sanitizeRuleName", () => { | |
| 16 | + it("should sanitize rule names for filenames", () => { | |
| 17 | + expect(sanitizeRuleName("My Test Rule")).toBe("my-test-rule"); | |
| 18 | + expect(sanitizeRuleName("Rule with @#$% chars")).toBe("rule-with-chars"); | |
| 19 | + expect(sanitizeRuleName("Multiple spaces")).toBe("multiple-spaces"); | |
| 20 | + expect(sanitizeRuleName("UPPERCASE-rule")).toBe("uppercase-rule"); | |
| 21 | + expect(sanitizeRuleName("already-sanitized")).toBe("already-sanitized"); | |
| 22 | + }); | |
| 23 | + | |
| 24 | + it("should handle empty and edge case inputs", () => { | |
| 25 | + expect(sanitizeRuleName("")).toBe(""); | |
| 26 | + expect(sanitizeRuleName(" ")).toBe(""); | |
| 27 | + expect(sanitizeRuleName("123")).toBe("123"); | |
| 28 | + expect(sanitizeRuleName("rule-with-numbers-123")).toBe( | |
| 29 | + "rule-with-numbers-123", | |
| 30 | + ); | |
| 31 | + }); | |
| 32 | +}); | |
| 33 | + | |
| 34 | +describe("createMarkdownWithFrontmatter", () => { | |
| 35 | + it("should create properly formatted markdown with frontmatter", () => { | |
| 36 | + const frontmatter = { | |
| 37 | + name: "Test Rule", | |
| 38 | + description: "A test rule", | |
| 39 | + globs: "*.ts", | |
| 40 | + }; | |
| 41 | + const markdown = "# Test Rule\n\nThis is a test rule."; | |
| 42 | + | |
| 43 | + const result = createMarkdownWithFrontmatter(frontmatter, markdown); | |
| 44 | + | |
| 45 | + // The exact quote style doesn't matter as long as it parses correctly | |
| 46 | + expect(result).toContain("name: Test Rule"); | |
| 47 | + expect(result).toContain("description: A test rule"); | |
| 48 | + expect(result).toContain("globs:"); | |
| 49 | + expect(result).toContain("*.ts"); | |
| 50 | + expect(result).toContain("---\n\n# Test Rule\n\nThis is a test rule."); | |
| 51 | + }); | |
| 52 | + | |
| 53 | + it("should handle empty frontmatter", () => { | |
| 54 | + const frontmatter = {}; | |
| 55 | + const markdown = "# Simple Rule\n\nJust markdown content."; | |
| 56 | + | |
| 57 | + const result = createMarkdownWithFrontmatter(frontmatter, markdown); | |
| 58 | + | |
| 59 | + const expected = `--- | |
| 60 | +{} | |
| 61 | +--- | |
| 62 | + | |
| 63 | +# Simple Rule | |
| 64 | + | |
| 65 | +Just markdown content.`; | |
| 66 | + | |
| 67 | + expect(result).toBe(expected); | |
| 68 | + }); | |
| 69 | + | |
| 70 | + it("should create content that can be parsed back correctly", () => { | |
| 71 | + const originalFrontmatter = { | |
| 72 | + name: "Roundtrip Test", | |
| 73 | + description: "Testing roundtrip parsing", | |
| 74 | + globs: ["*.js", "*.ts"], | |
| 75 | + alwaysApply: true, | |
| 76 | + }; | |
| 77 | + const originalMarkdown = | |
| 78 | + "# Roundtrip Test\n\nThis should parse back correctly."; | |
| 79 | + | |
| 80 | + const created = createMarkdownWithFrontmatter( | |
| 81 | + originalFrontmatter, | |
| 82 | + originalMarkdown, | |
| 83 | + ); | |
| 84 | + const parsed = markdownToRule(created, mockPackageId); | |
| 85 | + | |
| 86 | + expect(parsed.name).toBe(originalFrontmatter.name); | |
| 87 | + expect(parsed.description).toBe(originalFrontmatter.description); | |
| 88 | + expect(parsed.globs).toEqual(originalFrontmatter.globs); | |
| 89 | + expect(parsed.alwaysApply).toBe(originalFrontmatter.alwaysApply); | |
| 90 | + expect(parsed.rule).toBe(originalMarkdown); | |
| 91 | + }); | |
| 92 | +}); | |
| 93 | + | |
| 94 | +describe("createRuleMarkdown", () => { | |
| 95 | + it("should create rule markdown with all options", () => { | |
| 96 | + const result = createRuleMarkdown("Test Rule", "This is the rule content", { | |
| 97 | + description: "Test description", | |
| 98 | + globs: ["*.ts", "*.js"], | |
| 99 | + alwaysApply: true, | |
| 100 | + }); | |
| 101 | + | |
| 102 | + const parsed = markdownToRule(result, mockPackageId); | |
| 103 | + | |
| 104 | + expect(parsed.description).toBe("Test description"); | |
| 105 | + expect(parsed.globs).toEqual(["*.ts", "*.js"]); | |
| 106 | + expect(parsed.alwaysApply).toBe(true); | |
| 107 | + expect(parsed.rule).toBe("This is the rule content"); | |
| 108 | + }); | |
| 109 | + | |
| 110 | + it("should create rule markdown with minimal options", () => { | |
| 111 | + const result = createRuleMarkdown("Simple Rule", "Simple content"); | |
| 112 | + | |
| 113 | + const parsed = markdownToRule(result, mockPackageId); | |
| 114 | + | |
| 115 | + expect(parsed.description).toBeUndefined(); | |
| 116 | + expect(parsed.globs).toBeUndefined(); | |
| 117 | + expect(parsed.alwaysApply).toBeUndefined(); | |
| 118 | + expect(parsed.rule).toBe("Simple content"); | |
| 119 | + }); | |
| 120 | + | |
| 121 | + it("should handle string globs", () => { | |
| 122 | + const result = createRuleMarkdown("String Glob Rule", "Content", { | |
| 123 | + globs: "*.py", | |
| 124 | + }); | |
| 125 | + | |
| 126 | + const parsed = markdownToRule(result, mockPackageId); | |
| 127 | + expect(parsed.globs).toBe("*.py"); | |
| 128 | + }); | |
| 129 | + | |
| 130 | + it("should trim description and globs", () => { | |
| 131 | + const result = createRuleMarkdown("Trim Test", "Content", { | |
| 132 | + description: " spaced description ", | |
| 133 | + globs: " *.ts ", | |
| 134 | + }); | |
| 135 | + | |
| 136 | + const parsed = markdownToRule(result, mockPackageId); | |
| 137 | + expect(parsed.description).toBe("spaced description"); | |
| 138 | + expect(parsed.globs).toBe("*.ts"); | |
| 139 | + }); | |
| 140 | + | |
| 141 | + it("should handle alwaysApply false explicitly", () => { | |
| 142 | + const result = createRuleMarkdown("Always Apply False", "Content", { | |
| 143 | + alwaysApply: false, | |
| 144 | + }); | |
| 145 | + | |
| 146 | + const parsed = markdownToRule(result, mockPackageId); | |
| 147 | + expect(parsed.alwaysApply).toBe(false); | |
| 148 | + }); | |
| 149 | +}); |
added packages/config-yaml/src/markdown/createMarkdownRule.ts +56 −0
| @@ -0,0 +1,56 @@ | ||
| 1 | +import * as YAML from "yaml"; | |
| 2 | +import { RuleFrontmatter } from "./markdownToRule.js"; | |
| 3 | + | |
| 4 | +export const RULE_FILE_EXTENSION = "md"; | |
| 5 | + | |
| 6 | +/** | |
| 7 | + * Sanitizes a rule name for use in filenames (removes special chars, replaces spaces with dashes) | |
| 8 | + */ | |
| 9 | +export 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 | + */ | |
| 20 | +export 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 | + */ | |
| 31 | +export 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 | +} |
added packages/config-yaml/src/markdown/index.ts +2 −0
| @@ -0,0 +1,2 @@ | ||
| 1 | +export * from "./createMarkdownRule.js"; | |
| 2 | +export * from "./markdownToRule.js"; |
added packages/config-yaml/src/markdown/markdownToRule.test.ts +173 −0
| @@ -0,0 +1,173 @@ | ||
| 1 | +import { PackageIdentifier } from "../browser.js"; | |
| 2 | +import { markdownToRule } from "./markdownToRule.js"; | |
| 3 | + | |
| 4 | +describe("markdownToRule", () => { | |
| 5 | + // Use a mock PackageIdentifier for testing | |
| 6 | + const mockId: PackageIdentifier = { | |
| 7 | + uriType: "file", | |
| 8 | + filePath: "/path/to/file", | |
| 9 | + }; | |
| 10 | + | |
| 11 | + it("should convert markdown with frontmatter to a rule", () => { | |
| 12 | + const content = `--- | |
| 13 | +globs: "**/test/**/*.kt" | |
| 14 | +name: Custom Name | |
| 15 | +--- | |
| 16 | + | |
| 17 | +# Test Rule | |
| 18 | + | |
| 19 | +This is a test rule.`; | |
| 20 | + | |
| 21 | + const result = markdownToRule(content, mockId); | |
| 22 | + expect(result.rule).toBe("# Test Rule\n\nThis is a test rule."); | |
| 23 | + expect(result.globs).toBe("**/test/**/*.kt"); | |
| 24 | + expect(result.name).toBe("Custom Name"); | |
| 25 | + }); | |
| 26 | + | |
| 27 | + it("should correctly parse markdown with YAML frontmatter", () => { | |
| 28 | + const content = `--- | |
| 29 | +globs: "**/test/**/*.kt" | |
| 30 | +--- | |
| 31 | + | |
| 32 | +# Test Rule | |
| 33 | + | |
| 34 | +This is a test rule.`; | |
| 35 | + | |
| 36 | + const result = markdownToRule(content, mockId); | |
| 37 | + expect(result.globs).toBe("**/test/**/*.kt"); | |
| 38 | + expect(result.rule).toBe("# Test Rule\n\nThis is a test rule."); | |
| 39 | + expect(result.name).toBe("/path/to/file"); // Should use packageIdentifierToDisplayName result | |
| 40 | + }); | |
| 41 | + | |
| 42 | + it("should handle missing frontmatter", () => { | |
| 43 | + const content = `# Test Rule | |
| 44 | + | |
| 45 | +This is a test rule without frontmatter.`; | |
| 46 | + | |
| 47 | + const result = markdownToRule(content, mockId); | |
| 48 | + expect(result.globs).toBeUndefined(); | |
| 49 | + expect(result.rule).toBe(content); | |
| 50 | + expect(result.name).toBe("/path/to/file"); // Should use packageIdentifierToDisplayName result | |
| 51 | + }); | |
| 52 | + | |
| 53 | + it("should handle empty frontmatter", () => { | |
| 54 | + const content = `--- | |
| 55 | +--- | |
| 56 | + | |
| 57 | +# Test Rule | |
| 58 | + | |
| 59 | +This is a test rule with empty frontmatter.`; | |
| 60 | + | |
| 61 | + const result = markdownToRule(content, mockId); | |
| 62 | + expect(result.globs).toBeUndefined(); | |
| 63 | + expect(result.rule).toBe( | |
| 64 | + "# Test Rule\n\nThis is a test rule with empty frontmatter.", | |
| 65 | + ); | |
| 66 | + expect(result.name).toBe("/path/to/file"); // Should use packageIdentifierToDisplayName result | |
| 67 | + }); | |
| 68 | + | |
| 69 | + it("should handle frontmatter with whitespace", () => { | |
| 70 | + const content = `--- | |
| 71 | +globs: "**/test/**/*.kt" | |
| 72 | +--- | |
| 73 | + | |
| 74 | +# Test Rule | |
| 75 | + | |
| 76 | +This is a test rule.`; | |
| 77 | + | |
| 78 | + const result = markdownToRule(content, mockId); | |
| 79 | + expect(result.globs).toBe("**/test/**/*.kt"); | |
| 80 | + expect(result.rule).toBe("# Test Rule\n\nThis is a test rule."); | |
| 81 | + expect(result.name).toBe("/path/to/file"); // Should use packageIdentifierToDisplayName result | |
| 82 | + }); | |
| 83 | + | |
| 84 | + it("should handle Windows line endings (CRLF)", () => { | |
| 85 | + // Using \r\n for CRLF line endings | |
| 86 | + const content = `---\r | |
| 87 | +globs: "**/test/**/*.kt"\r | |
| 88 | +---\r | |
| 89 | +\r | |
| 90 | +# Test Rule\r | |
| 91 | +\r | |
| 92 | +This is a test rule.`; | |
| 93 | + | |
| 94 | + const result = markdownToRule(content, mockId); | |
| 95 | + expect(result.globs).toBe("**/test/**/*.kt"); | |
| 96 | + // The result should be normalized to \n | |
| 97 | + expect(result.rule).toBe("# Test Rule\n\nThis is a test rule."); | |
| 98 | + expect(result.name).toBe("/path/to/file"); // Should use packageIdentifierToDisplayName result | |
| 99 | + }); | |
| 100 | + | |
| 101 | + it("should handle malformed frontmatter", () => { | |
| 102 | + const content = `--- | |
| 103 | +globs: - "**/test/**/*.kt" | |
| 104 | +invalid: yaml: content | |
| 105 | +--- | |
| 106 | + | |
| 107 | +# Test Rule | |
| 108 | + | |
| 109 | +This is a test rule.`; | |
| 110 | + | |
| 111 | + // Should treat as only markdown when frontmatter is malformed | |
| 112 | + const result = markdownToRule(content, mockId); | |
| 113 | + expect(result.globs).toBeUndefined(); | |
| 114 | + expect(result.rule).toBe(content); | |
| 115 | + expect(result.name).toBe("/path/to/file"); // Should use packageIdentifierToDisplayName result | |
| 116 | + }); | |
| 117 | + | |
| 118 | + it("should use packageIdentifierToDisplayName if no heading or frontmatter name", () => { | |
| 119 | + const content = `--- | |
| 120 | +globs: "**/test/**/*.kt" | |
| 121 | +--- | |
| 122 | + | |
| 123 | +# Test Rule Title | |
| 124 | + | |
| 125 | +This is a test rule.`; | |
| 126 | + | |
| 127 | + const result = markdownToRule(content, mockId); | |
| 128 | + expect(result.name).toBe("/path/to/file"); // Should use packageIdentifierToDisplayName result | |
| 129 | + }); | |
| 130 | + | |
| 131 | + it("should use packageIdentifierToDisplayName if no heading or frontmatter name (no heading)", () => { | |
| 132 | + const content = `--- | |
| 133 | +globs: "**/test/**/*.kt" | |
| 134 | +--- | |
| 135 | + | |
| 136 | +This is a test rule without a heading.`; | |
| 137 | + | |
| 138 | + const result = markdownToRule(content, mockId); | |
| 139 | + expect(result.name).toBe("/path/to/file"); // Should use packageIdentifierToDisplayName result | |
| 140 | + }); | |
| 141 | + | |
| 142 | + it("should include description from frontmatter", () => { | |
| 143 | + const content = `--- | |
| 144 | +globs: "**/test/**/*.kt" | |
| 145 | +name: Test Rule | |
| 146 | +description: This is a rule description from frontmatter | |
| 147 | +--- | |
| 148 | + | |
| 149 | +# Test Rule | |
| 150 | + | |
| 151 | +This is the content of the rule.`; | |
| 152 | + | |
| 153 | + const result = markdownToRule(content, mockId); | |
| 154 | + expect(result.description).toBe( | |
| 155 | + "This is a rule description from frontmatter", | |
| 156 | + ); | |
| 157 | + }); | |
| 158 | + | |
| 159 | + it("should include alwaysApply from frontmatter", () => { | |
| 160 | + const content = `--- | |
| 161 | +globs: "**/test/**/*.kt" | |
| 162 | +name: Test Rule | |
| 163 | +alwaysApply: false | |
| 164 | +--- | |
| 165 | + | |
| 166 | +# Test Rule | |
| 167 | + | |
| 168 | +This is a rule with alwaysApply explicitly set to false.`; | |
| 169 | + | |
| 170 | + const result = markdownToRule(content, mockId); | |
| 171 | + expect(result.alwaysApply).toBe(false); | |
| 172 | + }); | |
| 173 | +}); |
added packages/config-yaml/src/markdown/markdownToRule.ts +62 −0
| @@ -0,0 +1,62 @@ | ||
| 1 | +import * as YAML from "yaml"; | |
| 2 | +import { | |
| 3 | + PackageIdentifier, | |
| 4 | + packageIdentifierToDisplayName, | |
| 5 | +} from "../browser.js"; | |
| 6 | +import { RuleObject } from "../schemas/index.js"; | |
| 7 | + | |
| 8 | +export interface RuleFrontmatter { | |
| 9 | + globs?: RuleObject["globs"]; | |
| 10 | + name?: RuleObject["name"]; | |
| 11 | + description?: RuleObject["description"]; | |
| 12 | + alwaysApply?: RuleObject["alwaysApply"]; | |
| 13 | +} | |
| 14 | + | |
| 15 | +/** | |
| 16 | + * Parses markdown content with YAML frontmatter | |
| 17 | + */ | |
| 18 | +export function parseMarkdownRule(content: string): { | |
| 19 | + frontmatter: RuleFrontmatter; | |
| 20 | + markdown: string; | |
| 21 | +} { | |
| 22 | + // Normalize line endings to \n | |
| 23 | + const normalizedContent = content.replace(/\r\n/g, "\n"); | |
| 24 | + | |
| 25 | + // More reliable frontmatter detection | |
| 26 | + const parts = normalizedContent.split(/^---\s*$/m); | |
| 27 | + | |
| 28 | + // If we have at least 3 parts (before ---, frontmatter, after ---), we have frontmatter | |
| 29 | + if (parts.length >= 3) { | |
| 30 | + const frontmatterStr = parts[1]; | |
| 31 | + // Join the remaining parts back together (in case there are more --- in the markdown) | |
| 32 | + const markdownContent = parts.slice(2).join("---"); | |
| 33 | + | |
| 34 | + try { | |
| 35 | + // Parse YAML frontmatter | |
| 36 | + const frontmatter = YAML.parse(frontmatterStr) || {}; // Handle empty frontmatter | |
| 37 | + return { frontmatter, markdown: markdownContent.trim() }; | |
| 38 | + } catch (e) { | |
| 39 | + // Error parsing frontmatter, treat as markdown only | |
| 40 | + console.warn("Error parsing markdown frontmatter:", e); | |
| 41 | + return { frontmatter: {}, markdown: normalizedContent }; | |
| 42 | + } | |
| 43 | + } | |
| 44 | + | |
| 45 | + // No frontmatter found | |
| 46 | + return { frontmatter: {}, markdown: normalizedContent }; | |
| 47 | +} | |
| 48 | + | |
| 49 | +export function markdownToRule( | |
| 50 | + rule: string, | |
| 51 | + id: PackageIdentifier, | |
| 52 | +): RuleObject { | |
| 53 | + const { frontmatter, markdown } = parseMarkdownRule(rule); | |
| 54 | + | |
| 55 | + return { | |
| 56 | + name: frontmatter.name ?? packageIdentifierToDisplayName(id), | |
| 57 | + rule: markdown, | |
| 58 | + globs: frontmatter.globs, | |
| 59 | + description: frontmatter.description, | |
| 60 | + alwaysApply: frontmatter.alwaysApply, | |
| 61 | + }; | |
| 62 | +} |
modified packages/config-yaml/src/schemas/index.ts +16 −0
| @@ -39,6 +39,7 @@ | ||
| 39 | 39 | }); |
| 40 | 40 | |
| 41 | 41 | export type DocsConfig = z.infer<typeof docSchema>; |
| 42 | + | |
| 42 | 43 | const ruleObjectSchema = z.object({ |
| 43 | 44 | name: z.string(), |
| 44 | 45 | rule: z.string(), |
| @@ -48,8 +49,23 @@ | ||
| 48 | 49 | }); |
| 49 | 50 | const ruleSchema = z.union([z.string(), ruleObjectSchema]); |
| 50 | 51 | |
| 52 | +/** | |
| 53 | + * A schema for rules.json files | |
| 54 | + */ | |
| 55 | +export const rulesJsonSchema = z.object({ | |
| 56 | + name: z.string(), | |
| 57 | + version: z.string(), | |
| 58 | + author: z.string().optional(), | |
| 59 | + license: z.string().optional(), | |
| 60 | + rules: z.record(z.string(), z.string()).optional(), | |
| 61 | +}); | |
| 62 | + | |
| 51 | 63 | export type Rule = z.infer<typeof ruleSchema>; |
| 52 | 64 | export type RuleObject = z.infer<typeof ruleObjectSchema>; |
| 65 | +/** | |
| 66 | + * A schema for rules.json files | |
| 67 | + */ | |
| 68 | +export type RulesJson = z.infer<typeof rulesJsonSchema>; | |
| 53 | 69 | |
| 54 | 70 | const defaultUsesSchema = z.string(); |
| 55 | 71 |