refactor(plugin-blog): style improvements in blogUtils (#5338)

* Minor refactor

Signed-off-by: Josh-Cena <sidachen2003@gmail.com>

* Revert forEach change

Signed-off-by: Josh-Cena <sidachen2003@gmail.com>
This commit is contained in:
Joshua Chen 2021-08-11 21:40:33 +08:00 committed by GitHub
parent a2ab4d33c8
commit 0c0c14120e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -43,7 +43,7 @@ export function getSourceToPermalink(
); );
} }
const DATE_FILENAME_REGEX = /^(?<date>\d{4}[-\/]\d{1,2}[-\/]\d{1,2})[-\/]?(?<text>.*?)(\/index)?.mdx?$/; const DATE_FILENAME_REGEX = /^(?<date>\d{4}[-/]\d{1,2}[-/]\d{1,2})[-/]?(?<text>.*?)(\/index)?.mdx?$/;
type ParsedBlogFileName = { type ParsedBlogFileName = {
date: Date | undefined; date: Date | undefined;
@ -60,22 +60,13 @@ export function parseBlogFileName(
const text = dateFilenameMatch.groups!.text!; const text = dateFilenameMatch.groups!.text!;
// Always treat dates as UTC by adding the `Z` // Always treat dates as UTC by adding the `Z`
const date = new Date(`${dateString}Z`); const date = new Date(`${dateString}Z`);
// TODO use replaceAll once we require NodeJS 16 const slugDate = dateString.replace(/-/g, '/');
const slugDate = dateString.replace('-', '/').replace('-', '/');
const slug = `/${slugDate}/${text}`; const slug = `/${slugDate}/${text}`;
return { return {date, text, slug};
date,
text,
slug,
};
} else { } else {
const text = blogSourceRelative.replace(/(\/index)?\.mdx?$/, ''); const text = blogSourceRelative.replace(/(\/index)?\.mdx?$/, '');
const slug = `/${text}`; const slug = `/${text}`;
return { return {date: undefined, text, slug};
date: undefined,
text,
slug,
};
} }
} }
@ -204,11 +195,10 @@ async function processBlogSourceFile(
return new Date(frontMatter.date); return new Date(frontMatter.date);
} else if (parsedBlogFileName.date) { } else if (parsedBlogFileName.date) {
return parsedBlogFileName.date; return parsedBlogFileName.date;
} else { }
// Fallback to file create time // Fallback to file create time
return (await fs.stat(blogSourceAbsolute)).birthtime; return (await fs.stat(blogSourceAbsolute)).birthtime;
} }
}
const date = await getDate(); const date = await getDate();
const formattedDate = formatBlogPostDate(i18n.currentLocale, date); const formattedDate = formatBlogPostDate(i18n.currentLocale, date);
@ -246,9 +236,8 @@ async function processBlogSourceFile(
]); ]);
return getEditUrl(blogPathRelative, contentPathEditUrl); return getEditUrl(blogPathRelative, contentPathEditUrl);
} else {
return undefined;
} }
return undefined;
} }
return { return {