From f18f92372aa71f62dfe3be2e79828ffe993e3e4c Mon Sep 17 00:00:00 2001 From: Xetera Date: Mon, 29 Jul 2019 19:31:49 -0700 Subject: [PATCH] added a graphql field for the link to the current markdown file on github --- gatsby-node.js | 8 ++++++++ scripts/buildHelpers.js | 12 ++++++++++++ 2 files changed, 20 insertions(+) diff --git a/gatsby-node.js b/gatsby-node.js index b33000d..31093c5 100644 --- a/gatsby-node.js +++ b/gatsby-node.js @@ -62,6 +62,14 @@ exports.onCreateNode = async ({ node, getNode, actions }) => { const { frontmatter } = node const isDoc = Boolean(!frontmatter.path) + const githubLink = helper.findMarkdownLink(node.fileAbsolutePath) + + createNodeField({ + node, + name: "githubLink", + value: githubLink, + }) + if (!isDoc) { return } diff --git a/scripts/buildHelpers.js b/scripts/buildHelpers.js index 6f86bfd..411709d 100644 --- a/scripts/buildHelpers.js +++ b/scripts/buildHelpers.js @@ -1,5 +1,17 @@ const DEFAULT_AVATAR_MODULO = 5 module.exports = { + findMarkdownLink(absolutePath) { + // We're assuming this path isn't going to change + const start = absolutePath.lastIndexOf("src/content") + + if (!start) { + throw Error(`Markdown file ${absolutePath} is outside of src/content`) + } + + const route = absolutePath.slice(start) + // Same as this + return `https://github.com/the-programmers-hangout/website/tree/master/${route}` + }, splitHash(identifier) { const hashLocation = identifier.lastIndexOf("#")