From 501ab9abad7d29ef3d32d302cc243eef22aae3d9 Mon Sep 17 00:00:00 2001 From: ChloeLin Date: Mon, 22 Jan 2024 16:07:28 +0800 Subject: [PATCH 1/5] remove irrelevant forEach --- gatsby-node.js | 45 --------------------------------------------- 1 file changed, 45 deletions(-) diff --git a/gatsby-node.js b/gatsby-node.js index 84cfa7850..e6cc95314 100644 --- a/gatsby-node.js +++ b/gatsby-node.js @@ -133,29 +133,6 @@ exports.createPages = async ({ graphql, actions }) => { const ruleTemplate = require.resolve('./src/templates/rule.js'); result.data.categories.nodes.forEach((node) => { - // Find any categories that can't resolve a rule - node.frontmatter.index.forEach((inCat) => { - var match = false; - - result.data.rules.nodes.forEach((rulenode) => { - if (rulenode.frontmatter.uri == inCat) { - match = true; - } - if (rulenode.frontmatter.redirects) { - rulenode.frontmatter.redirects.forEach((redirect) => { - if (redirect == inCat) { - match = true; - } - }); - } - }); - - if (match == false) { - // eslint-disable-next-line no-console - console.log(node.parent.name + ' cannot find rule ' + inCat); - } - }); - // Create the page for the category createPage({ path: node.parent.name, @@ -169,28 +146,6 @@ exports.createPages = async ({ graphql, actions }) => { }); result.data.rules.nodes.forEach((node) => { - // Find any rules missing a category - var match = false; - if (!node.frontmatter.archivedreason) { - result.data.categories.nodes.forEach((catNode) => { - catNode.frontmatter.index.forEach((inCat) => { - if (node.frontmatter.uri == inCat) { - match = true; - } - }); - }); - } else { - match = true; - } - if (match == false) { - // eslint-disable-next-line no-console - console.log( - 'https://www.ssw.com.au/rules/' + - node.frontmatter.uri + - ' is missing a category' - ); - } - // Create the page for the rule createPage({ path: node.frontmatter.uri, From b33c0ce61aa60d00f980aa956ad170ecb5960d88 Mon Sep 17 00:00:00 2001 From: ChloeLin Date: Tue, 23 Jan 2024 10:24:39 +0800 Subject: [PATCH 2/5] remove unused query --- src/pages/archived.js | 9 ------- src/pages/orphaned.js | 62 ------------------------------------------- 2 files changed, 71 deletions(-) diff --git a/src/pages/archived.js b/src/pages/archived.js index f2fee0687..eb8b327a1 100644 --- a/src/pages/archived.js +++ b/src/pages/archived.js @@ -170,17 +170,11 @@ function ArchivedWithQuery(props) { } ) { nodes { - html frontmatter { type title index } - parent { - ... on File { - name - } - } } } topCategories: allMarkdownRemark( @@ -190,7 +184,6 @@ function ArchivedWithQuery(props) { } ) { nodes { - html frontmatter { type title @@ -211,7 +204,6 @@ function ArchivedWithQuery(props) { } ) { nodes { - html frontmatter { type title @@ -221,7 +213,6 @@ function ArchivedWithQuery(props) { parent { ... on File { name - relativeDirectory } } } diff --git a/src/pages/orphaned.js b/src/pages/orphaned.js index 5e1c10079..856671ca6 100644 --- a/src/pages/orphaned.js +++ b/src/pages/orphaned.js @@ -91,20 +91,6 @@ const Orphaned = ({ data }) => { {' - '} {rules.length} {rules.length > 1 ? 'Rules' : 'Rule'} - - - - - -
@@ -275,47 +261,6 @@ Orphaned.propTypes = { function OrphanedWithQuery(props) { const data = useStaticQuery(graphql` query OrphanedQuery { - main: allMarkdownRemark( - filter: { - fileAbsolutePath: { regex: "/(categories)/" } - frontmatter: { type: { eq: "main" } } - } - ) { - nodes { - html - frontmatter { - type - title - index - } - parent { - ... on File { - name - } - } - } - } - topCategories: allMarkdownRemark( - filter: { - fileAbsolutePath: { regex: "/(categories)/" } - frontmatter: { type: { eq: "top-category" } } - } - ) { - nodes { - html - frontmatter { - type - title - index - } - parent { - ... on File { - name - relativeDirectory - } - } - } - } categories: allMarkdownRemark( filter: { fileAbsolutePath: { regex: "/(categories)/" } @@ -323,19 +268,12 @@ function OrphanedWithQuery(props) { } ) { nodes { - html frontmatter { type title archivedreason index } - parent { - ... on File { - name - relativeDirectory - } - } } } rules: allMarkdownRemark( From 737f9e5a256f545d90cfb42f443b55b609cfa887 Mon Sep 17 00:00:00 2001 From: ChloeLin Date: Tue, 23 Jan 2024 11:06:18 +0800 Subject: [PATCH 3/5] remove unnecessary filter in category.js --- gatsby-node.js | 12 ++++++------ src/templates/category.js | 23 ++++++++++++++--------- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/gatsby-node.js b/gatsby-node.js index e6cc95314..4d39e7c8f 100644 --- a/gatsby-node.js +++ b/gatsby-node.js @@ -161,12 +161,12 @@ exports.createPages = async ({ graphql, actions }) => { }); }); - const profilePage = require.resolve('./src/pages/profile.js'); - createPage({ - path: `${siteConfig.pathPrefix}/people/`, - matchPath: `${siteConfig.pathPrefix}/people/:gitHubUsername`, - component: profilePage, - }); + // const profilePage = require.resolve('./src/pages/profile.js'); + // createPage({ + // path: `${siteConfig.pathPrefix}/people/`, + // matchPath: `${siteConfig.pathPrefix}/people/:gitHubUsername`, + // component: profilePage, + // }); }; exports.onPostBuild = async ({ store, pathPrefix }) => { diff --git a/src/templates/category.js b/src/templates/category.js index 81b947086..e66da9282 100644 --- a/src/templates/category.js +++ b/src/templates/category.js @@ -48,13 +48,14 @@ export default function Category({ data }) { greyBox: GreyBox, }; - var rules = data.rule.nodes - .filter((r) => { - return !r.frontmatter.archivedreason; - }) - .filter((r) => { - return category.frontmatter.index.includes(r.frontmatter.uri); - }); + // var rules = data.rule.nodes + // .filter((r) => { + // return !r.frontmatter.archivedreason; + // }) + // .filter((r) => { + // return category.frontmatter.index.includes(r.frontmatter.uri); + // }); + let rules = data.rule.nodes; return (
@@ -156,7 +157,7 @@ export default function Category({ data }) {
    {category.frontmatter.index.map((r, i) => { - var rule = rules.find((rr) => rr.frontmatter.uri == r); + let rule = rules.find((rr) => rr.frontmatter.uri == r); if (!rule) { return; } @@ -330,7 +331,11 @@ export const query = graphql` } } } - rule: allMarkdownRemark(filter: { frontmatter: { uri: { in: $index } } }) { + rule: allMarkdownRemark( + filter: { + frontmatter: { archivedreason: { eq: null }, uri: { in: $index } } + } + ) { nodes { excerpt(format: HTML, pruneLength: 500) frontmatter { From 161911ac4f8d28bb8214fa86653d71b2343da53d Mon Sep 17 00:00:00 2001 From: ChloeLin Date: Tue, 23 Jan 2024 15:24:39 +0800 Subject: [PATCH 4/5] test orphaned template --- gatsby-node.js | 36 +++++++++--- src/pages/profile.js | 3 - src/{pages => templates}/orphaned.js | 86 ++++++---------------------- 3 files changed, 45 insertions(+), 80 deletions(-) rename src/{pages => templates}/orphaned.js (83%) diff --git a/gatsby-node.js b/gatsby-node.js index 4d39e7c8f..a100d03e8 100644 --- a/gatsby-node.js +++ b/gatsby-node.js @@ -1,4 +1,3 @@ -const siteConfig = require('./site-config'); const { createFilePath } = require('gatsby-source-filesystem'); const appInsights = require('applicationinsights'); const makePluginData = require('./src/helpers/plugin-data'); @@ -98,14 +97,10 @@ exports.createPages = async ({ graphql, actions }) => { } frontmatter { index - redirects - experts - consulting } parent { ... on File { name - relativeDirectory } } } @@ -120,9 +115,9 @@ exports.createPages = async ({ graphql, actions }) => { frontmatter { uri title - archivedreason related redirects + archivedreason } } } @@ -140,12 +135,30 @@ exports.createPages = async ({ graphql, actions }) => { context: { slug: node.fields.slug, index: node.frontmatter.index, - redirects: node.frontmatter.redirects, }, }); }); + const orphanedRules = []; + result.data.rules.nodes.forEach((node) => { + let match = false; + if (!node.frontmatter.archivedreason) { + result.data.categories.nodes.forEach((catNode) => { + catNode.frontmatter.index.forEach((inCat) => { + if (node.frontmatter.uri == inCat) { + match = true; + } + }); + }); + } else { + match = true; + } + + if (match == false) { + orphanedRules.push(node.frontmatter.uri); + } + // Create the page for the rule createPage({ path: node.frontmatter.uri, @@ -167,6 +180,15 @@ exports.createPages = async ({ graphql, actions }) => { // matchPath: `${siteConfig.pathPrefix}/people/:gitHubUsername`, // component: profilePage, // }); + + const orphanedPage = require.resolve('./src/templates/orphaned.js'); + createPage({ + path: '/orphaned/', + component: orphanedPage, + context: { + index: orphanedRules, + }, + }); }; exports.onPostBuild = async ({ store, pathPrefix }) => { diff --git a/src/pages/profile.js b/src/pages/profile.js index 292466250..cc2a59c23 100644 --- a/src/pages/profile.js +++ b/src/pages/profile.js @@ -153,9 +153,6 @@ function ProfileWithQuery(props) { title uri guid - authors { - title - } } htmlAst } diff --git a/src/pages/orphaned.js b/src/templates/orphaned.js similarity index 83% rename from src/pages/orphaned.js rename to src/templates/orphaned.js index 856671ca6..1e3e9e646 100644 --- a/src/pages/orphaned.js +++ b/src/templates/orphaned.js @@ -1,9 +1,8 @@ import React, { useRef, useState, useEffect } from 'react'; -import { useStaticQuery, graphql } from 'gatsby'; +import { graphql } from 'gatsby'; import PropTypes from 'prop-types'; import { config } from '@fortawesome/fontawesome-svg-core'; import Breadcrumb from '../components/breadcrumb/breadcrumb'; -import Tooltip from '../components/tooltip/tooltip'; import RadioButton from '../components/radio-button/radio-button'; import { Link } from 'gatsby'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; @@ -44,39 +43,7 @@ const Orphaned = ({ data }) => { setSelectedOption(e.target.value); }; - /** - * Get all rules that don't have an associated category - * @param {Object} rules All rule nodes - * @param {Object} categories All category nodes - * @return {array} All rules without an associated category - */ - const findOrphanedRules = (rules, categories) => { - const orphanedRules = []; - - rules.nodes.forEach((node) => { - // Find any rules missing a category - var match = false; - if (!node.frontmatter.archivedreason) { - categories.nodes.forEach((catNode) => { - catNode.frontmatter.index.forEach((inCat) => { - if (node.frontmatter.uri == inCat) { - match = true; - } - }); - }); - } else { - match = true; - } - if (match == false) { - orphanedRules.push(node); - } - }); - - return orphanedRules; - }; - - const category = data.categories.nodes[0]; - const rules = findOrphanedRules(data.rules, data.categories); + const rules = data.rules.nodes; return (
    @@ -258,41 +225,20 @@ Orphaned.propTypes = { location: PropTypes.object.isRequired, }; -function OrphanedWithQuery(props) { - const data = useStaticQuery(graphql` - query OrphanedQuery { - categories: allMarkdownRemark( - filter: { - fileAbsolutePath: { regex: "/(categories)/" } - frontmatter: { type: { eq: "category" } } - } - ) { - nodes { - frontmatter { - type - title - archivedreason - index - } - } - } - rules: allMarkdownRemark( - filter: { frontmatter: { type: { eq: "rule" } } } - ) { - nodes { - frontmatter { - uri - archivedreason - title - } - html - excerpt(format: HTML, pruneLength: 500) +export default Orphaned; + +export const query = graphql` + query ($index: [String]!) { + rules: allMarkdownRemark(filter: { frontmatter: { uri: { in: $index } } }) { + nodes { + frontmatter { + uri + archivedreason + title } + html + excerpt(format: HTML, pruneLength: 500) } } - `); - - return ; -} - -export default OrphanedWithQuery; + } +`; From b4fbab7e2f9a651d66a0b399a60f2d028d410635 Mon Sep 17 00:00:00 2001 From: ChloeLin Date: Wed, 24 Jan 2024 16:20:39 +0800 Subject: [PATCH 5/5] test using createPage for category.js --- gatsby-node.js | 16 ++++++++++++++-- src/pages/index.js | 16 ++++------------ src/templates/category.js | 31 +++---------------------------- 3 files changed, 21 insertions(+), 42 deletions(-) diff --git a/gatsby-node.js b/gatsby-node.js index a100d03e8..59a1444d3 100644 --- a/gatsby-node.js +++ b/gatsby-node.js @@ -109,6 +109,7 @@ exports.createPages = async ({ graphql, actions }) => { filter: { frontmatter: { type: { in: ["rule"] } } } ) { nodes { + excerpt(format: HTML, pruneLength: 500) fields { slug } @@ -118,7 +119,9 @@ exports.createPages = async ({ graphql, actions }) => { related redirects archivedreason + guid } + htmlAst } } } @@ -126,15 +129,24 @@ exports.createPages = async ({ graphql, actions }) => { const categoryTemplate = require.resolve('./src/templates/category.js'); const ruleTemplate = require.resolve('./src/templates/rule.js'); - result.data.categories.nodes.forEach((node) => { + let indexedRule = []; + result.data.rules.nodes.forEach((rule) => { + if ( + node.frontmatter.index.includes(rule.frontmatter.uri) && + !rule.frontmatter.archivedreason + ) { + indexedRule.push(rule); + } + }); + // Create the page for the category createPage({ path: node.parent.name, component: categoryTemplate, context: { slug: node.fields.slug, - index: node.frontmatter.index, + pageRules: indexedRule, }, }); }); diff --git a/src/pages/index.js b/src/pages/index.js index b07243ccf..9186904dc 100644 --- a/src/pages/index.js +++ b/src/pages/index.js @@ -17,10 +17,6 @@ import { config } from '@fortawesome/fontawesome-svg-core'; config.autoAddCss = false; const Index = ({ data, location }) => { - const notArchivedRules = data.rules.nodes.filter( - (r) => !r.frontmatter.archivedreason - ); - return (
    @@ -54,7 +50,7 @@ const Index = ({ data, location }) => { ); @@ -122,11 +118,6 @@ function IndexWithQuery(props) { title index } - parent { - ... on File { - name - } - } } } topCategories: allMarkdownRemark( @@ -162,7 +153,6 @@ function IndexWithQuery(props) { type title index - archivedreason } parent { ... on File { @@ -173,7 +163,9 @@ function IndexWithQuery(props) { } } rules: allMarkdownRemark( - filter: { frontmatter: { type: { eq: "rule" } } } + filter: { + frontmatter: { archivedreason: { eq: null }, type: { eq: "rule" } } + } ) { nodes { frontmatter { diff --git a/src/templates/category.js b/src/templates/category.js index e66da9282..bad074808 100644 --- a/src/templates/category.js +++ b/src/templates/category.js @@ -29,7 +29,7 @@ const appInsights = new ApplicationInsights({ appInsights.loadAppInsights(); -export default function Category({ data }) { +export default function Category({ data, pageContext }) { const linkRef = useRef(); const category = data.markdownRemark; @@ -48,14 +48,7 @@ export default function Category({ data }) { greyBox: GreyBox, }; - // var rules = data.rule.nodes - // .filter((r) => { - // return !r.frontmatter.archivedreason; - // }) - // .filter((r) => { - // return category.frontmatter.index.includes(r.frontmatter.uri); - // }); - let rules = data.rule.nodes; + const rules = pageContext.pageRules; return (
    @@ -312,7 +305,7 @@ Category.propTypes = { }; export const query = graphql` - query ($slug: String!, $index: [String]!) { + query ($slug: String!) { markdownRemark(fields: { slug: { eq: $slug } }) { htmlAst frontmatter { @@ -331,23 +324,5 @@ export const query = graphql` } } } - rule: allMarkdownRemark( - filter: { - frontmatter: { archivedreason: { eq: null }, uri: { in: $index } } - } - ) { - nodes { - excerpt(format: HTML, pruneLength: 500) - frontmatter { - uri - archivedreason - title - guid - consulting - experts - } - htmlAst - } - } } `;