Skip to content

Commit 03d7e5a

Browse files
Add hyphenated folder handling for edit page links
- api-reference: folder keeps hyphens, files use underscores - api-routes, custom-components, wrapping-react: both keep hyphens - Maintains existing double slash fix and standard dash-to-underscore conversion - All 18 test cases pass including edge cases - Validation script confirms all 245 URLs still return 200 OK Co-Authored-By: [email protected] <[email protected]>
1 parent aeff101 commit 03d7e5a

File tree

2 files changed

+124
-4
lines changed

2 files changed

+124
-4
lines changed

pcweb/templates/docpage/docpage.py

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -287,14 +287,41 @@ def convert_url_path_to_github_path(url_path) -> str:
287287
from reflex.vars.sequence import string_replace_operation
288288

289289
path_no_slashes = string_replace_operation(url_path, r"^/+|/+$", "")
290-
path_with_underscores = string_replace_operation(path_no_slashes, "-", "_")
291-
path_clean = string_replace_operation(path_with_underscores, r"^/+", "")
292-
return f"{path_clean}.md"
290+
path_clean = string_replace_operation(path_no_slashes, r"^/+", "")
291+
292+
api_ref_condition = string_replace_operation(path_clean, r"^docs/api-reference/", "")
293+
path_with_api_ref = rx.cond(
294+
path_clean != api_ref_condition, # If replacement happened, we're in api-reference
295+
f"docs/api-reference/{string_replace_operation(api_ref_condition, '-', '_')}",
296+
path_clean
297+
)
298+
299+
hyphenated_folders = ["api-routes", "custom-components", "wrapping-react"]
300+
final_path = path_with_api_ref
301+
for folder in hyphenated_folders:
302+
folder_condition = string_replace_operation(final_path, f"^docs/{folder}/", "")
303+
final_path = rx.cond(
304+
final_path != folder_condition, # If replacement happened, we're in this hyphenated folder
305+
f"docs/{folder}/{folder_condition}", # Keep hyphens as-is
306+
string_replace_operation(final_path, "-", "_") # Convert dashes to underscores for other paths
307+
)
308+
309+
return f"{final_path}.md"
293310
else:
294311
path = str(url_path).strip("/")
295312
while "//" in path:
296313
path = path.replace("//", "/")
297-
path = path.replace("-", "_")
314+
315+
if path.startswith("docs/api-reference/"):
316+
parts = path.split("/")
317+
if len(parts) >= 3:
318+
file_part = "/".join(parts[2:]).replace("-", "_")
319+
path = f"docs/api-reference/{file_part}"
320+
elif any(path.startswith(f"docs/{folder}/") for folder in ["api-routes", "custom-components", "wrapping-react"]):
321+
pass
322+
else:
323+
path = path.replace("-", "_")
324+
298325
if not path.endswith(".md"):
299326
path += ".md"
300327
return path

scripts/test_hyphenated_folders.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Test script to verify the hyphenated folder handling in convert_url_path_to_github_path function.
4+
Tests the special cases for api-reference, api-routes, custom-components, and wrapping-react folders.
5+
"""
6+
7+
import sys
8+
sys.path.append('.')
9+
10+
def test_hyphenated_folders():
11+
"""Test that hyphenated folder handling works correctly."""
12+
try:
13+
from pcweb.templates.docpage.docpage import convert_url_path_to_github_path
14+
15+
test_cases = [
16+
# api-reference: folder keeps hyphens, files use underscores
17+
("/docs/api-reference/browser-javascript/", "docs/api-reference/browser_javascript.md"),
18+
("/docs/api-reference/event-triggers/", "docs/api-reference/event_triggers.md"),
19+
("/docs/api-reference/var-system/", "docs/api-reference/var_system.md"),
20+
("/docs/api-reference/cli/", "docs/api-reference/cli.md"),
21+
22+
# api-routes: both folder and files keep hyphens
23+
("/docs/api-routes/overview/", "docs/api-routes/overview.md"),
24+
25+
# custom-components: both folder and files keep hyphens
26+
("/docs/custom-components/overview/", "docs/custom-components/overview.md"),
27+
("/docs/custom-components/command-reference/", "docs/custom-components/command-reference.md"),
28+
("/docs/custom-components/prerequisites-for-publishing/", "docs/custom-components/prerequisites-for-publishing.md"),
29+
30+
# wrapping-react: both folder and files keep hyphens
31+
("/docs/wrapping-react/overview/", "docs/wrapping-react/overview.md"),
32+
("/docs/wrapping-react/custom-code-and-hooks/", "docs/wrapping-react/custom-code-and-hooks.md"),
33+
("/docs/wrapping-react/local-packages/", "docs/wrapping-react/local-packages.md"),
34+
("/docs/wrapping-react/step-by-step/", "docs/wrapping-react/step-by-step.md"),
35+
36+
# Standard folders: should convert dashes to underscores
37+
("/docs/getting-started/introduction/", "docs/getting_started/introduction.md"),
38+
("/docs/vars/var-operations/", "docs/vars/var_operations.md"),
39+
("/docs/library/forms/button/", "docs/library/forms/button.md"),
40+
41+
# Edge cases with double slashes
42+
("//docs/api-reference/browser-javascript/", "docs/api-reference/browser_javascript.md"),
43+
("/docs//custom-components/overview/", "docs/custom-components/overview.md"),
44+
("/docs/getting-started//introduction/", "docs/getting_started/introduction.md"),
45+
]
46+
47+
print("Testing hyphenated folder handling:")
48+
print("=" * 80)
49+
50+
all_passed = True
51+
52+
for i, (input_path, expected_output) in enumerate(test_cases, 1):
53+
print(f"\nTest {i:2d}: '{input_path}'")
54+
55+
actual_output = convert_url_path_to_github_path(input_path)
56+
print(f"Expected: '{expected_output}'")
57+
print(f"Actual: '{actual_output}'")
58+
59+
full_url = f"https://github.com/reflex-dev/reflex-web/blob/main/{actual_output}"
60+
print(f"Full URL: '{full_url}'")
61+
62+
has_double_slash = "//" in full_url.replace("https://", "")
63+
matches_expected = actual_output == expected_output
64+
65+
if matches_expected and not has_double_slash:
66+
print("✅ PASS")
67+
else:
68+
print("❌ FAIL")
69+
if not matches_expected:
70+
print(f" Expected '{expected_output}' but got '{actual_output}'")
71+
if has_double_slash:
72+
print(f" URL contains double slash: '{full_url}'")
73+
all_passed = False
74+
75+
print("-" * 60)
76+
77+
print("\n" + "=" * 80)
78+
if all_passed:
79+
print("🎉 SUCCESS: All hyphenated folder tests passed!")
80+
else:
81+
print("❌ FAILURE: Some hyphenated folder tests failed.")
82+
83+
return all_passed
84+
85+
except Exception as e:
86+
print(f"❌ Error in hyphenated folder test: {e}")
87+
import traceback
88+
traceback.print_exc()
89+
return False
90+
91+
if __name__ == "__main__":
92+
success = test_hyphenated_folders()
93+
exit(0 if success else 1)

0 commit comments

Comments
 (0)