canghe-format-markdown
Formats plain text or markdown files with frontmatter, titles, summaries, headings, bold, lists, and code blocks. Use when user asks to "format markdown", "beautify article", "add formatting", or improve article layout. Outputs to {filename}-formatted.md.
1实现原理 · 为什么它能做到
分工明确:内容分析与格式化由 Claude(LLM)完成 Step 1-6,脚本只在最后一步做排版修复(Step 7)。
Claude performs content analysis and formatting (Steps 1-6), then runs the script for typography fixes (Step 7).
先检测内容类型:命中 frontmatter/标题/bold/列表/代码块/引用任一即判为 markdown,全不命中才当纯文本。
| Has `---` YAML frontmatter | Markdown |
已检测到 markdown 时给用户三选一:优化格式、保留原样、只做排版修复(后者直接改原文件)。
1. Optimize formatting (Recommended)
自动补 frontmatter:把 title/slug/summary/coverImage 等元字段写入 YAML,summary 由模型生成为 100-150 字摘要。
| `summary` | Generate engaging summary (100-150 characters) |
标题去重:若正文首行是 H1 则抽到 frontmatter 的 title 并从正文删除,避免正文再出现 H1 重复。
2. If first line is H1 → extract to frontmatter `title`, remove H1 from body
输出命名固定为 {original-filename}-formatted.md,且覆盖前会把已存在的同名产物按时间戳改名备份。
mv "{filename}-formatted.md" "{filename}-formatted.backup-$(date +%Y%m%d-%H%M%S).md"
排版脚本 main.ts 读入文件、按选项处理、再原地写回;处理是确定性的字符串/语法树变换。
writeFileSync(filePath, formattedContent, "utf-8");
脚本提供多项排版开关:CJK 强调修复、CJK/英文间距、ASCII→全角引号、frontmatter YAML 格式化。
| `scripts/quotes.ts` | Replace ASCII quotes with fullwidth quotes |
间距修复不是自研实现,而是通过 execSync 调外部 npx autocorrect-node CLI。
execSync(`npx autocorrect-node --fix "${filePath}"`, { stdio: "inherit" });
脚本依赖 remark 生态(remark-cjk-friendly / remark-frontmatter / remark-gfm / unified 等),用 YAML 库读写 frontmatter。
"remark-cjk-friendly": "^1.1.0",
2核心能力
3外部依赖
| Kind | Dependency / Endpoint |
|---|---|
| cli | bun / npx(npx -y bun 运行 main.ts) |
| package | autocorrect-node(npx 第三方 CLI,由 autocorrect.ts 调起) |
| package | remark 生态(remark-cjk-friendly / remark-frontmatter / remark-gfm / unified 等) |
| network | registry.npmjs.org(依赖与 npx 包拉取来源) |
4管控建议与判定依据
- scripts/: 执行本地脚本或命令: scripts/main.ts(npx -y bun 运行:读文件→排版处理→原地写回)
- 网络端点: 声明外部网络访问: registry.npmjs.org(npx 首次拉取 autocorrect-node 与 bun 依赖;npx 包版本未在本仓锁定)
- 本地工作目录: 文件读写与工件输出: {filename}-formatted.md(正式产物)
5第二遍独立确认
- [ok] 无业务网络请求,仅 npx 拉包 — §3 URL 命中集中在 package-lock.json 的 registry/赞助链接;脚本无业务端点调用,唯一外发是 npx 拉 autocorrect-node。
- [ok] 本地读写与原地修改 — main.ts 以 readFileSync/writeFileSync 同路径读写;SKILL.md 的「Typography fixes only」路径直接改原文件,无副本。
- [ok] CJK 排版三能力 — emphasis(remark-cjk-friendly)、spacing(autocorrect-node)、quotes(quotes.ts 全角替换)三项在脚本职责与 SKILL.md 选项中均有对应。
- [ok] execSync 命令注入面 — autocorrect.ts 用模板串把 filePath 插进 shell 命令;同类 baoyu 版用 spawnSync(npxCmd, [...args]) 的数组形式,本版更弱,已记入 injection_surface 与 risks。
- [ok] 无凭证读取 — 目录内无 env/keychain/cookie 读取;仅检查 EXTEND.md 偏好文件是否存在(非凭证)。
- [ok] 与 baoyu 同型实现的差异 — canghe 版无 references/ 与分析底稿,目录更精简(7 文件);排版脚本形态由 spawnSync 改为 execSync。
- [ok] 依赖可复现性 — remark 生态在 package.json/package-lock.json/bun.lock 有锁定;但 npx autocorrect-node 未锁定具体版本。
6结论
535df884a21e0d2e…