Skip to content
This repository has been archived by the owner on Apr 21, 2023. It is now read-only.

Commit

Permalink
fix: prevent gatsby file detection from breaking path field (#1226)
Browse files Browse the repository at this point in the history
Fixes #1088

This should fix the path issues that people intermittently run into.
Gatsby sees the relative path (and the parent node with an absolute
path), joins on the parent path and the relative path, and presumes it's
a file... which is normally good, but which here is not what we want
beacuse then we're getting a resolver of type file instead of type
string referring to the relative path of the document.

This seems to fix a (long standing?) bug where the "Edit this Page"
button is broken, as well. e.g. see [this page](https://reactjs.org/community/support.html) which _should_ have an Edit this Page button

I'd need to think more as to whether there's a cleaner fix here, but
this seems to work pretty well!
  • Loading branch information
DSchau authored and alexkrolick committed Oct 3, 2018
1 parent f2d39be commit 7d12082
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 18 deletions.
7 changes: 5 additions & 2 deletions gatsby/onCreateNode.js
Expand Up @@ -6,6 +6,8 @@

'use strict';

const path = require('path');

// Parse date information out of blog post filename.
const BLOG_POST_FILENAME_REGEX = /([0-9]+)\-([0-9]+)\-([0-9]+)\-(.+)\.md$/;

Expand All @@ -30,7 +32,7 @@ module.exports = exports.onCreateNode = ({node, actions, getNode}) => {
switch (node.internal.type) {
case 'MarkdownRemark':
const {permalink, redirect_from} = node.frontmatter;
const {relativePath} = getNode(node.parent);
const {relativePath, sourceInstanceName} = getNode(node.parent);

let slug = permalink;

Expand Down Expand Up @@ -71,10 +73,11 @@ module.exports = exports.onCreateNode = ({node, actions, getNode}) => {
});

// Used to generate a GitHub edit link.
// this presumes that the name in gastby-config.js refers to parent folder
createNodeField({
node,
name: 'path',
value: relativePath,
value: path.join(sourceInstanceName, relativePath),
});

// Used by createPages() above to register redirects.
Expand Down
2 changes: 1 addition & 1 deletion src/components/MarkdownPage/MarkdownPage.js
Expand Up @@ -112,7 +112,7 @@ const MarkdownPage = ({
<div css={{marginTop: 80}}>
<a
css={sharedStyles.articleLayout.editLink}
href={`https://github.com/reactjs/reactjs.org/tree/master/content/${
href={`https://github.com/reactjs/reactjs.org/tree/master/${
markdownRemark.fields.path
}`}>
Edit this page
Expand Down
4 changes: 1 addition & 3 deletions src/pages/docs/error-decoder.html.js
Expand Up @@ -107,9 +107,7 @@ export const pageQuery = graphql`
markdownRemark(fields: {slug: {eq: $slug}}) {
html
fields {
path {
id
}
path
}
frontmatter {
title
Expand Down
4 changes: 1 addition & 3 deletions src/templates/blog.js
Expand Up @@ -58,9 +58,7 @@ export const pageQuery = graphql`
}
fields {
date(formatString: "MMMM DD, YYYY")
path {
id
}
path
slug
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/templates/community.js
Expand Up @@ -33,9 +33,7 @@ export const pageQuery = graphql`
prev
}
fields {
path {
id
}
path
slug
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/templates/docs.js
Expand Up @@ -38,9 +38,7 @@ export const pageQuery = graphql`
prev
}
fields {
path {
id
}
path
slug
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/templates/tutorial.js
Expand Up @@ -36,9 +36,7 @@ export const pageQuery = graphql`
prev
}
fields {
path {
id
}
path
slug
}
}
Expand Down

0 comments on commit 7d12082

Please sign in to comment.