Skip to content

Allow URL shortening for non-gitlab.com GitLab repositories #2065

Open
@mattpitkin

Description

@mattpitkin

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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions