Skip to content

Commit

Permalink
feat: support extracting datetime from post filename (#32)
Browse files Browse the repository at this point in the history
closes #31 

* feat: support extracting datetime from post filename

* parse fileName once
  • Loading branch information
chawyehsu authored and egoist committed Mar 26, 2019
1 parent ca88e54 commit 3334bce
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions packages/saber/lib/Source.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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 =
Expand Down

0 comments on commit 3334bce

Please sign in to comment.