feat(v2): docs last update timestamp and author (#1829)

* feat(v2): docs last update timestamp and author

* misc(v2): changelog

* misc(v2): better error messages
This commit is contained in:
Yangshun Tay 2019-10-10 21:45:39 -07:00 committed by GitHub
parent 54e9e025d8
commit 4fe6ae3c24
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 305 additions and 36 deletions

View file

@ -8,21 +8,37 @@
import fs from 'fs-extra';
import path from 'path';
import {parse, normalizeUrl} from '@docusaurus/utils';
import {Order, MetadataRaw} from './types';
import {DocusaurusConfig} from '@docusaurus/types';
export default async function processMetadata(
source: string,
docsDir: string,
order: Order,
siteConfig: Partial<DocusaurusConfig>,
docsBasePath: string,
siteDir: string,
editUrl?: string,
): Promise<MetadataRaw> {
const filepath = path.join(docsDir, source);
import lastUpdate from './lastUpdate';
import {Order, MetadataRaw} from './types';
const fileString = await fs.readFile(filepath, 'utf-8');
type Args = {
source: string;
docsDir: string;
order: Order;
siteConfig: Partial<DocusaurusConfig>;
docsBasePath: string;
siteDir: string;
editUrl?: string;
showLastUpdateAuthor?: boolean;
showLastUpdateTime?: boolean;
};
export default async function processMetadata({
source,
docsDir,
order,
siteConfig,
docsBasePath,
siteDir,
editUrl,
showLastUpdateAuthor,
showLastUpdateTime,
}: Args): Promise<MetadataRaw> {
const filePath = path.join(docsDir, source);
const fileString = await fs.readFile(filePath, 'utf-8');
const {frontMatter: metadata = {}, excerpt} = parse(fileString);
// Default id is the file name.
@ -52,7 +68,7 @@ export default async function processMetadata(
}
// Cannot use path.join() as it resolves '../' and removes the '@site'. Let webpack loader resolve it.
const aliasedPath = `@site/${path.relative(siteDir, filepath)}`;
const aliasedPath = `@site/${path.relative(siteDir, filePath)}`;
metadata.source = aliasedPath;
// Build the permalink.
@ -87,5 +103,20 @@ export default async function processMetadata(
metadata.editUrl = normalizeUrl([editUrl, source]);
}
if (showLastUpdateAuthor || showLastUpdateTime) {
const fileLastUpdateData = lastUpdate(filePath);
if (fileLastUpdateData) {
const {author, timestamp} = fileLastUpdateData;
if (showLastUpdateAuthor && author) {
metadata.lastUpdatedBy = author;
}
if (showLastUpdateTime && timestamp) {
metadata.lastUpdatedAt = timestamp;
}
}
}
return metadata as MetadataRaw;
}