Skip to content

Commit

Permalink
Improve blog post date parsing
Browse files Browse the repository at this point in the history
Previously the parsed date was automatically fomratted as UTC resulting
in a added time of 00:00:00.000+0000 which conflicts with the way the
date will be rendered through the UI.
This commit changes the date to only use the "pure" date format
(yyyy-MM-dd) without including any time and zone information.

GH-129
  • Loading branch information
arcticicestudio committed Mar 9, 2019
1 parent ee637ee commit c14b7c2
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 15 deletions.
13 changes: 6 additions & 7 deletions .gatsby/onCreateNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,17 @@ const { ROUTE_BLOG, ROUTE_DOCS } = require("../src/config/routes/mappings");

/**
* Extracts the date of a blog post from the given path using the `REGEX_BLOG_POST_DATE` regular expression.
* Note that the returned date is in UTC format to be independent of the time zone. The exact time of the day will be
* parsed from the blog posts frontmatter "publishTime" field.
* The exact time of the day will be parsed from the blog posts frontmatter "publishTime" field.
*
* @private
* @method extractDateFromPath
* @method extractBlogPostDateFromPath
* @param {string} path The path from which the blog post date should be extracted.
* @return {string|null} The extracted blog post date in UTC format as JSON string if the given path matches the
* regular expression, `null` otherwise.
* @return {string|null} The extracted blog post date if the given path matches the regular expression,
* `null` otherwise.
*/
const extractBlogPostDateFromPath = path => {
const date = REGEX_BLOG_POST_DATE.exec(path);
return date ? new Date(Date.UTC(date[1], date[2] - 1, date[3])).toJSON() : null;
return date ? `${date[1]}-${date[2]}-${date[3]}` : null;
};

/**
Expand Down Expand Up @@ -64,7 +63,7 @@ const onCreateNode = ({ node, getNode, actions }) => {
createNodeField({
node,
name: `${nodeFields.date.name}`,
value: extractBlogPostDateFromPath(relativePath)
value: date
});
createNodeField({
node,
Expand Down
4 changes: 2 additions & 2 deletions src/config/project/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ import { getSemVerFromGitDescribe } from "utils";
const COPYRIGHT_YEAR = new Date().getFullYear();

/**
* The date format for the "date-fns" library according to ISO 8601.
* The date format for the [date-fns](https://date-fns.org) library according to ISO 8601.
*
* @constant {string}
* @see https://date-fns.org/v1.29.0/docs/format
* @see https://en.wikipedia.org/wiki/ISO_8601
*/
const DATE_FORMAT = "yyyy-MM-ddTHH:mm:ssxx";
const DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ssxx";

/**
* The name of the current Git branch extracted from the local Git repository.
Expand Down
7 changes: 1 addition & 6 deletions src/data/graphql/fragments.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,12 @@ export const gqlFragmentContentDocsPageFrontmatter = graphql`

/**
* GraphQL fragment for the node fields of an MDX blog post.
* Gatsby internally relies on Moment.js for the provided `formatString` function.
* The allowed tokens can be found in the official Moment.js documentation.
*
* @see https://momentjs.com/docs/#/displaying/format
* @see https://www.gatsbyjs.org/docs/graphql-reference/#format
*/
export const gqlFragmentContentBlogPostFields = graphql`
fragment contentBlogPostFields on Mdx {
fields {
contentSourceType
date(formatString: "YYYY-MM-DDTHH:MM:ssZZ")
date
relativeDirectory
slug
slugParentRoute
Expand Down

0 comments on commit c14b7c2

Please sign in to comment.