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