Files
timmy-config/deploy/gitea-a11y/custom/templates/custom/time_relative.tmpl

44 lines
1.3 KiB
Cheetah

{{/*
Gitea a11y fix: R4 — <time> elements for relative timestamps
Provides a reusable template helper that wraps relative timestamps
in <time datetime="..."> elements with machine-readable ISO 8601 dates.
Usage in templates:
{{template "custom/time_relative" (dict "Time" .CreatedUnix "Relative" .CreatedRelative)}}
Deploy to: custom/templates/custom/time_relative.tmpl
*/}}
{{/* Renders a timestamp as <time> with both relative text and machine-readable datetime */}}
{{define "custom/time_relative"}}
{{if and .Time .Relative}}
<time datetime="{{.Time.Format "2006-01-02T15:04:05Z07:00"}}" title="{{.Time.Format "Jan 02, 2006 15:04"}}">
{{.Relative}}
</time>
{{else if .Relative}}
<span>{{.Relative}}</span>
{{end}}
{{end}}
{{/*
Alternative: render from Unix timestamp
Usage: {{template "custom/time_from_unix" (dict "Unix" .CreatedUnix "Relative" .CreatedRelative)}}
*/}}
{{define "custom/time_from_unix"}}
{{if .Relative}}
<time datetime="" data-unix="{{.Unix}}" title="">{{.Relative}}</time>
<script>
(function() {
var el = document.currentScript.previousElementSibling;
var unix = parseInt(el.getAttribute('data-unix'));
if (unix) {
var d = new Date(unix * 1000);
el.setAttribute('datetime', d.toISOString());
el.setAttribute('title', d.toLocaleString());
}
})();
</script>
{{end}}
{{end}}