Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Refactor lookups to use IDs rather than paths
  • Loading branch information
adamyonk committed Oct 1, 2019
1 parent f835cf1 commit 2f930bc
Show file tree
Hide file tree
Showing 37 changed files with 87 additions and 113 deletions.
93 changes: 18 additions & 75 deletions gatsby-config.js
@@ -1,3 +1,5 @@
const { formatPath } = require("./src/util/formatPath")

module.exports = {
siteMetadata: {
title: "Adam Jahnke ☕️🏍 (adamyonk)",
Expand All @@ -8,65 +10,6 @@ module.exports = {
plugins: [
{ resolve: "@rhysforyou/gatsby-plugin-react-helmet-async" },
{ resolve: "gatsby-plugin-styled-jsx" },
// {
// resolve: `gatsby-plugin-feed`,
// options: {
// query: `
// {
// site {
// siteMetadata {
// title
// description
// siteUrl
// site_url: siteUrl
// }
// }
// }
// `,
// feeds: [
// {
// serialize: ({ query: { site, allMarkdownRemark } }) => {
// return allMarkdownRemark.edges.map(edge => {
// return Object.assign({}, edge.node.frontmatter, {
// description: edge.node.excerpt,
// date: edge.node.frontmatter.date,
// url: site.siteMetadata.siteUrl + edge.node.frontmatter.path,
// guid: site.siteMetadata.siteUrl + edge.node.frontmatter.path,
// custom_elements: [{ "content:encoded": edge.node.html }],
// })
// })
// },
// query: `
// {
// allMarkdownRemark(
// limit: 1000,
// sort: { order: DESC, fields: [frontmatter___date] },
// filter: {frontmatter: {
// published: { eq: true },
// templateKey: { eq: "post" },
// }}
// ) {
// edges {
// node {
// excerpt
// html
// id
// frontmatter {
// date
// path
// title
// }
// }
// }
// }
// }
// `,
// output: "/rss.xml",
// title: "Gatsby RSS Feed",
// },
// ],
// },
// },
{
resolve: "gatsby-plugin-feed-generator",
options: {
Expand Down Expand Up @@ -97,10 +40,10 @@ module.exports = {
node {
html
id
fileAbsolutePath
frontmatter {
date
updated
path
title
}
}
Expand All @@ -116,12 +59,11 @@ module.exports = {
}) => {
return edges.map(
({
edge: {
node: {
html,
id,
frontmatter: { link, title, date, updated, path },
},
node: {
html,
id,
fileAbsolutePath,
frontmatter: { link, title, date, updated },
},
}) => {
return {
Expand All @@ -132,7 +74,8 @@ module.exports = {
html,
id,
title,
url: site.siteMetadata.siteUrl + path,
url:
site.siteMetadata.siteUrl + formatPath(fileAbsolutePath),
}
},
)
Expand All @@ -149,10 +92,10 @@ module.exports = {
edges {
node {
html
fileAbsolutePath
id
frontmatter {
date
path
price
title
updated
Expand All @@ -170,12 +113,11 @@ module.exports = {
}) => {
return edges.map(
({
edge: {
node: {
html,
id,
frontmatter: { price, title, date, updated, path },
},
node: {
html,
id,
fileAbsolutePath,
frontmatter: { price, title, date, updated },
},
}) => {
return {
Expand All @@ -184,7 +126,8 @@ module.exports = {
date_published: date,
html,
title: `${title}: ${price}`,
url: site.siteMetadata.siteUrl + path,
url:
site.siteMetadata.siteUrl + formatPath(fileAbsolutePath),
}
},
)
Expand Down
61 changes: 35 additions & 26 deletions gatsby-node.js
@@ -1,5 +1,6 @@
require("@babel/polyfill")
const path = require("path")
const { formatPath } = require("./src/util/formatPath")

exports.createPages = ({ actions, graphql }) => {
const { createPage } = actions
Expand All @@ -14,8 +15,10 @@ exports.createPages = ({ actions, graphql }) => {
node {
excerpt(pruneLength: 400)
html
fileAbsolutePath
id
frontmatter {
tags
templateKey
path
date
Expand All @@ -24,46 +27,51 @@ exports.createPages = ({ actions, graphql }) => {
}
}
}
posts: allMarkdownRemark(
filter: { frontmatter: { templateKey: { eq: "post" } } }
limit: 1000
) {
edges {
node {
frontmatter {
tags
}
}
}
}
}
`).then(result => {
if (result.errors) {
return Promise.reject(result.errors)
`).then(({ errors, data }) => {
if (errors) {
return Promise.reject(errors)
}
result.data.posts.edges

// Generate tag index pages
data.pages.edges
// Loop through all the posts and gather unique tags
.reduce((a, { node: { frontmatter: { tags } } }) => {
if (!tags) {
return a
}
for (const tag of tags) {
a.add(tag)
}
return a
}, new Set())
// Create an index for each tag
.forEach(tag => {
createPage({
path: `/tags/${tag}`,
component: path.resolve(`src/templates/tag.js`),
context: { tag }, // additional data can be passed via context
context: { tag },
})
})
result.data.pages.edges.forEach(({ node }) => {
createPage({
path: node.frontmatter.path,
component: path.resolve(
`src/templates/${String(node.frontmatter.templateKey || "page")}.js`,
),
context: {}, // additional data can be passed via context
})
})

// Generate all pages
data.pages.edges.forEach(
({
node: {
fileAbsolutePath,
id,
frontmatter: { tags, templateKey },
},
}) => {
createPage({
path: formatPath(fileAbsolutePath),
component: path.resolve(
`src/templates/${String(templateKey || "page")}.js`,
),
context: { id }, // additional data can be passed via context
})
},
)
})
}

Expand All @@ -75,6 +83,7 @@ exports.createSchemaCustomization = ({ actions: { createTypes } }) => {
}
type Frontmatter {
link: String
tags: [String!]
}
`
createTypes(typeDefs)
Expand Down
6 changes: 4 additions & 2 deletions src/components/PostList.js
Expand Up @@ -2,14 +2,16 @@ import React from "react"
import Link from "gatsby-link"
import Date from "./Date"
import Tags from "./Tags"
import { formatPath } from "../util/formatPath"

export default ({ posts }) => (
<ul className="posts">
{posts.map(
({
node: {
id,
frontmatter: { date, path, price, title, tags, updated },
fileAbsolutePath,
frontmatter: { date, price, title, tags, updated },
},
}) => (
<li key={id}>
Expand All @@ -18,7 +20,7 @@ export default ({ posts }) => (
<Date date={updated || date} />
</div>
)}
<Link to={path}>{title}</Link>
<Link to={formatPath(fileAbsolutePath)}>{title}</Link>
{price && (
<div className="tags">
{parseFloat(price).toLocaleString("en-US", {
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions src/pages/for-sale.js
Expand Up @@ -29,6 +29,7 @@ export const tagPageQuery = graphql`
) {
edges {
node {
fileAbsolutePath
id
frontmatter {
date
Expand Down
1 change: 1 addition & 0 deletions src/pages/index.js
Expand Up @@ -59,6 +59,7 @@ export const pageQuery = graphql`
edges {
node {
id
fileAbsolutePath
frontmatter {
date
path
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 10 additions & 0 deletions src/pages/posts/2019-09-30-simple-post-tagging-in-gatsby.md
@@ -0,0 +1,10 @@
---
templateKey: "post"
title: "Simple Post Tagging in Gatsby"
date: 2019-09-30T11:29:53-05:00
updated: 2019-09-30T11:29:53-05:00
tags: [gatsby]
published: true
---

Gatsby
@@ -1,7 +1,6 @@
---
templateKey: "post"
title: Screencasting Using QuickTime
path: /screencasting-using-quicktime
date: 2018-12-29 11:13:31 -0500
tags: [tools]
published: true
Expand Down
4 changes: 2 additions & 2 deletions src/templates/page.js
Expand Up @@ -38,8 +38,8 @@ export default ({
}

export const pageQuery = graphql`
query Page($path: String!) {
markdownRemark(frontmatter: { path: { eq: $path } }) {
query Page($id: String!) {
markdownRemark(id: { eq: $id }) {
html
frontmatter {
path
Expand Down
4 changes: 2 additions & 2 deletions src/templates/post.js
Expand Up @@ -36,8 +36,8 @@ export default ({
}

export const pageQuery = graphql`
query BlogPostByPath($path: String!) {
markdownRemark(frontmatter: { path: { eq: $path } }) {
query BlogPostByPath($id: String!) {
markdownRemark(id: { eq: $id }) {
html
timeToRead
frontmatter {
Expand Down
4 changes: 2 additions & 2 deletions src/templates/sale.js
Expand Up @@ -53,8 +53,8 @@ export default ({
}

export const pageQuery = graphql`
query SalePostByPath($path: String!) {
markdownRemark(frontmatter: { path: { eq: $path } }) {
query SalePostByPath($id: String!) {
markdownRemark(id: { eq: $id }) {
html
frontmatter {
date
Expand Down
7 changes: 4 additions & 3 deletions src/templates/tag.js
Expand Up @@ -4,13 +4,13 @@ import { graphql } from "gatsby"
import PostList from "../components/PostList"
import Layout from "../components/layout"

export default ({ data, pageContext }) => {
export default ({ data, pageContext: { tag } }) => {
const { edges: posts = [] } = (data && data.allMarkdownRemark) || {}
return (
<Layout>
<section>
<Helmet title={`Posts tagged "${pageContext.tag}" | Adam Jahnke`} />
<h1>Posts tagged &laquo;{pageContext.tag}&raquo;</h1>
<Helmet title={`Posts tagged "${tag}" | Adam Jahnke`} />
<h1>Posts tagged &laquo;{tag}&raquo;</h1>
<PostList posts={posts} />
</section>
</Layout>
Expand All @@ -27,6 +27,7 @@ export const tagPageQuery = graphql`
) {
edges {
node {
fileAbsolutePath
id
frontmatter {
date
Expand Down
8 changes: 8 additions & 0 deletions src/util/formatPath.js
@@ -0,0 +1,8 @@
exports.formatPath = path => {
// Generate page paths based on their location in the filesystem
return path
.replace(/.*\/src\/pages/, "") // Use path from /src/pages onwards
.replace(/\.md$/, "") // strip extension
.replace(/\/index$/, "") // strip "index" for textbundle-types
.replace(/\d\d(\d\d)?-\d\d-\d\d-/, "") // strip date
}

1 comment on commit 2f930bc

@vercel
Copy link

@vercel vercel bot commented on 2f930bc Oct 1, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.