markdown-fixer

語法修得乾淨,意思一字不動。

51 條規則,只改語法,不改你寫的字。

cargo install markdown-fixer-cli --locked

看它怎麼修

四段真實輸入,輸出是 0.2.0 直接執行後貼上,沒有改寫。

標題與清單 heading.md

MDX010 · MDX030 ×2 · 3 個可修正

# 與 - 後面各補一個空格,標題與清單的文字完全不動。

修正前heading.md紅線:回報位置
#標題

-item
-第二項
修正後heading.md紅塊:插入的字元
# 標題

- item
- 第二項
$ markdown-fixer --check heading.md
heading.md
  1:2        warning MDX010  Add a space after the heading marker: "# " rather than "#". (fixable)
  3:2        warning MDX030  Add a space after the "-" list marker. (fixable)
  4:2        warning MDX030  Add a space after the "-" list marker. (fixable)

3 problem(s) in 1 file(s): 3 fixable, 0 error(s), 3 warning(s), 0 info.

$ markdown-fixer --write heading.md
heading.md
  No problems.
  Fixed 3 issue(s).

Fixed 3 issue(s) in 1 file(s).
No remaining Markdown issues.

表格 table.md

MDX060 · 1 個可修正

分隔列每格補到三個連字號,原本的靠右對齊保留。

修正前table.md紅線:回報位置
| 欄位 | 值 |
| - | --: |
| a | 1 |
修正後table.md紅塊:插入的字元
| 欄位 | 值 |
| --- | ---: |
| a | 1 |
$ markdown-fixer --check table.md
table.md
  2:3        warning MDX060  Table delimiter cell has fewer than three hyphens. (fixable)

1 problem(s) in 1 file(s): 1 fixable, 0 error(s), 1 warning(s), 0 info.

$ markdown-fixer --write table.md
table.md
  No problems.
  Fixed 1 issue(s).

Fixed 1 issue(s) in 1 file(s).
No remaining Markdown issues.

CJK 粗體 cjk.md

MDX042 ×3 · 3 個可修正

把全形冒號移到 ** 外面,GitHub 上的粗體才會生效,意思不變。

修正前cjk.md紅線:回報位置
**審查執行:**2026-11-04
**Spec 來源:**本次工作
修正後cjk.md紅塊:移動後的位置
**審查執行**:2026-11-04
**Spec 來源**:本次工作
$ markdown-fixer --check cjk.md
cjk.md
  1:1        warning MDX042  Unpaired `**` next to CJK or full-width punctuation renders literally instead of emphasis. Move the full-width punctuation outside the delimiters (`**X:**Y` becomes `**X**:Y`) or reword. (fixable)
  1:8        warning MDX042  Unpaired `**` next to CJK or full-width punctuation renders literally instead of emphasis. Move the full-width punctuation outside the delimiters (`**X:**Y` becomes `**X**:Y`) or reword. (fixable)
  2:11       warning MDX042  Unpaired `**` next to CJK or full-width punctuation renders literally instead of emphasis. Move the full-width punctuation outside the delimiters (`**X:**Y` becomes `**X**:Y`) or reword. (fixable)

3 problem(s) in 1 file(s): 3 fixable, 0 error(s), 3 warning(s), 0 info.

$ markdown-fixer --write cjk.md
cjk.md
  No problems.
  Fixed 3 issue(s).

Fixed 3 issue(s) in 1 file(s).
No remaining Markdown issues.

空白與檔尾 whitespace.md

MDX004 · MDX005 · MDX003 · 3 個可修正

行尾三個空格只留下強制換行用的兩個、三個空行收成一個、再補上檔尾換行。

修正前whitespace.md· 第 3、4 行將刪除
段落一。···



段落二沒有換行結尾
修正後whitespace.md少了兩行,內容不變
段落一。

段落二沒有換行結尾
$ markdown-fixer --check whitespace.md
whitespace.md
  1:5        warning MDX004  Trailing whitespace. Exactly two trailing spaces create a hard line break; other trailing whitespace is noise. (fixable)
  3:1        warning MDX005  Multiple consecutive blank lines. Use one blank line between blocks. (fixable)
  5:1        warning MDX003  File does not end with a newline. (fixable)

3 problem(s) in 1 file(s): 3 fixable, 0 error(s), 3 warning(s), 0 info.

$ markdown-fixer --write whitespace.md
whitespace.md
  No problems.
  Fixed 3 issue(s).

Fixed 3 issue(s) in 1 file(s).
No remaining Markdown issues.

只改語法,不改意思。

每個候選修正都要先用 parser 重新渲染比對。渲染結果不同就不套用,在輸出裡列成 skipped;--write 也一定要指定路徑,不會自作主張動整個目錄。

原樣保留

  • fenced code 與 inline code
  • YAML frontmatter、raw HTML 區塊與 HTML 註解
  • 數學式、URL、連結目的地
  • 剛好兩個行尾空格,視為刻意的換行

這是行導向的啟發式檢查器,不是完整的 CommonMark parser。MDX010 與 MDX030 本質上有歧義,--write 之後請看 diff。

