Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add option to scan and register Markdown anchors #39

Merged
merged 14 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/mkdocs_autorefs/references.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,9 @@ def _scan_anchors(self, parent: Element, pending_anchors: _PendingAnchors) -> No
# We found an anchor. Record its id if it has one.
if anchor_id := el.get("id"):
pawamoy marked this conversation as resolved.
Show resolved Hide resolved
pending_anchors.append(anchor_id)
# If the element has text or a link, it's not an alias.
# Non-whitespace text after the element interrupts the chain, aliases can't apply.
if el.tail and el.tail.strip():
if el.text or el.get("href") or (el.tail and el.tail.strip()):
pending_anchors.flush()

elif el.tag == "p":
Expand Down
10 changes: 9 additions & 1 deletion tests/test_references.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,14 @@ def test_register_markdown_anchors() -> None:
[](){#alias5}
[](){#alias6}
Decoy.
## Heading more
## Heading more1

[](){#alias7}
[decoy](){#alias8}
[](){#alias9}
## Heading more2

[](){#alias10}
""",
),
)
Expand All @@ -273,4 +278,7 @@ def test_register_markdown_anchors() -> None:
"alias5": "page#alias5",
"alias6": "page#alias6",
"alias7": "page#alias7",
"alias8": "page#alias8",
"alias9": "page#heading-more2",
"alias10": "page#alias10",
}
oprypin marked this conversation as resolved.
Show resolved Hide resolved