agentleFS
weiyvshanweiyvshan/claude-skills/skills/markdown-outline/SKILL.md

markdown-outline

Skill0 starsChanged 10 months agoUnlicenseView on GitHub

用于读取、提取、分析和编辑Markdown文件大纲结构的高级技能。支持精确的行号追踪和标题索引表,实现准确的增删改查操作。提供多种输出格式(outline、JSON、detailed),支持大纲对比与批量修改。适用于技术文档分析、内容重构、目录生成和大型Markdown文档导航等场景。

  • Installs packages
  • In this repositoryOne other file it gives its agents.

The file

Files
SKILL.md

Markdown 大纲技能

功能概述

这个技能提供了一套完整的工具来处理Markdown文件的大纲结构:

功能说明
精确行号追踪每个标题都有完整的行号映射,支持同名标题的精确识别
标题索引表建立双向映射:标题文本→行号列表,行号→标题对象
多种输出格式outline(带编号)、json、detailed(含完整索引)
精确增删改查通过行号定位同名标题,支持插入、删除、替换、调整层级
大纲对比对比两个时期的大纲,自动识别差异并批量应用修改
上下文预览查看标题周围的上下文内容,便于安全编辑
自动备份编辑时自动创建 .bak 备份文件

代码结构

scripts/
├── markdown_outline.py    # 主入口,命令行工具
└── modules/
    ├── __init__.py        # 模块初始化
    ├── heading.py         # 数据结构(Heading, HeadingIndex)
    ├── extractor.py       # 标题提取器
    ├── formatter.py       # 输出格式化器
    ├── editor.py          # 标题编辑器
    └── diff.py            # 大纲对比工具

模块职责

模块职责关键类/函数
heading.py数据结构定义Heading, HeadingIndex
extractor.py标题提取HeadingExtractor.extract()
formatter.py输出格式化OutlineFormatter
editor.py编辑操作HeadingEditor
diff.py对比修改diff_files, apply_diff

核心数据结构

Heading(标题)

{
  "text": "标题文本",
  "level": 2,
  "line_number": 10,
  "end_line": 11,
  "type": "atx"
}

HeadingIndex(标题索引表)

from modules.heading import Heading, HeadingIndex

index = HeadingIndex()
index.build(headings)

# 查找同名标题的所有行号
index.text_to_lines["小结"]  # [9, 41]

# 通过行号查找标题
index.line_to_heading[9]  # Heading object

何时使用此技能

在以下场景中使用此技能:

  • 分析大型Markdown文档的结构
  • 为技术文档生成目录
  • 重构文档的标题层级
  • 导航复杂文档的章节结构
  • 将Markdown大纲转换为其他格式
  • 精确编辑同名标题中的某一个
  • 对比两个版本的大纲差异并批量修改

基本用法

1. 提取大纲(带行号)

cd scripts/
python markdown_outline.py document.md

# 输出示例:
# 1. 第一章 概述 (行1)
#   1.1. 简介 (行5)
#   1.2. 小结 (行9)
# 2. 第二章 快速入门 (行13)

2. 输出格式选择

参数格式示例
默认outline1.1. 标题 (行5)
--format jsonJSON{"text":"标题","level":1,...}
--format detailed详细JSON包含完整索引表
--format simple简单列表- 标题 (行5)
# 详细格式(含完整索引表)
python markdown_outline.py --format detailed -o detail.json document.md

3. 批量处理

# 处理多个文件
python markdown_outline.py *.md

# 保存到文件(避免控制台编码问题)
python markdown_outline.py -o outline.txt document.md

精确编辑功能

核心原理

标题文本 → 索引表查找 → 精确匹配行号 → 安全编辑

1. 替换标题文本

# 精确替换第10行的"简介"(推荐)
python markdown_outline.py --replace-text "简介" "概述" --line 10 document.md

2. 调整标题层级

# 精确调整第15行的"小结"为三级标题
python markdown_outline.py --adjust-level "小结" 3 --line 15 document.md

3. 插入新标题

# 在第5行后插入2级新标题
python markdown_outline.py --insert "新章节" 2 5 document.md

4. 删除标题

# 删除第20行的"临时标题"
python markdown_outline.py --delete "临时标题" --line 20 document.md

5. 查看上下文

