Skip to content

Commit 52cfab3

Browse files
authored
Merge pull request #1041 from mikeesto/add-tag-pages
Add tags to blog posts & create tag pages
2 parents 92fabee + 1a69ed1 commit 52cfab3

File tree

9 files changed

+441
-209
lines changed

9 files changed

+441
-209
lines changed

gatsby-node.js

+105-73
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
const path = require("path")
22
const sortLibs = require("./scripts/sort-libraries")
3-
const globby = require('globby');
4-
const frontmatterParser = require('parser-front-matter');
5-
const { readFile } = require("fs-extra");
6-
const { promisify } = require('util');
3+
const globby = require("globby")
4+
const frontmatterParser = require("parser-front-matter")
5+
const { readFile } = require("fs-extra")
6+
const { promisify } = require("util")
77

88
exports.onCreatePage = async ({ page, actions }) => {
99
const { createPage, deletePage } = actions
@@ -13,59 +13,62 @@ exports.onCreatePage = async ({ page, actions }) => {
1313
sourcePath: path.relative(__dirname, page.componentPath),
1414
}
1515
if (page.path === "/code" || page.path === "/code/") {
16-
const markdownFilePaths = await globby('src/content/code/**/*.md');
16+
const markdownFilePaths = await globby("src/content/code/**/*.md")
1717
const codeData = {}
18-
const slugMap = require('./src/content/code/slug-map.json');
19-
const parse$ = promisify(frontmatterParser.parse);
20-
await Promise.all(markdownFilePaths.map(async markdownFilePath => {
18+
const slugMap = require("./src/content/code/slug-map.json")
19+
const parse$ = promisify(frontmatterParser.parse)
20+
await Promise.all(
21+
markdownFilePaths.map(async markdownFilePath => {
22+
const markdownFileContent = await readFile(markdownFilePath, "utf-8")
23+
let {
24+
data: { name, description, url, github, npm, gem },
25+
content: howto,
26+
} = await parse$(markdownFileContent, undefined)
27+
howto = howto.trim()
28+
const pathArr = markdownFilePath.split("/")
29+
if (markdownFilePath.includes("language-support")) {
30+
const languageSupportDirIndex = pathArr.indexOf("language-support")
31+
const languageNameSlugIndex = languageSupportDirIndex + 1
32+
const languageNameSlug = pathArr[languageNameSlugIndex]
33+
const languageName = slugMap[languageNameSlug]
34+
codeData.Languages = codeData.Languages || {}
35+
codeData.Languages[languageName] =
36+
codeData.Languages[languageName] || {}
2137

22-
const markdownFileContent = await readFile(markdownFilePath, "utf-8")
23-
let {
24-
data: { name, description, url, github, npm, gem },
25-
content: howto,
26-
} = await parse$(markdownFileContent, undefined)
27-
howto = howto.trim();
28-
const pathArr = markdownFilePath.split("/")
29-
if (markdownFilePath.includes("language-support")) {
30-
const languageSupportDirIndex = pathArr.indexOf("language-support")
31-
const languageNameSlugIndex = languageSupportDirIndex + 1
32-
const languageNameSlug = pathArr[languageNameSlugIndex]
33-
const languageName = slugMap[languageNameSlug]
34-
codeData.Languages = codeData.Languages || {}
35-
codeData.Languages[languageName] = codeData.Languages[languageName] || {}
36-
37-
const categoryNameSlugIndex = languageSupportDirIndex + 2
38-
const categoryNameSlug = pathArr[categoryNameSlugIndex]
39-
const categoryName = slugMap[categoryNameSlug]
40-
codeData.Languages[languageName][categoryName] = codeData.Languages[languageName][categoryName] || []
41-
codeData.Languages[languageName][categoryName].push({
42-
name,
43-
description,
44-
howto,
45-
url,
46-
github,
47-
npm,
48-
gem,
49-
sourcePath: markdownFilePath,
50-
})
51-
} else {
52-
const codeDirIndex = pathArr.indexOf("code")
53-
const categoryNameSlugIndex = codeDirIndex + 1
54-
const categoryNameSlug = pathArr[categoryNameSlugIndex]
55-
const categoryName = slugMap[categoryNameSlug]
56-
codeData[categoryName] = codeData[categoryName] || []
57-
codeData[categoryName].push({
58-
name,
59-
description,
60-
howto,
61-
url,
62-
github,
63-
npm,
64-
gem,
65-
sourcePath: markdownFilePath,
66-
})
67-
}
68-
}))
38+
const categoryNameSlugIndex = languageSupportDirIndex + 2
39+
const categoryNameSlug = pathArr[categoryNameSlugIndex]
40+
const categoryName = slugMap[categoryNameSlug]
41+
codeData.Languages[languageName][categoryName] =
42+
codeData.Languages[languageName][categoryName] || []
43+
codeData.Languages[languageName][categoryName].push({
44+
name,
45+
description,
46+
howto,
47+
url,
48+
github,
49+
npm,
50+
gem,
51+
sourcePath: markdownFilePath,
52+
})
53+
} else {
54+
const codeDirIndex = pathArr.indexOf("code")
55+
const categoryNameSlugIndex = codeDirIndex + 1
56+
const categoryNameSlug = pathArr[categoryNameSlugIndex]
57+
const categoryName = slugMap[categoryNameSlug]
58+
codeData[categoryName] = codeData[categoryName] || []
59+
codeData[categoryName].push({
60+
name,
61+
description,
62+
howto,
63+
url,
64+
github,
65+
npm,
66+
gem,
67+
sourcePath: markdownFilePath,
68+
})
69+
}
70+
})
71+
)
6972
const languageList = []
7073
let sortedTools = []
7174
await Promise.all([
@@ -139,11 +142,17 @@ exports.createPages = async ({ graphql, actions }) => {
139142
sublinks
140143
sidebarTitle
141144
date
145+
tags
142146
}
143147
id
144148
}
145149
}
146150
}
151+
tagsGroup: allMarkdownRemark {
152+
group(field: frontmatter___tags) {
153+
fieldValue
154+
}
155+
}
147156
}
148157
`)
149158

@@ -177,13 +186,24 @@ exports.createPages = async ({ graphql, actions }) => {
177186
// Note that this is mutated
178187
let sideBardata = {}
179188

180-
181189
// Sidebar items to add which don't come from markdown
182190
const additionalSidebarItems = {
183-
foundation: [{
184-
name: "GraphQL Foundation",
185-
links: [{ frontmatter: { sidebarTitle:"Foundation Members", title: "Foundation Members", permalink: "/foundation/members/", date: null, category: "GraphQL Foundation" } }]
186-
}]
191+
foundation: [
192+
{
193+
name: "GraphQL Foundation",
194+
links: [
195+
{
196+
frontmatter: {
197+
sidebarTitle: "Foundation Members",
198+
title: "Foundation Members",
199+
permalink: "/foundation/members/",
200+
date: null,
201+
category: "GraphQL Foundation",
202+
},
203+
},
204+
],
205+
},
206+
],
187207
}
188208

189209
// E.g.
@@ -204,7 +224,10 @@ exports.createPages = async ({ graphql, actions }) => {
204224
parent: { relativeDirectory, sourceInstanceName },
205225
} = node
206226

207-
if (sourceInstanceName !== "content" || relativeDirectory.includes("code/")) {
227+
if (
228+
sourceInstanceName !== "content" ||
229+
relativeDirectory.includes("code/")
230+
) {
208231
return
209232
}
210233

@@ -216,13 +239,10 @@ exports.createPages = async ({ graphql, actions }) => {
216239
} else {
217240
pagesGroupedByFolder = {
218241
...pagesGroupedByFolder,
219-
[relativeDirectory]: [
220-
...pagesGroupedByFolder[relativeDirectory],
221-
node,
222-
],
242+
[relativeDirectory]: [...pagesGroupedByFolder[relativeDirectory], node],
223243
}
224244
}
225-
245+
226246
allPages.push({
227247
permalink,
228248
relativeDirectory,
@@ -231,8 +251,8 @@ exports.createPages = async ({ graphql, actions }) => {
231251
sourcePath: path.relative(__dirname, node.fileAbsolutePath),
232252
})
233253
})
234-
235-
// Loop through the sections in the sidebar, mutating the
254+
255+
// Loop through the sections in the sidebar, mutating the
236256
// next and previous objects for different
237257
Object.entries(pagesGroupedByFolder).map(([folder, pages]) => {
238258
let pagesByUrl = {}
@@ -266,7 +286,7 @@ exports.createPages = async ({ graphql, actions }) => {
266286
return
267287
}
268288
})
269-
289+
270290
if (!firstPage) {
271291
throw new Error(`First page not found in ${folder}`)
272292
}
@@ -309,18 +329,17 @@ exports.createPages = async ({ graphql, actions }) => {
309329
categoriesMap[currentCategory.name] = currentCategory
310330
}
311331

312-
sideBardata[folder] = Object.values(categoriesMap)
332+
sideBardata[folder] = Object.values(categoriesMap)
313333
})
314334

315335
Object.entries(additionalSidebarItems).map(([folder, sections]) => {
316336
sections.forEach(s => {
317337
const originalLinks = sideBardata[folder].find(l => l.name === s.name)
318-
originalLinks.links = [...originalLinks.links, ...s.links]
338+
originalLinks.links = [...originalLinks.links, ...s.links]
319339
})
320340
})
321341

322-
323-
// Use all the set up data to now tell Gatsby to create pages
342+
// Use all the set up data to now tell Gatsby to create pages
324343
// on the site
325344
allPages.forEach(page => {
326345
createPage({
@@ -334,4 +353,17 @@ exports.createPages = async ({ graphql, actions }) => {
334353
},
335354
})
336355
})
356+
357+
// Create tag pages
358+
const tagTemplate = path.resolve("src/templates/tags.tsx")
359+
const tags = result.data.tagsGroup.group
360+
tags.forEach(tag => {
361+
createPage({
362+
path: `/tags/${tag.fieldValue}/`,
363+
component: tagTemplate,
364+
context: {
365+
tag: tag.fieldValue,
366+
},
367+
})
368+
})
337369
}

0 commit comments

Comments
 (0)