Elffuss BraidHub

kikocis/continue public

9875eaa07e45 · 3.3 KiBRawPermalink
1import path from "path";
2import * as YAML from "yaml";
3import {
4 PackageIdentifier,
5 packageIdentifierToDisplayName,
6} from "../browser.js";
7import { RuleObject } from "../schemas/index.js";
8
9export interface RuleFrontmatter {
10 globs?: RuleObject["globs"];
11 regex?: RuleObject["regex"];
12 name?: RuleObject["name"];
13 description?: RuleObject["description"];
14 alwaysApply?: RuleObject["alwaysApply"];
15}
16
17/**
18 * Parses markdown content with YAML frontmatter
19 */
20export function parseMarkdownRule(content: string): {
21 frontmatter: RuleFrontmatter;
22 markdown: string;
23} {
24 // Normalize line endings to \n
25 const normalizedContent = content.replace(/\r\n/g, "\n");
26
27 // More reliable frontmatter detection
28 const parts = normalizedContent.split(/^---\s*$/m);
29
30 // If we have at least 3 parts (before ---, frontmatter, after ---), we have frontmatter
31 if (parts.length >= 3) {
32 const frontmatterStr = parts[1];
33 // Join the remaining parts back together (in case there are more --- in the markdown)
34 const markdownContent = parts.slice(2).join("---");
35
36 try {
37 // Parse YAML frontmatter
38 const frontmatter = YAML.parse(frontmatterStr) || {}; // Handle empty frontmatter
39 return { frontmatter, markdown: markdownContent.trim() };
40 } catch (e) {
41 // Error parsing frontmatter, treat as markdown only
42 console.warn("Error parsing markdown frontmatter:", e);
43 return { frontmatter: {}, markdown: normalizedContent };
44 }
45 }
46
47 // No frontmatter found
48 return { frontmatter: {}, markdown: normalizedContent };
49}
50
51export function getRuleName(
52 frontmatter: RuleFrontmatter,
53 id: PackageIdentifier,
54): string {
55 if (frontmatter.name) {
56 return frontmatter.name;
57 }
58
59 const displayName = packageIdentifierToDisplayName(id);
60
61 // If it's a file identifier, extract the last two parts of the file path
62 if (id.uriType === "file") {
63 // Handle both forward slashes and backslashes, get the last two segments
64 const segments = displayName.split(/[/\\]/);
65 const lastTwoParts = segments.slice(-2);
66 return lastTwoParts.join("/");
67 }
68
69 // Otherwise return the display name as-is (for slug identifiers)
70 return displayName;
71}
72
73function getGlobPattern(
74 globs: RuleFrontmatter["globs"],
75 id: PackageIdentifier,
76) {
77 if (id.uriType !== "file") {
78 return globs;
79 }
80 let dir = path.dirname(id.fileUri);
81
82 // TODO: This is likely causing bugs for Windows users.
83 // We can't access `core/util/uri.ts` from this package though.
84 if (dir.includes(".continue")) {
85 return globs;
86 }
87 if (!dir.endsWith("/")) {
88 dir = dir.concat("/");
89 }
90 const prependDirAndApplyGlobstar = (glob: string) => {
91 if (glob.startsWith("**")) {
92 return dir.concat(glob);
93 }
94 return dir.concat("**/", glob);
95 };
96 if (!globs) {
97 return dir.concat("**/*");
98 }
99 if (Array.isArray(globs)) {
100 return globs.map(prependDirAndApplyGlobstar);
101 }
102 return prependDirAndApplyGlobstar(globs);
103}
104
105export function markdownToRule(
106 rule: string,
107 id: PackageIdentifier,
108): RuleObject {
109 const { frontmatter, markdown } = parseMarkdownRule(rule);
110
111 return {
112 name: getRuleName(frontmatter, id),
113 rule: markdown,
114 globs: getGlobPattern(frontmatter.globs, id),
115 regex: frontmatter.regex,
116 description: frontmatter.description,
117 alwaysApply: frontmatter.alwaysApply,
118 sourceFile: id.uriType === "file" ? id.fileUri : undefined,
119 };
120}