CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-pydata-sphinx-theme

Bootstrap-based Sphinx theme from the PyData community

Pending
Overview
Eval results
Files

link-processing.mddocs/

Link Processing

Transform system for automatically shortening and styling GitHub and GitLab repository links in documentation, making them more readable and visually consistent.

Capabilities

Link Shortening Transform

Transforms GitHub and GitLab links to shorter, more readable formats.

class ShortenLinkTransform(SphinxPostTransform):
    """
    Transform to shorten GitHub/GitLab links.
    
    Automatically processes external links to GitHub and GitLab repositories,
    converting full URLs to shortened, readable formats. Only transforms links
    where the display text matches the URL (meaning the user hasn't customized
    the link text).
    
    Attributes:
    - default_priority (int): Transform priority (400)
    - formats (tuple): Supported output formats ('html',)
    - supported_platform (ClassVar[dict]): Mapping of domains to platform names
    - platform (str): Current platform being processed
    """
    
    def run(self, **kwargs):
        """
        Run the Transform object.
        
        Processes all reference nodes in the document, checking for GitHub
        and GitLab URLs that can be shortened. Only modifies links where
        the display text equals the URL.
        
        Parameters:
        - **kwargs: Additional arguments (unused)
        """
    
    def parse_url(self, uri: ParseResult) -> str:
        """
        Parse the content of the url with respect to the selected platform.
        
        Converts full repository URLs to shortened formats based on the
        platform (GitHub or GitLab) and the type of content being linked.
        
        Parameters:
        - uri (ParseResult): Parsed URL components
        
        Returns:
        str: Shortened, formatted URL text
        """

Supported Platforms

GitHub Links

  • Domain: github.com
  • CSS Class: github

GitLab Links

  • Domain: gitlab.com
  • CSS Class: gitlab

Link Transformations

GitHub Examples

Repository Links:

https://github.com/pydata/pydata-sphinx-theme
→ pydata/pydata-sphinx-theme

Issue Links:

https://github.com/pydata/pydata-sphinx-theme/issues/123
→ pydata/pydata-sphinx-theme#123

Pull Request Links:

https://github.com/pydata/pydata-sphinx-theme/pull/456
→ pydata/pydata-sphinx-theme#456

Discussion Links:

https://github.com/pydata/pydata-sphinx-theme/discussions/789
→ pydata/pydata-sphinx-theme#789

Project Board Links:

https://github.com/orgs/pydata/projects/5
→ pydata/projects#5

GitLab Examples

Repository Links:

https://gitlab.com/group/subgroup/project
→ group/subgroup/project

Issue Links:

https://gitlab.com/group/project/-/issues/123
→ group/project#123

Merge Request Links:

https://gitlab.com/group/project/-/merge_requests/456
→ group/project!456

Complex Group Structure:

https://gitlab.com/group/subgroup1/subgroup2/project/-/issues/789
→ group/subgroup1/subgroup2/project#789

Usage Examples

Automatic Processing

Links are automatically processed when they appear in reStructuredText:

See the issue at https://github.com/pydata/pydata-sphinx-theme/issues/123 for details.

Check out this merge request: https://gitlab.com/mygroup/myproject/-/merge_requests/45

Results in:

<a class="reference external github" href="https://github.com/pydata/pydata-sphinx-theme/issues/123">
    pydata/pydata-sphinx-theme#123
</a>

<a class="reference external gitlab" href="https://gitlab.com/mygroup/myproject/-/merge_requests/45">
    mygroup/myproject!45  
</a>

Custom Link Text (No Processing)

Links with custom text are left unchanged:

`Custom link text <https://github.com/pydata/pydata-sphinx-theme/issues/123>`_

Results in:

<a class="reference external" href="https://github.com/pydata/pydata-sphinx-theme/issues/123">
    Custom link text
</a>

Platform Detection

Links to different platforms:

- https://github.com/user/repo → github class added
- https://gitlab.com/user/repo → gitlab class added  
- https://bitbucket.org/user/repo → no processing (not supported)

CSS Styling

The transform adds CSS classes for styling:

/* Example custom styling */
a.github {
    color: #24292f;
    font-weight: 500;
}

a.github::before {
    content: "🐙 ";
}

a.gitlab {
    color: #fc6d26;
    font-weight: 500; 
}

a.gitlab::before {
    content: "🦊 ";
}

/* Dark theme variants */
html[data-theme="dark"] a.github {
    color: #f0f6fc;
}

html[data-theme="dark"] a.gitlab {
    color: #fca326;
}

Advanced Configuration

Custom Platform Support

While the transform only supports GitHub and GitLab by default, you can extend it:

# Custom extension (hypothetical)
from pydata_sphinx_theme.short_link import ShortenLinkTransform

class CustomShortenLinkTransform(ShortenLinkTransform):
    supported_platform = {
        "github.com": "github",
        "gitlab.com": "gitlab", 
        "bitbucket.org": "bitbucket",  # Add Bitbucket support
        "codeberg.org": "codeberg",    # Add Codeberg support
    }
    
    def parse_url(self, uri):
        if self.platform == "bitbucket":
            # Custom Bitbucket parsing logic
            pass
        elif self.platform == "codeberg": 
            # Custom Codeberg parsing logic
            pass
        else:
            return super().parse_url(uri)

Disable Link Processing

If you want to disable automatic link shortening:

# conf.py - Remove the transform (advanced)
def setup(app):
    # Remove the post-transform if already added
    transforms = app.registry.get_post_transforms()
    # Custom logic to remove ShortenLinkTransform
    pass

Integration with Other Extensions

The link processing works well with other Sphinx extensions:

With sphinx.ext.extlinks

# conf.py
extlinks = {
    'gh': ('https://github.com/%s', 'GitHub: %s'),
    'gl': ('https://gitlab.com/%s', 'GitLab: %s'),
}
Use :gh:`pydata/pydata-sphinx-theme/issues/123` for short links.

Or use the full URL https://github.com/pydata/pydata-sphinx-theme/issues/123 for automatic processing.

With Link Checking

# conf.py
extensions = [
    'sphinx.ext.linkcheck',
    # other extensions
]

# Link checking works normally with shortened links
linkcheck_ignore = [
    r'https://github\.com/.*/issues/\d+',  # Skip checking GitHub issues
]

Technical Details

Transform Priority

The transform runs with priority 400 in the post-transform phase, after most content processing but before final HTML generation.

URL Parsing

The transform uses Python's urllib.parse.urlparse() for robust URL parsing and reconstruction.

Node Processing

The transform uses the theme's compatibility function traverse_or_findall() to work with different docutils versions.

Performance

  • Minimal overhead: Only processes external reference nodes
  • Smart detection: Only transforms links where display text matches URL
  • CSS classes: Adds styling hooks without requiring JavaScript

Install with Tessl CLI

npx tessl i tessl/pypi-pydata-sphinx-theme

docs

edit-page.md

html-translation.md

index.md

link-processing.md

logo-management.md

syntax-highlighting.md

template-management.md

theme-setup.md

toc-processing.md

utilities.md

tile.json