Files
doc-forge/tests/cli/test_mkdocs_links.py
Vishesh 'ironeagle' Bangotra 09118115a4
All checks were successful
continuous-integration/drone/tag Build is passing
fix(mkdocs): rebase cross-kind wiki links under directory URLs
Wiki pages linking to a sibling kind (for example ../lib/...) were emitted verbatim by MkDocs and, with use_directory_urls, resolved into a non-existent <kind>/<kind>/ path. Add a docforge-linkfix plugin (doc-forge 0.0.8) that rebases those links to the repository root.
2026-10-17 17:22:23 +05:30

56 lines
1.5 KiB
Python

from types import SimpleNamespace
from docforge.mkdocs_links import LinkFixPlugin
def _page(dest_uri: str):
return SimpleNamespace(file=SimpleNamespace(dest_uri=dest_uri))
def _fix(markdown: str, dest_uri: str) -> str:
return LinkFixPlugin().on_page_markdown(
markdown,
page=_page(dest_uri),
config=SimpleNamespace(),
files=SimpleNamespace(),
)
def test_root_page_keeps_single_parent():
src = "[lib](../lib/pkg/index.md)"
assert _fix(src, "index.html") == src
def test_nested_page_adds_directory_depth():
assert _fix("[lib](../lib/pkg/index.md)", "01_overview/index.html") == (
"[lib](../../lib/pkg/index.md)"
)
def test_deeply_nested_page_adds_two_levels():
src = "[lib](../lib/pkg/index.md)"
got = _fix(src, "04_development/02_releasing/index.html")
assert got == "[lib](../../../lib/pkg/index.md)"
def test_multiple_cross_kind_links_are_rebased():
src = "See [a](../lib/a) and [b](../api/b)."
got = _fix(src, "01_overview/index.html")
assert got == "See [a](../../lib/a) and [b](../../api/b)."
def test_wiki_kind_is_rebased():
assert _fix("[w](../wiki/index.md)", "01_overview/index.html") == (
"[w](../../wiki/index.md)"
)
def test_relative_within_kind_untouched():
src = "[next](02_install.md) and [prev](../index.md)"
assert _fix(src, "01_overview/index.html") == src
def test_absolute_and_url_links_untouched():
src = "[site](https://example.com/lib/x) and [root](/lib/x) and [r](lib/x)"
assert _fix(src, "a/index.html") == src