Skip to content

Commit bb0928e

Browse files
committed
fix: resolve internal links against baseURL for non-root deployments
Several internal links (e.g. in navbar.html, footer.html, cards/project.html) used plain {{ .url }} without baseURL handling. This only worked when baseURL had no subpath; it breaks on any non-root deployment, e.g. some site served from "person.dev/me/" rather than the domain root "person.dev". The problematic call sites now distinguish external (http/https) URLs (left untouched) from internal ones, which are now resolved via a new `helpers/get-page-url.html` partial. `helpers/get-section-url.html` is refactored to use the same helper instead of inlining `site.BaseURL | relLangURL` directly. The brand, home, blog, and notes links in navbar.html, which already inlined this same `path.Join (site.BaseURL | relLangURL) ...` pattern ad hoc, are likewise refactored onto the shared helper, so there is now a single place that resolves internal links against baseURL.
1 parent 3eaca38 commit bb0928e

5 files changed

Lines changed: 28 additions & 12 deletions

File tree

layouts/partials/cards/project.html

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@
44
{{- $target := .target | default "blank" -}}
55
{{- $targetAttr := "_blank" -}}
66
{{- if eq $target "self" -}}{{- $targetAttr = "_self" -}}{{- end -}}
7-
<a href="{{ if .repo }}{{ .repo }}{{ else if .url }}{{ .url }}{{ else }}javascript:void(0){{ end }}" {{ if or
8-
.repo .url }}target="{{ $targetAttr }}" rel="noopener" {{ end }}>
7+
{{/* Process internal links (not http[s]://) */}}
8+
{{- $projectURL := .url -}}
9+
{{- if and $projectURL (not (or (hasPrefix $projectURL "http://") (hasPrefix $projectURL "https://"))) -}}
10+
{{- $projectURL = partial "helpers/get-page-url.html" $projectURL -}}
11+
{{- end -}}
12+
<a href="{{ if .repo }}{{ .repo }}{{ else if $projectURL }}{{ $projectURL }}{{ else }}javascript:void(0){{ end }}" {{ if or
13+
.repo $projectURL }}target="{{ $targetAttr }}" rel="noopener" {{ end }}>
914
{{ if .image }}
1015
<div class="card-head">
1116
{{ $imageImage:= resources.Get .image}}
@@ -80,9 +85,9 @@ <h5 class="card-title mb-0">{{ .name }}</h5>
8085
<!-- Place this tag where you want the button to render. -->
8186
<a class="github-button project-btn d-none" href="{{ .repo }}" data-icon="octicon-standard"
8287
data-show-count="true" aria-label="Star {{ .name }}">{{ i18n "project_star" }}</a>
83-
{{ else if .url }}
88+
{{ else if $projectURL }}
8489
<span>
85-
<a class="btn btn-outline-info btn-sm" href="{{ .url }}" target="{{ $targetAttr }}" rel="noopener">{{ i18n "project_details" }}</a>
90+
<a class="btn btn-outline-info btn-sm" href="{{ $projectURL }}" target="{{ $targetAttr }}" rel="noopener">{{ i18n "project_details" }}</a>
8691
</span>
8792
{{ end }}
8893
</div>

layouts/partials/footer.html

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,13 @@ <h5>{{ i18n "navigation" }}</h5>
8686
{{ if $customMenusEnabled }}
8787
{{ range $customMenus }}
8888
{{ if .showOnFooter }}
89+
{{ $menuURL := .url }}
90+
{{/* Process internal links (not http[s]://) */}}
91+
{{ if not (or (hasPrefix $menuURL "http://") (hasPrefix $menuURL "https://")) }}
92+
{{ $menuURL = partial "helpers/get-page-url.html" $menuURL }}
93+
{{ end }}
8994
<li class="nav-item">
90-
<a class="smooth-scroll" href="{{ .url }}">{{ .name }}</a>
95+
<a class="smooth-scroll" href="{{ $menuURL }}">{{ .name }}</a>
9196
</li>
9297
{{ end }}
9398
{{ end }}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{- path.Join (site.BaseURL | relLangURL) (strings.Trim . "/") -}}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{{- if page.IsHome -}}
22
#{{ partial "helpers/get-section-id.html" . }}
33
{{- else -}}
4-
{{ site.BaseURL | relLangURL }}#{{ partial "helpers/get-section-id.html" . }}
5-
{{- end -}}
4+
{{ partial "helpers/get-page-url.html" "" }}#{{ partial "helpers/get-section-id.html" . }}
5+
{{- end -}}

layouts/partials/navigators/navbar.html

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
<i data-feather="sidebar"></i>
8080
</button>
8181
{{ end }}
82-
<a class="navbar-brand" href="{{ site.BaseURL | relLangURL }}">
82+
<a class="navbar-brand" href="{{ partial "helpers/get-page-url.html" "" }}">
8383
{{ if $logo }}
8484
<img src="{{ $logo }}" id="logo" alt="Logo">
8585
{{ end }}
@@ -99,7 +99,7 @@
9999
<div class="collapse navbar-collapse dynamic-navbar" id="top-nav-items">
100100
<ul class="nav navbar-nav ms-auto">
101101
<li class="nav-item">
102-
<a class="nav-link" href="{{ if .IsHome }}#home{{else}}{{ site.BaseURL | relLangURL }}#home{{end}}">{{ i18n "home" }}</a>
102+
<a class="nav-link" href="{{ if .IsHome }}#home{{else}}{{ partial "helpers/get-page-url.html" "" }}#home{{end}}">{{ i18n "home" }}</a>
103103
</li>
104104
{{ if $sections }}
105105
{{ $sectionCount := 1 }}
@@ -136,18 +136,23 @@
136136
{{ end }}
137137
{{ if $blogEnabled }}
138138
<li class="nav-item">
139-
<a class="nav-link" id="blog-link" href="{{ path.Join (site.BaseURL | relLangURL) $blogSection }}">{{ $blogTitle }}</a>
139+
<a class="nav-link" id="blog-link" href="{{ partial "helpers/get-page-url.html" $blogSection }}">{{ $blogTitle }}</a>
140140
</li>
141141
{{ end }}
142142
{{ if $notesEnabled }}
143143
<li class="nav-item">
144-
<a class="nav-link" id="note-link" href="{{ path.Join (site.BaseURL | relLangURL) "notes" }}">{{ $notesTitle }}</a>
144+
<a class="nav-link" id="note-link" href="{{ partial "helpers/get-page-url.html" "notes" }}">{{ $notesTitle }}</a>
145145
</li>
146146
{{ end }}
147147
{{ range $customMenus }}
148148
{{ if (not .hideFromNavbar) }}
149+
{{ $menuURL := .url }}
150+
{{/* Process internal links (not http[s]://) */}}
151+
{{ if not (or (hasPrefix $menuURL "http://") (hasPrefix $menuURL "https://")) }}
152+
{{ $menuURL = partial "helpers/get-page-url.html" $menuURL }}
153+
{{ end }}
149154
<li class="nav-item">
150-
<a class="nav-link" href="{{ .url }}">{{ .name }}</a>
155+
<a class="nav-link" href="{{ $menuURL }}">{{ .name }}</a>
151156
</li>
152157
{{ end }}
153158
{{ end }}

0 commit comments

Comments
 (0)