Fix conditional rendering for post metadata

You would think this is something like this is already in the theme but
it turns out it isn't. This should fix pages that are primarily
non-content (i.e., about pages, contact).
This commit is contained in:
Gabriel Arazas 2023-02-27 16:46:11 +08:00
parent 11c0a312d9
commit 044a9482a7
2 changed files with 67 additions and 64 deletions

View File

@ -23,38 +23,40 @@
</article>
</main>
<hr aria-hidden="true"/>
<div class="post__meta--single" aria-hidden="true">
<!-- List the author(s) of the article. -->
{{- with .Params.author }}
{{- $names := slice }}
{{- range . }}{{ $names = $names | append .name }}{{ end }}
<p data-content-author="{{ jsonify $names }}">{{ i18n "published_by" }}: {{ delimit $names ", " (printf " %s " (i18n "and")) }}</p>
{{- end }}
<!-- Only show the creation date if the `date` field is valid. -->
{{- if ne (.PublishDate.Format "2006") "0001" -}}
<!-- Publication date. -->
<p data-content-publication-date="{{ .PublishDate.Format "2006-01-02T15:04:05Z07:00" }}">{{ i18n "published_on" }}: <time datetime="{{ .PublishDate.Format "2006-01-02T15:04:05Z07:00" }}">{{ .PublishDate.Format "2006-01-02" }}</time></p>
{{- end }}
<!-- Modification date (will only appear if the publication date is less than the modified date.) -->
{{- if ne .PublishDate .Lastmod }}
<p data-content-modification-date="{{ .Lastmod.Format "2006-01-02T15:04:05Z07:00" }}">{{ i18n "updated_on" }}: <time datetime="{{ .Lastmod.Format "2006-01-02T15:04:05Z07:00" }}">{{ .Lastmod.Format "2006-01-02" }}</time></p>
{{- end }}
<!-- Link the translated versions if the article has at least one translated page. -->
{{- if .IsTranslated }}
{{- $lang_links := slice }}
{{- $langs := slice }}
{{- range .Translations }}
{{- $lang := cond (ne .Language.LanguageName nil) .Language.LanguageName .Lang }}
{{ $lang_links = $lang_links | append (printf "<a href=%s>%s</a>" .Permalink $lang) }}
{{ $langs = $langs | append $lang }}
{{ if or (or .PublishDate .Lastmod) .IsTranslated }}
<hr aria-hidden="true"/>
<div class="post__meta--single" aria-hidden="true">
<!-- List the author(s) of the article. -->
{{- with .Params.author }}
{{- $names := slice }}
{{- range . }}{{ $names = $names | append .name }}{{ end }}
<p data-content-author="{{ jsonify $names }}">{{ i18n "published_by" }}: {{ delimit $names ", " (printf " %s " (i18n "and")) }}</p>
{{- end }}
<p data-content-translations="{{ jsonify $langs }}">{{ i18n "available_translations" }}: {{ delimit $lang_links "," (printf " %s " (i18n "and")) }}</p>
{{- end }}
</div>
<!-- Only show the creation date if the `date` field is valid. -->
{{- if ne (.PublishDate.Format "2006") "0001" -}}
<!-- Publication date. -->
<p data-content-publication-date="{{ .PublishDate.Format "2006-01-02T15:04:05Z07:00" }}">{{ i18n "published_on" }}: <time datetime="{{ .PublishDate.Format "2006-01-02T15:04:05Z07:00" }}">{{ .PublishDate.Format "2006-01-02" }}</time></p>
{{- end }}
<!-- Modification date (will only appear if the publication date is less than the modified date.) -->
{{- if ne .PublishDate .Lastmod }}
<p data-content-modification-date="{{ .Lastmod.Format "2006-01-02T15:04:05Z07:00" }}">{{ i18n "updated_on" }}: <time datetime="{{ .Lastmod.Format "2006-01-02T15:04:05Z07:00" }}">{{ .Lastmod.Format "2006-01-02" }}</time></p>
{{- end }}
<!-- Link the translated versions if the article has at least one translated page. -->
{{- if .IsTranslated }}
{{- $lang_links := slice }}
{{- $langs := slice }}
{{- range .Translations }}
{{- $lang := cond (ne .Language.LanguageName nil) .Language.LanguageName .Lang }}
{{ $lang_links = $lang_links | append (printf "<a href=%s>%s</a>" .Permalink $lang) }}
{{ $langs = $langs | append $lang }}
{{- end }}
<p data-content-translations="{{ jsonify $langs }}">{{ i18n "available_translations" }}: {{ delimit $lang_links "," (printf " %s " (i18n "and")) }}</p>
{{- end }}
</div>
{{ end }}
</div>
{{ end }}

