Merge PR #560: deploy/gitea-a11y/custom/templates/custom/time_relative.tmpl
This commit is contained in:
@@ -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
|
||||
*/}}
|
||||
|
||||
{{/* 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"}}">
|
||||
@@ -13,6 +21,10 @@
|
||||
{{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>
|
||||
@@ -20,7 +32,11 @@
|
||||
(function() {
|
||||
var el = document.currentScript.previousElementSibling;
|
||||
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>
|
||||
{{end}}
|
||||
|
||||
Reference in New Issue
Block a user