diff --git a/api/services/wiki/content.py b/api/services/wiki/content.py index 75c90b91a..df1da7102 100644 --- a/api/services/wiki/content.py +++ b/api/services/wiki/content.py @@ -9,6 +9,7 @@ import re from dataclasses import dataclass +from urllib.parse import quote @dataclass @@ -20,6 +21,19 @@ class RepoUrlContext: default_branch: str +def _quote_git_ref(value: str) -> str: + """Percent-encode a branch or repo-relative path for a URL path. + + A CommonMark link destination written as ``](url)`` ends at the first + space or ``)``. Those characters are normal in repository paths + (``docs/My Notes.md``, Next.js ``src/app/(marketing)/page.tsx``), so the + raw path cuts the link off before the file name. Preserve existing + percent escapes while encoding stray percent signs. + """ + quoted = quote(value, safe="/%") + return re.sub(r"%(?![0-9A-Fa-f]{2})", "%25", quoted) + + def generate_file_url(file_path: str, ctx: RepoUrlContext) -> str: """Build a host-specific web URL for a repository-relative file path. @@ -27,12 +41,14 @@ def generate_file_url(file_path: str, ctx: RepoUrlContext) -> str: """ if ctx.type == "local" or not ctx.repo_url: return file_path + path = _quote_git_ref(file_path) + branch = _quote_git_ref(ctx.default_branch) if ctx.type == "github": - return f"{ctx.repo_url}/blob/{ctx.default_branch}/{file_path}" + return f"{ctx.repo_url}/blob/{branch}/{path}" if ctx.type == "gitlab": - return f"{ctx.repo_url}/-/blob/{ctx.default_branch}/{file_path}" + return f"{ctx.repo_url}/-/blob/{branch}/{path}" if ctx.type == "bitbucket": - return f"{ctx.repo_url}/src/{ctx.default_branch}/{file_path}" + return f"{ctx.repo_url}/src/{branch}/{path}" return file_path diff --git a/tests/backend/services/test_wiki_content.py b/tests/backend/services/test_wiki_content.py index 33f92183b..0241f61ab 100644 --- a/tests/backend/services/test_wiki_content.py +++ b/tests/backend/services/test_wiki_content.py @@ -1,3 +1,5 @@ +import re + from api.services.wiki.content import ( RepoUrlContext, generate_file_url, @@ -98,3 +100,57 @@ def test_single_line_citation(): def test_whole_file_citation_no_lines(): out = post_process_wiki_content("[README.md]()", ["README.md"], GITHUB) assert f"[README.md]({BASE}/README.md)" in out + + +def _link_destinations(markdown: str) -> list[str]: + """CommonMark destinations in `](url)`. A raw space or `)` ends the URL.""" + return re.findall(r"\]\(([^)\s]*)\)", markdown) + + +def test_route_group_parentheses_stay_inside_the_link(): + # Next.js route groups are directories named "(marketing)". A raw ")" ends + # the Markdown destination, so the link never reaches page.tsx. + path = "src/app/(marketing)/page.tsx" + out = post_process_wiki_content(f"[{path}:10]()", [path], GITHUB) + encoded = "src/app/%28marketing%29/page.tsx" + assert f"[{path}:10]({BASE}/{encoded}#L10)" in out + assert all("(" not in dest and ")" not in dest for dest in _link_destinations(out)) + assert any(dest.endswith(f"/{encoded}#L10") for dest in _link_destinations(out)) + + +def test_space_in_file_name_is_percent_encoded(): + path = "docs/My Notes.md" + out = post_process_wiki_content(f"See [{path}]().", [path], GITHUB) + encoded = "docs/My%20Notes.md" + assert f"[{path}]({BASE}/{encoded})" in out + assert all(" " not in dest for dest in _link_destinations(out)) + + +def test_web_hosts_encode_parentheses_without_changing_link_text(): + path = "src/app/(marketing)/page.tsx" + encoded = "src/app/%28marketing%29/page.tsx" + contexts = ( + RepoUrlContext("github", "https://github.com/o/r", "main"), + RepoUrlContext("gitlab", "https://gitlab.com/o/r", "main"), + RepoUrlContext("bitbucket", "https://bitbucket.org/o/r", "main"), + ) + + for ctx in contexts: + out = post_process_wiki_content(f"[{path}]()", [path], ctx) + assert f"[{path}](" in out + assert f"/{encoded})" in out + + +def test_already_encoded_path_is_not_double_encoded(): + path = "docs/My%20Notes%20%28draft%29.md" + assert generate_file_url(path, GITHUB) == f"{BASE}/{path}" + + +def test_stray_percent_sign_is_encoded(): + assert generate_file_url("docs/100%.md", GITHUB) == f"{BASE}/docs/100%25.md" + + +def test_local_paths_are_not_percent_encoded(): + ctx = RepoUrlContext("local", None, "main") + path = "src/app/(marketing)/page.tsx" + assert generate_file_url(path, ctx) == path