Skip to content

Commit

Permalink
Detect headers under list
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderDokuchaev committed Jun 26, 2024
1 parent c9942ff commit a29ff3e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
9 changes: 7 additions & 2 deletions md_dead_link_check/preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from git import Repo

RE_HEADER = r"^[#]{1,6}\s*(.*)"
RE_HEADER = r"^(?:\s*[-+*]\s+|)[#]{1,6}\s*(.*)"
RE_LINK = r"([!]{0,1})\[([^\]!]*)\]\(([^()\s]+(?:\([^()\s]*\))*)\s*(.*?)\)"
RE_HTML_A_TAG_ID = r"<\w+\s+(?:[^>]*?\s+)?id=([\"'])(.*?)\1"
RE_HTML_A_TAG_HREF = r"<\w+\s+(?:[^>]*?\s+)?href=([\"'])(.*?)\1"
Expand Down Expand Up @@ -96,7 +96,12 @@ def process_md_file(path: Path, root_dir: Path) -> MarkdownInfo:
# Detect headers
res = re.match(RE_HEADER, line)
if res:
fragment = process_header_to_fragment(res.group(1))
_fragment = process_header_to_fragment(res.group(1))
fragment = _fragment
repeat = 0
while fragment in fragments:
repeat += 1
fragment = f"{_fragment}-{repeat}"
fragments.append(fragment)
continue

Expand Down
9 changes: 9 additions & 0 deletions tests/test_md_files/a.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,12 @@ Some text
[top](#)

[top](./b.md#)


- ## badge

+ ## badge

[0](#badge)
[1](#badge-1)
[1](#badge-2)
17 changes: 17 additions & 0 deletions tests/test_preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ def test_process_md_file():
"links",
"header-with-quotes-and-math",
"badge",
"badge-1",
"badge-2",
]

ref_links = [
Expand Down Expand Up @@ -110,5 +112,20 @@ def test_process_md_file():
location=Path("tests/test_md_files/a.md"),
line_num=42,
),
LinkInfo(
link="#badge",
location=Path("tests/test_md_files/a.md"),
line_num=49,
),
LinkInfo(
link="#badge-1",
location=Path("tests/test_md_files/a.md"),
line_num=50,
),
LinkInfo(
link="#badge-2",
location=Path("tests/test_md_files/a.md"),
line_num=51,
),
]
assert md_info.links == ref_links

0 comments on commit a29ff3e

Please sign in to comment.