Skip to content

Commit

Permalink
parse href
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderDokuchaev committed Mar 4, 2024
1 parent 6eae966 commit 09916dd
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
13 changes: 9 additions & 4 deletions md_dead_link_check/preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

RE_HEADER = r"^[#]{1,6}\s*(.*)"
RE_LINK = r"[!]{0,1}\[[^\]!]*\]\((([^\s)]+)(?:\s*(.*?))?)\)"
RE_HTML_A_TAG_ID = r"<a\s+id=[\"'](.*)[\"']>.*?<\/a>"
RE_HTML_A_TAG_ID = r"<a\s+id=[\"'](.*?)[\"']>.*?<\/a>"
RE_HTML_A_TAG_HREF = r"<a\s+href=[\"'](.*?)[\"']>.*?<\/a>"
RE_SUB = r"[$`][^`]+?[$`]"


Expand Down Expand Up @@ -102,9 +103,13 @@ def process_md_file(path: Path, root_dir: Path) -> MarkdownInfo:

# Detect id under a tag <a id="introduction"></a>
matches = re.findall(RE_HTML_A_TAG_ID, line)
if matches:
for id in matches:
fragments.append(id)
for id in matches:
fragments.append(id)

# Detect links under a tag <a href="introduction"></a>
matches = re.findall(RE_HTML_A_TAG_HREF, line)
for link in matches:
links.append(LinkInfo(link, path, line_num))
return MarkdownInfo(path=path, fragments=fragments, links=links)


Expand Down
2 changes: 1 addition & 1 deletion tests/test_md_files/a.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Some text

[github](https://github.com/AlexanderDokuchaev)
[b](./b.md) [d.a](b.md) `[A+B](A)`
[d.a](./d/a.md)
<a href="./d/a.md">d.a</a><a href="./d/a.md">d.a</a>
[d.a](/tests/test_md_files/d/a.md "tag")

### Header with `quotes` and $math$
Expand Down
5 changes: 5 additions & 0 deletions tests/test_preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ def test_process_md_file():
location=Path("tests/test_md_files/a.md"),
line_num=29,
),
LinkInfo(
link="./d/a.md",
location=Path("tests/test_md_files/a.md"),
line_num=29,
),
LinkInfo(
link="/tests/test_md_files/d/a.md",
location=Path("tests/test_md_files/a.md"),
Expand Down

0 comments on commit 09916dd

Please sign in to comment.