Skip to content

Latest commit

 

History

History
81 lines (61 loc) · 2.74 KB

File metadata and controls

81 lines (61 loc) · 2.74 KB

GitHub Source Link

Overview

gohan exposes the GitHub URL of each article's source Markdown file so that themes can render an "Edit this page on GitHub" (or "View source") link. The feature is entirely config-driven — no Go code is required from theme authors.

Configuration

Add github_repo (and optionally github_branch) to the site block in config.yaml:

site:
  title: "My Blog"
  base_url: "https://example.com"
  github_repo: "https://github.com/owner/repo"   # required to enable the feature
  github_branch: "main"                           # optional, defaults to "main"
Field Type Default Description
github_repo string "" (disabled) Base URL of the GitHub repository
github_branch string "main" Branch to link to

When github_repo is empty the feature is effectively disabled; the {{with}} guard in the template snippet below handles this automatically.

Data Model

SiteConfig gains two new exported fields:

// SiteConfig holds site-wide metadata.
type SiteConfig struct {
    // ...existing fields...

    // GitHubRepo is the base URL of the GitHub repository hosting this site's
    // content (e.g. "https://github.com/owner/repo"). When set, templates can
    // render an "Edit this page" link using .ContentPath.
    GitHubRepo   string `yaml:"github_repo"`
    // GitHubBranch is the branch used to build the edit URL. Defaults to "main".
    GitHubBranch string `yaml:"github_branch"`
}

ProcessedArticle gains one new field:

// ProcessedArticle holds derived data generated by the renderer.
type ProcessedArticle struct {
    // ...existing fields...

    // ContentPath is the content-dir-relative path to the source Markdown file
    // (e.g. "posts/hello-world.md"). Used to generate GitHub edit/view links.
    ContentPath string
}

Template Usage

The simplest article template snippet:

{{with .Config.Site.GitHubRepo}}
<a href="{{.}}/blob/{{$.Config.Site.GitHubBranch}}/content/{{$.Article.ContentPath}}"
   target="_blank" rel="noopener noreferrer">
  Edit this page on GitHub
</a>
{{end}}

Note: The content/ prefix inside the URL path assumes the source Markdown files live in content/ relative to the repository root. Adjust the literal prefix to match your repository layout if different.

Implementation Notes

  • ContentPath is computed in internal/processor/processor_impl.go by computeContentPath, which calls filepath.Rel(contentDir, article.FilePath) and normalises the result to forward slashes via filepath.ToSlash.
  • The GitHubBranch default ("main") is applied in internal/config/config.go's applyDefaults function.
  • No additional build step or plugin is required.