Skip to content

Commit 3f81f2c

Browse files
committed
Replace pathlib.Path.relative_to with os.path.relpath
The `pathlib` method expects one path to be the sub path of the other. The `os.path` function is able to find a relative path even if they diverge, by adding `../`. Signed-off-by: Sahas Subramanian <[email protected]>
1 parent 2021740 commit 3f81f2c

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

docs/_scripts/macros.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
"""This module defines macros for use in Markdown files."""
55

6+
import os
67
import pathlib
78
from typing import Any
89

@@ -57,7 +58,11 @@ def glossary(term: str, text: str | None = None) -> str:
5758
"""
5859
current_path = pathlib.Path(env.page.file.src_uri)
5960
glossary_path = pathlib.Path("user-guide/glossary.md")
60-
link_path = glossary_path.relative_to(current_path.parent)
61+
# This needs to use `os.path.relpath` instead of `pathlib.Path.relative_to`
62+
# because the latter expects one path to be a parent of the other, which is not
63+
# always the case, for example when referencing the glossary from the API
64+
# reference.
65+
link_path = os.path.relpath(glossary_path, current_path.parent)
6166
return f"[{text or term}]({link_path}#{_slugify(term)})"
6267

6368
# The code below is a temporary workaround to make `mkdocs-macros` work with

0 commit comments

Comments
 (0)