From 9c394585249f7d994e16c821dbcb48dfe132cb4b Mon Sep 17 00:00:00 2001 From: Kevin Kandlbinder Date: Sun, 7 Nov 2021 14:42:34 +0000 Subject: [PATCH] Add RSS feed --- gatsby-config.js | 84 ++++++++++++++++++++++ gatsby-node.js | 177 +++++------------------------------------------ package.json | 1 + yarn.lock | 38 ++++++++++ 4 files changed, 142 insertions(+), 158 deletions(-) diff --git a/gatsby-config.js b/gatsby-config.js index 1ab89cb..80260f1 100644 --- a/gatsby-config.js +++ b/gatsby-config.js @@ -139,5 +139,89 @@ module.exports = { generateMatchPathRewrites: false, }, }, + { + resolve: `gatsby-plugin-feed`, + options: { + query: ` + { + site { + siteMetadata { + title + description + siteUrl + site_url: siteUrl + } + } + } + `, + feeds: [ + { + serialize: ({ query: { site, blog } }) => { + return blog.nodes.map((node) => { + if (!node.childMdx) return null; + + return { + title: node.childMdx.frontmatter.title, + description: node.childMdx.excerpt, + date: node.childMdx.frontmatter.date, + url: + site.siteMetadata.siteUrl + + `/${ + node.childMdx.frontmatter.language + }/blog/${ + node.childMdx.frontmatter.section + ? node.childMdx.frontmatter + .section + "/" + : "" + }${ + node.childMdx.frontmatter.published + }/${node.childMdx.frontmatter.url}`, + guid: + site.siteMetadata.siteUrl + + `/${ + node.childMdx.frontmatter.language + }/blog/${ + node.childMdx.frontmatter.section + ? node.childMdx.frontmatter + .section + "/" + : "" + }${ + node.childMdx.frontmatter.published + }/${node.childMdx.frontmatter.url}`, + section: node.childMdx.frontmatter.section, + }; + }); + }, + query: ` + { + blog: allFile( + filter: { sourceInstanceName: { eq: "blogContent" } } + ) { + nodes { + childMdx { + id + body + excerpt + frontmatter { + platform + tags + title + url + section + language + published(formatString: "YYYY/MM") + date: published + } + } + } + } + } + `, + output: "/blog/feed.xml", + title: extConfig.siteName + " Blog", + }, + ], + }, + }, ], }; diff --git a/gatsby-node.js b/gatsby-node.js index bc4f5ca..8172804 100644 --- a/gatsby-node.js +++ b/gatsby-node.js @@ -1,6 +1,5 @@ /* eslint-disable no-undef */ const path = require(`path`); -const fs = require("fs"); const { paginate } = require("gatsby-awesome-pagination"); exports.createPages = async ({ actions, graphql, reporter }) => { @@ -45,27 +44,26 @@ exports.createPages = async ({ actions, graphql, reporter }) => { } result.data.allProjectsJson.nodes.forEach((node) => { - // eslint-disable-next-line no-undef - console.log( - "Creating Page: ", - `/${node.lang}/projects/${node.urlname}` + if (node.lang === "ignoreme") return; + + reporter.info( + "Creating Page: " + `/${node.lang}/projects/${node.urlname}` ); - if (node.lang !== "ignoreme") - createPage({ - path: `/${node.lang}/projects/${node.urlname}`, - component: projectTemplate, - context: { - lang: node.lang, - urlname: node.urlname, - }, - }); + createPage({ + path: `/${node.lang}/projects/${node.urlname}`, + component: projectTemplate, + context: { + lang: node.lang, + urlname: node.urlname, + }, + }); }); const blogListingTemplate = path.resolve(`src/templates/blogListing.js`); const blogTemplate = path.resolve(`src/templates/blogPost.js`); - console.log("Creating blog listing..."); + reporter.info("Creating blog listings..."); ["en", "de"].forEach((lang) => paginate({ @@ -92,7 +90,7 @@ exports.createPages = async ({ actions, graphql, reporter }) => { ) { processedSections.push(node.childMdx.frontmatter.section); - console.log( + reporter.info( "Creating section listing for " + node.childMdx.frontmatter.section + "..." @@ -117,12 +115,11 @@ exports.createPages = async ({ actions, graphql, reporter }) => { ); } - // eslint-disable-next-line no-undef - console.log( - "Creating Page: ", - `/${node.childMdx.frontmatter.language}/blog/${ - node.childMdx.frontmatter.section ?? "blog" - }/${node.childMdx.frontmatter.url}` + reporter.info( + "Creating Page: " + + `/${node.childMdx.frontmatter.language}/blog/${ + node.childMdx.frontmatter.section ?? "blog" + }/${node.childMdx.frontmatter.url}` ); createPage({ @@ -179,139 +176,3 @@ exports.createPages = async ({ actions, graphql, reporter }) => { }); }); }; - -const config = require("./config.js"); - -exports.onPostBuild = async ({ graphql, reporter }) => { - console.log("Building static api..."); - - const apiPrefix = "./public/api"; - - if (!fs.existsSync(apiPrefix)) fs.mkdirSync(apiPrefix); - - fs.writeFileSync( - `${apiPrefix}.json`, - JSON.stringify({ - success: true, - endpoints: { - projects: [ - { - name: "Projects Overview", - description: - "Returns overview of all available projects", - path: "/api/projects.json", - }, - { - name: "Projects Overview for Language", - description: - "Returns overview of all available projects in a specified language", - path: "/api/projects/:lang.json", - }, - { - name: "Get specific Project", - description: - "Returns specific project in specified language", - path: "/api/projects/:lang/:slug.json", - }, - ], - }, - }) - ); - - const projectsPrefix = apiPrefix + "/projects"; - - if (!fs.existsSync(projectsPrefix)) fs.mkdirSync(projectsPrefix); - - await graphql(` - query { - allProjectsJson { - nodes { - urlname - shortDescription - name - links { - github - website - } - lang - image { - publicURL - } - featured - } - } - } - `).then((res) => { - if (res.errors) { - reporter.panicOnBuild(`Error while running GraphQL query.`); - return; - } - - let projects = res.data.allProjectsJson.nodes.filter((project) => { - return project.lang !== "ignoreme"; - }); - - fs.writeFileSync( - `${projectsPrefix}.json`, - JSON.stringify({ - success: true, - projects: projects.map((project) => { - return { - slug: project.urlname, - lang: project.lang, - api: `/api/projects/${project.lang}/${project.urlname}.json`, - }; - }), - }) - ); - - config.languages.forEach((lang) => { - if (!fs.existsSync(`${projectsPrefix}/${lang}`)) - fs.mkdirSync(`${projectsPrefix}/${lang}`); - - fs.writeFileSync( - `${projectsPrefix}/${lang}.json`, - JSON.stringify({ - success: true, - projects: projects - .filter((project) => { - return project.lang == lang; - }) - .map((project) => { - return { - slug: project.urlname, - lang: project.lang, - api: `/api/projects/${project.lang}/${project.urlname}.json`, - }; - }), - }) - ); - }); - - projects.forEach((project) => { - fs.writeFileSync( - `${projectsPrefix}/${project.lang}/${project.urlname}.json`, - JSON.stringify({ - success: true, - project: { - slug: project.urlname, - lang: project.lang, - name: project.name, - shortDescription: project.shortDescription, - longDescription: project.longDescription, - links: - project.links !== null - ? { - github: project.links.github, - website: project.links.website, - } - : null, - image: project.image.publicURL, - featured: project.featured, - frontend: `/${project.lang}/projects/${project.urlname}`, - }, - }) - ); - }); - }); -}; diff --git a/package.json b/package.json index 220f48d..45bdea6 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ "gatsby-awesome-pagination": "0.3.8", "gatsby-cli": "4.1.0", "gatsby-plugin-asset-path": "3.0.4", + "gatsby-plugin-feed": "^4.1.0", "gatsby-plugin-image": "2.1.0", "gatsby-plugin-manifest": "4.1.0", "gatsby-plugin-mdx": "3.1.0", diff --git a/yarn.lock b/yarn.lock index d73ee05..7c0c172 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6043,6 +6043,19 @@ gatsby-plugin-eslint@4.0.1: dependencies: webpack-merge "^5.8.0" +gatsby-plugin-feed@^4.1.0: + version "4.1.0" + resolved "https://registry.yarnpkg.com/gatsby-plugin-feed/-/gatsby-plugin-feed-4.1.0.tgz#b560fa447935144bec2bdf4c874cdff62b9bf16f" + integrity sha512-6bJ+g+UbOBiaIHj1jFFHewaS3SDLBaldjwoZmslYfyoxKSXzW3Cl9S0xyEFwFbiXqjUbDJJeeifIjbsHauAKQQ== + dependencies: + "@babel/runtime" "^7.15.4" + "@hapi/joi" "^15.1.1" + common-tags "^1.8.0" + fs-extra "^10.0.0" + gatsby-plugin-utils "^2.1.0" + lodash.merge "^4.6.2" + rss "^1.2.2" + gatsby-plugin-image@2.1.0: version "2.1.0" resolved "https://registry.yarnpkg.com/gatsby-plugin-image/-/gatsby-plugin-image-2.1.0.tgz#71f3568ce7558a84fbcf06ca5ea9b00a1cd9d41d" @@ -8952,6 +8965,18 @@ mime-db@1.49.0: resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.50.0.tgz#abd4ac94e98d3c0e185016c67ab45d5fde40c11f" integrity sha512-9tMZCDlYHqeERXEHO9f/hKfNXhre5dK2eE/krIvUjZbS2KPcqGDfNShIWS1uW9XOTKQKqK6qbeOci18rbfW77A== +mime-db@~1.25.0: + version "1.25.0" + resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.25.0.tgz#c18dbd7c73a5dbf6f44a024dc0d165a1e7b1c392" + integrity sha1-wY29fHOl2/b0SgJNwNFloeexw5I= + +mime-types@2.1.13: + version "2.1.13" + resolved "https://registry.yarnpkg.com/mime-types/-/mime-types-2.1.13.tgz#e07aaa9c6c6b9a7ca3012c69003ad25a39e92a88" + integrity sha1-4HqqnGxrmnyjASxpADrSWjnpKog= + dependencies: + mime-db "~1.25.0" + mime-types@^2.1.12, mime-types@^2.1.27, mime-types@^2.1.30, mime-types@~2.1.24: version "2.1.32" resolved "https://registry.yarnpkg.com/mime-types/-/mime-types-2.1.32.tgz#1d00e89e7de7fe02008db61001d9e02852670fd5" @@ -11238,6 +11263,14 @@ rimraf@^3.0.0, rimraf@^3.0.2: dependencies: glob "^7.1.3" +rss@^1.2.2: + version "1.2.2" + resolved "https://registry.yarnpkg.com/rss/-/rss-1.2.2.tgz#50a1698876138133a74f9a05d2bdc8db8d27a921" + integrity sha1-UKFpiHYTgTOnT5oF0r3I240nqSE= + dependencies: + mime-types "2.1.13" + xml "1.0.1" + run-async@^2.4.0: version "2.4.1" resolved "https://registry.yarnpkg.com/run-async/-/run-async-2.4.1.tgz#8440eccf99ea3e70bd409d49aab88e10c189a455" @@ -13459,6 +13492,11 @@ xml2js@^0.4.5: sax ">=0.6.0" xmlbuilder "~11.0.0" +xml@1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/xml/-/xml-1.0.1.tgz#78ba72020029c5bc87b8a81a3cfcd74b4a2fc1e5" + integrity sha1-eLpyAgApxbyHuKgaPPzXS0ovweU= + xmlbuilder@~11.0.0: version "11.0.1" resolved "https://registry.yarnpkg.com/xmlbuilder/-/xmlbuilder-11.0.1.tgz#be9bae1c8a046e76b31127726347d0ad7002beb3"