generated from gethinode/mod-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from gethinode/develop
feat: add helper method to convert absolute paths to mounted folders
- Loading branch information
Showing
1 changed file
with
26 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 -}} |