From 75c4b3321b93e4a46eb558bee49bcf13914bf7f0 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Mon, 16 Sep 2019 23:38:26 +0300 Subject: [PATCH] Update tag pages Improve speed, move all of the querying to node --- gatsby-node.js | 17 ++++++++++--- src/docs/components/SnippetCard.js | 3 +-- src/docs/templates/TagPage.js | 41 ++++++------------------------ 3 files changed, 22 insertions(+), 39 deletions(-) diff --git a/gatsby-node.js b/gatsby-node.js index 72d4f2206..336855526 100644 --- a/gatsby-node.js +++ b/gatsby-node.js @@ -131,7 +131,7 @@ exports.createResolvers = ({ createResolvers }) => createResolvers({ const html = await resolver(node, args); return { full: `${html}`, - text: `${getTextualContent(html)}`, + text: `${getTextualContent(html, true)}`, code: `${optimizeAllNodes(getCodeBlocks(html).code)}`, example: `${optimizeAllNodes(getCodeBlocks(html).example)}` }; @@ -209,20 +209,29 @@ exports.createPages = ({ graphql, actions }) => { // Create tag pages. const tags = [...new Set( - snippets.map(snippet => (snippet.tags || {primary: null}).primary) + snippets.map(snippet => (snippet.node.tags || {primary: null}).primary) )] .filter(Boolean) .sort((a, b) => a.localeCompare(b)); - + tags.forEach(tag => { const tagPath = `/tag/${toKebabCase(tag)}/`; + const taggedSnippets = snippets + .filter(snippet => snippet.node.tags.primary === tag) + .filter(snippet => !snippet.node.archived) + .map(({node}) => ({ + title: node.title, + html: node.html.text, + tags: node.tags.all, + id: node.slug.slice(1) + })); const tagRegex = `/^\\s*${tag}/`; createPage({ path: tagPath, component: tagPage, context: { tag, - tagRegex, + snippets: taggedSnippets }, }); }); diff --git a/src/docs/components/SnippetCard.js b/src/docs/components/SnippetCard.js index 0b02ab6e4..071ac8a97 100644 --- a/src/docs/components/SnippetCard.js +++ b/src/docs/components/SnippetCard.js @@ -115,13 +115,12 @@ const ShortCard = ({

- {snippetData.title}

diff --git a/src/docs/templates/TagPage.js b/src/docs/templates/TagPage.js index 101605ad2..0c7a5a802 100644 --- a/src/docs/templates/TagPage.js +++ b/src/docs/templates/TagPage.js @@ -13,8 +13,8 @@ import { capitalize, getRawCodeBlocks as getCodeBlocks } from '../util'; // Individual snippet category/tag page // =================================================== const TagRoute = props => { - const posts = props.data.allMarkdownRemark.edges; const tag = props.pageContext.tag; + const snippets = props.pageContext.snippets; React.useEffect(() => { props.dispatch(pushNewPage(capitalize(tag), props.path)); @@ -26,16 +26,16 @@ const TagRoute = props => {

{capitalize(tag)}

Click on a snippet card to view the snippet.

- {posts && - posts.map(({ node }) => ( + {snippets && + snippets.map(snippet => ( v.trim()), - id: node.fields.slug.slice(1), + title: snippet.title, + html: snippet.html, + tags: snippet.tags, + id: snippet.id, }} isDarkMode={props.isDarkMode} /> @@ -54,28 +54,3 @@ export default connect( }), null, )(TagRoute); - -export const tagPageQuery = graphql` - query TagPage($tagRegex: String) { - allMarkdownRemark( - limit: 1000 - sort: { fields: [frontmatter___title], order: ASC } - filter: { fileAbsolutePath: { regex: "/snippets(?!_archive)/" }, frontmatter: { tags: { regex: $tagRegex } } } - ) { - totalCount - edges { - node { - id - html - fields { - slug - } - frontmatter { - title - tags - } - } - } - } - } -`;