Merge 97b0c53bc8f4677177e9c46793270cb5f525cf6d into a1cb044262d33d11860feb847c4c30aa281aee82

This commit is contained in:
Arash Shakery 2023-12-23 00:28:40 -07:00 committed by GitHub
commit 1c5cf0ab8e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 1 deletions

19
assets/js/datelocale.ts Normal file
View File

@ -0,0 +1,19 @@
function formatDate(date: string | null) {
if (!date) return date;
return new Intl.DateTimeFormat(process.env.lang_code, {
year: 'numeric',
month: 'long',
day: 'numeric',
}).format(new Date(date));
}
if ('Intl' in window) {
window.addEventListener('load', () => {
for(const span of [...document.querySelectorAll('.localizable-date')]) {
const localizedDate = formatDate(span.getAttribute('title'));
if (localizedDate) {
span.innerHTML = localizedDate;
}
}
});
}

View File

@ -89,6 +89,19 @@
{{- end }} {{- end }}
{{- end -}} {{- end -}}
{{- /* localize dates */}}
{{- if (not site.Params.disableDateLocalization) }}
{{ $defines := dict "process.env.lang_code" (printf `"%s"` site.LanguageCode) }}
{{ $opts := dict "targetPath" "datelocale.ts" "defines" $defines }}
{{- if not site.Params.assets.disableFingerprinting }}
{{ $built := resources.Get "js/datelocale.ts" | js.Build $opts | fingerprint }}
<script defer crossorigin="anonymous" src="{{ $built.Permalink }}" integrity="{{ $built.Data.Integrity }}"></script>
{{- else }}
{{ $built := resources.Get "js/datelocale.js" | js.Build $opts }}
<script defer crossorigin="anonymous" src="{{ $built.Permalink }}"></script>
{{- end }}
{{- end }}
{{- /* Highlight.js */}} {{- /* Highlight.js */}}
{{- $isHLJSdisabled := (site.Params.assets.disableHLJS | default .Params.disableHLJS ) }} {{- $isHLJSdisabled := (site.Params.assets.disableHLJS | default .Params.disableHLJS ) }}
{{- if (and (eq .Kind "page") (ne .Layout "archives") (ne .Layout "search") (not $isHLJSdisabled)) }} {{- if (and (eq .Kind "page") (ne .Layout "archives") (ne .Layout "search") (not $isHLJSdisabled)) }}

View File

@ -1,7 +1,7 @@
{{- $scratch := newScratch }} {{- $scratch := newScratch }}
{{- if not .Date.IsZero -}} {{- if not .Date.IsZero -}}
{{- $scratch.Add "meta" (slice (printf "<span title='%s'>%s</span>" (.Date) (.Date | time.Format (default "January 2, 2006" site.Params.DateFormat)))) }} {{- $scratch.Add "meta" (slice (printf "<span class='localizable-date' title='%s'>%s</span>" (.Date) (.Date | time.Format (default "January 2, 2006" site.Params.DateFormat)))) }}
{{- end }} {{- end }}
{{- if (.Param "ShowReadingTime") -}} {{- if (.Param "ShowReadingTime") -}}