Files
anki/docs/cogdocs.py
dea2a1f93a feat: Docs converter (#5483)
closes #5064 

Should let you quickly import to mintlify from the repos that use the
existing format.

a small problem that `DOCS_RELATIVE_LINK_REPLACEMENTS` needs to be set
manually for links between related translations but other than that
should be good.

I've run it on `en/addons` `en/faqs` `en/manual` and on `ar/manual`
`ar/faqs`. We might need to be careful to not overwrite any changes that
have been merged in this repo if there are any.

I've set it to copy only files where it can find them in the existing
mintlify docs because this allows us to more easily keep the site
structure. It does mean that there are some pages which aren't copied
and I'm going to have to look into that at some point.

But it should be good enough to look through and to import any remaining
changes left in the existing repos.

Example usage:
```
python tools/docs-converter.py [Source repo] [Target tab] [Target language code]
```

```
python tools/docs-converter.py ../anki-manual manual en
python tools/docs-converter.py ../anki-faqs faqs ar
```

---------

Co-authored-by: Abdo <abdo@abdnh.net>
Co-authored-by: Fernando Lins <1887601+fernandolins@users.noreply.github.com>
2026-09-10 15:10:22 +01:00

32 lines
1.2 KiB
Python

# Copyright: Ankitects Pty Ltd and contributors
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import re
ANKI_DOCS_URL = "https://anki.mintlify.app"
def get_file_contents(file_path):
file_path = f"docs-site/developers/{file_path}.mdx"
with open(file_path, "r", encoding="utf-8") as f:
text = "".join(f.readlines()[3:]) + "\n" # Skip the first three lines (front matter)
def rewrite_link(match):
target = match.group(1)
# Keep already-absolute links untouched.
if re.match(r"^[a-zA-Z][a-zA-Z0-9+.-]*:", target):
return f"]({target})"
# Resolve same-page anchors against the current document.
if target.startswith("#") or target.startswith("./#"):
target = target.removeprefix("./")
target = f"/developers/{file_path.split('/')[-1].removesuffix('.mdx')}{target}"
elif target.startswith("./"):
target = target[1:]
elif not target.startswith("/"):
target = f"/{target}"
return f"]({ANKI_DOCS_URL}{target})"
# Convert relative links to absolute links.
text = re.sub(r"\]\(([^)]+)\)", rewrite_link, text)
return text