File size: 1,535 Bytes
7bedc79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import type { Rule } from "../core/types.js";
import type { ConfigParser } from "./base.js";
import { nextRuleId, classifyRule, extractRuleBlocks } from "./base.js";

/**
 * Cursor config parser.
 * Reads: .cursor/rules/*.mdc (frontmatter + markdown body)
 * Also supports .cursorrules (legacy plain text, one rule per line)
 */
export const cursorParser: ConfigParser = {
  agent: "cursor",
  filePatterns: [".cursor/rules/*.mdc", ".cursorrules"],

  parse(content: string, filePath: string): Rule[] {
    let body = content;

    // .mdc files have YAML frontmatter
    if (filePath.endsWith(".mdc")) {
      const fmMatch = content.match(/^---\n([\s\S]*?)\n---\n([\s\S]*)$/);
      if (fmMatch) {
        body = fmMatch[2];
      }
    }

    // .cursorrules: plain text, each line is its own rule
    if (filePath.endsWith(".cursorrules")) {
      return body
        .split("\n")
        .map((l) => l.trim())
        .filter((l) => l.length > 5)
        .map((line) => ({
          id: nextRuleId(),
          category: classifyRule(line),
          content: line,
          scope: "project" as const,
          source: { agent: "cursor" as const, file: filePath },
          status: "active" as const,
        }));
    }

    const blocks = extractRuleBlocks(body);
    return blocks.map((block) => ({
      id: nextRuleId(),
      category: classifyRule(block),
      content: block,
      scope: "project" as const,
      source: { agent: "cursor" as const, file: filePath },
      status: "active" as const,
    }));
  },
};