mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-02 11:47:23 +02:00
refactor(docs,blog): last update timestamp should be in milliseconds instead of seconds (#9963)
This commit is contained in:
parent
6c724ed857
commit
465cf4d82c
8 changed files with 35 additions and 29 deletions
|
@ -539,7 +539,7 @@ describe('last update', () => {
|
||||||
'website-blog-with-last-update',
|
'website-blog-with-last-update',
|
||||||
);
|
);
|
||||||
|
|
||||||
const lastUpdateFor = (date: string) => new Date(date).getTime() / 1000;
|
const lastUpdateFor = (date: string) => new Date(date).getTime();
|
||||||
|
|
||||||
it('author and time', async () => {
|
it('author and time', async () => {
|
||||||
const plugin = await getPlugin(
|
const plugin = await getPlugin(
|
||||||
|
|
|
@ -8,7 +8,12 @@
|
||||||
import {jest} from '@jest/globals';
|
import {jest} from '@jest/globals';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import {loadContext} from '@docusaurus/core/src/server/site';
|
import {loadContext} from '@docusaurus/core/src/server/site';
|
||||||
import {createSlugger, posixPath, DEFAULT_PLUGIN_ID} from '@docusaurus/utils';
|
import {
|
||||||
|
createSlugger,
|
||||||
|
posixPath,
|
||||||
|
DEFAULT_PLUGIN_ID,
|
||||||
|
GIT_FALLBACK_LAST_UPDATE_DATE,
|
||||||
|
} from '@docusaurus/utils';
|
||||||
import {createSidebarsUtils} from '../sidebars/utils';
|
import {createSidebarsUtils} from '../sidebars/utils';
|
||||||
import {
|
import {
|
||||||
processDocMetadata,
|
processDocMetadata,
|
||||||
|
@ -474,7 +479,7 @@ describe('simple site', () => {
|
||||||
custom_edit_url: 'https://github.com/customUrl/docs/lorem.md',
|
custom_edit_url: 'https://github.com/customUrl/docs/lorem.md',
|
||||||
unrelated_front_matter: "won't be part of metadata",
|
unrelated_front_matter: "won't be part of metadata",
|
||||||
},
|
},
|
||||||
lastUpdatedAt: 1539502055,
|
lastUpdatedAt: GIT_FALLBACK_LAST_UPDATE_DATE,
|
||||||
lastUpdatedBy: 'Author',
|
lastUpdatedBy: 'Author',
|
||||||
tags: [],
|
tags: [],
|
||||||
unlisted: false,
|
unlisted: false,
|
||||||
|
@ -571,7 +576,7 @@ describe('simple site', () => {
|
||||||
},
|
},
|
||||||
title: 'Custom Last Update',
|
title: 'Custom Last Update',
|
||||||
},
|
},
|
||||||
lastUpdatedAt: new Date('1/1/2000').getTime() / 1000,
|
lastUpdatedAt: new Date('1/1/2000').getTime(),
|
||||||
lastUpdatedBy: 'Custom Author (processed by parseFrontMatter)',
|
lastUpdatedBy: 'Custom Author (processed by parseFrontMatter)',
|
||||||
sidebarPosition: undefined,
|
sidebarPosition: undefined,
|
||||||
tags: [],
|
tags: [],
|
||||||
|
@ -609,7 +614,7 @@ describe('simple site', () => {
|
||||||
},
|
},
|
||||||
title: 'Last Update Author Only',
|
title: 'Last Update Author Only',
|
||||||
},
|
},
|
||||||
lastUpdatedAt: 1539502055,
|
lastUpdatedAt: GIT_FALLBACK_LAST_UPDATE_DATE,
|
||||||
lastUpdatedBy: 'Custom Author (processed by parseFrontMatter)',
|
lastUpdatedBy: 'Custom Author (processed by parseFrontMatter)',
|
||||||
sidebarPosition: undefined,
|
sidebarPosition: undefined,
|
||||||
tags: [],
|
tags: [],
|
||||||
|
@ -647,7 +652,7 @@ describe('simple site', () => {
|
||||||
},
|
},
|
||||||
title: 'Last Update Date Only',
|
title: 'Last Update Date Only',
|
||||||
},
|
},
|
||||||
lastUpdatedAt: new Date('1/1/2000').getTime() / 1000,
|
lastUpdatedAt: new Date('1/1/2000').getTime(),
|
||||||
lastUpdatedBy: 'Author',
|
lastUpdatedBy: 'Author',
|
||||||
sidebarPosition: undefined,
|
sidebarPosition: undefined,
|
||||||
tags: [],
|
tags: [],
|
||||||
|
|
|
@ -16,7 +16,7 @@ function LastUpdatedAtDate({
|
||||||
}: {
|
}: {
|
||||||
lastUpdatedAt: number;
|
lastUpdatedAt: number;
|
||||||
}): JSX.Element {
|
}): JSX.Element {
|
||||||
const atDate = new Date(lastUpdatedAt * 1000);
|
const atDate = new Date(lastUpdatedAt);
|
||||||
|
|
||||||
const dateTimeFormat = useDateTimeFormat({
|
const dateTimeFormat = useDateTimeFormat({
|
||||||
day: 'numeric',
|
day: 'numeric',
|
||||||
|
|
|
@ -23,7 +23,7 @@ import type {
|
||||||
} from '@docusaurus/plugin-content-blog';
|
} from '@docusaurus/plugin-content-blog';
|
||||||
import type {DocusaurusConfig} from '@docusaurus/types';
|
import type {DocusaurusConfig} from '@docusaurus/types';
|
||||||
|
|
||||||
const convertDate = (dateMs: number) => new Date(dateMs * 1000).toISOString();
|
const convertDate = (dateMs: number) => new Date(dateMs).toISOString();
|
||||||
|
|
||||||
function getBlogPost(
|
function getBlogPost(
|
||||||
blogPostContent: PropBlogPostContent,
|
blogPostContent: PropBlogPostContent,
|
||||||
|
|
|
@ -52,13 +52,13 @@ describe('getFileCommitDate', () => {
|
||||||
getFileCommitDate(path.join(repoDir, 'test.txt'), {}),
|
getFileCommitDate(path.join(repoDir, 'test.txt'), {}),
|
||||||
).resolves.toEqual({
|
).resolves.toEqual({
|
||||||
date: new Date('2020-06-19'),
|
date: new Date('2020-06-19'),
|
||||||
timestamp: new Date('2020-06-19').getTime() / 1000,
|
timestamp: new Date('2020-06-19').getTime(),
|
||||||
});
|
});
|
||||||
await expect(
|
await expect(
|
||||||
getFileCommitDate(path.join(repoDir, 'dest.txt'), {}),
|
getFileCommitDate(path.join(repoDir, 'dest.txt'), {}),
|
||||||
).resolves.toEqual({
|
).resolves.toEqual({
|
||||||
date: new Date('2020-09-13'),
|
date: new Date('2020-09-13'),
|
||||||
timestamp: new Date('2020-09-13').getTime() / 1000,
|
timestamp: new Date('2020-09-13').getTime(),
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
it('returns latest commit date', async () => {
|
it('returns latest commit date', async () => {
|
||||||
|
@ -66,13 +66,13 @@ describe('getFileCommitDate', () => {
|
||||||
getFileCommitDate(path.join(repoDir, 'test.txt'), {age: 'newest'}),
|
getFileCommitDate(path.join(repoDir, 'test.txt'), {age: 'newest'}),
|
||||||
).resolves.toEqual({
|
).resolves.toEqual({
|
||||||
date: new Date('2020-09-13'),
|
date: new Date('2020-09-13'),
|
||||||
timestamp: new Date('2020-09-13').getTime() / 1000,
|
timestamp: new Date('2020-09-13').getTime(),
|
||||||
});
|
});
|
||||||
await expect(
|
await expect(
|
||||||
getFileCommitDate(path.join(repoDir, 'dest.txt'), {age: 'newest'}),
|
getFileCommitDate(path.join(repoDir, 'dest.txt'), {age: 'newest'}),
|
||||||
).resolves.toEqual({
|
).resolves.toEqual({
|
||||||
date: new Date('2020-11-13'),
|
date: new Date('2020-11-13'),
|
||||||
timestamp: new Date('2020-11-13').getTime() / 1000,
|
timestamp: new Date('2020-11-13').getTime(),
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
it('returns latest commit date with author', async () => {
|
it('returns latest commit date with author', async () => {
|
||||||
|
@ -83,7 +83,7 @@ describe('getFileCommitDate', () => {
|
||||||
}),
|
}),
|
||||||
).resolves.toEqual({
|
).resolves.toEqual({
|
||||||
date: new Date('2020-06-19'),
|
date: new Date('2020-06-19'),
|
||||||
timestamp: new Date('2020-06-19').getTime() / 1000,
|
timestamp: new Date('2020-06-19').getTime(),
|
||||||
author: 'Caroline',
|
author: 'Caroline',
|
||||||
});
|
});
|
||||||
await expect(
|
await expect(
|
||||||
|
@ -93,7 +93,7 @@ describe('getFileCommitDate', () => {
|
||||||
}),
|
}),
|
||||||
).resolves.toEqual({
|
).resolves.toEqual({
|
||||||
date: new Date('2020-09-13'),
|
date: new Date('2020-09-13'),
|
||||||
timestamp: new Date('2020-09-13').getTime() / 1000,
|
timestamp: new Date('2020-09-13').getTime(),
|
||||||
author: 'Caroline',
|
author: 'Caroline',
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -105,7 +105,7 @@ describe('getFileCommitDate', () => {
|
||||||
}),
|
}),
|
||||||
).resolves.toEqual({
|
).resolves.toEqual({
|
||||||
date: new Date('2020-09-13'),
|
date: new Date('2020-09-13'),
|
||||||
timestamp: new Date('2020-09-13').getTime() / 1000,
|
timestamp: new Date('2020-09-13').getTime(),
|
||||||
author: 'Caroline',
|
author: 'Caroline',
|
||||||
});
|
});
|
||||||
await expect(
|
await expect(
|
||||||
|
@ -115,7 +115,7 @@ describe('getFileCommitDate', () => {
|
||||||
}),
|
}),
|
||||||
).resolves.toEqual({
|
).resolves.toEqual({
|
||||||
date: new Date('2020-11-13'),
|
date: new Date('2020-11-13'),
|
||||||
timestamp: new Date('2020-11-13').getTime() / 1000,
|
timestamp: new Date('2020-11-13').getTime(),
|
||||||
author: 'Josh-Cena',
|
author: 'Josh-Cena',
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -103,7 +103,7 @@ describe('getFileLastUpdate', () => {
|
||||||
|
|
||||||
describe('readLastUpdateData', () => {
|
describe('readLastUpdateData', () => {
|
||||||
const testDate = '2021-01-01';
|
const testDate = '2021-01-01';
|
||||||
const testDateTime = new Date(testDate).getTime() / 1000;
|
const testTimestamp = new Date(testDate).getTime();
|
||||||
const testAuthor = 'ozaki';
|
const testAuthor = 'ozaki';
|
||||||
|
|
||||||
it('read last time show author time', async () => {
|
it('read last time show author time', async () => {
|
||||||
|
@ -112,7 +112,7 @@ describe('readLastUpdateData', () => {
|
||||||
{showLastUpdateAuthor: true, showLastUpdateTime: true},
|
{showLastUpdateAuthor: true, showLastUpdateTime: true},
|
||||||
{date: testDate},
|
{date: testDate},
|
||||||
);
|
);
|
||||||
expect(lastUpdatedAt).toEqual(testDateTime);
|
expect(lastUpdatedAt).toEqual(testTimestamp);
|
||||||
expect(lastUpdatedBy).toBe(GIT_FALLBACK_LAST_UPDATE_AUTHOR);
|
expect(lastUpdatedBy).toBe(GIT_FALLBACK_LAST_UPDATE_AUTHOR);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -133,7 +133,7 @@ describe('readLastUpdateData', () => {
|
||||||
{author: testAuthor, date: testDate},
|
{author: testAuthor, date: testDate},
|
||||||
);
|
);
|
||||||
expect(lastUpdatedBy).toEqual(testAuthor);
|
expect(lastUpdatedBy).toEqual(testAuthor);
|
||||||
expect(lastUpdatedAt).toEqual(testDateTime);
|
expect(lastUpdatedAt).toEqual(testTimestamp);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('read last default show none', async () => {
|
it('read last default show none', async () => {
|
||||||
|
@ -191,7 +191,7 @@ describe('readLastUpdateData', () => {
|
||||||
{date: testDate},
|
{date: testDate},
|
||||||
);
|
);
|
||||||
expect(lastUpdatedBy).toBeUndefined();
|
expect(lastUpdatedBy).toBeUndefined();
|
||||||
expect(lastUpdatedAt).toEqual(testDateTime);
|
expect(lastUpdatedAt).toEqual(testTimestamp);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('read last author show time', async () => {
|
it('read last author show time', async () => {
|
||||||
|
@ -211,7 +211,7 @@ describe('readLastUpdateData', () => {
|
||||||
{author: testAuthor, date: testDate},
|
{author: testAuthor, date: testDate},
|
||||||
);
|
);
|
||||||
expect(lastUpdatedBy).toBeUndefined();
|
expect(lastUpdatedBy).toBeUndefined();
|
||||||
expect(lastUpdatedAt).toEqual(testDateTime);
|
expect(lastUpdatedAt).toEqual(testTimestamp);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('read last author show author only - both front matter', async () => {
|
it('read last author show author only - both front matter', async () => {
|
||||||
|
|
|
@ -39,7 +39,7 @@ export async function getFileCommitDate(
|
||||||
): Promise<{
|
): Promise<{
|
||||||
/** Relevant commit date. */
|
/** Relevant commit date. */
|
||||||
date: Date;
|
date: Date;
|
||||||
/** Timestamp in **seconds**, as returned from git. */
|
/** Timestamp returned from git, converted to **milliseconds**. */
|
||||||
timestamp: number;
|
timestamp: number;
|
||||||
}>;
|
}>;
|
||||||
/**
|
/**
|
||||||
|
@ -66,7 +66,7 @@ export async function getFileCommitDate(
|
||||||
): Promise<{
|
): Promise<{
|
||||||
/** Relevant commit date. */
|
/** Relevant commit date. */
|
||||||
date: Date;
|
date: Date;
|
||||||
/** Timestamp in **seconds**, as returned from git. */
|
/** Timestamp returned from git, converted to **milliseconds**. */
|
||||||
timestamp: number;
|
timestamp: number;
|
||||||
/** The author's name, as returned from git. */
|
/** The author's name, as returned from git. */
|
||||||
author: string;
|
author: string;
|
||||||
|
@ -150,8 +150,9 @@ export async function getFileCommitDate(
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const timestamp = Number(match.groups!.timestamp);
|
const timestampInSeconds = Number(match.groups!.timestamp);
|
||||||
const date = new Date(timestamp * 1000);
|
const timestamp = timestampInSeconds * 1_000;
|
||||||
|
const date = new Date(timestamp);
|
||||||
|
|
||||||
if (includeAuthor) {
|
if (includeAuthor) {
|
||||||
return {date, timestamp, author: match.groups!.author!};
|
return {date, timestamp, author: match.groups!.author!};
|
||||||
|
|
|
@ -14,7 +14,7 @@ import {
|
||||||
} from './gitUtils';
|
} from './gitUtils';
|
||||||
import type {PluginOptions} from '@docusaurus/types';
|
import type {PluginOptions} from '@docusaurus/types';
|
||||||
|
|
||||||
export const GIT_FALLBACK_LAST_UPDATE_DATE = 1539502055;
|
export const GIT_FALLBACK_LAST_UPDATE_DATE = 1539502055000;
|
||||||
|
|
||||||
export const GIT_FALLBACK_LAST_UPDATE_AUTHOR = 'Author';
|
export const GIT_FALLBACK_LAST_UPDATE_AUTHOR = 'Author';
|
||||||
|
|
||||||
|
@ -31,9 +31,9 @@ async function getGitLastUpdate(filePath: string): Promise<LastUpdateData> {
|
||||||
}
|
}
|
||||||
|
|
||||||
export type LastUpdateData = {
|
export type LastUpdateData = {
|
||||||
/** A timestamp in **seconds**, directly acquired from `git log`. */
|
/** A timestamp in **milliseconds**, usually read from `git log` */
|
||||||
lastUpdatedAt?: number;
|
lastUpdatedAt?: number;
|
||||||
/** The author's name directly acquired from `git log`. */
|
/** The author's name, usually coming from `git log` */
|
||||||
lastUpdatedBy?: string;
|
lastUpdatedBy?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -105,7 +105,7 @@ export async function readLastUpdateData(
|
||||||
|
|
||||||
const frontMatterAuthor = lastUpdateFrontMatter?.author;
|
const frontMatterAuthor = lastUpdateFrontMatter?.author;
|
||||||
const frontMatterTimestamp = lastUpdateFrontMatter?.date
|
const frontMatterTimestamp = lastUpdateFrontMatter?.date
|
||||||
? new Date(lastUpdateFrontMatter.date).getTime() / 1000
|
? new Date(lastUpdateFrontMatter.date).getTime()
|
||||||
: undefined;
|
: undefined;
|
||||||
|
|
||||||
// We try to minimize git last update calls
|
// We try to minimize git last update calls
|
||||||
|
|
Loading…
Add table
Reference in a new issue