Skip to content

Commit

Permalink
feat: uses git history to determine date last modified
Browse files Browse the repository at this point in the history
  • Loading branch information
Cayla Hamann committed Jul 10, 2020
1 parent 6fa13dd commit 45e54a0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
12 changes: 12 additions & 0 deletions gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const path = require(`path`);
const { execSync } = require('child_process');

const getFileRelativePath = (absolutePath) =>
absolutePath.replace(`${process.cwd()}/`, '');
Expand Down Expand Up @@ -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;
Expand Down
10 changes: 4 additions & 6 deletions src/templates/GuideTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -28,7 +28,7 @@ const GuideTemplate = ({ data }) => {
</div>
<MDXContainer>{body}</MDXContainer>
<div className={styles.lastUpdated}>
{`Page last modified on ${parent.modifiedTime}`}
{`Page last modified on ${fields.gitAuthorTime}`}
</div>
</Layout>
);
Expand All @@ -48,10 +48,8 @@ export const pageQuery = graphql`
title
description
}
parent {
... on File {
modifiedTime(formatString: "MMMM DD, YYYY")
}
fields {
gitAuthorTime(formatString: "MMMM DD, YYYY")
}
}
}
Expand Down

0 comments on commit 45e54a0

Please sign in to comment.