Skip to content

Commit c737407

Browse files
OzakIOneslorber
andcommitted
refactor(theme): dates should be formatted on the client-side instead of in nodejs code (#9868)
Co-authored-by: OzakIOne <OzakIOne@users.noreply.github.com> Co-authored-by: sebastien <lorber.sebastien@gmail.com>
1 parent 0d8777c commit c737407

File tree

15 files changed

+83
-162
lines changed

15 files changed

+83
-162
lines changed

packages/docusaurus-plugin-content-blog/src/__tests__/index.test.ts

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ const markdown: MarkdownConfig = {
4141
}
4242
return result;
4343
},
44+
remarkRehypeOptions: undefined,
4445
};
4546

4647
function findByTitle(
@@ -175,7 +176,6 @@ describe('blog plugin', () => {
175176
description: `date inside front matter`,
176177
authors: [],
177178
date: new Date('2019-01-01'),
178-
formattedDate: 'January 1, 2019',
179179
frontMatter: {
180180
date: new Date('2019-01-01'),
181181
tags: ['date'],
@@ -220,7 +220,6 @@ describe('blog plugin', () => {
220220
},
221221
],
222222
date: new Date('2018-12-14'),
223-
formattedDate: 'December 14, 2018',
224223
frontMatter: {
225224
authors: [
226225
{
@@ -256,7 +255,6 @@ describe('blog plugin', () => {
256255
title: 'Simple Slug',
257256
},
258257
date: new Date('2020-08-16'),
259-
formattedDate: 'August 16, 2020',
260258
frontMatter: {
261259
date: '2020/08/16',
262260
slug: '/hey/my super path/héllô',
@@ -302,7 +300,6 @@ describe('blog plugin', () => {
302300
title: 'draft',
303301
},
304302
date: new Date('2020-08-15'),
305-
formattedDate: 'August 15, 2020',
306303
frontMatter: {
307304
author: 'Sébastien Lorber',
308305
author_title: 'Docusaurus maintainer',
@@ -328,7 +325,6 @@ describe('blog plugin', () => {
328325
description: '',
329326
authors: [],
330327
date: new Date('2019-01-02'),
331-
formattedDate: 'January 2, 2019',
332328
frontMatter: {
333329
date: new Date('2019-01-02'),
334330
},
@@ -343,39 +339,6 @@ describe('blog plugin', () => {
343339
});
344340
});
345341

346-
it('builds simple website blog with localized dates', async () => {
347-
const siteDir = path.join(__dirname, '__fixtures__', 'website');
348-
const blogPostsFrench = await getBlogPosts(siteDir, {}, getI18n('fr'));
349-
expect(blogPostsFrench).toHaveLength(10);
350-
expect(blogPostsFrench[0]!.metadata.formattedDate).toMatchInlineSnapshot(
351-
`"23 juillet 2023"`,
352-
);
353-
expect(blogPostsFrench[1]!.metadata.formattedDate).toMatchInlineSnapshot(
354-
`"6 mars 2021"`,
355-
);
356-
expect(blogPostsFrench[2]!.metadata.formattedDate).toMatchInlineSnapshot(
357-
`"5 mars 2021"`,
358-
);
359-
expect(blogPostsFrench[3]!.metadata.formattedDate).toMatchInlineSnapshot(
360-
`"16 août 2020"`,
361-
);
362-
expect(blogPostsFrench[4]!.metadata.formattedDate).toMatchInlineSnapshot(
363-
`"15 août 2020"`,
364-
);
365-
expect(blogPostsFrench[5]!.metadata.formattedDate).toMatchInlineSnapshot(
366-
`"27 février 2020"`,
367-
);
368-
expect(blogPostsFrench[6]!.metadata.formattedDate).toMatchInlineSnapshot(
369-
`"27 février 2020"`,
370-
);
371-
expect(blogPostsFrench[7]!.metadata.formattedDate).toMatchInlineSnapshot(
372-
`"2 janvier 2019"`,
373-
);
374-
expect(blogPostsFrench[8]!.metadata.formattedDate).toMatchInlineSnapshot(
375-
`"1 janvier 2019"`,
376-
);
377-
});
378-
379342
it('handles edit URL with editLocalizedBlogs: true', async () => {
380343
const siteDir = path.join(__dirname, '__fixtures__', 'website');
381344
const blogPosts = await getBlogPosts(siteDir, {editLocalizedFiles: true});
@@ -476,11 +439,6 @@ describe('blog plugin', () => {
476439
// We know the file exists and we know we have git
477440
const result = getFileCommitDate(noDateSourceFile, {age: 'oldest'});
478441
const noDateSourceTime = result.date;
479-
const formattedDate = Intl.DateTimeFormat('en', {
480-
day: 'numeric',
481-
month: 'long',
482-
year: 'numeric',
483-
}).format(noDateSourceTime);
484442

485443
expect({
486444
...getByTitle(blogPosts, 'no date').metadata,
@@ -494,7 +452,6 @@ describe('blog plugin', () => {
494452
description: `no date`,
495453
authors: [],
496454
date: noDateSourceTime,
497-
formattedDate,
498455
frontMatter: {},
499456
tags: [],
500457
prevItem: undefined,

packages/docusaurus-plugin-content-blog/src/blogUtils.ts

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -164,25 +164,6 @@ export function parseBlogFileName(
164164
return {date: undefined, text, slug};
165165
}
166166

167-
function formatBlogPostDate(
168-
locale: string,
169-
date: Date,
170-
calendar: string,
171-
): string {
172-
try {
173-
return new Intl.DateTimeFormat(locale, {
174-
day: 'numeric',
175-
month: 'long',
176-
year: 'numeric',
177-
timeZone: 'UTC',
178-
calendar,
179-
}).format(date);
180-
} catch (err) {
181-
logger.error`Can't format blog post date "${String(date)}"`;
182-
throw err;
183-
}
184-
}
185-
186167
async function parseBlogPostMarkdownFile({
187168
filePath,
188169
parseFrontMatter,
@@ -289,11 +270,6 @@ async function processBlogSourceFile(
289270
}
290271

291272
const date = await getDate();
292-
const formattedDate = formatBlogPostDate(
293-
i18n.currentLocale,
294-
date,
295-
i18n.localeConfigs[i18n.currentLocale]!.calendar,
296-
);
297273

298274
const title = frontMatter.title ?? contentTitle ?? parsedBlogFileName.text;
299275
const description = frontMatter.description ?? excerpt ?? '';
@@ -348,7 +324,6 @@ async function processBlogSourceFile(
348324
title,
349325
description,
350326
date,
351-
formattedDate,
352327
tags: normalizeFrontMatterTags(tagsBasePath, frontMatter.tags),
353328
readingTime: showReadingTime
354329
? options.readingTime({

packages/docusaurus-plugin-content-blog/src/plugin-content-blog.d.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,6 @@ yarn workspace v1.22.19image` is a collocated image path, this entry will be the
192192
* into a string.
193193
*/
194194
readonly date: Date;
195-
/**
196-
* Publish date formatted according to the locale, so that the client can
197-
* render the date regardless of the existence of `Intl.DateTimeFormat`.
198-
*/
199-
readonly formattedDate: string;
200195
/** Full link including base URL. */
201196
readonly permalink: string;
202197
/**

packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/index.test.ts.snap

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ exports[`simple website content 1`] = `
5252
"description": "Images",
5353
"draft": false,
5454
"editUrl": undefined,
55-
"formattedLastUpdatedAt": undefined,
5655
"frontMatter": {
5756
"id": "baz",
5857
"pagination_label": "baz pagination_label",
@@ -105,7 +104,6 @@ exports[`simple website content 2`] = `
105104
"description": "Hi, Endilie here :)",
106105
"draft": false,
107106
"editUrl": undefined,
108-
"formattedLastUpdatedAt": undefined,
109107
"frontMatter": {
110108
"id": "hello",
111109
"sidebar_label": "Hello sidebar_label",
@@ -151,7 +149,6 @@ exports[`simple website content 3`] = `
151149
"description": "This is custom description",
152150
"draft": false,
153151
"editUrl": undefined,
154-
"formattedLastUpdatedAt": undefined,
155152
"frontMatter": {
156153
"description": "This is custom description",
157154
"id": "bar",
@@ -1971,7 +1968,6 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha
19711968
"description": "Getting started text",
19721969
"draft": false,
19731970
"editUrl": undefined,
1974-
"formattedLastUpdatedAt": undefined,
19751971
"frontMatter": {},
19761972
"id": "getting-started",
19771973
"lastUpdatedAt": undefined,
@@ -1999,7 +1995,6 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha
19991995
"description": "Installation text",
20001996
"draft": false,
20011997
"editUrl": undefined,
2002-
"formattedLastUpdatedAt": undefined,
20031998
"frontMatter": {},
20041999
"id": "installation",
20052000
"lastUpdatedAt": undefined,
@@ -2030,7 +2025,6 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha
20302025
"description": "Guide 1 text",
20312026
"draft": false,
20322027
"editUrl": undefined,
2033-
"formattedLastUpdatedAt": undefined,
20342028
"frontMatter": {
20352029
"id": "guide1",
20362030
"sidebar_position": 1,
@@ -2064,7 +2058,6 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha
20642058
"description": "Guide 2 text",
20652059
"draft": false,
20662060
"editUrl": undefined,
2067-
"formattedLastUpdatedAt": undefined,
20682061
"frontMatter": {
20692062
"id": "guide2",
20702063
},
@@ -2097,7 +2090,6 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha
20972090
"description": "Guide 2.5 text",
20982091
"draft": false,
20992092
"editUrl": undefined,
2100-
"formattedLastUpdatedAt": undefined,
21012093
"frontMatter": {
21022094
"id": "guide2.5",
21032095
"sidebar_position": 2.5,
@@ -2131,7 +2123,6 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha
21312123
"description": "Guide 3 text",
21322124
"draft": false,
21332125
"editUrl": undefined,
2134-
"formattedLastUpdatedAt": undefined,
21352126
"frontMatter": {
21362127
"id": "guide3",
21372128
"sidebar_position": 3,
@@ -2165,7 +2156,6 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha
21652156
"description": "Guide 4 text",
21662157
"draft": false,
21672158
"editUrl": undefined,
2168-
"formattedLastUpdatedAt": undefined,
21692159
"frontMatter": {
21702160
"id": "guide4",
21712161
},
@@ -2198,7 +2188,6 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha
21982188
"description": "Guide 5 text",
21992189
"draft": false,
22002190
"editUrl": undefined,
2201-
"formattedLastUpdatedAt": undefined,
22022191
"frontMatter": {
22032192
"id": "guide5",
22042193
},
@@ -2231,7 +2220,6 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha
22312220
"description": "API Overview text",
22322221
"draft": false,
22332222
"editUrl": undefined,
2234-
"formattedLastUpdatedAt": undefined,
22352223
"frontMatter": {},
22362224
"id": "API/api-overview",
22372225
"lastUpdatedAt": undefined,
@@ -2262,7 +2250,6 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha
22622250
"description": "Client API text",
22632251
"draft": false,
22642252
"editUrl": undefined,
2265-
"formattedLastUpdatedAt": undefined,
22662253
"frontMatter": {},
22672254
"id": "API/Core APIs/Client API",
22682255
"lastUpdatedAt": undefined,
@@ -2293,7 +2280,6 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha
22932280
"description": "Server API text",
22942281
"draft": false,
22952282
"editUrl": undefined,
2296-
"formattedLastUpdatedAt": undefined,
22972283
"frontMatter": {},
22982284
"id": "API/Core APIs/Server API",
22992285
"lastUpdatedAt": undefined,
@@ -2324,7 +2310,6 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha
23242310
"description": "Plugin API text",
23252311
"draft": false,
23262312
"editUrl": undefined,
2327-
"formattedLastUpdatedAt": undefined,
23282313
"frontMatter": {},
23292314
"id": "API/Extension APIs/Plugin API",
23302315
"lastUpdatedAt": undefined,
@@ -2355,7 +2340,6 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha
23552340
"description": "Theme API text",
23562341
"draft": false,
23572342
"editUrl": undefined,
2358-
"formattedLastUpdatedAt": undefined,
23592343
"frontMatter": {},
23602344
"id": "API/Extension APIs/Theme API",
23612345
"lastUpdatedAt": undefined,
@@ -2386,7 +2370,6 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha
23862370
"description": "API End text",
23872371
"draft": false,
23882372
"editUrl": undefined,
2389-
"formattedLastUpdatedAt": undefined,
23902373
"frontMatter": {},
23912374
"id": "API/api-end",
23922375
"lastUpdatedAt": undefined,
@@ -2566,7 +2549,6 @@ exports[`site with partial autogenerated sidebars docs in partially generated si
25662549
"description": "API End text",
25672550
"draft": false,
25682551
"editUrl": undefined,
2569-
"formattedLastUpdatedAt": undefined,
25702552
"frontMatter": {},
25712553
"id": "API/api-end",
25722554
"lastUpdatedAt": undefined,
@@ -2594,7 +2576,6 @@ exports[`site with partial autogenerated sidebars docs in partially generated si
25942576
"description": "API Overview text",
25952577
"draft": false,
25962578
"editUrl": undefined,
2597-
"formattedLastUpdatedAt": undefined,
25982579
"frontMatter": {},
25992580
"id": "API/api-overview",
26002581
"lastUpdatedAt": undefined,
@@ -2625,7 +2606,6 @@ exports[`site with partial autogenerated sidebars docs in partially generated si
26252606
"description": "Plugin API text",
26262607
"draft": false,
26272608
"editUrl": undefined,
2628-
"formattedLastUpdatedAt": undefined,
26292609
"frontMatter": {},
26302610
"id": "API/Extension APIs/Plugin API",
26312611
"lastUpdatedAt": undefined,
@@ -2656,7 +2636,6 @@ exports[`site with partial autogenerated sidebars docs in partially generated si
26562636
"description": "Theme API text",
26572637
"draft": false,
26582638
"editUrl": undefined,
2659-
"formattedLastUpdatedAt": undefined,
26602639
"frontMatter": {},
26612640
"id": "API/Extension APIs/Theme API",
26622641
"lastUpdatedAt": undefined,
@@ -2716,7 +2695,6 @@ exports[`versioned website (community) content 1`] = `
27162695
"description": "Team current version (translated)",
27172696
"draft": false,
27182697
"editUrl": undefined,
2719-
"formattedLastUpdatedAt": undefined,
27202698
"frontMatter": {
27212699
"title": "Team title translated",
27222700
},
@@ -2743,7 +2721,6 @@ exports[`versioned website (community) content 2`] = `
27432721
"description": "Team 1.0.0",
27442722
"draft": false,
27452723
"editUrl": undefined,
2746-
"formattedLastUpdatedAt": undefined,
27472724
"frontMatter": {},
27482725
"id": "team",
27492726
"lastUpdatedAt": undefined,
@@ -3023,7 +3000,6 @@ exports[`versioned website content 1`] = `
30233000
"description": "This is next version of bar.",
30243001
"draft": false,
30253002
"editUrl": undefined,
3026-
"formattedLastUpdatedAt": undefined,
30273003
"frontMatter": {
30283004
"slug": "barSlug",
30293005
"tags": [
@@ -3074,7 +3050,6 @@ exports[`versioned website content 2`] = `
30743050
"description": "Bar 1.0.1 !",
30753051
"draft": false,
30763052
"editUrl": undefined,
3077-
"formattedLastUpdatedAt": undefined,
30783053
"frontMatter": {},
30793054
"id": "foo/bar",
30803055
"lastUpdatedAt": undefined,
@@ -3102,7 +3077,6 @@ exports[`versioned website content 3`] = `
31023077
"description": "Hello next !",
31033078
"draft": false,
31043079
"editUrl": undefined,
3105-
"formattedLastUpdatedAt": undefined,
31063080
"frontMatter": {
31073081
"slug": "/",
31083082
},
@@ -3132,7 +3106,6 @@ exports[`versioned website content 4`] = `
31323106
"description": "Hello 1.0.1 !",
31333107
"draft": false,
31343108
"editUrl": undefined,
3135-
"formattedLastUpdatedAt": undefined,
31363109
"frontMatter": {
31373110
"slug": "/",
31383111
},
@@ -3162,7 +3135,6 @@ exports[`versioned website content 5`] = `
31623135
"description": "Baz 1.0.0 ! This will be deleted in next subsequent versions.",
31633136
"draft": false,
31643137
"editUrl": undefined,
3165-
"formattedLastUpdatedAt": undefined,
31663138
"frontMatter": {},
31673139
"id": "foo/baz",
31683140
"lastUpdatedAt": undefined,

0 commit comments

Comments
 (0)