From 7fbec96b09b823773b249737d2d2609745a1f0a1 Mon Sep 17 00:00:00 2001 From: Omer Bektas Date: Sun, 16 Aug 2026 11:58:05 +0300 Subject: [PATCH] Ignore README headings inside code blocks --- repodx.py | 10 ++++++++++ tests/test_repodx.py | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/repodx.py b/repodx.py index ea32c42..8db8f64 100644 --- a/repodx.py +++ b/repodx.py @@ -112,8 +112,18 @@ def check_gitignore(repo_path): def markdown_headings(markdown_text): headings = [] + inside_fenced_code_block = False for line in markdown_text.splitlines(): + stripped_line = line.strip() + + if stripped_line.startswith("```") or stripped_line.startswith("~~~"): + inside_fenced_code_block = not inside_fenced_code_block + continue + + if inside_fenced_code_block: + continue + match = re.match(r"^\s{0,3}#{1,6}\s+(.+?)\s*$", line) if match: diff --git a/tests/test_repodx.py b/tests/test_repodx.py index 1eb6965..0a43ae5 100644 --- a/tests/test_repodx.py +++ b/tests/test_repodx.py @@ -235,6 +235,45 @@ def test_check_readme_reports_unreadable_file(self): self.assertEqual(len(result), 1) self.assertTrue(result[0].startswith("Could not read README.md:")) + def test_check_readme_ignores_heading_like_lines_inside_code_blocks(self): + with tempfile.TemporaryDirectory() as temp_dir: + repo_path = Path(temp_dir) + (repo_path / "README.md").write_text( + "# Project\n\n" + "```python\n" + "# Installation\n" + "# Usage\n" + "```\n", + encoding="utf-8", + ) + + result = repodx.check_readme(repo_path) + + self.assertEqual( + result, + [ + "Missing README heading: Installation", + "Missing README heading: Usage", + ], + ) + + def test_check_readme_accepts_real_headings_around_code_blocks(self): + with tempfile.TemporaryDirectory() as temp_dir: + repo_path = Path(temp_dir) + (repo_path / "README.md").write_text( + "# Project\n\n" + "## Installation\n\n" + "```python\n" + "# Not a heading\n" + "```\n\n" + "## Usage\n", + encoding="utf-8", + ) + + result = repodx.check_readme(repo_path) + + self.assertEqual(result, []) + def test_check_readme_reports_os_error(self): with tempfile.TemporaryDirectory() as temp_dir: repo_path = Path(temp_dir)