Skip to content

Commit 3bee325

Browse files
committed
Add partial static-path.html
The existing `static-path` shortcode already satisfies most needs of pages, but a page's inline shortcode cannot make use of the `static-path` shortcode because Hugo does not support nested shortcodes. Inline shortcodes can call partials though, so this commit creates a partial with the same functionality for use from inline shortcodes.
1 parent 4754a39 commit 3bee325

1 file changed

Lines changed: 94 additions & 0 deletions

File tree

layouts/partials/static-path.html

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
{{/*
2+
Based on this site's convention for organizing static files, returns the full
3+
relative path of a static file to the site base URL. The returned path's value
4+
depends on the current page's permalink, section, kind, and draft status.
5+
6+
This site's static files are organized in the following convention:
7+
- The files are organized by their types under different directories in the
8+
union of all static directories, like 'static/img/' for images, and
9+
'static/res/' for miscellaneous resources.
10+
- Under each type's directory are static files of that type for the section of
11+
this site whose name is the same as the directory, such as
12+
'static/img/collections/' for all images used in the 'collections' section,
13+
and 'static/res/posts/' for all resources used in the 'posts' section.
14+
15+
With respect to the basic conventions above, the exact path relative to a
16+
section's directory is determined as follows:
17+
- For each post that is not a draft: The post's date in 'YYYY-MM-DD' format,
18+
followed by a hyphen, and the last element of the post's permalink with any
19+
extension stripped. e.g. '2006/01/02/lorem-ipsum.html' ->
20+
'static/img/posts/2006-01-02-lorem-ipsum/'
21+
- For each draft post: The string 'drafts/', followed by the last element of
22+
the post's permalink with any extension stripped. e.g.
23+
'2006/01/02/lorem-ipsum.html' -> 'static/img/posts/drafts/lorem-ipsum/'
24+
- For each page in other sections: The page's permalink relative to the root of
25+
the section it is in, with any extension stripped. e.g.
26+
- 'collections/hugo/shortcode.html' ->
27+
'static/img/collections/hugo/shortcode/'
28+
- 'collections/hugo.html' -> 'static/img/collections/hugo/'
29+
30+
Finally, under every path derived from the above rules, a directory whose name
31+
is the same as a locale's language code (i.e. what '.Language.Lang' returns)
32+
can be optionally created to store localized static files for the locale. Any
33+
static files not in such a directory are unlocalized files that may be shared
34+
across all multilingual variants of the site. For example:
35+
- 'static/img/posts/2006-01-02-lorem-ipsum/en/' contains the post's images
36+
that are localized for English
37+
- 'static/img/posts/2006-01-02-lorem-ipsum/' is for images that can be shared
38+
among all locales
39+
40+
The context should be a dictionary with these key-value pairs:
41+
- page: The page whose static file path is queried
42+
- type: The static file's type, such as 'img' and 'res'
43+
- file: The static file's base name, like 'hugo.png'; optional -- if not
44+
specified, the root path to all static files of the specified type (without
45+
trailing slash) is returned instead
46+
- l10n: Optional -- if specified and truthy, then a path to the file's
47+
localized version for the current locale will be returned
48+
- abs: Optional -- if specified and truthy, then the absolute path to the file
49+
will be returned instead of the relative path
50+
*/}}
51+
52+
{{- $page := .page }}
53+
{{- $type := .type }}
54+
{{- $file := index . "file" | default "" }}
55+
{{- $l10n := index . "l10n" | default false }}
56+
{{- $abs := index . "abs" | default false }}
57+
58+
{{- with $file }}
59+
{{- $file = printf "/%s" . }}
60+
{{- end }}
61+
{{- $l10nDir := cond $l10n (printf "/%s" $page.Language.Lang) "" }}
62+
63+
{{- $staticPath := false }}
64+
65+
{{- with $page }}
66+
{{- $path := "" }}
67+
{{- $pageBaseName := path.BaseName .RelPermalink }}
68+
{{- if eq .Section "posts" }}
69+
{{- $path = printf "%s/posts/%s%s%s%s"
70+
$type
71+
(cond .Draft "drafts/" (.Date.Format "2006-01-02-"))
72+
$pageBaseName
73+
$l10nDir
74+
$file
75+
}}
76+
{{- else }}
77+
{{- /* Hugo starts .Page.RelPermalink with .Site.LanguagePrefix
78+
and may end it with '/', which can be removed by path.Clean */}}
79+
{{- $pageURLParent := replace (path.Clean .RelPermalink | path.Dir)
80+
.Site.LanguagePrefix ""
81+
1
82+
}}
83+
{{- $path = printf "%s%s/%s%s%s"
84+
$type
85+
$pageURLParent
86+
$pageBaseName
87+
$l10nDir
88+
$file
89+
}}
90+
{{- end }}
91+
{{- $staticPath = cond $abs (absURL $path) (relURL $path) }}
92+
{{- end -}}
93+
94+
{{- return $staticPath }}

0 commit comments

Comments
 (0)