diff --git a/packages/saber/lib/Source.js b/packages/saber/lib/Source.js index af40bd6f3..5943edfc9 100644 --- a/packages/saber/lib/Source.js +++ b/packages/saber/lib/Source.js @@ -61,11 +61,17 @@ module.exports = class Source { getPage(file) { const { api } = this - const slug = file.relative - // Remove leading _posts/ - .replace(/^_posts\//, '') - // Remove extension - .replace(/\.[a-z]+$/i, '') + + // A regex parsing RFC3339 date followed by {_,-}, and ended by some characters + const fileNameRegex = /^(((\d{4})-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])(T([01]\d|2[0-3]):([0-5]\d):([0-5]\d|60)(\.\d+)?(Z|(\+|-)([01]\d|2[0-3]):([0-5]\d)))?)(_|-))?(.+$)/ + const parsedFileName = fileNameRegex.exec( + file.relative + // Remove leading _posts/ + .replace(/^_posts\//, '') + // Remove extension + .replace(/\.[a-z]+$/i, '') + ) + const slug = parsedFileName[16] const page = { attributes: { @@ -99,8 +105,14 @@ module.exports = class Source { // These attributes depend on other attributes // And transformers can update the attributes // So we set them after the transformers + + // Read createdAt from page attribute + // Or fallback to the date in fileName or the birthtime of the file page.attributes.createdAt = new Date( - page.attributes.createdAt || page.attributes.date || file.birthtime + page.attributes.createdAt || + page.attributes.date || + parsedFileName[2] || + file.birthtime ) page.attributes.type =