gatsby-node.js (3133B)
1 const path = require('path'); 2 3 const createTagPages = function(createPage, posts) { 4 const allTagsTemplate = path.resolve( 5 'src', 6 'templates', 7 'allTagsTemplate.js' 8 ); 9 const singleTagTemplate = path.resolve( 10 'src', 11 'templates', 12 'singleTagTemplate.js' 13 ); 14 const postsByTag = {}; 15 posts.forEach(({ node }) => { 16 if (node.frontmatter.tags) { 17 node.frontmatter.tags.forEach(tag => { 18 if (!postsByTag[tag]) postsByTag[tag] = []; 19 postsByTag[tag].push(node); 20 }); 21 } 22 }); 23 const tags = Object.keys(postsByTag); 24 25 createPage({ 26 path: '/tags', 27 component: allTagsTemplate, 28 context: { tags, postsByTag }, 29 }); 30 31 tags.forEach(tag => { 32 const posts = postsByTag[tag]; 33 createPage({ 34 path: `/tags/${tag}`, 35 component: singleTagTemplate, 36 context: { 37 posts, 38 tag, 39 }, 40 }); 41 }); 42 }; 43 44 const createAuthorPages = function(createPage, posts) { 45 const allAuthorsTemplate = path.resolve( 46 'src', 47 'templates', 48 'allAuthorsTemplate.js' 49 ); 50 const singleAuthorTemplate = path.resolve( 51 'src', 52 'templates', 53 'singleAuthorTemplate.js' 54 ); 55 const postsByAuthor = {}; 56 posts.forEach(({ node }) => { 57 if (node.frontmatter.author) { 58 const author = node.frontmatter.author; 59 if (!postsByAuthor[author]) postsByAuthor[author] = []; 60 postsByAuthor[author].push(node); 61 } 62 }); 63 const authors = Object.keys(postsByAuthor); 64 65 createPage({ 66 path: '/authors', 67 component: allAuthorsTemplate, 68 context: { authors, postsByAuthor }, 69 }); 70 71 authors.forEach(author => { 72 const posts = postsByAuthor[author]; 73 createPage({ 74 path: `/authors/${author}`, 75 component: singleAuthorTemplate, 76 context: { 77 author, 78 posts, 79 }, 80 }); 81 }); 82 }; 83 84 exports.createPages = function({ graphql, actions }) { 85 const { createPage } = actions; 86 87 return new Promise(function(resolve, reject) { 88 const blogPostTemplate = path.resolve('src', 'templates', 'blogPost.js'); 89 resolve( 90 graphql( 91 ` 92 query { 93 allMarkdownRemark( 94 sort: { order: DESC, fields: [frontmatter___date] } 95 filter: { frontmatter: { draft: { ne: true } } } 96 ) { 97 edges { 98 node { 99 frontmatter { 100 attributed 101 author 102 misattributed 103 path 104 tags 105 title 106 unverified 107 } 108 } 109 } 110 } 111 } 112 ` 113 ).then(function(result) { 114 const posts = result.data.allMarkdownRemark.edges; 115 createTagPages(createPage, posts); 116 createAuthorPages(createPage, posts); 117 posts.forEach(function({ node }, index) { 118 createPage({ 119 path: 'quote' + node.frontmatter.path, 120 component: blogPostTemplate, 121 context: { 122 pathSlug: node.frontmatter.path, 123 }, 124 }); 125 resolve(); 126 }); 127 }) 128 ); 129 }); 130 };