Skip to content

Commit

Permalink
#13 add faq topic categories
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleMit committed Apr 7, 2020
1 parent 3d08e50 commit 7350910
Show file tree
Hide file tree
Showing 6 changed files with 898 additions and 180 deletions.
60 changes: 59 additions & 1 deletion faq-site/.eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,59 @@ module.exports = function(eleventyConfig) {
"node_modules/mark.js/dist/mark.min.js": "/assets/mark.js"
});

eleventyConfig.addCollection("FaqsByTopic", col => {
let allFaqs = col.items[0].data.faqs.qnaDocuments

// get all sorted topics
let sortedTopics = col.items[0].data.topics

// get all new topics
let allTopics = allFaqs.map(faq => {
let catMetadata = faq.metadata.find(m => m.name === "category")
return catMetadata ? catMetadata.value : ""
}).filter(cat => cat)

// deduplicate
let allUniqTopics = [...new Set(allTopics)];

// get new topics
let newTopics = allUniqTopics.filter(at => {
let faqTopicExists = sortedTopics.some(st => {
let nameExists = stringsAlphaEqual(st, at)
return nameExists
})
return !faqTopicExists
})

// merge topics
let mergeTopics = sortedTopics.concat(newTopics)

// remove chitchat
let publishTopics = mergeTopics.filter(t => t.toLowerCase() !== "chitchat")

// map topics array into {topic: name, faqs: []}
let topicCollection = publishTopics.map(topic => {

let faqs = allFaqs.filter(faq => {
let catMetadata = faq.metadata.find(m => m.name === "category")
let faqMatchesTopic = catMetadata && stringsAlphaEqual(catMetadata.value, topic)
return faqMatchesTopic
})

let properName = topic.charAt(0).toUpperCase() + topic.slice(1);

return {
name: properName,
faqs: faqs
}
})

// make sure faqs exist for topic
let publishTopicCollection = topicCollection.filter(t => t.faqs.length)

return publishTopicCollection;
});

// add filters
eleventyConfig.addFilter("slugify", function(s) {
// strip special chars
Expand All @@ -28,4 +81,9 @@ module.exports = function(eleventyConfig) {
// https://www.11ty.io/docs/config/#default-template-engine-for-markdown-files
markdownTemplateEngine: "njk",
};
};
};


function stringsAlphaEqual(a, b) {
return a.toLowerCase().replace(/[^\w]/g, '') === b.toLowerCase().replace(/[^\w]/g, '')
}
Loading

0 comments on commit 7350910

Please sign in to comment.