Merge PR #560: deploy/gitea-a11y/custom/templates/custom/time_relative.tmpl

This commit is contained in:
Merge Bot
2026-04-16 05:11:50 +00:00
parent 67aa59ca04
commit 7baa599dd5

View File

@@ -1,8 +1,16 @@
{{/* {{/*
Gitea a11y fix: R4 <time> elements for relative timestamps 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 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"}} {{define "custom/time_relative"}}
{{if and .Time .Relative}} {{if and .Time .Relative}}
<time datetime="{{.Time.Format "2006-01-02T15:04:05Z07:00"}}" title="{{.Time.Format "Jan 02, 2006 15:04"}}"> <time datetime="{{.Time.Format "2006-01-02T15:04:05Z07:00"}}" title="{{.Time.Format "Jan 02, 2006 15:04"}}">
@@ -13,6 +21,10 @@
{{end}} {{end}}
{{end}} {{end}}
{{/*
Alternative: render from Unix timestamp
Usage: {{template "custom/time_from_unix" (dict "Unix" .CreatedUnix "Relative" .CreatedRelative)}}
*/}}
{{define "custom/time_from_unix"}} {{define "custom/time_from_unix"}}
{{if .Relative}} {{if .Relative}}
<time datetime="" data-unix="{{.Unix}}" title="">{{.Relative}}</time> <time datetime="" data-unix="{{.Unix}}" title="">{{.Relative}}</time>
@@ -20,7 +32,11 @@
(function() { (function() {
var el = document.currentScript.previousElementSibling; var el = document.currentScript.previousElementSibling;
var unix = parseInt(el.getAttribute('data-unix')); var unix = parseInt(el.getAttribute('data-unix'));
if (unix) { el.setAttribute('datetime', new Date(unix * 1000).toISOString()); el.setAttribute('title', new Date(unix * 1000).toLocaleString()); } if (unix) {
var d = new Date(unix * 1000);
el.setAttribute('datetime', d.toISOString());
el.setAttribute('title', d.toLocaleString());
}
})(); })();
</script> </script>
{{end}} {{end}}