refactor: better regex & path parsing for metadata (#1492)

* Don't need using `toString` after `path.basename`

* Use `parse` instead of `basename+extname`
This commit is contained in:
Hongarc 2019-05-19 15:54:29 +07:00 committed by Endi
parent 9a18b7430a
commit 89582c7b7a

View file

@ -143,7 +143,7 @@ function processMetadata(file, refDir) {
const rawContent = result.rawContent; const rawContent = result.rawContent;
if (!metadata.id) { if (!metadata.id) {
metadata.id = path.basename(file, path.extname(file)); metadata.id = path.parse(file).name;
} }
if (metadata.id.includes('/')) { if (metadata.id.includes('/')) {
throw new Error('Document id cannot include "/".'); throw new Error('Document id cannot include "/".');
@ -374,10 +374,7 @@ function generateMetadataBlog() {
} }
const metadata = blog.getMetadata(file); const metadata = blog.getMetadata(file);
// Extract, YYYY, MM, DD from the file name // Extract, YYYY, MM, DD from the file name
const filePathDateArr = path const filePathDateArr = path.basename(file).split('-');
.basename(file)
.toString()
.split('-');
metadata.date = new Date( metadata.date = new Date(
`${filePathDateArr[0]}-${filePathDateArr[1]}-${ `${filePathDateArr[0]}-${filePathDateArr[1]}-${
filePathDateArr[2] filePathDateArr[2]