|
| 1 | +const fs = require(`fs`) |
| 2 | +const path = require(`path`) |
| 3 | +const mkdirp = require(`mkdirp`) |
| 4 | +const kebab = require(`lodash.kebabcase`) |
| 5 | +const Debug = require(`debug`) |
| 6 | + |
| 7 | +const Posts = require.resolve(`./src/templates/posts`) |
| 8 | +const Post = require.resolve(`./src/templates/post`) |
| 9 | +const Tag = require.resolve(`./src/templates/tag`) |
| 10 | + |
| 11 | +const debug = Debug(`gatsby-theme-blog-mdx`) |
| 12 | + |
| 13 | +exports.createPages = async ({ graphql, actions }, pluginOptions) => { |
| 14 | + const { createPage, createRedirect } = actions |
| 15 | + |
| 16 | + const { postsPath = `/blog` } = pluginOptions |
| 17 | + |
| 18 | + const result = await graphql(` |
| 19 | + { |
| 20 | + mdxPages: allMdx( |
| 21 | + sort: { fields: [frontmatter___date], order: DESC } |
| 22 | + filter: { frontmatter: { draft: { ne: true }, archived: { ne: true } } } |
| 23 | + ) { |
| 24 | + edges { |
| 25 | + node { |
| 26 | + id |
| 27 | + excerpt |
| 28 | + timeToRead |
| 29 | + tableOfContents |
| 30 | + headings { |
| 31 | + value |
| 32 | + } |
| 33 | + parent { |
| 34 | + ... on File { |
| 35 | + name |
| 36 | + sourceInstanceName |
| 37 | + } |
| 38 | + } |
| 39 | + frontmatter { |
| 40 | + path |
| 41 | + tags |
| 42 | + title |
| 43 | + redirects |
| 44 | + date(formatString: "MMMM DD, YYYY") |
| 45 | + } |
| 46 | + code { |
| 47 | + scope |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + `) |
| 54 | + |
| 55 | + if (result.errors) { |
| 56 | + console.log(result.errors) |
| 57 | + throw new Error(`Could not query posts`, result.errors) |
| 58 | + } |
| 59 | + |
| 60 | + const { mdxPages } = result.data |
| 61 | + |
| 62 | + // Collect tags |
| 63 | + const allTags = mdxPages.edges.reduce((acc, post) => { |
| 64 | + const { |
| 65 | + node: { frontmatter = {} }, |
| 66 | + } = post |
| 67 | + return acc.concat(frontmatter.tags || []) |
| 68 | + }, []) |
| 69 | + const tags = [...new Set(allTags)] |
| 70 | + |
| 71 | + // Create post pages and redirects |
| 72 | + mdxPages.edges.forEach(({ node }) => { |
| 73 | + const fallbackPath = `/${node.parent.sourceInstanceName}/${ |
| 74 | + node.parent.name |
| 75 | + }` |
| 76 | + const path = node.frontmatter.path || fallbackPath |
| 77 | + |
| 78 | + if (node.frontmatter.redirects) { |
| 79 | + node.frontmatter.redirects.forEach(fromPath => { |
| 80 | + createRedirect({ |
| 81 | + fromPath, |
| 82 | + toPath: path, |
| 83 | + redirectInBrowser: true, |
| 84 | + isPermanent: true, |
| 85 | + }) |
| 86 | + }) |
| 87 | + } |
| 88 | + |
| 89 | + createPage({ |
| 90 | + path, |
| 91 | + context: node, |
| 92 | + component: Post, |
| 93 | + }) |
| 94 | + }) |
| 95 | + |
| 96 | + // Create post list page |
| 97 | + createPage({ |
| 98 | + path: postsPath, |
| 99 | + component: Posts, |
| 100 | + }) |
| 101 | + |
| 102 | + // Create tag list pages |
| 103 | + tags.forEach(tag => { |
| 104 | + const path = `/tags/${kebab(tag)}` |
| 105 | + |
| 106 | + createPage({ |
| 107 | + path, |
| 108 | + component: Tag, |
| 109 | + context: { |
| 110 | + tag, |
| 111 | + }, |
| 112 | + }) |
| 113 | + }) |
| 114 | +} |
| 115 | + |
| 116 | +exports.onPreBootstrap = ({ store }) => { |
| 117 | + const { program } = store.getState() |
| 118 | + |
| 119 | + const dirs = [ |
| 120 | + path.join(program.directory, `posts`), |
| 121 | + path.join(program.directory, `src/pages`), |
| 122 | + path.join(program.directory, `src/data`), |
| 123 | + ] |
| 124 | + |
| 125 | + dirs.forEach(dir => { |
| 126 | + debug(`Initializing ${dir} directory`) |
| 127 | + if (!fs.existsSync(dir)) { |
| 128 | + mkdirp.sync(dir) |
| 129 | + } |
| 130 | + }) |
| 131 | +} |
0 commit comments