pydata-sphinx-theme icon indicating copy to clipboard operation
pydata-sphinx-theme copied to clipboard

Allow URL shortening for non-gitlab.com GitLab repositories

Open mattpitkin opened this issue 11 months ago • 2 comments

Currently, the ShortLinkTransform class is hardcoded to work for URLs in the github.com or gitlab.com domains. It would be nice if it would also work with non-gitlab.com domained GitLab repositories, if you have specified a gitlab_url in the Sphinx conf.py html_context dictionary, e.g.

html_context = {
    "gitlab_url": "https://gitlab.mydomain.com/",
    ...
}

I've created my own hacked version of the pydata-sphinx-theme, by editing the setup() function to contain:

    if hasattr(app.config, "html_context"):
        gitlab_url = app.config.html_context.get("gitlab_url", "")

        if gitlab_url.startswith("https://"):
            gitlab_url = {gitlab_url[8:].rstrip("/"): "gitlab"}
        elif gitlab_url.startswith("http://"):
            gitlab_url = {gitlab_url[7:].rstrip("/"): "gitlab"}
        else:
            gitlab_url = {}

        class ShortenLinkTransformCustom(short_link.ShortenLinkTransform):
            supported_platform = short_link.ShortenLinkTransform.supported_platform
            supported_platform.update(gitlab_url)

        app.add_post_transform(ShortenLinkTransformCustom)
    else:
        app.add_post_transform(short_link.ShortenLinkTransform)

where I've created a new locally scoped class that add the required URL into the supported_platform class attribute from the ShortenLinkTransform class. This is not particularly elegant, and there's probably a better way, but if this is considered useful I can open a PR with the changes (and some added documentation).

mattpitkin avatar Dec 03 '24 14:12 mattpitkin