refactor: improve robustness of fname_to_url function

This commit is contained in:
Paul Gauthier (aider) 2024-10-21 14:23:07 -07:00
parent 949b7ece36
commit b69d131810

View file

@ -40,26 +40,42 @@ def get_package_files():
def fname_to_url(filepath): def fname_to_url(filepath):
website = "website/" website = "website"
index = "/index.md" index = "index.md"
md = ".md" md = ".md"
dump(filepath) # Convert to Path object for easier manipulation
path = Path(filepath)
docid = "" # Split the path into parts
if filepath.startswith("website/_includes/"): parts = path.parts
pass
elif filepath.startswith(website):
docid = filepath[len(website) :]
if filepath.endswith(index): # Find the 'website' part in the path
filepath = filepath[: -len(index)] + "/" try:
elif filepath.endswith(md): website_index = parts.index(website)
filepath = filepath[: -len(md)] + ".html" except ValueError:
return "" # 'website' not found in the path
docid = "https://aider.chat/" + filepath # Extract the part of the path starting from 'website'
relevant_parts = parts[website_index + 1:]
return docid # Handle _includes directory
if relevant_parts and relevant_parts[0] == "_includes":
return ""
# Join the remaining parts
url_path = '/'.join(relevant_parts)
# Handle index.md and other .md files
if url_path.endswith(index):
url_path = url_path[:-len(index)]
elif url_path.endswith(md):
url_path = url_path[:-len(md)] + ".html"
# Ensure the URL starts and ends with '/'
url_path = url_path.strip('/')
return f"https://aider.chat/{url_path}"
def get_index(): def get_index():