首页 / 全部技能 / 内容创作 / canghe-format-markdown
内容创作 · freestylefly/canghe-skills

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.

L4 高权限 · 分级授权与审计V1 已固定执行命令联网读写文件Dual-pass Audit
Author freestyleflyGitHub freestylefly/canghe-skills ↗License MIT(仓库根 LICENSE)Commit dd0bf35595
管控等级描述 skill 的能力与建议配套的管控措施,不代表质量或安全认证,也不表示可用或不可用。是否使用由用户和管理者决定。不同 agent 宿主对权限的约束各不相同,请勿假定宿主一定会提供安全兜底。第三方 skill 版权归原作者,使用风险由使用者自行承担。

1实现原理 · 为什么它能做到

分工明确:内容分析与格式化由 Claude(LLM)完成 Step 1-6,脚本只在最后一步做排版修复(Step 7)。

skills/canghe-format-markdown/SKILL.md
Claude performs content analysis and formatting (Steps 1-6), then runs the script for typography fixes (Step 7).
这里在做什么:这是“LLM 出结构 + 脚本出排版”的混合型 skill;脚本负责确定性排版,不负责理解内容。

先检测内容类型:命中 frontmatter/标题/bold/列表/代码块/引用任一即判为 markdown,全不命中才当纯文本。

skills/canghe-format-markdown/SKILL.md
| Has `---` YAML frontmatter | Markdown |
这里在做什么:类型决定后续分支——纯文本直接进 Step 2 格式化,markdown 则先询问用户。

已检测到 markdown 时给用户三选一:优化格式、保留原样、只做排版修复(后者直接改原文件)。

skills/canghe-format-markdown/SKILL.md
1. Optimize formatting (Recommended)
这里在做什么:三条路径决定后续跳过哪些 Step;「只做排版修复」会跳过产物拷贝、原地改用户源文件。

自动补 frontmatter:把 title/slug/summary/coverImage 等元字段写入 YAML,summary 由模型生成为 100-150 字摘要。

skills/canghe-format-markdown/SKILL.md
| `summary` | Generate engaging summary (100-150 characters) |
这里在做什么:slug 从文件路径推断或由标题生成;coverImage 检查同目录 imgs/cover.png 存在才用相对路径。

标题去重:若正文首行是 H1 则抽到 frontmatter 的 title 并从正文删除,避免正文再出现 H1 重复。

skills/canghe-format-markdown/SKILL.md
2. If first line is H1 → extract to frontmatter `title`, remove H1 from body
这里在做什么:标题候选由模型生成 3 个并用 AskUserQuestion 让用户选,超时取推荐项。

输出命名固定为 {original-filename}-formatted.md,且覆盖前会把已存在的同名产物按时间戳改名备份。

skills/canghe-format-markdown/SKILL.md
mv "{filename}-formatted.md" "{filename}-formatted.backup-$(date +%Y%m%d-%H%M%S).md"
这里在做什么:final.md → final-formatted.md;已存在则先备份为 final-formatted.backup-20260128-143052.md 再写。

排版脚本 main.ts 读入文件、按选项处理、再原地写回;处理是确定性的字符串/语法树变换。

skills/canghe-format-markdown/scripts/main.ts
writeFileSync(filePath, formattedContent, "utf-8");
这里在做什么:main.ts 用 readFileSync 读、处理、writeFileSync 写同一路径,所以“只做排版修复”会直接改用户原文件。

脚本提供多项排版开关:CJK 强调修复、CJK/英文间距、ASCII→全角引号、frontmatter YAML 格式化。

skills/canghe-format-markdown/SKILL.md
| `scripts/quotes.ts` | Replace ASCII quotes with fullwidth quotes |
这里在做什么:--quotes/--spacing/--emphasis 及各自的 --no-* 反向开关;脚本默认 spacing+emphasis 开、quotes 关。

间距修复不是自研实现,而是通过 execSync 调外部 npx autocorrect-node CLI。

skills/canghe-format-markdown/scripts/autocorrect.ts
execSync(`npx autocorrect-node --fix "${filePath}"`, { stdio: "inherit" });
这里在做什么:filePath 被字符串插值拼进 shell 命令执行——这是本 skill 的注入面(见 security.injection_surface)。

脚本依赖 remark 生态(remark-cjk-friendly / remark-frontmatter / remark-gfm / unified 等),用 YAML 库读写 frontmatter。

skills/canghe-format-markdown/scripts/package.json
"remark-cjk-friendly": "^1.1.0",
这里在做什么:依赖声明与锁定在 package.json / package-lock.json / bun.lock,靠 npx -y bun 直接运行 TS。

2核心能力

01纯文本/半结构化文本 → 规范 markdown 产物
02frontmatter/title/slug/summary/coverImage 自动生成
03CJK 强调(emphasis/bold)标点修复
04CJK/英文混排间距(autocorrect)
05ASCII 引号 → 全角引号替换
06覆盖前自动时间戳备份
07交互式标题选择(三候选 + 用户确认)
08markdown 检测后的三选一工作流(优化/保留/仅排版)

3外部依赖

KindDependency / Endpoint
clibun / npx(npx -y bun 运行 main.ts)
packageautocorrect-node(npx 第三方 CLI,由 autocorrect.ts 调起)
packageremark 生态(remark-cjk-friendly / remark-frontmatter / remark-gfm / unified 等)
networkregistry.npmjs.org(依赖与 npx 包拉取来源)

4管控建议与判定依据

建议配套管控措施
面向 Agent
分级或逐次授权,建议在沙箱运行
面向管理者
审批流程、可回滚、全程审计
判定依据(文件路径与关键语句摘要)
  • scripts/: 执行本地脚本或命令: scripts/main.ts(npx -y bun 运行:读文件→排版处理→原地写回)
  • 网络端点: 声明外部网络访问: registry.npmjs.org(npx 首次拉取 autocorrect-node 与 bun 依赖;npx 包版本未在本仓锁定)
  • 本地工作目录: 文件读写与工件输出: {filename}-formatted.md(正式产物)
审计说明: 。判级对象=本 skill 目录(SKILL.md + scripts/{main,quotes,autocorrect}.ts + package.json/package-lock.json/bun.lock):目录内自身行为=本地读写(读用户 md、写 -formatted.md、原地改原文件、写备份),加一次固定公开端点外发(autocorrect.ts 的 execSync 会在默认 spacing 开启时用 npx 从 registry.npmjs.org 拉取并执行第三方 CLI,版本未在本仓锁定)。无凭证/密钥读取、无逆向/冒充/绕过防護/安全降级、无把用户内容送业务后端,故为黄而非橙红。留意点:① “仅排版修复”路径会原地修改用户源文件;② autocorrect.ts 的 execSync 字符串插值存在命令注入面(同类实现若用 args 数组可避免);③ 跑未锁版本的 npx 包引入供应链不确定性。

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结论

  • CJK 优先排版:emphasis 修复 + 中英间距 + 全角引号,明显面向中文写作场景。
  • 有备份纪律:覆盖同名产物前先按时间戳改名,降低误覆盖损失。
  • 输出结构固定:产物统一叫 {filename}-formatted.md,配合 frontmatter/title/summary 生成。
  • 本地处理、无业务网络请求:除 npx 拉包外不把文章内容发往任何服务。
  • 保守格式化约束:明确要求不改原意、不加原文没有的内容。
  • 适合: 适合中文写作者把草稿/半结构化文本整理成规范 markdown(补 frontmatter、标题、列表、中英间距与全角引号),或作为发文前的固定排版工序。
    不适合: 不适合需要严格不改动源文件的场景(“仅排版修复”会原地写);不适合完全离线、无 npx/Node 环境;不适合对第三方 npx 包供应链敏感、要求锁定版本的团队流程。
    Install Agent-Ready
    ① Verified Mirror 2026-10-10
    Option A · Direct DownloadDownload canghe-format-markdown.tar.gz
    sha256: 535df884a21e0d2e…
    Option B · Copy Guide for Agent
    install.json
    agent 读 JSON 指南后会自动从本站下载安装,无需更多说明。
    ② Upstream GitHub
    GitHub Repo ↗
    Pinned to commit dd0bf35595.