Skip to main content

Golem Documentation

Michael Bernstein

Source Links Plugin

The source_links plugin generates canonical source links for every documentation page, allowing readers to view the source file or submit edits directly on GitHub, GitLab, or custom git services.

Overview & Features

  • Automatic Git Detection: Auto-detects the remote repository URL (origin) and active default branch from local .git metadata if not explicitly specified.
  • Multi-Provider Support: Pre-configured URL schemas for GitHub and GitLab, with fully customizable formatting templates for Gitea, Bitbucket, or self-hosted servers.
  • Seamless Template Integration: Automatically enriches the Chameleon template context with ready-to-render URLs utilized by Golem's default skeleton.pt footer.

Enabling the Plugin

Add "source_links" to your plugins array in golem.toml:

golem.toml
TOML
[plugins]
plugins = [
    "source_links",
]

Or in pyproject.toml:

pyproject.toml
TOML
[tool.golem.plugins]
plugins = [
    "source_links",
]

Configuration Reference

Configure the plugin in the [plugins.source_links] table in golem.toml:

golem.toml
TOML
[plugins.source_links]
repo_url = "https://github.com/webmaven/golem"
branch = "main"
docs_dir = "docs"
provider = "github"
Configuration Options

Option

Type

Default

Description

repo_url

str

Auto-detected

Web URL of the git repository (e.g. https://github.com/owner/repo). If omitted, Golem inspects local git remotes (origin). Normalizes SSH and SCP shorthand (git@github.com:...).

branch

str

Auto-detected

Default git branch to target for viewing and editing. If omitted, Golem inspects the current HEAD or default branch, falling back to "main".

docs_dir

str

content_dir

Subdirectory within the repository where documentation files reside (e.g. "docs"). Ensures correct relative repository paths.

provider

str

Auto-detected

Git host provider: "github", "gitlab", or "custom". Automatically inferred from repo_url hostname if omitted.

edit_url_template

str

Provider default

Custom Python format string for constructing edit URLs. Available placeholder tokens: {repo_url}, {branch}, {doc_path}.

view_url_template

str

Provider default

Custom Python format string for constructing view URLs. Available placeholder tokens: {repo_url}, {branch}, {doc_path}.

Git Remote Auto-Detection

When repo_url is omitted, source_links executes git config --get remote.origin.url (falling back to direct inspection of .git/config).

It automatically converts common SSH clone URLs to HTTPS browser URLs:

  • git@github.com:webmaven/golem.git -> https://github.com/webmaven/golem
  • ssh://git@gitlab.com/group/project.git -> https://gitlab.com/group/project

Custom Git Providers

For self-hosted instances (such as Gitea or Bitbucket), set provider = "custom" and supply custom URL templates:

golem.toml
TOML
[plugins.source_links]
repo_url = "https://gitea.example.com/team/docs"
branch = "master"
docs_dir = "docs"
provider = "custom"
edit_url_template = "{repo_url}/_edit/{branch}/{doc_path}"
view_url_template = "{repo_url}/src/branch/{branch}/{doc_path}"

Template Context Variables

When active, source_links injects the following variables into every page's template rendering scope:

Variable

Type

Description

source_edit_url

str \| None

Web URL to edit the source document directly in the git provider's web editor.

source_view_url

str \| None

Web URL to view the raw source document on the git hosting platform.

source_repo_url

str \| None

Normalized root URL of the git repository.

source_provider

str

Identified git provider identifier ("github", "gitlab", or "custom").

Chameleon Template Integration

In custom themes or overrides, you can render edit buttons and repository links using Chameleon TAL:

themes/mytheme/skeleton.pt
HTML
<!-- Edit this page link -->
<div tal:condition="source_edit_url | nothing" class="edit-page-container">
    <a href="${source_edit_url}" class="edit-link" target="_blank" rel="noopener noreferrer">
        <svg class="icon-pencil" width="16" height="16" viewBox="0 0 24 24">...</svg>
        <span>Edit this page on ${source_provider.capitalize()}</span>
    </a>
</div>

<!-- Header repository link -->
<header>
    <a href="${source_repo_url}" tal:condition="source_repo_url | nothing" class="repo-badge">
        View Repository
    </a>
</header>

API Reference

class SourceLinksPlugin

PYTHON
class SourceLinksPlugin(GolemPlugin):

Golem plugin providing canonical repository view and edit links in page template contexts.

Injects source_repo_url, source_edit_url, source_view_url, source_url, and source_provider

into the Chameleon template context for each rendered document.

repo_url (str | None, optional)

Canonical repository web URL. Auto-detected from git remote if None.

branch (str | None, optional)

Git branch name to link against. Auto-detected if None.

docs_dir (str | None, optional)

Path prefix for documentation files inside the repository.

provider (str | None, optional)

Git provider ("github", "gitlab", or "custom").

edit_url_template (str | None, optional)

Custom format string for edit links.

view_url_template (str | None, optional)

Custom format string for view links.

root_dir (Path | None, optional)

Base directory used for local git repository discovery.

Attributes

name

Default value: 'source_links'

root_dir

Default value: root_dir.resolve() if root_dir else Path.cwd().resolve()

repo_url

(str | None)

Default value: normalize_repo_url(repo_url)

branch

Default value: branch

provider

Default value: provider

docs_dir

Default value: docs_dir.strip('/') if docs_dir else None

edit_url_template

Default value: edit_url_template

view_url_template

Default value: view_url_template

init

PYTHON
def __init__(self, repo_url: str | None = None, branch: str | None = None, docs_dir: str | None = None, provider: str | None = None, edit_url_template: str | None = None, view_url_template: str | None = None, root_dir: Path | None = None, **extra: Any) -> None:

from_config

PYTHON
@classmethod
def from_config(cls, config: Any = None) -> SourceLinksPlugin:

Construct a SourceLinksPlugin instance from GolemConfig or a configuration dictionary.

config (Any, optional)

GolemConfig instance or dictionary of site/plugin options.

SourceLinksPlugin

Configured plugin instance.

on_template_context

PYTHON
@hookimpl
def on_template_context(self, context: PageContext, doc_path: Path) -> PageContext:

Inject repository, file edit, and file view URLs into template context.

context (PageContext)

Chameleon template context dictionary.

doc_path (Path)

Path to the documentation source file being processed.

PageContext

Enriched template context containing source URL keys.