Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(www): Filter posts by date #9400

Merged
merged 7 commits into from
Nov 5, 2018
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions www/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const url = require(`url`)
const getpkgjson = require(`get-package-json-from-github`)
const parseGHUrl = require(`parse-github-url`)
const { GraphQLClient } = require(`graphql-request`)
const moment = require(`moment`)

require(`dotenv`).config({
path: `.env.${process.env.NODE_ENV}`,
Expand Down Expand Up @@ -143,6 +144,7 @@ exports.createPages = ({ graphql, actions }) => {
fields {
slug
package
released
}
frontmatter {
title
Expand Down Expand Up @@ -231,9 +233,13 @@ exports.createPages = ({ graphql, actions }) => {
return undefined
})

const releasedBlogPosts = blogPosts.filter(post =>
_.get(post, `node.fields.released`)
)
pieh marked this conversation as resolved.
Show resolved Hide resolved

// Create blog-list pages.
const postsPerPage = 8
const numPages = Math.ceil(blogPosts.length / postsPerPage)
const numPages = Math.ceil(releasedBlogPosts.length / postsPerPage)

Array.from({ length: numPages }).forEach((_, i) => {
createPage({
Expand All @@ -250,7 +256,9 @@ exports.createPages = ({ graphql, actions }) => {

// Create blog-post pages.
blogPosts.forEach((edge, index) => {
const next = index === 0 ? null : blogPosts[index - 1].node
let next = index === 0 ? null : blogPosts[index - 1].node
if (next && !_.get(next, `fields.released`)) next = null

const prev =
index === blogPosts.length - 1 ? null : blogPosts[index + 1].node

Expand All @@ -265,7 +273,7 @@ exports.createPages = ({ graphql, actions }) => {
})
})

const tagLists = blogPosts
const tagLists = releasedBlogPosts
.filter(post => _.get(post, `node.frontmatter.tags`))
.map(post => _.get(post, `node.frontmatter.tags`))

Expand Down Expand Up @@ -386,7 +394,7 @@ exports.createPages = ({ graphql, actions }) => {
})
}

// Create slugs for files.
// Create slugs for files, set released status for blog posts.
exports.onCreateNode = ({ node, actions, getNode, reporter }) => {
const { createNodeField } = actions
let slug
Expand Down Expand Up @@ -419,6 +427,12 @@ exports.onCreateNode = ({ node, actions, getNode, reporter }) => {
} else {
slug = `/${parsedFilePath.dir}/`
}

// Set released status for blog posts.
if (_.includes(parsedFilePath.dir, `blog`)) {
const released = moment().isSameOrAfter(_.get(node, `frontmatter.date`))
createNodeField({ node, name: `released`, value: released })
}
}
// Add slugs for package READMEs.
if (
Expand Down
1 change: 1 addition & 0 deletions www/src/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ export const pageQuery = graphql`
filter: {
frontmatter: { draft: { ne: true } }
fileAbsolutePath: { regex: "/docs.blog/" }
fields: { released: { eq: true } }
}
) {
edges {
Expand Down
1 change: 1 addition & 0 deletions www/src/templates/tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export const pageQuery = graphql`
filter: {
frontmatter: { tags: { in: [$tag] } }
fileAbsolutePath: { regex: "/docs.blog/" }
fields: { released: { eq: true } }
}
) {
totalCount
Expand Down
1 change: 1 addition & 0 deletions www/src/templates/template-blog-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export const pageQuery = graphql`
filter: {
frontmatter: { draft: { ne: true } }
fileAbsolutePath: { regex: "/docs.blog/" }
fields: { released: { eq: true } }
}
limit: $limit
skip: $skip
Expand Down