# 查看第10行标题的周围内容
python markdown_outline.py --show-context 10 document.md

大纲对比与批量修改(核心功能)

对比流程

┌──────────────────────────────────────────────────────────┐
│  步骤1: 提取修改前的大纲                                   │
│  $ python scripts/markdown_outline.py document.md \      │
│         --format json -o before.json                     │
│                                                          │
│  步骤2: 使用其他工具修改文档(手动/AI/其他)               │
│                                                          │
│  步骤3: 提取修改后的大纲                                   │
│  $ python scripts/markdown_outline.py document.md \      │
│         --format json -o after.json                      │
│                                                          │
│  步骤4: 对比两个大纲,查看差异                             │
│  $ python scripts/markdown_outline.py \                 │
│         --diff before.json after.json                    │
│                                                          │
│  步骤5: 批量应用修改                                       │
│  $ python scripts/markdown_outline.py \                 │
│         --diff before.json after.json \                  │
│         --apply --file document.md                       │
└──────────────────────────────────────────────────────────┘

Edit 类型说明

type说明字段
replace文本修改old_text, new_text, line_number
adjust_level层级调整text, old_level, new_level, line_number
insert插入标题text, level, after_line
delete删除标题text, line_number

在Python代码中使用模块

# 方式1: 临时添加到路径(推荐,仅当前进程有效,不污染环境)
import sys
sys.path.insert(0, 'scripts/modules')

from heading import Heading, HeadingIndex
from extractor import HeadingExtractor
from formatter import OutlineFormatter
from editor import HeadingEditor
from diff import diff_files, apply_diff, load_outline

# 提取标题
extractor = HeadingExtractor()
headings = extractor.extract(content)

# 构建索引
index = HeadingIndex()
index.build(headings)

# 格式化输出
formatter = OutlineFormatter()
print(formatter.format_outline(headings))

# 编辑操作
editor = HeadingEditor()
modified = editor.replace_text(content, "旧", "新", line_number=10)

# 大纲对比
diff = diff_files("before.json", "after.json")
result = apply_diff(content, diff)

关于路径污染的说明

不会污染环境! sys.path.insert() 的特点:

特性说明
作用范围仅当前Python进程
持续时间脚本执行期间
进程退出路径修改自动消失
全局影响无
# 示例
sys.path.insert(0, 'scripts/modules')  # ✓ 只在当前脚本中有效
# 脚本结束后,不会在系统留下任何痕迹
# 不会影响其他Python程序或环境

编码支持

脚本自动检测并处理多种文件编码:

  • UTF-8(默认)
  • GBK/GB2312(中文Windows)
  • CP936(简体中文代码页)

使用场景

场景1:手动精确修改

1. python script.md → 查看大纲,确定行号
2. python --replace-text "旧" "新" --line N → 精确修改
3. python script.md → 验证结果

场景2:AI辅助修改后批量应用

1. python --format json -o before.json → 备份原始大纲
2. AI/其他工具修改文档内容
3. python --format json -o after.json → 提取新大纲
4. python --diff before.json after.json → 查看差异
5. python --diff ... --apply --file → 批量应用修改

最佳实践

  • 编辑前备份: 脚本默认创建 .bak 备份文件
  • 同名标题用行号: 使用 --line N 参数避免误改
  • 先用详细格式分析: --format detailed 查看完整索引
  • 先预览后编辑: 使用 --show-context 确认目标
  • 批量修改用diff: 对比大纲差异,自动识别所有修改
  • 代码中使用模块: 导入 modules/ 目录实现程序化操作

故障排除

问题解决方案
控制台乱码使用 -o 保存到文件
找不到标题用详细格式查看索引表
同名标题混淆必须使用 --line 参数
修改后格式错误检查 .bak 备份文件恢复
模块导入错误脚本会自动处理路径,无需手动设置

关于环境安全

sys.path.insert() 不会污染环境!

# 脚本内(安全)
sys.path.insert(0, 'scripts/modules')  # 只在当前进程有效

# 对比(不会污染):
# - pip install        → 全局污染
# - 修改 site-packages → 全局污染
# - sys.path.insert()  → 仅当前进程,进程退出后消失

参考资料

详细的Markdown规范说明,请参阅 references/markdown_specs.md

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.