feat(content-blog): infer blog post date from git history (#6593)

This commit is contained in:
Felipe Santos 2022-02-09 13:18:32 -03:00 committed by GitHub
parent 665d164351
commit 6996ed2f2f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 127 additions and 57 deletions

View file

@ -5,14 +5,13 @@
* LICENSE file in the root directory of this source tree.
*/
import fs from 'fs-extra';
import path from 'path';
import pluginContentBlog from '../index';
import type {DocusaurusConfig, LoadContext, I18n} from '@docusaurus/types';
import {PluginOptionSchema} from '../pluginOptionSchema';
import type {BlogPost} from '../types';
import type {Joi} from '@docusaurus/utils-validation';
import {posixPath} from '@docusaurus/utils';
import {posixPath, getFileCommitDate} from '@docusaurus/utils';
import type {
PluginOptions,
EditUrlFunction,
@ -425,14 +424,15 @@ describe('loadBlog', () => {
);
const blogPosts = await getBlogPosts(siteDir);
const noDateSource = path.posix.join('@site', PluginPath, 'no date.md');
const noDateSourceBirthTime = (
await fs.stat(noDateSource.replace('@site', siteDir))
).birthtime;
const noDateSourceFile = path.posix.join(siteDir, PluginPath, 'no date.md');
// we know the file exist and we know we have git
const result = getFileCommitDate(noDateSourceFile, {age: 'oldest'});
const noDateSourceTime = result.date;
const formattedDate = Intl.DateTimeFormat('en', {
day: 'numeric',
month: 'long',
year: 'numeric',
}).format(noDateSourceBirthTime);
}).format(noDateSourceTime);
expect({
...getByTitle(blogPosts, 'no date').metadata,
@ -445,7 +445,7 @@ describe('loadBlog', () => {
title: 'no date',
description: `no date`,
authors: [],
date: noDateSourceBirthTime,
date: noDateSourceTime,
formattedDate,
frontMatter: {},
tags: [],

View file

@ -27,6 +27,7 @@ import {
Globby,
normalizeFrontMatterTags,
groupTaggedItems,
getFileCommitDate,
getContentPathList,
} from '@docusaurus/utils';
import type {LoadContext} from '@docusaurus/types';
@ -242,8 +243,17 @@ async function processBlogSourceFile(
} else if (parsedBlogFileName.date) {
return parsedBlogFileName.date;
}
// Fallback to file create time
return (await fs.stat(blogSourceAbsolute)).birthtime;
try {
const result = getFileCommitDate(blogSourceAbsolute, {
age: 'oldest',
includeAuthor: false,
});
return result.date;
} catch (e) {
logger.error(e);
return (await fs.stat(blogSourceAbsolute)).birthtime;
}
}
const date = await getDate();