Skip to content

Commit

Permalink
Merge pull request #17 from gethinode/develop
Browse files Browse the repository at this point in the history
feat: add helper method to convert absolute paths to mounted folders
  • Loading branch information
markdumay authored Feb 14, 2024
2 parents e1d6714 + 824236b commit e7b6e75
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions layouts/partials/utilities/GetTargetPath.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!--
This helper method converts paths that are relative to the repository's working directory to a Hugo mount folder.
Paths that do not start with '/' are returned as-is. The prefix of an absolute path is truncated if it matches
either with the page bundle folder (starting with "/content/"), the assets folder ("/assets/"), or the static
folder ("/static/").
The partial supports the following arguments:
"path" Required path of the resource, e.g. "/assets/img/example.jpg".
"page" Required page context, used to identify page bundle resources.
-->

{{ $path := .path -}}
{{ $page := .page -}}
{{ if hasPrefix $path "/" }}
{{ $pageContext := (path.Join "/content" $page.File.Dir) -}}
{{ if hasPrefix $path $pageContext }}
{{ $path = strings.TrimPrefix $pageContext $path -}}
{{ $path = strings.TrimPrefix "/" $path -}}
{{ else if hasPrefix $path "/assets/" }}
{{ $path = strings.TrimPrefix "/assets" $path -}}
{{ else if hasPrefix $path "/static/" }}
{{ $path = strings.TrimPrefix "/static" $path -}}
{{ end }}
{{ end }}

{{ return $path -}}

0 comments on commit e7b6e75

Please sign in to comment.