only export markdownToRule

This commit is contained in:
Patrick Erichsen 2025-06-16 11:23:15 -07:00
parent e210afa336
commit 0b3d388c00
6 changed files with 124 additions and 122 deletions

View file

@ -1,4 +1,3 @@
import { parseMarkdownRule } from "@continuedev/config-yaml";
import { ToolImpl } from ".";
export const requestRuleImpl: ToolImpl = async (args, extras) => {
@ -11,14 +10,11 @@ export const requestRuleImpl: ToolImpl = async (args, extras) => {
);
}
const fileContent = await extras.ide.readFile(rule.ruleFile);
const { markdown, frontmatter } = parseMarkdownRule(fileContent);
return [
{
name: frontmatter.name ?? "",
description: frontmatter.description ?? "",
content: markdown,
name: rule.name ?? "",
description: rule.description ?? "",
content: rule.rule,
uri: {
type: "file",
value: rule.ruleFile,

View file

@ -3,7 +3,7 @@ import {
createRuleMarkdown,
sanitizeRuleName,
} from "./createMarkdownRule.js";
import { parseMarkdownRule } from "./parseMarkdownRule.js";
import { markdownToRule } from "./markdownToRule.js";
describe("sanitizeRuleName", () => {
it("should sanitize rule names for filenames", () => {
@ -74,10 +74,13 @@ Just markdown content.`;
originalFrontmatter,
originalMarkdown,
);
const parsed = parseMarkdownRule(created);
const parsed = markdownToRule(created);
expect(parsed.frontmatter).toEqual(originalFrontmatter);
expect(parsed.markdown).toBe(originalMarkdown);
expect(parsed.name).toBe(originalFrontmatter.name);
expect(parsed.description).toBe(originalFrontmatter.description);
expect(parsed.globs).toEqual(originalFrontmatter.globs);
expect(parsed.alwaysApply).toBe(originalFrontmatter.alwaysApply);
expect(parsed.rule).toBe(originalMarkdown);
});
});
@ -89,23 +92,23 @@ describe("createRuleMarkdown", () => {
alwaysApply: true,
});
const parsed = parseMarkdownRule(result);
const parsed = markdownToRule(result);
expect(parsed.frontmatter.description).toBe("Test description");
expect(parsed.frontmatter.globs).toEqual(["*.ts", "*.js"]);
expect(parsed.frontmatter.alwaysApply).toBe(true);
expect(parsed.markdown).toBe("This is the rule content");
expect(parsed.description).toBe("Test description");
expect(parsed.globs).toEqual(["*.ts", "*.js"]);
expect(parsed.alwaysApply).toBe(true);
expect(parsed.rule).toBe("This is the rule content");
});
it("should create rule markdown with minimal options", () => {
const result = createRuleMarkdown("Simple Rule", "Simple content");
const parsed = parseMarkdownRule(result);
const parsed = markdownToRule(result);
expect(parsed.frontmatter.description).toBeUndefined();
expect(parsed.frontmatter.globs).toBeUndefined();
expect(parsed.frontmatter.alwaysApply).toBeUndefined();
expect(parsed.markdown).toBe("Simple content");
expect(parsed.description).toBeUndefined();
expect(parsed.globs).toBeUndefined();
expect(parsed.alwaysApply).toBeUndefined();
expect(parsed.rule).toBe("Simple content");
});
it("should handle string globs", () => {
@ -113,8 +116,8 @@ describe("createRuleMarkdown", () => {
globs: "*.py",
});
const parsed = parseMarkdownRule(result);
expect(parsed.frontmatter.globs).toBe("*.py");
const parsed = markdownToRule(result);
expect(parsed.globs).toBe("*.py");
});
it("should trim description and globs", () => {
@ -123,9 +126,9 @@ describe("createRuleMarkdown", () => {
globs: " *.ts ",
});
const parsed = parseMarkdownRule(result);
expect(parsed.frontmatter.description).toBe("spaced description");
expect(parsed.frontmatter.globs).toBe("*.ts");
const parsed = markdownToRule(result);
expect(parsed.description).toBe("spaced description");
expect(parsed.globs).toBe("*.ts");
});
it("should handle alwaysApply false explicitly", () => {
@ -133,7 +136,7 @@ describe("createRuleMarkdown", () => {
alwaysApply: false,
});
const parsed = parseMarkdownRule(result);
expect(parsed.frontmatter.alwaysApply).toBe(false);
const parsed = markdownToRule(result);
expect(parsed.alwaysApply).toBe(false);
});
});

View file

@ -1,5 +1,5 @@
import * as YAML from "yaml";
import { RuleFrontmatter } from "./parseMarkdownRule.js";
import { RuleFrontmatter } from "./markdownToRule.js";
export const RULE_FILE_EXTENSION = "md";

View file

@ -1,2 +1,2 @@
export * from "./createMarkdownRule.js";
export * from "./parseMarkdownRule.js";
export * from "./markdownToRule.js";

View file

@ -1,92 +1,4 @@
import { markdownToRule, parseMarkdownRule } from "./parseMarkdownRule.js";
describe("parseMarkdownRule", () => {
it("should correctly parse markdown with YAML frontmatter", () => {
const content = `---
globs: "**/test/**/*.kt"
---
# Test Rule
This is a test rule.`;
const result = parseMarkdownRule(content);
expect(result.frontmatter).toEqual({ globs: "**/test/**/*.kt" });
expect(result.markdown).toBe("# Test Rule\n\nThis is a test rule.");
});
it("should handle missing frontmatter", () => {
const content = `# Test Rule
This is a test rule without frontmatter.`;
const result = parseMarkdownRule(content);
expect(result.frontmatter).toEqual({});
expect(result.markdown).toBe(content);
});
it("should handle empty frontmatter", () => {
const content = `---
---
# Test Rule
This is a test rule with empty frontmatter.`;
const result = parseMarkdownRule(content);
expect(result.frontmatter).toEqual({});
expect(result.markdown).toBe(
"# Test Rule\n\nThis is a test rule with empty frontmatter.",
);
});
it("should handle frontmatter with whitespace", () => {
const content = `---
globs: "**/test/**/*.kt"
---
# Test Rule
This is a test rule.`;
const result = parseMarkdownRule(content);
expect(result.frontmatter).toEqual({ globs: "**/test/**/*.kt" });
expect(result.markdown).toBe("# Test Rule\n\nThis is a test rule.");
});
it("should handle Windows line endings (CRLF)", () => {
// Using \r\n for CRLF line endings
const content = `---\r
globs: "**/test/**/*.kt"\r
---\r
\r
# Test Rule\r
\r
This is a test rule.`;
const result = parseMarkdownRule(content);
expect(result.frontmatter).toEqual({ globs: "**/test/**/*.kt" });
// The result should be normalized to \n
expect(result.markdown).toBe("# Test Rule\n\nThis is a test rule.");
});
it("should handle malformed frontmatter", () => {
const content = `---
globs: - "**/test/**/*.kt"
invalid: yaml: content
---
# Test Rule
This is a test rule.`;
// Should treat as only markdown when frontmatter is malformed
const result = parseMarkdownRule(content);
expect(result.frontmatter).toEqual({});
expect(result.markdown).toBe(content);
});
});
import { markdownToRule } from "./markdownToRule.js";
describe("markdownToRule", () => {
it("should convert markdown with frontmatter to a rule", () => {
@ -105,6 +17,97 @@ This is a test rule.`;
expect(result.name).toBe("Custom Name");
});
it("should correctly parse markdown with YAML frontmatter", () => {
const content = `---
globs: "**/test/**/*.kt"
---
# Test Rule
This is a test rule.`;
const result = markdownToRule(content);
expect(result.globs).toBe("**/test/**/*.kt");
expect(result.rule).toBe("# Test Rule\n\nThis is a test rule.");
expect(result.name).toBe("Rule"); // Default name when not specified
});
it("should handle missing frontmatter", () => {
const content = `# Test Rule
This is a test rule without frontmatter.`;
const result = markdownToRule(content);
expect(result.globs).toBeUndefined();
expect(result.rule).toBe(content);
expect(result.name).toBe("Rule");
});
it("should handle empty frontmatter", () => {
const content = `---
---
# Test Rule
This is a test rule with empty frontmatter.`;
const result = markdownToRule(content);
expect(result.globs).toBeUndefined();
expect(result.rule).toBe(
"# Test Rule\n\nThis is a test rule with empty frontmatter.",
);
expect(result.name).toBe("Rule");
});
it("should handle frontmatter with whitespace", () => {
const content = `---
globs: "**/test/**/*.kt"
---
# Test Rule
This is a test rule.`;
const result = markdownToRule(content);
expect(result.globs).toBe("**/test/**/*.kt");
expect(result.rule).toBe("# Test Rule\n\nThis is a test rule.");
expect(result.name).toBe("Rule");
});
it("should handle Windows line endings (CRLF)", () => {
// Using \r\n for CRLF line endings
const content = `---\r
globs: "**/test/**/*.kt"\r
---\r
\r
# Test Rule\r
\r
This is a test rule.`;
const result = markdownToRule(content);
expect(result.globs).toBe("**/test/**/*.kt");
// The result should be normalized to \n
expect(result.rule).toBe("# Test Rule\n\nThis is a test rule.");
expect(result.name).toBe("Rule");
});
it("should handle malformed frontmatter", () => {
const content = `---
globs: - "**/test/**/*.kt"
invalid: yaml: content
---
# Test Rule
This is a test rule.`;
// Should treat as only markdown when frontmatter is malformed
const result = markdownToRule(content);
expect(result.globs).toBeUndefined();
expect(result.rule).toBe(content);
expect(result.name).toBe("Rule");
});
it("should use default name if no heading or frontmatter name", () => {
const content = `---
globs: "**/test/**/*.kt"
@ -118,7 +121,7 @@ This is a test rule.`;
expect(result.name).toBe("Rule"); // markdownToRule defaults to "Rule" when no name in frontmatter
});
it("should use default name if no heading or frontmatter name", () => {
it("should use default name if no heading or frontmatter name (no heading)", () => {
const content = `---
globs: "**/test/**/*.kt"
---
@ -146,7 +149,7 @@ This is the content of the rule.`;
);
});
it("should include `alwaysApply` from frontmatter", () => {
it("should include alwaysApply from frontmatter", () => {
const content = `---
globs: "**/test/**/*.kt"
name: Test Rule

View file

@ -11,7 +11,7 @@ export interface RuleFrontmatter {
/**
* Parses markdown content with YAML frontmatter
*/
export function parseMarkdownRule(content: string): {
function parseMarkdownRule(content: string): {
frontmatter: RuleFrontmatter;
markdown: string;
} {