DEV Community

Cover image for Extract AI Rules from Markdown Files with mq
Takahiro Sato
Takahiro Sato

Posted on Edited on

Extract AI Rules from Markdown Files with mq

Managing separate AI rule files for different systems? mq can generate them from one master file.

demo

The Problem

Each AI system needs its own rule file. Managing multiple separate Markdown files gets messy quickly.

Simple Solution

Keep all AI rules in one master file, then generate individual files:

$ mq 'nodes | sections(2) | filter(fn(section): let head = first(section) | contains(head, "Claude Rules");) | first() | map(decrease_header_level) | to_markdown_string()' master-ai-rules.md > claude-rules.md
Enter fullscreen mode Exit fullscreen mode

Generate Multiple AI Rule Files

# Extract different AI system rules from one master file
$ mq 'nodes | sections(2) | filter(fn(s): contains(first(s), "GPT Rules")) | first() | map(decrease_header_level) | to_markdown_string()' master-ai-rules.md > gpt-rules.md
$ mq 'nodes | sections(2) | filter(fn(s): contains(first(s), "Gemini Rules")) | first() | map(decrease_header_level) | to_markdown_string()' master-ai-rules.md > gemini-rules.md
$ mq 'nodes | sections(2) | filter(fn(s): contains(first(s), "Claude Rules")) | first() | map(decrease_header_level) | to_markdown_string()' master-ai-rules.md > claude-rules.md
Enter fullscreen mode Exit fullscreen mode

Why This Approach Works

  • Single source of truth: All AI rules in one master file
  • Easy maintenance: Update rules in one place
  • Automatic generation: Create individual files with one script
  • Version control friendly: Track changes in one file
  • No vendor lock-in: Generated files are plain Markdown, work anywhere

Try It

$ brew install harehare/tap/mq
$ mq 'nodes | sections(2) | filter(fn(s): contains(first(s), "Claude Rules")) | first() | map(decrease_header_level) | to_markdown_string()' master-ai-rules.md > claude-rules.md
Enter fullscreen mode Exit fullscreen mode

Now you can manage all AI rules in one file and generate individual rule files as needed!

Support


Top comments (1)

Collapse
 
jonghunyu profile image
Jonghun Yu •

Keeping one master rules file is a useful pattern. For the narrower case where you want the body of one uniquely named section:

texio section master-ai-rules.md "Claude Rules" --body-only > claude-rules.md
Enter fullscreen mode Exit fullscreen mode

Disclosure: I built Texio. It extracts sections by heading and fails if the heading is missing or ambiguous. Unlike the mq example here, this command preserves nested heading levels; mq's rebasing and broader queries are useful when you need those transformations. Texio also supports previewing a replacement of one section in the master file before writing it.