From 5acc2e8f9e1b53352d45c635ed4c489391f7e99f Mon Sep 17 00:00:00 2001 From: Mark Dumay <61946753+markdumay@users.noreply.github.com> Date: Fri, 16 Aug 2024 19:05:07 +0200 Subject: [PATCH 1/2] Support links to static files and page resources --- layouts/partials/assets/link.html | 19 ++++++++++++++++--- layouts/shortcodes/link.html | 10 ++++++++-- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/layouts/partials/assets/link.html b/layouts/partials/assets/link.html index 6f92c3db9..69c275987 100644 --- a/layouts/partials/assets/link.html +++ b/layouts/partials/assets/link.html @@ -59,9 +59,22 @@ {{ if not $isLocal }} {{ $ref := partial "utilities/GetPage.html" (dict "url" $destination "page" $page) }} {{- if not $ref -}} - {{- errorf "partial [assets/link.html] - Cannot find page: %s" $destination -}} - {{- $error = true -}} - {{- else -}} + {{ $pageContext := "" }} + {{ with $page.File }}{{ $pageContext = path.Join "/content" .Dir }}{{ end }} + {{ $isPageRes := fileExists (path.Join $pageContext $destination) }} + {{ $isStatic := fileExists (path.Join "static" $destination) }} + {{ if and (not $isPageRes) (not $isStatic) }} + {{- errorf "partial [assets/link.html] - Cannot find page or asset: '%s' at '%s'" $destination $page.File.Path -}} + {{- $error = true -}} + {{ else if $isPageRes }} + {{ $destination = urls.JoinPath "/" (strings.TrimPrefix "/content" $pageContext) $destination }} + {{ else if $isStatic }} + {{ $destination = urls.JoinPath "/" $destination }} + {{ end }} + {{- with $anchor }}{{ $destination = printf "%s#%s" (strings.TrimSuffix "/" $destination) . -}}{{ end -}} + {{ end }} + + {{- if and $ref not $error -}} {{- $destination = $ref.RelPermalink -}} {{- with $anchor }}{{ $destination = printf "%s#%s" (strings.TrimSuffix "/" $destination) . -}}{{ end -}} {{- end -}} diff --git a/layouts/shortcodes/link.html b/layouts/shortcodes/link.html index 91e5ad70a..74a93a07e 100644 --- a/layouts/shortcodes/link.html +++ b/layouts/shortcodes/link.html @@ -80,8 +80,14 @@ {{- if not $isExternal -}} {{ $ref := partial "utilities/GetPage.html" (dict "url" $url "page" .Page) }} {{- if not $ref -}} - {{- errorf "Cannot find page: '%s' at %s" $url .Position -}} - {{- $error = true -}} + {{ $pageContext := "" }} + {{ with .Page.File }}{{ $pageContext = path.Join "/content" .Dir }}{{ end }} + {{ $isPageRes := fileExists (path.Join $pageContext $url) }} + {{ $isStatic := fileExists (path.Join "/static" $url) }} + {{ if and (not $isPageRes) (not $isStatic) }} + {{- errorf "Cannot find page or asset: '%s' at %s" $url .Position -}} + {{- $error = true -}} + {{ end }} {{- end -}} {{- end -}} From 3ef857dba952166c769bf137d52db376a14ea967 Mon Sep 17 00:00:00 2001 From: Mark Dumay <61946753+markdumay@users.noreply.github.com> Date: Sat, 17 Aug 2024 08:00:03 +0200 Subject: [PATCH 2/2] Fix link error --- layouts/partials/assets/link.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/layouts/partials/assets/link.html b/layouts/partials/assets/link.html index 69c275987..d44a9ae25 100644 --- a/layouts/partials/assets/link.html +++ b/layouts/partials/assets/link.html @@ -74,7 +74,7 @@ {{- with $anchor }}{{ $destination = printf "%s#%s" (strings.TrimSuffix "/" $destination) . -}}{{ end -}} {{ end }} - {{- if and $ref not $error -}} + {{- if and $ref (not $error) -}} {{- $destination = $ref.RelPermalink -}} {{- with $anchor }}{{ $destination = printf "%s#%s" (strings.TrimSuffix "/" $destination) . -}}{{ end -}} {{- end -}}