Skip to content

Commit

Permalink
feat(www): Filter posts by date (#9400)
Browse files Browse the repository at this point in the history
Closes #9298
  • Loading branch information
jgierer12 authored and pieh committed Nov 5, 2018
1 parent b7d3b74 commit bbf1f15
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
28 changes: 23 additions & 5 deletions www/gatsby-node.js
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`)

let ecosystemFeaturedItems

Expand Down Expand Up @@ -156,6 +157,7 @@ exports.createPages = ({ graphql, actions }) => {
fields {
slug
package
released
}
frontmatter {
title
Expand Down Expand Up @@ -251,9 +253,13 @@ exports.createPages = ({ graphql, actions }) => {
return undefined
})

const releasedBlogPosts = blogPosts.filter(post =>
_.get(post, `node.fields.released`)
)

// 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 @@ -270,7 +276,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 @@ -285,7 +293,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 @@ -409,7 +417,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 @@ -442,6 +450,16 @@ exports.onCreateNode = ({ node, actions, getNode, reporter }) => {
} else {
slug = `/${parsedFilePath.dir}/`
}

// Set released status for blog posts.
if (_.includes(parsedFilePath.dir, `blog`)) {
let released = false
const date = _.get(node, `frontmatter.date`)
if (date) {
released = moment().isSameOrAfter(moment.utc(date))
}
createNodeField({ node, name: `released`, value: released })
}
}
// Add slugs for package READMEs.
if (
Expand Down Expand Up @@ -605,7 +623,7 @@ exports.onCreateNode = ({ node, actions, getNode, reporter }) => {

exports.onCreatePage = ({ page, actions }) => {
// add lists of featured items to Ecosystem page
if (page.path === "/ecosystem/") {
if (page.path === `/ecosystem/`) {
const { createPage, deletePage } = actions
const oldPage = Object.assign({}, page)

Expand Down
1 change: 1 addition & 0 deletions www/src/pages/index.js
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
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
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

0 comments on commit bbf1f15

Please sign in to comment.