mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-01 10:22:30 +02:00
feat(v2): blog support date front matter (#1702)
* feat(v2): blog support date front matter * feat(v2): blog support date front matter * feat(v2): blog support date front matter * feat(v2): blog support date front matter * Update CHANGELOG-2.x.md
This commit is contained in:
parent
d17a1ea9e3
commit
95ace0e4eb
6 changed files with 117 additions and 17 deletions
|
@ -11,13 +11,15 @@ const _ = require('lodash');
|
|||
const path = require('path');
|
||||
const {parse, normalizeUrl, docuHash} = require('@docusaurus/utils');
|
||||
|
||||
// TODO: Use a better slugify function that doesn't rely on a specific file extension.
|
||||
function fileToUrl(fileName) {
|
||||
return fileName
|
||||
.replace('-', '/')
|
||||
.replace('-', '/')
|
||||
.replace('-', '/')
|
||||
.replace(/\.mdx?$/, '');
|
||||
// YYYY-MM-DD-{name}.mdx?
|
||||
// prefer named capture, but old node version do not support
|
||||
const FILENAME_PATTERN = /^(\d{4}-\d{1,2}-\d{1,2})-?(.*?).mdx?$/;
|
||||
|
||||
function toUrl({date, link}) {
|
||||
return `${date
|
||||
.toISOString()
|
||||
.substring(0, '2019-01-01'.length)
|
||||
.replace(/-/g, '/')}/${link}`;
|
||||
}
|
||||
|
||||
const DEFAULT_OPTIONS = {
|
||||
|
@ -70,30 +72,40 @@ module.exports = function(context, opts) {
|
|||
const source = path.join(blogDir, relativeSource);
|
||||
const aliasedSource = `@site/${path.relative(siteDir, source)}`;
|
||||
const blogFileName = path.basename(relativeSource);
|
||||
// Extract, YYYY, MM, DD from the file name.
|
||||
const filePathDateArr = blogFileName.split('-');
|
||||
const date = new Date(
|
||||
`${filePathDateArr[0]}-${filePathDateArr[1]}-${
|
||||
filePathDateArr[2]
|
||||
}T06:00:00.000Z`,
|
||||
);
|
||||
|
||||
const fileString = await fs.readFile(source, 'utf-8');
|
||||
const {frontMatter, excerpt} = parse(fileString);
|
||||
|
||||
let date;
|
||||
// extract date and title from filename
|
||||
const match = blogFileName.match(FILENAME_PATTERN);
|
||||
let linkName = blogFileName.replace(/\.mdx?$/, '');
|
||||
if (match) {
|
||||
const [, dateString, name] = match;
|
||||
date = new Date(dateString);
|
||||
linkName = name;
|
||||
}
|
||||
// prefer usedefined date
|
||||
if (frontMatter.date) {
|
||||
date = new Date(frontMatter.date);
|
||||
}
|
||||
// use file create time for blog
|
||||
date = date || (await fs.stat(source)).birthtime;
|
||||
frontMatter.title = frontMatter.title || linkName;
|
||||
|
||||
blogPosts.push({
|
||||
id: frontMatter.id || blogFileName,
|
||||
id: frontMatter.id || frontMatter.title,
|
||||
metadata: {
|
||||
permalink: normalizeUrl([
|
||||
baseUrl,
|
||||
routeBasePath,
|
||||
frontMatter.id || fileToUrl(blogFileName),
|
||||
frontMatter.id || toUrl({date, link: linkName}),
|
||||
]),
|
||||
source: aliasedSource,
|
||||
description: frontMatter.description || excerpt,
|
||||
date,
|
||||
tags: frontMatter.tags,
|
||||
title: frontMatter.title || blogFileName,
|
||||
title: frontMatter.title,
|
||||
},
|
||||
});
|
||||
}),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue