Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
🧹 extract utility function
  • Loading branch information
ooloth committed Nov 27, 2019
commit a16e266526738c77b8e1efce62da8dce063f7287
13 changes: 10 additions & 3 deletions gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
// 1. Generate string versions of tutorial array fields for faster search
///////////////////////////////////////////////////////////////////////////////////

// TODO: unit test this utility
function convertArrayToString(array) {
return array ? array.join(` `).toLowerCase() : ``
}

exports.onCreateNode = ({ node, actions }) => {
const { createNodeField } = actions

if (node.internal.type === 'TutorialsYaml') {
createNodeField({
node,
name: `formatsAsString`,
value: node.formats ? node.formats.join(` `).toLowerCase() : ``,
value: convertArrayToString(node.formats),
})

createNodeField({
node,
name: `topicsAsString`,
value: node.topics ? node.topics.join(` `).toLowerCase() : ``,
value: convertArrayToString(node.topics),
})

createNodeField({
node,
name: `authorsAsString`,
value: node.authors ? node.authors.join(` `).toLowerCase() : ``,
value: convertArrayToString(node.authors),
})
}
}
Expand Down Expand Up @@ -90,6 +95,8 @@ exports.createPages = async ({ graphql, reporter }) => {
const { tutorialsWithDates, tutorialsWithoutDates } = result.data
const tutorials = [...tutorialsWithDates.nodes, ...tutorialsWithoutDates.nodes]

// TODO: extract repeated patterns into utilities and test them...

// Create a sorted list of all unique formats
const formatArrays = tutorials.map(tutorial => tutorial.formats)
const formats = [
Expand Down