| 1 | import { |
| 2 | ConfigValidationError, |
| 3 | markdownToRule, |
| 4 | } from "@continuedev/config-yaml"; |
| 5 | import { IDE, RuleWithSource } from "../.."; |
| 6 | import { getAllDotContinueDefinitionFiles } from "../loadLocalAssistants"; |
| 7 | |
| 8 | /** |
| 9 | * Loads rules from markdown files in the .continue/rules directory |
| 10 | */ |
| 11 | export async function loadMarkdownRules(ide: IDE): Promise<{ |
| 12 | rules: RuleWithSource[]; |
| 13 | errors: ConfigValidationError[]; |
| 14 | }> { |
| 15 | const errors: ConfigValidationError[] = []; |
| 16 | const rules: RuleWithSource[] = []; |
| 17 | |
| 18 | try { |
| 19 | // Get all .md files from .continue/rules |
| 20 | const markdownFiles = await getAllDotContinueDefinitionFiles( |
| 21 | ide, |
| 22 | { includeGlobal: true, includeWorkspace: true, fileExtType: "markdown" }, |
| 23 | "rules", |
| 24 | ); |
| 25 | |
| 26 | // Filter to just .md files |
| 27 | const mdFiles = markdownFiles.filter((file) => file.path.endsWith(".md")); |
| 28 | |
| 29 | // Process each markdown file |
| 30 | for (const file of mdFiles) { |
| 31 | try { |
| 32 | const rule = markdownToRule(file.content, { |
| 33 | uriType: "file", |
| 34 | filePath: file.path, |
| 35 | }); |
| 36 | rules.push({ ...rule, source: "rules-block", ruleFile: file.path }); |
| 37 | } catch (e) { |
| 38 | errors.push({ |
| 39 | fatal: false, |
| 40 | message: `Failed to parse markdown rule file ${file.path}: ${e instanceof Error ? e.message : e}`, |
| 41 | }); |
| 42 | } |
| 43 | } |
| 44 | } catch (e) { |
| 45 | errors.push({ |
| 46 | fatal: false, |
| 47 | message: `Error loading markdown rule files: ${e instanceof Error ? e.message : e}`, |
| 48 | }); |
| 49 | } |
| 50 | |
| 51 | return { rules, errors }; |
| 52 | } |