Skip to content

Commit

Permalink
only disable warnings + fix frontmatter date type
Browse files Browse the repository at this point in the history
  • Loading branch information
slorber committed May 14, 2021
1 parent 28cb3aa commit 3a65621
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 22 deletions.
21 changes: 8 additions & 13 deletions packages/docusaurus-plugin-content-blog/src/blogFrontMatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export type BlogPostFrontMatter = {
tags?: (string | Tag)[];
slug?: string;
draft?: boolean;
date?: string;
date?: Date;

author?: string;
author_title?: string;
Expand Down Expand Up @@ -66,18 +66,13 @@ const BlogFrontMatterSchema = Joi.object<BlogPostFrontMatter>({
keywords: Joi.array().items(Joi.string().required()),
hide_table_of_contents: Joi.boolean(),

/*
TODO re-enable later, our v1 blog posts use those frontmatter fields
authorURL: Joi.string().uri().warning('deprecate.error', {
alternative: '"author_url"',
}),
authorTitle: Joi.string().warning('deprecate.error', {
alternative: '"author_title"',
}),
authorImageURL: Joi.string().uri().warning('deprecate.error', {
alternative: '"author_image_url"',
}),
*/
// TODO re-enable warnings later, our v1 blog posts use those older frontmatter fields
authorURL: Joi.string().uri(),
// .warning('deprecate.error', { alternative: '"author_url"'}),
authorTitle: Joi.string(),
// .warning('deprecate.error', { alternative: '"author_title"'}),
authorImageURL: Joi.string().uri(),
// .warning('deprecate.error', { alternative: '"author_image_url"'}),
})
.unknown()
.messages({
Expand Down
17 changes: 9 additions & 8 deletions packages/docusaurus-plugin-content-blog/src/blogUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export function getSourceToPermalink(

// YYYY-MM-DD-{name}.mdx?
// Prefer named capture, but older Node versions do not support it.
const FILENAME_PATTERN = /^(\d{4}-\d{1,2}-\d{1,2})-?(.*?).mdx?$/;
const DATE_FILENAME_PATTERN = /^(\d{4}-\d{1,2}-\d{1,2})-?(.*?).mdx?$/;

function toUrl({date, link}: DateLink) {
return `${date
Expand Down Expand Up @@ -165,24 +165,24 @@ export async function generateBlogPosts(
);
}

let date;
let date: Date | undefined;
// Extract date and title from filename.
const match = blogFileName.match(FILENAME_PATTERN);
const dateFilenameMatch = blogFileName.match(DATE_FILENAME_PATTERN);
let linkName = blogFileName.replace(/\.mdx?$/, '');

if (match) {
const [, dateString, name] = match;
if (dateFilenameMatch) {
const [, dateString, name] = dateFilenameMatch;
date = new Date(dateString);
linkName = name;
}

// Prefer user-defined date.
if (frontMatter.date) {
date = new Date(frontMatter.date);
date = frontMatter.date;
}

// Use file create time for blog.
date = date || (await fs.stat(source)).birthtime;
date = date ?? (await fs.stat(source)).birthtime;
const formattedDate = new Intl.DateTimeFormat(i18n.currentLocale, {
day: 'numeric',
month: 'long',
Expand All @@ -193,7 +193,8 @@ export async function generateBlogPosts(
const description = frontMatter.description ?? excerpt ?? '';

const slug =
frontMatter.slug || (match ? toUrl({date, link: linkName}) : linkName);
frontMatter.slug ||
(dateFilenameMatch ? toUrl({date, link: linkName}) : linkName);

const permalink = normalizeUrl([baseUrl, routeBasePath, slug]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export function validateFrontMatter<T>(

if (error) {
const frontMatterString = JSON.stringify(frontMatter, null, 2);
const errorDetails = (error as Joi.ValidationError).details;
const errorDetails = error.details;
const invalidFields = errorDetails.map(({path}) => path).join(', ');
const errorMessages = errorDetails
.map(({message}) => ` - ${message}`)
Expand Down

0 comments on commit 3a65621

Please sign in to comment.