From 45e54a085dd8f90cf8e1c3b910b4c07946d0c9cd Mon Sep 17 00:00:00 2001 From: Cayla Hamann Date: Fri, 10 Jul 2020 11:20:27 -0400 Subject: [PATCH] feat: uses git history to determine date last modified --- gatsby-node.js | 12 ++++++++++++ src/templates/GuideTemplate.js | 10 ++++------ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/gatsby-node.js b/gatsby-node.js index cf7e593c1..c63b83cd5 100644 --- a/gatsby-node.js +++ b/gatsby-node.js @@ -1,4 +1,5 @@ const path = require(`path`); +const { execSync } = require('child_process'); const getFileRelativePath = (absolutePath) => absolutePath.replace(`${process.cwd()}/`, ''); @@ -59,6 +60,17 @@ exports.createPages = async ({ actions, graphql, reporter }) => { exports.onCreateNode = ({ node, actions }) => { // if we don't have a relative path, attempt to get one + if (node.internal.type === 'Mdx' && node.fileAbsolutePath) { + const gitAuthorTime = execSync( + `git log -1 --pretty=format:%aI ${node.fileAbsolutePath}` + ).toString(); + actions.createNodeField({ + node, + name: 'gitAuthorTime', + value: gitAuthorTime, + }); + } + if (node.context && !node.context.fileRelativePath) { const { createPage } = actions; const { path, component } = node; diff --git a/src/templates/GuideTemplate.js b/src/templates/GuideTemplate.js index 75d273bc5..cbcfcba64 100644 --- a/src/templates/GuideTemplate.js +++ b/src/templates/GuideTemplate.js @@ -11,7 +11,7 @@ import styles from './GuideTemplate.module.scss'; const GuideTemplate = ({ data }) => { const { mdx } = data; - const { frontmatter, body, parent } = mdx; + const { frontmatter, body, fields } = mdx; const { title, description, duration } = frontmatter; return ( @@ -28,7 +28,7 @@ const GuideTemplate = ({ data }) => { {body}
- {`Page last modified on ${parent.modifiedTime}`} + {`Page last modified on ${fields.gitAuthorTime}`}
); @@ -48,10 +48,8 @@ export const pageQuery = graphql` title description } - parent { - ... on File { - modifiedTime(formatString: "MMMM DD, YYYY") - } + fields { + gitAuthorTime(formatString: "MMMM DD, YYYY") } } }