0.12.10:內文標題不再拆成新紀錄
CHANGELOG.mdDocs/usage.mdpyproject.tomlsrc/changelogkit/__init__.pysrc/changelogkit/backfill.pysrc/changelogkit/history.pysrc/changelogkit/lang/en.jsonsrc/changelogkit/lang/ja.jsonsrc/changelogkit/lang/zh-CN.jsonsrc/changelogkit/lang/zh-TW.jsontests/test_changelogkit.py
commit 851def1d3594401a91f4d846ca0299f478b42c95Author: Nuclear0709 <agp05432@gmail.com>Date: Sat Nov 21 07:28:47 2026 +0800 0.12.10:內文標題不再拆成新紀錄 diff --git a/CHANGELOG.md b/CHANGELOG.md--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -1,5 +1,20 @@ # Changelog +## 2026.11.21 changelogkit 0.12.10 發佈++**類型**: 修正 · **檔案**: `history.py`、`backfill.py`、`Docs/usage.md`++### 問題 - 新增紀錄的內文若含 `##` 標題,會被解析成多筆紀錄++### 修改 - 內文標題自動降為 `###`,紀錄邊界只認日期++- `build_block` 會把問題/根因內文與修改細節中的 `#`/`##` 轉成 `###`,並補上標題前後空行。+- `parse_entries`、`_heading_indices` 與 `backfill` 只把 `## YYYY.MM.DD` 當成紀錄邊界;內文的 `## 子標題` 留在同一筆 `raw` 中,不再拆成日期為空的紀錄。+- 完全沒有日期標題的舊檔仍 fallback 到所有 `##`,讓 `backfill` 可以補日期。+- 文件與四語系表單提示同步說明子標題會自動轉為 `###`。++---+ ## 2026.11.15 changelogkit 0.12.9 發佈 **類型**: 修正 · **檔案**: `assets/markdown.js`、`assets/file.js`、`assets/file.css`diff --git a/Docs/usage.md b/Docs/usage.md--- a/Docs/usage.md+++ b/Docs/usage.md@@ -20,6 +20,8 @@ changelogkit docs-init # 印出可貼上的 docs TOML `add` 的標題是唯一的位置參數;內文用 `--problem`/`--cause`/`--change` 提供。各節第一行是摘要(會接在 `### 問題 - …` 等標題後),其餘行是細節;`--change` 可重複,第一筆當摘要、其餘轉成條列;`--change-detail` 在修改摘要下補一段多行說明。Web 表單分「修改摘要」(單行)、「修改細節」(段落)、「其他修改」(每行一項)。只給標題時插入空骨架。 +內文中的 `#`/`##` 子標題會自動轉成 `###`,並補上標題前後空行;紀錄邊界只認 `## YYYY.MM.DD`,避免單筆紀錄的內文被拆成多筆。+ 摘要末尾的 `.,;:!?。,;:!?` 會自動移除(markdownlint MD026);`…`、`—` 不在規則內,會保留;內文與條列不受影響。 未安裝時用 `python -m changelogkit …`(`PYTHONPATH` 指向 `src/`)。diff --git a/pyproject.toml b/pyproject.toml--- a/pyproject.toml+++ b/pyproject.toml@@ -4,7 +4,7 @@ build-backend = "hatchling.build" [project] name = "changelogkit"-version = "0.12.9"+version = "0.12.10" description = "本機變更歷史 GUI 與開發索引:解析 ChangeHistory.md、唯讀索引 Git/文件/API 宣告" readme = "README.md" requires-python = ">=3.10"diff --git a/src/changelogkit/__init__.py b/src/changelogkit/__init__.py--- a/src/changelogkit/__init__.py+++ b/src/changelogkit/__init__.py@@ -53,4 +53,4 @@ def _installed_version(): # 版號來源優先序:原始碼 checkout 的 pyproject → 已安裝中介資料 → 字面值備援。 # 優先原始碼,讓 `PYTHONPATH=src` 直接跑時「所見即所跑」,不被環境中殘留的舊安裝誤導。-__version__ = _pyproject_version() or _installed_version() or "0.12.9"+__version__ = _pyproject_version() or _installed_version() or "0.12.10"diff --git a/src/changelogkit/backfill.py b/src/changelogkit/backfill.py--- a/src/changelogkit/backfill.py+++ b/src/changelogkit/backfill.py@@ -53,8 +53,12 @@ def _strip_subs(lines): def _entry_heads(lines):- """真正的 ``## `` 標題索引;忽略 HTML 註解與程式碼區塊內的同型字串。"""- heads, fence, in_comment = [], False, False+ """紀錄標題索引;忽略 HTML 註解與程式碼區塊內的同型字串。++ 有日期標題時只認日期標題,避免內文的 ``## 子標題`` 被當成另一筆;+ 完全沒有日期標題的舊檔才 fallback 到所有 ``##``,讓回填仍可運作。+ """+ dated, all_heads, fence, in_comment = [], [], False, False for index, line in enumerate(lines): stripped = line.strip() if stripped.startswith(("```", "~~~")):@@ -69,8 +73,10 @@ def _entry_heads(lines): in_comment = False continue if line.startswith("## "):- heads.append(index)- return heads+ all_heads.append(index)+ if DATED.match(line):+ dated.append(index)+ return dated or all_heads def _sort_key(line):diff --git a/src/changelogkit/history.py b/src/changelogkit/history.py--- a/src/changelogkit/history.py+++ b/src/changelogkit/history.py@@ -50,7 +50,7 @@ def parse_entries(text=None): """ text = read_history() if text is None else text lines = strip_comments(text).splitlines()- heads = [i for i, line in enumerate(lines) if line.startswith("## ")]+ heads = _heading_indices(lines) entries = [] for k, start in enumerate(heads): end = heads[k + 1] if k + 1 < len(heads) else len(lines)@@ -77,9 +77,21 @@ def read_history(): def _heading_indices(lines):- """真實(非註解內)的 ``'## '`` 標題行索引。"""- out, in_comment = [], False+ """真實(非註解/程式碼區塊內)的紀錄標題索引。++ 只有 ``## YYYY.MM.DD`` 才是紀錄邊界;內文若出現 ``## 子標題``,+ 不能被當成另一筆紀錄。完全沒有日期標題的舊檔才 fallback 到所有+ ``##``,讓 ``backfill`` 仍有機會補日期。+ """+ dated, all_heads = [], []+ fence, in_comment = False, False for i, line in enumerate(lines):+ stripped = line.strip()+ if stripped.startswith(("```", "~~~")):+ fence = not fence+ continue+ if fence:+ continue if "<!--" in line: in_comment = True if in_comment:@@ -87,8 +99,10 @@ def _heading_indices(lines): in_comment = False continue if line.startswith("## "):- out.append(i)- return out+ all_heads.append(i)+ if DATE_RE.match(line.rstrip()):+ dated.append(i)+ return dated or all_heads # ─────────────────────────── 組版 ───────────────────────────@@ -122,6 +136,59 @@ def _summary_body(text): return _heading_text(parts[0]), "\n".join(parts[1:]).strip() +_HEADING_LINE = re.compile(r"^(#{1,6})(\s+.*)$")+_FENCE_LINE = re.compile(r"^\s*(```|~~~)")+++def _normalize_body(text):+ """把使用者內文中的標題降階到 ``###``,避免被解析成新紀錄。++ 紀錄本身是 ``##``;內文的 ``#``/``##`` 若原樣寫入,會和下一筆紀錄+ 同層,網頁與 CLI 都會把它們當成獨立紀錄。這裡只處理非程式碼區塊內的+ 標題,並補上標題前後空行(markdownlint MD022)。+ """+ lines = str(text or "").splitlines()+ out, fence = [], None+ for line in lines:+ fence_match = _FENCE_LINE.match(line)+ if fence_match:+ marker = fence_match.group(1)+ if fence is None:+ fence = marker+ elif fence == marker:+ fence = None+ out.append(line)+ continue+ if fence is None:+ heading = _HEADING_LINE.match(line)+ if heading:+ if out and out[-1].strip():+ out.append("")+ out.append("#" * max(3, len(heading.group(1))) + heading.group(2))+ out.append("")+ continue+ out.append(line)++ # 收斂連續空行(程式碼區塊內保留原樣)。+ cleaned, fence, blank = [], None, 0+ for line in out:+ fence_match = _FENCE_LINE.match(line)+ if fence_match:+ marker = fence_match.group(1)+ if fence is None:+ fence = marker+ elif fence == marker:+ fence = None+ if fence is None and line.strip() == "":+ blank += 1+ if blank > 1:+ continue+ else:+ blank = 0+ cleaned.append(line)+ return "\n".join(cleaned).strip()++ def _norm_refs(refs): """把相關文件正規化為字串清單(接受 list 或每行一筆的字串)。""" if not refs:@@ -164,10 +231,10 @@ def build_block( continue lines.append("### %s - %s" % (label, summary)) if body:- lines += ["", body]+ lines += ["", _normalize_body(body)] lines.append("") # 修改:第一項當標題摘要,其餘為條列;細節另成一段。- detail = str(change_detail or "").strip()+ detail = _normalize_body(change_detail) if changes: summary, rest = _heading_text(changes[0]), changes[1:] lines.append("### 修改 - %s" % summary)diff --git a/src/changelogkit/lang/en.json b/src/changelogkit/lang/en.json--- a/src/changelogkit/lang/en.json+++ b/src/changelogkit/lang/en.json@@ -11,7 +11,7 @@ "cli.add.problem": "Problem (first line = summary)", "cli.add.cause": "Root cause (first line = summary; optional)", "cli.add.change": "Change item (first line = summary; repeatable)",- "cli.add.change_detail": "Details under the change summary (multi-line)",+ "cli.add.change_detail": "Details under the change summary (multi-line; #/## subheadings become ###)", "cli.add.date": "Date YYYY.MM.DD (default today)", "cli.add.time": "Time HH:MM (default now)", "cli.add.ref": "Related document (repeatable)",@@ -105,7 +105,7 @@ "form.cause_ph": "Root cause summary (details on following lines)", "form.change_summary": "Change summary", "form.change_detail": "Change details",- "form.change_detail_ph": "Additional explanation; multiple lines allowed",+ "form.change_detail_ph": "Additional explanation; multiple lines allowed; #/## becomes ###", "form.change_summary_ph": "One-line summary of the change", "form.other_changes": "Other changes (one per line, optional)", "form.other_changes_ph": "Another change Another change",diff --git a/src/changelogkit/lang/ja.json b/src/changelogkit/lang/ja.json--- a/src/changelogkit/lang/ja.json+++ b/src/changelogkit/lang/ja.json@@ -11,7 +11,7 @@ "cli.add.problem": "問題(先頭行=要約)", "cli.add.cause": "原因(先頭行=要約、任意)", "cli.add.change": "変更項目(先頭行=要約、複数可)",- "cli.add.change_detail": "変更要約の下に置く詳細(複数行可)",+ "cli.add.change_detail": "変更要約の下に置く詳細(複数行可;#/## の小見出しは ### に変換)", "cli.add.date": "日付 YYYY.MM.DD(既定は今日)", "cli.add.time": "時刻 HH:MM(既定は現在)", "cli.add.ref": "関連ドキュメント(複数可)",@@ -105,7 +105,7 @@ "form.cause_ph": "根本原因の要約 (以降の行に詳細)", "form.change_summary": "変更の要約", "form.change_detail": "変更の詳細",- "form.change_detail_ph": "補足説明;複数行可",+ "form.change_detail_ph": "補足説明;複数行可;#/## は自動で ### に変換", "form.change_summary_ph": "変更を一言で", "form.other_changes": "その他の変更(1 行 1 項目、任意)", "form.other_changes_ph": "別の変更 さらに別の変更",diff --git a/src/changelogkit/lang/zh-CN.json b/src/changelogkit/lang/zh-CN.json--- a/src/changelogkit/lang/zh-CN.json+++ b/src/changelogkit/lang/zh-CN.json@@ -11,7 +11,7 @@ "cli.add.problem": "问题(首行=摘要)", "cli.add.cause": "根因(首行=摘要,可省略)", "cli.add.change": "修改项(首行=摘要,可重复)",- "cli.add.change_detail": "修改摘要下的细节说明(可多行)",+ "cli.add.change_detail": "修改摘要下的细节说明(可多行;#/## 子标题会转为 ###)", "cli.add.date": "日期 YYYY.MM.DD(默认今天)", "cli.add.time": "时间 HH:MM(默认现在)", "cli.add.ref": "相关文档(可重复)",@@ -105,7 +105,7 @@ "form.cause_ph": "根因摘要 (其后可换行写详述)", "form.change_summary": "修改摘要", "form.change_detail": "修改细节",- "form.change_detail_ph": "补充说明;可多行",+ "form.change_detail_ph": "补充说明;可多行;#/## 会自动转为 ###", "form.change_summary_ph": "一句话摘要", "form.other_changes": "其他修改(每行一项,选填)", "form.other_changes_ph": "另一项修改 再一项修改",diff --git a/src/changelogkit/lang/zh-TW.json b/src/changelogkit/lang/zh-TW.json--- a/src/changelogkit/lang/zh-TW.json+++ b/src/changelogkit/lang/zh-TW.json@@ -11,7 +11,7 @@ "cli.add.problem": "問題(首行=摘要)", "cli.add.cause": "根因(首行=摘要,可省略)", "cli.add.change": "修改項目(首行=摘要,可重複)",- "cli.add.change_detail": "修改摘要下的細節說明(可多行)",+ "cli.add.change_detail": "修改摘要下的細節說明(可多行;#/## 子標題會轉為 ###)", "cli.add.date": "日期 YYYY.MM.DD(預設今天)", "cli.add.time": "時間 HH:MM(預設現在)", "cli.add.ref": "相關文件(可重複)",@@ -105,7 +105,7 @@ "form.cause_ph": "根因摘要 (其後可換行寫詳述)", "form.change_summary": "修改摘要", "form.change_detail": "修改細節",- "form.change_detail_ph": "補充說明;可多行",+ "form.change_detail_ph": "補充說明;可多行;#/## 會自動轉為 ###", "form.change_summary_ph": "一句話摘要", "form.other_changes": "其他修改(每行一項,選填)", "form.other_changes_ph": "另一項修改 再一項修改",diff --git a/tests/test_changelogkit.py b/tests/test_changelogkit.py--- a/tests/test_changelogkit.py+++ b/tests/test_changelogkit.py@@ -590,6 +590,21 @@ class HistoryTests(unittest.TestCase): history.insert_block(history.build_block("2026.02.01", "新紀錄", "功能", "b.swift")) self.assertEqual(history.parse_entries()[0]["title"], "新紀錄") + def test_insert_block_with_body_subheading_stays_one_entry(self):+ with TempRepo():+ history.insert_block(+ history.build_block(+ "2026.02.03",+ "單筆紀錄",+ "修復",+ "",+ change_detail="## 結束直播\n- 內容",+ )+ )+ titles = [entry["title"] for entry in history.parse_entries()]+ self.assertEqual(titles[0], "單筆紀錄")+ self.assertNotIn("結束直播", titles)+ def test_sort_lines_orders_by_time(self): lines = [ "# History",@@ -676,6 +691,34 @@ class HistoryTests(unittest.TestCase): self.assertIn("- 其他項目", block) self.assertLess(block.index("細節一"), block.index("- 其他項目")) + def test_build_block_demotes_body_headings(self):+ block = history.build_block(+ "2026.02.02",+ "內文標題",+ "修復",+ "",+ problem="問題摘要\n## 問題細節\n內容",+ change_detail="# 大標\n## 中標\n- 項目",+ )+ self.assertIn("### 問題細節\n\n內容", block)+ self.assertIn("### 大標\n\n### 中標\n\n- 項目", block)+ self.assertEqual(sum(1 for line in block.splitlines() if line.startswith("## ")), 1)+ entries = history.parse_entries(block)+ self.assertEqual(len(entries), 1)+ self.assertEqual(entries[0]["title"], "內文標題")++ def test_parse_entries_ignores_body_subheadings(self):+ text = "# H\n\n## 2026.01.01 10:00 主紀錄\n\n## 結束直播\n\n- 內容\n\n## 2026.01.02 10:00 下一筆\n"+ entries = history.parse_entries(text)+ self.assertEqual([e["title"] for e in entries], ["主紀錄", "下一筆"])+ self.assertIn("## 結束直播", entries[0]["raw"])++ def test_parse_entries_keeps_undated_legacy_file(self):+ text = "# H\n\n## 沒有日期的條目\n\n內容\n"+ entries = history.parse_entries(text)+ self.assertEqual(len(entries), 1)+ self.assertIsNone(entries[0]["date"])+ def test_build_block_strips_md026_punctuation(self): block = history.build_block( "2026.06.01",