diff --git a/src/mkdocs_autorefs/references.py b/src/mkdocs_autorefs/references.py index d1cb2c3..8dd29a6 100644 --- a/src/mkdocs_autorefs/references.py +++ b/src/mkdocs_autorefs/references.py @@ -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"): 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": diff --git a/tests/test_references.py b/tests/test_references.py index 077bb9b..15617c9 100644 --- a/tests/test_references.py +++ b/tests/test_references.py @@ -257,9 +257,14 @@ def test_register_markdown_anchors() -> None: [](){#alias5} [](){#alias6} Decoy. - ## Heading more + ## Heading more1 [](){#alias7} + [decoy](){#alias8} + [](){#alias9} + ## Heading more2 + + [](){#alias10} """, ), ) @@ -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", }