fix(routing): handle redirects for nested wiki routes

Updated the route to accept nested paths for wiki titles.
Added logic to handle redirects within wiki articles, checking
for redirection messages and appropriately redirecting if the
"redirect" query parameter is not set to "no".

This ensures smoother navigation between articles, even when
dealing with paths that include slashes, which helps enhance
user experience.

Fixes #3.
This commit is contained in:
Kumi 2024-08-17 11:42:09 +02:00
parent dbb3ab0e98
commit 6f5863b746
Signed by: kumi
GPG key ID: ECBCC9082395383F

View file

@ -109,7 +109,7 @@ def search():
)
@app.route("/<project>/<lang>/wiki/<title>")
@app.route("/<project>/<lang>/wiki/<path:title>")
def wiki_article(project, lang, title):
language_projects = app.languages.get(lang, {}).get("projects", {})
base_url = language_projects.get(project)
@ -127,6 +127,17 @@ def wiki_article(project, lang, title):
soup = BeautifulSoup(article_html, "html.parser")
redirect_message = soup.find("div", class_="redirectMsg")
if redirect_message and not (request.args.get("redirect") == "no"):
redirect_dest = redirect_message.find("a")["title"]
logger.debug(f"Redirecting to {redirect_dest}")
destination = url_for(
"wiki_article", project=project, lang=lang, title=redirect_dest
)
logger.debug(f"Redirect URL: {destination}")
return redirect(destination)
for a in soup.find_all("a", href=True):
href = a["href"]