Skip to content

Commit

Permalink
fix: Fix cross-references from the root index page
Browse files Browse the repository at this point in the history
If there's any xref inside docs/index.md, `url_a` ends up being '', and `url_a[-1]` fails.

Issue #184: #184
PR #185: #185
  • Loading branch information
oprypin authored Dec 6, 2020
1 parent 22a9e4b commit 9c9f2a0
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
8 changes: 1 addition & 7 deletions src/mkdocstrings/references.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,6 @@ def relative_url(url_a: str, url_b: str) -> str:
Returns:
The relative URL to go from A to B.
"""
directory_url = False
if url_a[-1] == "/":
url_a = url_a.rstrip("/")
directory_url = True
parts_a = url_a.split("/")
url_b, anchor = url_b.split("#", 1)
parts_b = url_b.split("/")
Expand All @@ -81,9 +77,7 @@ def relative_url(url_a: str, url_b: str) -> str:
parts_b.pop(0)

# go up as many times as remaining a parts' depth
levels = len(parts_a)
if not directory_url:
levels -= 1
levels = len(parts_a) - 1
parts_relative = [".."] * levels + parts_b # noqa: WPS435 (list multiply ok)
relative = "/".join(parts_relative)
return f"{relative}#{anchor}"
Expand Down
5 changes: 5 additions & 0 deletions tests/test_references.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
("a/b/c.html", "d.html#e", "../../d.html#e"),
("a/b.html", "c/d.html#e", "../c/d.html#e"),
("a/b/index.html", "a/b/c/d.html#e", "c/d.html#e"),
("", "#x", "#x"),
('a/', "#x", "../#x"),
('a/b.html', "#x", "../#x"),
("", "a/#x", "a/#x"),
("", "a/b.html#x", "a/b.html#x"),
],
)
def test_relative_url(current_url, to_url, href_url):
Expand Down

0 comments on commit 9c9f2a0

Please sign in to comment.