View File

@ -1,37 +1,38 @@
{{- /*
The component where it will display the metadata of the post.
This is used on list templates but feel free to change that.
*/ -}}
{{- $RFC3339 := "2006-01-02T15:04:05Z07:00" }}
<div class="list post__meta" aria-hidden="true">
{{- if ne (.PublishDate.Format "2006") "0001" -}}
<span>
{{ partial "components/heroicon.html" "calendar" }}
<time datetime="{{ .PublishDate.Format $RFC3339 }}" title="{{ i18n "published_on" }}" style="margin-left: 0.5em;">
{{ .PublishDate.Format "2006-01-02" }}
</time>
</span>
{{- end }}
{{- if and .GitInfo (ne (.PublishDate.Format "2006-01-02") (.Lastmod.Format "2006-01-02")) }}
<span>
{{ partial "components/heroicon.html" "arrow-path" }}
<time datetime="{{ .Lastmod.Format $RFC3339 }}" title="{{ i18n "updated_on" }}" style="margin-left: 0.5em;">
{{ .Lastmod.Format "2006-01-02" }}
</time>
</span>
{{- end }}
{{- /* The authors are appended with 'et al' if there's more than one author in the map. */ -}}
{{ with .Params.author }}
{{- $names := slice }}
{{- range . }}{{ $names = $names | append .name }}{{ end }}
{{- $author := index (first 1 $names) 0 }}
<span data-authors="{{ jsonify $names }}" title="{{ i18n "published_by" }}">
{{ partial "components/heroicon.html" "pencil" }}
<span style="margin-left: 0.5em;">
{{ $author }}{{ if ge (len .) 2 }}, et al.{{ end }}
{{- if or .PublishDate .Lastmod }}
{{- $RFC3339 := "2006-01-02T15:04:05Z07:00" }}
<div class="list post__meta" aria-hidden="true">
{{- if .PublishDate -}}
<span>
{{ partial "components/heroicon.html" "calendar" }}
<time datetime="{{ .PublishDate.Format $RFC3339 }}" title="{{ i18n "published_on" }}" style="margin-left: 0.5em;">
{{ .PublishDate.Format "2006-01-02" }}
</time>
</span>
</span>
{{ end }}
</div>
{{- end }}
{{- if ne (.PublishDate.Format "2006-01-02") (.Lastmod.Format "2006-01-02") }}
<span>
{{ partial "components/heroicon.html" "arrow-path" }}
<time datetime="{{ .Lastmod.Format $RFC3339 }}" title="{{ i18n "updated_on" }}" style="margin-left: 0.5em;">
{{ .Lastmod.Format "2006-01-02" }}
</time>
</span>
{{- end }}
{{- /* The authors are appended with 'et al' if there's more than one author in the map. */ -}}
{{- with .Params.author }}
{{- $names := slice }}
{{- range . }}{{ $names = $names | append .name }}{{ end }}
{{- $author := index (first 1 $names) 0 }}
<span data-authors="{{ jsonify $names }}" title="{{ i18n "published_by" }}">
{{ partial "components/heroicon.html" "pencil" }}
<span style="margin-left: 0.5em;">
{{ $author }}{{ if ge (len .) 2 }}, et al.{{ end }}
</span>
</span>
{{- end }}
</div>
{{- end }}