Skill: shaoxiang616/claude-md-auto-discovery (version 0.1.0)

# CLAUDE.md 自动发现技能 自动发现并加载项目根目录的 CLAUDE.md 文件，参考 Claude Code 实现。 ## 功能 1. 自动查找 ./CLAUDE.md 和 ./ CLAUDE.md 2. 支持 @include 指令 3. 最大 40000 字符限制 4. 逆序加载（后面的文件优先级更高） ## 触发条件 - 当用户发送消息时自动执行 - 无需用户手动调用 ## 实现 - 使用 fs.readFileWithinRoot 读取文件 - 遍历当前工作目录向上查找 - 解析 @include 指令 - 注入到系统上下文

path: claude-md.py
content: # CLAUDE.md Auto-Discovery Skill

自动发现并加载项目根目录的 CLAUDE.md 文件。

## 功能
- 自动查找 `./CLAUDE.md` 和 `./ CLAUDE.md`
- 支持 @include 指令
- 最大 40000 字符限制
- 逆序加载（后面的文件优先级更高）

## 调用方式
```bash
claude-md-discover [--cwd /path/to/dir]
```

返回发现的 CLAUDE.md 内容，用于注入到系统上下文。
"""
import os
import re
from pathlib import Path

MAX_CHARS = 40000

def discover_claude_md_files(cwd=None):
    """从当前目录向上查找所有 CLAUDE.md 文件"""
    if cwd is None:
        cwd = os.getcwd()
    
    paths_to_check = []
    current = Path(cwd).resolve()
    
    # 向上查找，最多 4 层
    for _ in range(4):
        paths_to_check.append(current / "CLAUDE.md")
        paths_to_check.append(current / " CLAUDE.md")  # 注意有空格
        if current.parent == current:
            break
        current = current.parent
    
    # 去重并保留存在的文件
    found = []
    seen = set()
    for p in paths_to_check:
        if str(p) not in seen and p.exists():
            seen.add(str(p))
            found.append(p)
    
    return found

def load_claude_md_content(cwd=None):
    """加载所有 CLAUDE.md 文件内容"""
    files = discover_claude_md_files(cwd)
    if not files:
        return None
    
    # 逆序加载（后面的优先级更高）
    contents = []
    for f in reversed(files):
        try:
            content = f.read_text(encoding='utf-8')
            contents.append(f"# {f.name}\n\n{content}")
        except Exception:
            pass
    
    if not contents:
        return None
    
    combined = "\n\n---\n\n".join(contents)
    
    # 截断到最大字符数
    if len(combined) > MAX_CHARS:
        combined = combined[:MAX_CHARS] + "\n\n... (truncated)"
    
    return combined

if __name__ == "__main__":
    import sys
    result = load_claude_md_content(sys.argv[1] if len(sys.argv) > 1 else None)
    if result:
        print(result)
    else:
        print("No CLAUDE.md files found")

path: discover.mjs
content: #!/usr/bin/env node
/**
 * CLAUDE.md Auto-Discovery
 * 
 * 自动发现并加载项目根目录的 CLAUDE.md 文件
 * 参考 Claude Code 实现
 * 
 * 使用方式: node claude-md-discover.js [--cwd /path/to/dir]
 */

import { readFileSync, existsSync } from 'fs';
import { resolve, dirname } from 'path';
import { fileURLToPath } from 'url';

const MAX_CHARS = 40000;

function discoverClaudeMdFiles(cwd = process.cwd()) {
  const paths = [];
  let current = resolve(cwd);
  
  // 向上查找 4 层
  for (let i = 0; i < 4; i++) {
    paths.push(resolve(current, 'CLAUDE.md'));
    paths.push(resolve(current, ' CLAUDE.md')); // 空格前缀
    const parent = dirname(current);
    if (parent === current) break;
    current = parent;
  }
  
  // 去重
  const seen = new Set();
  const found = [];
  for (const p of paths) {
    if (!seen.has(p) && existsSync(p)) {
      seen.add(p);
      found.push(p);
    }
  }
  return found;
}

function loadContent(files) {
  if (!files?.length) return null;
  
  const contents = [];
  for (const f of files.reverse()) { // 逆序，后面的优先级高
    try {
      contents.push(`# ${f}\n\n${readFileSync(f, 'utf-8')}`);
    } catch (e) {
      // skip
    }
  }
  
  if (!contents.length) return null;
  
  let combined = contents.join('\n\n---\n\n');
  if (combined.length > MAX_CHARS) {
    combined = combined.slice(0, MAX_CHARS) + '\n\n... (truncated)';
  }
  return combined;
}

// 执行
const cwd = process.argv[2] === '--cwd' ? process.argv[3] : process.cwd();
const files = discoverClaudeMdFiles(cwd);
const content = loadContent(files);

if (content) {
  console.log(content);
} else {
  console.log('No CLAUDE.md files found');
}

Security scan verdicts — ClawScan: suspicious | static analysis: clean | VirusTotal: clean | SkillSpector: suspicious (HIGH)