From 5734c9b8fae06c837308795bf158d896fc57f50b Mon Sep 17 00:00:00 2001 From: Edward Shen Date: Fri, 27 Nov 2020 15:25:22 -0500 Subject: [PATCH] Add support for hidden pages --- gatsby-browser.js | 2 +- gatsby-config.js | 3 ++ gatsby-node.js | 18 ++++++++++-- package.json | 1 + ...m-ipsum.mdx => 2020-01-01-lorem-ipsum.mdx} | 8 +++-- src/pages/index.mdx | 11 ++++--- src/templates/notes.tsx | 29 ++++++++++++++----- yarn.lock | 5 ++++ 8 files changed, 58 insertions(+), 19 deletions(-) rename src/notes/{2020-09-30-lorem-ipsum.mdx => 2020-01-01-lorem-ipsum.mdx} (97%) diff --git a/gatsby-browser.js b/gatsby-browser.js index 5d8736f..f53e89f 100644 --- a/gatsby-browser.js +++ b/gatsby-browser.js @@ -1,2 +1,2 @@ import './src/styles/global.css'; -import "prismjs/themes/prism-solarizedlight.css"; \ No newline at end of file +import 'prismjs/themes/prism-solarizedlight.css'; diff --git a/gatsby-config.js b/gatsby-config.js index f55e5d2..09ebc78 100644 --- a/gatsby-config.js +++ b/gatsby-config.js @@ -105,5 +105,8 @@ module.exports = { ] } }, + { + resolve: 'gatsby-plugin-mdx-frontmatter' + }, ], }; diff --git a/gatsby-node.js b/gatsby-node.js index 2ebb979..2d377ad 100644 --- a/gatsby-node.js +++ b/gatsby-node.js @@ -14,6 +14,7 @@ exports.createPages = async ({ actions, graphql, reporter }) => { node { frontmatter { path + hidden } id } @@ -27,7 +28,7 @@ exports.createPages = async ({ actions, graphql, reporter }) => { return; } - const posts = result.data.allMdx.edges; + const posts = result.data.allMdx.edges.filter(({ node }) => !node.frontmatter.hidden); const postsPerPage = 10; const numPages = Math.ceil(posts.length / postsPerPage); const rootPath = "/notes"; @@ -46,7 +47,9 @@ exports.createPages = async ({ actions, graphql, reporter }) => { }) }); - posts.forEach(({ node }) => { + // Must decouple to ensure hidden pages are still rendered, just excluded from + // notes mapping + result.data.allMdx.edges.forEach(({ node }) => { createPage({ path: `${rootPath}/${node.frontmatter.path}`, component: noteTemplate, @@ -56,3 +59,14 @@ exports.createPages = async ({ actions, graphql, reporter }) => { }); }); } +exports.createSchemaCustomization = ({ actions: { createTypes } }) => { + createTypes(` + type Mdx implements Node { + frontmatter: MdxFrontmatter + } + + type MdxFrontmatter { + hidden: Boolean + } + `); +}; diff --git a/package.json b/package.json index 78f9602..8331c7c 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "@mdx-js/react": "^1.6.18", "gatsby": "^2.24.63", "gatsby-plugin-mdx": "^1.2.41", + "gatsby-plugin-mdx-frontmatter": "^0.0.2", "gatsby-plugin-react-helmet": "^3.3.11", "gatsby-plugin-typography": "^2.5.11", "gatsby-remark-prismjs": "^3.5.13", diff --git a/src/notes/2020-09-30-lorem-ipsum.mdx b/src/notes/2020-01-01-lorem-ipsum.mdx similarity index 97% rename from src/notes/2020-09-30-lorem-ipsum.mdx rename to src/notes/2020-01-01-lorem-ipsum.mdx index 6147f58..a505c02 100644 --- a/src/notes/2020-09-30-lorem-ipsum.mdx +++ b/src/notes/2020-01-01-lorem-ipsum.mdx @@ -2,16 +2,18 @@ path: "lorem-ipsum" date: 2020-01-01 title: "Lorem Ipsum" +hidden: true --- -# h1 Heading 8-) +Hey! You've found the secret page that I use to test out style changes. + +# h1 Heading ## h2 Heading ### h3 Heading #### h4 Heading ##### h5 Heading ###### h6 Heading - ## Horizontal Rules ___ @@ -103,7 +105,7 @@ Sample text here... Syntax highlighting -``` js +```js var foo = function (bar) { return bar++; }; diff --git a/src/pages/index.mdx b/src/pages/index.mdx index 0c4ad8a..d914a80 100644 --- a/src/pages/index.mdx +++ b/src/pages/index.mdx @@ -12,14 +12,13 @@ import Navbar from "../components/navbar"; ----- -Hey there. I'm just an software engineer with interests in distributed systems, -safety and correctness, cybersecurity, homelabbing, homebrewing (both kinds!), -and keyboards. My favorite language is Rust, and my most disliked is -Python—I've been bitten by its… "features" too many times, -unfortunately. +Hey there. I'm just an software engineer with varying degrees of interest in +distributed systems, safety and correctness, cybersecurity, homelabbing, +homebrewing (both kinds!), and keyboards. My favorite language is Rust, and my +most disliked is Python. #### Contact Info The best way to contact me for any reason is by email. My address is [hi (at) this website], and I'll generally respond in a few days. Feel free to send an -another email if I don't respond in a week or so. \ No newline at end of file +another email if I don't respond in a week or so. diff --git a/src/templates/notes.tsx b/src/templates/notes.tsx index 60f2f16..3f0497c 100644 --- a/src/templates/notes.tsx +++ b/src/templates/notes.tsx @@ -1,6 +1,11 @@ +/* + * This file handles rendering the pages that let users select and find notes. + * See note.tsx for rendering a single note. + */ + import React from "react"; import Navbar from "../components/navbar"; -import { graphql, PageProps } from "gatsby"; +import { graphql } from "gatsby"; import style from "./notes.module.css"; export default ({ data, pageContext }) => { @@ -14,6 +19,8 @@ export default ({ data, pageContext }) => { prevPageLink = <<; } + const shouldDisplayNav = numPages !== 1; + return ( <> @@ -31,11 +38,19 @@ export default ({ data, pageContext }) => { ) })} -

- {prevPageLink} - {currentPage} - {currentPage !== numPages && >>} -

+ {shouldDisplayNav && +

+ {prevPageLink} + {currentPage} + {currentPage !== numPages && + + >> + + } +

+ } ) } @@ -44,7 +59,7 @@ export const query = graphql` query IndexQuery($skip: Int!, $limit: Int!) { allMdx( sort: { order: DESC, fields: [frontmatter___date] } - filter: { fileAbsolutePath: { glob: "**/src/notes/*" } } + filter: { fileAbsolutePath: { glob: "**/src/notes/*" }, frontmatter: {hidden: {ne: true}} } limit: $limit skip: $skip ) { diff --git a/yarn.lock b/yarn.lock index 22863fb..215ca60 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5863,6 +5863,11 @@ gatsby-plugin-eslint@^2.0.8: resolved "https://registry.yarnpkg.com/gatsby-plugin-eslint/-/gatsby-plugin-eslint-2.0.8.tgz#1ff7f546a427d274dc2de3932ba29822aae330c3" integrity sha512-vAMy37povmQJNg6ZxY78fkWR3pKwG8MNMhO3u+4vXj2MYT5avhFvHPJTAb126ZCuygf30gAWlpwbV50zP894Jw== +gatsby-plugin-mdx-frontmatter@^0.0.2: + version "0.0.2" + resolved "https://registry.yarnpkg.com/gatsby-plugin-mdx-frontmatter/-/gatsby-plugin-mdx-frontmatter-0.0.2.tgz#5db9d83bc356b2fcd3b360314f79bbf6a2bbb57a" + integrity sha512-nGDVNP7gLRnwyRP3rvt27R327GpeY3Ve3OVjiCmrdgkg6ApWCDMlY1XFfuY4iZDJwZXiuhmqrRZUPY5rV5xPJQ== + gatsby-plugin-mdx@^1.2.41: version "1.2.41" resolved "https://registry.yarnpkg.com/gatsby-plugin-mdx/-/gatsby-plugin-mdx-1.2.41.tgz#56b110050170f6c466d915a39a0094596989c8ef"