Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support extracting datetime from post filename #32

Merged
merged 2 commits into from
Mar 26, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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