From 0af8ab3b84a375d3f076ef27974aa63125ad5c8a Mon Sep 17 00:00:00 2001 From: Kohei Yoshino Date: Fri, 20 Dec 2024 16:58:07 -0500 Subject: [PATCH] Use UTC to fill date in entry slug Fix #278 --- src/lib/services/contents/entry/slug.js | 2 +- src/lib/services/contents/entry/slug.spec.js | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/lib/services/contents/entry/slug.js b/src/lib/services/contents/entry/slug.js index d3cdfecd..24645265 100644 --- a/src/lib/services/contents/entry/slug.js +++ b/src/lib/services/contents/entry/slug.js @@ -66,7 +66,7 @@ export const fillSlugTemplate = ( currentSlug, entryFilePath, locale, - dateTimeParts = getDateTimeParts(), + dateTimeParts = getDateTimeParts({ timeZone: 'UTC' }), }, ) => { const { diff --git a/src/lib/services/contents/entry/slug.spec.js b/src/lib/services/contents/entry/slug.spec.js index f9692b60..17ec97a7 100644 --- a/src/lib/services/contents/entry/slug.spec.js +++ b/src/lib/services/contents/entry/slug.spec.js @@ -1,3 +1,4 @@ +import { getDateTimeParts } from '@sveltia/utils/datetime'; import { describe, expect, test } from 'vitest'; import { siteConfig } from '$lib/services/config'; import { fillSlugTemplate } from '$lib/services/contents/entry/slug'; @@ -45,6 +46,16 @@ describe('Test fillSlugTemplate()', () => { ); }); + test('date/time', () => { + // The time zone should be UTC, not local + const { year, month, day, hour, minute, second } = getDateTimeParts({ timeZone: 'UTC' }); + const template = '{{year}}-{{month}}-{{day}}-{{hour}}-{{minute}}-{{second}}'; + + expect(fillSlugTemplate(template, { collection, content: {} })).toEqual( + `${year}-${month}-${day}-${hour}-${minute}-${second}`, + ); + }); + test('random ID fallback', () => { expect(fillSlugTemplate('{{title}}', { collection, content: {} })).toMatch(/[0-9a-f]{12}/); expect(fillSlugTemplate('{{name}}', { collection, content: {} })).toMatch(/[0-9a-f]{12}/);