misc(v2): change blog front matter to snake_case (#1989)

* misc(v2): change blog front matter to snake_case

* Fix blog image url

* Add docs for change

* More migration docs
This commit is contained in:
Yangshun Tay 2019-11-19 00:08:53 -08:00 committed by Endi
parent edf4c16c93
commit 65965e060a
11 changed files with 65 additions and 44 deletions

View file

@ -2,10 +2,9 @@
id: hola
title: Hola
author: Gao Wei
authorTitle: Docusaurus Core Team
authorURL: https://github.com/wgao19
authorImageURL: https://avatars1.githubusercontent.com/u/2055384?v=4
authorTwitter: wgao19
author_title: Docusaurus Core Team
author_url: https://github.com/wgao19
author_image_url: https://avatars1.githubusercontent.com/u/2055384?v=4
tags: [hola, docusaurus]
---

View file

@ -2,10 +2,9 @@
id: hello-world
title: Hello
author: Endilie Yacop Sucipto
authorTitle: Maintainer of Docusaurus
authorURL: https://github.com/endiliey
authorImageURL: https://avatars1.githubusercontent.com/u/17883920?s=460&v=4
authorTwitter: endiliey
author_title: Maintainer of Docusaurus
author_url: https://github.com/endiliey
author_image_url: https://avatars1.githubusercontent.com/u/17883920?s=460&v=4
tags: [hello, docusaurus]
---

View file

@ -2,10 +2,9 @@
id: welcome
title: Welcome
author: Yangshun Tay
authorTitle: Front End Engineer @ Facebook
authorURL: https://github.com/yangshun
authorImageURL: https://avatars0.githubusercontent.com/u/1315101?s=400&v=4
authorTwitter: yangshunz
author_title: Front End Engineer @ Facebook
author_url: https://github.com/yangshun
author_image_url: https://avatars0.githubusercontent.com/u/1315101?s=400&v=4
tags: [facebook, hello, docusaurus]
---

View file

@ -1,3 +1,10 @@
/**
* Copyright (c) 2017-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import fs from 'fs-extra';
import globby from 'globby';
import path from 'path';
@ -11,7 +18,7 @@ export function truncate(fileString: string, truncateMarker: RegExp | string) {
}
// YYYY-MM-DD-{name}.mdx?
// prefer named capture, but old node version do not support
// prefer named capture, but old Node version does not support.
const FILENAME_PATTERN = /^(\d{4}-\d{1,2}-\d{1,2})-?(.*?).mdx?$/;
function toUrl({date, link}: DateLink) {
@ -93,7 +100,8 @@ export async function generateBlogPosts(
await Promise.all(
blogFiles.map(async (relativeSource: string) => {
// Cannot use path.join() as it resolves '../' and removes the '@site'. Let webpack loader resolve it.
// Cannot use path.join() as it resolves '../' and removes the '@site'.
// Let webpack loader resolve it.
const source = path.join(blogDir, relativeSource);
const aliasedSource = `@site/${path.relative(siteDir, source)}`;
const blogFileName = path.basename(relativeSource);
@ -102,7 +110,7 @@ export async function generateBlogPosts(
const {frontMatter, excerpt} = parse(fileString);
let date;
// extract date and title from filename
// Extract date and title from filename.
const match = blogFileName.match(FILENAME_PATTERN);
let linkName = blogFileName.replace(/\.mdx?$/, '');
if (match) {
@ -110,11 +118,11 @@ export async function generateBlogPosts(
date = new Date(dateString);
linkName = name;
}
// prefer usedefined date
// Prefer user-defined date.
if (frontMatter.date) {
date = new Date(frontMatter.date);
}
// use file create time for blog
// Use file create time for blog.
date = date || (await fs.stat(source)).birthtime;
frontMatter.title = frontMatter.title || linkName;
@ -135,6 +143,7 @@ export async function generateBlogPosts(
});
}),
);
blogPosts.sort(
(a, b) => b.metadata.date.getTime() - a.metadata.date.getTime(),
);

View file

@ -4,6 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import fs from 'fs-extra';
import _ from 'lodash';
import path from 'path';

View file

@ -14,6 +14,21 @@ import MDXComponents from '@theme/MDXComponents';
import styles from './styles.module.css';
const MONTHS = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December',
];
function BlogPostItem(props) {
const {
children,
@ -23,32 +38,20 @@ function BlogPostItem(props) {
isBlogPostPage = false,
} = props;
const {date, permalink, tags} = metadata;
const {author, authorURL, authorTitle, authorFBID, title} = frontMatter;
const {author, title} = frontMatter;
const authorURL = frontMatter.author_url || frontMatter.authorURL;
const authorTitle = frontMatter.author_title || frontMatter.authorTitle;
const authorImageURL =
frontMatter.author_image_url || frontMatter.authorImageURL;
const renderPostHeader = () => {
const TitleHeading = isBlogPostPage ? 'h1' : 'h2';
const match = date.substring(0, 10).split('-');
const year = match[0];
const month = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December',
][parseInt(match[1], 10) - 1];
const month = MONTHS[parseInt(match[1], 10) - 1];
const day = parseInt(match[2], 10);
const authorImageURL = authorFBID
? `https://graph.facebook.com/${authorFBID}/picture/?height=200&width=200`
: frontMatter.authorImageURL;
return (
<header>
<TitleHeading