整份檔案拒絕修正

  • MDX081frontmatter 沒有關閉
  • MDX083HTML 註解沒有關閉
  • MDX053fence 的 info string 不合法
  • MDX085$$ 數學區塊沒有關閉
  • MDX086引言巢狀超過深度上限

這五條即使列在 --ignore-rule 也照樣阻擋,整份文件保持原樣並回報原因。

  1. 讀取檔案
  2. 產生候選修正
  3. 重新渲染比對
  4. 寫入或略過

行為以 Node.js 參考實作(markdown-autocorrect skill 的 markdown-fixer.mjs)為準,用 golden 語料與差異模糊測試驗證。關閉渲染保護時,輸出與 Node 版逐位元組相同。

哪一條該由人判斷,寫得清楚。

分類與條數取自 0.2.0 的 --list-rules。

21 條可自動修正,30 條只報告。7 條 error 會讓 CI 失敗,8 條 info 只有在 --strict 時才算失敗。

51 條規則依分類的數量、可自動修正數與規則編號
分類 條數 可自動修正 規則
標題 10 4 MDX010 MDX011 MDX012 MDX013 MDX014 MDX015 MDX016 MDX017 MDX018 MDX019
結構 7 1 MDX080 MDX081 MDX082 MDX083 MDX084 MDX085 MDX086
空白與換行 6 5 MDX003 MDX004 MDX005 MDX006 MDX007 MDX008
連結與圖片 6 0 MDX070 MDX071 MDX072 MDX073 MDX074 MDX075
清單 5 2 MDX030 MDX031 MDX032 MDX033 MDX035
表格 4 2 MDX060 MDX061 MDX062 MDX063
程式碼圍籬 4 1 MDX050 MDX051 MDX052 MDX053
強調 3 2 MDX040 MDX041 MDX042
編碼 3 2 MDX001 MDX002 MDX009
樣式 2 1 MDX090 MDX091
引言 1 1 MDX020

完整目錄用 markdown-fixer --list-rules 印出;--rule 與 --ignore-rule 可以逐條開關,--max-line-length 與 --cjk-typography 預設不啟用。

同一份規則,三種呼叫方式。

同一個輸入,得到同樣的問題與同樣的文字。

命令列

一次檢查多個檔案或目錄;--write 只寫入明確指定的檔案。

常用旗標
# 只報告,不寫檔
markdown-fixer --check README.md docs/

# 只寫入明確指定的檔案
markdown-fixer --write README.md docs/guide.md

# CI 用的 JSON 輸出
markdown-fixer --check --json docs/

# 從標準輸入讀,修正後的完整文字輸出到標準輸出
pbpaste | markdown-fixer --stdin --write --quiet

# 判斷一段文字像不像 Markdown
pbpaste | markdown-fixer --stdin --detect
  • 0沒有失敗的問題
  • 1還有 warning 或 error(--strict 時 info 也算)
  • 2用法、I/O 或解析錯誤

Rust 函式庫

不做 I/O、不讀環境變數、不印東西,可以嵌進任何應用程式。片段處理與執行緒安全見 docs/library.md。

Cargo 相依
cargo add markdown-fixer
診斷與修正
use markdown_fixer::{Options, diagnose, fix_document};

fn main() {
    let text = "#Title\n\n-item\n";
    let options = Options::default();

    for p in diagnose(text, &options) {
        println!("{}:{} {} {}", p.line, p.column, p.rule, p.message);
    }

    let result = fix_document(text, &options);
    if result.refused {
        eprintln!("not fixed: {}", result.reason);
    }
    assert_eq!(result.text, "# Title\n\n- item\n");
}

C ABI

提供 cdylib 與 staticlib,標頭檔是手寫的 markdown_fixer.h。Swift、C#、Go、Python 都能直接呼叫,完整 API 見 docs/ffi.md。

從 C 呼叫
#include <stdio.h>
#include <string.h>
#include "markdown_fixer.h"

int main(void) {
    const char *doc = "#Title\n\n-item\n";
    char *json = mdfix_diagnose(doc, strlen(doc), NULL);
    printf("%s\n", json);
    mdfix_string_free(json);
    return 0;
}

單一執行檔,按熱鍵就修好剪貼簿。

數值取自專案 README 的本機量測。

沒有執行期相依,不需要 Node.js。冷啟動的延遲小到可以放在熱鍵後面。

15 KB 片段,啟動到輸出
6.5ms
macOS aarch64 release 執行檔
1.1MB
執行期相依
0

實際時間依機器與檔案大小而異。

搭配 Karabiner-Elements,按 左 Option + Shift + V,把剪貼簿裡的 Markdown 修正後直接貼上。設定檔與 Linux、Windows 的對應寫法都在範例目錄。

剪貼簿熱鍵設定

兩行指令就開始。

需要 Rust 1.85 以上。

cargo install markdown-fixer-cli --locked
markdown-fixer --check README.md docs/
下載預先編譯版本

macOS、Linux、Windows 的 CLI 與 C ABI 函式庫,每個檔案附 .sha256 與 SHA256SUMS。shell 安裝程式、Homebrew tap 與 npm 包裝還在規劃中。