Browse Source

Fix constructing URL for static assets

pull/14710/head
Yurii Motov 6 months ago
parent
commit
44f25ad0ac
  1. 23
      scripts/doc_parsing_utils.py

23
scripts/doc_parsing_utils.py

@ -8,6 +8,7 @@ HEADER_WITH_PERMALINK_RE = re.compile(r"^(#{1,6}) (.+?)(\s*\{\s*#.*\s*\})?\s*$")
HEADER_LINE_RE = re.compile(r"^(#{1,6}) (.+?)(?:\s*\{\s*(#.*)\s*\})?\s*$") HEADER_LINE_RE = re.compile(r"^(#{1,6}) (.+?)(?:\s*\{\s*(#.*)\s*\})?\s*$")
TIANGOLO_COM = "https://fastapi.tiangolo.com" TIANGOLO_COM = "https://fastapi.tiangolo.com"
ASSETS_URL_PREFIXES = ("/img/", "/css/", "/js/")
MARKDOWN_LINK_RE = re.compile( MARKDOWN_LINK_RE = re.compile(
r"(?<!\\)(?<!\!)" # not an image ![...] and not escaped \[...] r"(?<!\\)(?<!\!)" # not an image ![...] and not escaped \[...]
@ -271,15 +272,20 @@ def extract_markdown_links(lines: list[str]) -> list[tuple[str, int]]:
return links return links
def _add_lang_code_to_url(url: str, lang_code: str) -> str:
if url.startswith(TIANGOLO_COM):
rel_url = url[len(TIANGOLO_COM) :]
if not rel_url.startswith(ASSETS_URL_PREFIXES):
url = url.replace(TIANGOLO_COM, f"{TIANGOLO_COM}/{lang_code}")
return url
def _construct_markdown_link( def _construct_markdown_link(
url: str, text: str, title: str | None, attributes: str | None, lang_code: str url: str, text: str, title: str | None, attributes: str | None, lang_code: str
) -> str: ) -> str:
""" """
Construct a markdown link, adjusting the URL for the given language code if needed. Construct a markdown link, adjusting the URL for the given language code if needed.
""" """
url = _add_lang_code_to_url(url, lang_code)
if url.startswith(TIANGOLO_COM):
url = url.replace(TIANGOLO_COM, f"{TIANGOLO_COM}/{lang_code}")
if title: if title:
link = f'[{text}]({url} "{title}")' link = f'[{text}]({url} "{title}")'
@ -404,10 +410,13 @@ def _construct_html_link(
for attribute in attributes: for attribute in attributes:
if attribute["name"] == "href": if attribute["name"] == "href":
original_url = attribute["value"] original_url = attribute["value"]
if original_url.startswith(TIANGOLO_COM):
url = original_url.replace(TIANGOLO_COM, f"{TIANGOLO_COM}/{lang_code}") url = _add_lang_code_to_url(original_url, lang_code)
else:
url = original_url # if original_url.startswith(TIANGOLO_COM):
# url = original_url.replace(TIANGOLO_COM, f"{TIANGOLO_COM}/{lang_code}")
# else:
# url = original_url
attributes_upd.append( attributes_upd.append(
HTMLLinkAttribute(name="href", quote=attribute["quote"], value=url) HTMLLinkAttribute(name="href", quote=attribute["quote"], value=url)
) )

Loading…
Cancel
Save