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(gatsby-theme-blog-core): Support filter, limit options #98

Merged
merged 3 commits into from
Apr 12, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions packages/gatsby-theme-blog-core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ module.exports = {
| `mdxOtherwiseConfigured` | `false` | Set this flag `true` if `gatsby-plugin-mdx` is already configured for your site. |
| `excerptLength` | `140` | Length of the auto-generated excerpt of a blog post |
| `imageMaxWidth` | `1380` | Set the max width of images in your blog posts. This applies to your featured image in frontmatter as well. |
| `filter` | `{}` | Set the posts filter, for example: `{ frontmatter: { draft: {ne: true} } }` |
| `limit` | `1000` | Set the amount of pages that should be generated |

#### Example usage

Expand Down
31 changes: 22 additions & 9 deletions packages/gatsby-theme-blog-core/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,18 +268,28 @@ const PostsTemplate = require.resolve(`./src/templates/posts-query`)

exports.createPages = async ({ graphql, actions, reporter }, themeOptions) => {
const { createPage } = actions
const { basePath, imageMaxWidth } = withDefaults(themeOptions)
const { basePath, imageMaxWidth, filter, limit } = withDefaults(themeOptions)

const result = await graphql(`
{
allBlogPost(sort: { fields: [date, title], order: DESC }, limit: 1000) {
nodes {
id
slug
const result = await graphql(
`
query($limit: Int!, $filter: BlogPostFilterInput) {
allBlogPost(
sort: { fields: [date, title], order: DESC }
filter: $filter
limit: $limit
) {
nodes {
id
slug
}
}
}
`,
{
filter,
limit,
}
`)
)

if (result.errors) {
reporter.panic(result.errors)
Expand Down Expand Up @@ -310,6 +320,9 @@ exports.createPages = async ({ graphql, actions, reporter }, themeOptions) => {
createPage({
path: basePath,
component: PostsTemplate,
context: {},
context: {
filter,
limit,
},
})
}
8 changes: 6 additions & 2 deletions packages/gatsby-theme-blog-core/src/templates/posts-query.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import PostsPage from "../components/posts"
export default PostsPage

export const query = graphql`
query PostsQuery {
query PostsQuery($limit: Int!, $filter: BlogPostFilterInput) {
site {
siteMetadata {
title
Expand All @@ -14,7 +14,11 @@ export const query = graphql`
}
}
}
allBlogPost(sort: { fields: [date, title], order: DESC }, limit: 1000) {
allBlogPost(
sort: { fields: [date, title], order: DESC }
filter: $filter
limit: $limit
) {
nodes {
id
excerpt
Expand Down
5 changes: 4 additions & 1 deletion packages/gatsby-theme-blog-core/utils/default-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ module.exports = (themeOptions) => {
const assetPath = themeOptions.assetPath || `content/assets`
const excerptLength = themeOptions.excerptLength || 140
const imageMaxWidth = themeOptions.imageMaxWidth || 1380

const filter = themeOptions.filter || {}
const limit = themeOptions.limit || 1000
return {
basePath,
contentPath,
assetPath,
excerptLength,
imageMaxWidth,
filter,
limit,
}
}
2 changes: 2 additions & 0 deletions packages/gatsby-theme-blog/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ module.exports = {
| `excerptLength` | `140` | Length of the auto-generated excerpt of a blog post |
| `webfontURL` | `''` | URL for the webfont you'd like to include. Be sure that your local theme does not override it. |
| `imageMaxWidth` | `1380` | Set the max width of images in your blog posts. This applies to your featured image in frontmatter as well. |
| `filter` | `{}` | Set the posts filter, for example: `{ frontmatter: { draft: {ne: true} } }` |
| `limit` | `1000` | Set the amount of pages that should be generated |

#### Example configuration

Expand Down