From 15ff2e2e986215c5090399b3b3ab0e13bf8b6b19 Mon Sep 17 00:00:00 2001 From: Sarah Schneider Date: Wed, 14 Apr 2021 10:34:49 -0400 Subject: [PATCH 01/10] update categories-for-support-team middleware --- middleware/categories-for-support-team.js | 111 ++++++++++------------ middleware/index.js | 2 +- 2 files changed, 51 insertions(+), 62 deletions(-) diff --git a/middleware/categories-for-support-team.js b/middleware/categories-for-support-team.js index b288bc3b13..b4ed5b3e82 100644 --- a/middleware/categories-for-support-team.js +++ b/middleware/categories-for-support-team.js @@ -1,65 +1,54 @@ -// This middleware serves a file that's used by the GitHub support team -// to quickly search for Help articles by title and insert the link to -// the article into a reply to a customer. const path = require('path') -const matter = require('gray-matter') -const readFileAsync = require('../lib/readfile-async') -const dotcomDir = path.join(__dirname, '../content/github') -const dotcomIndex = path.join(dotcomDir, 'index.md') -const linkRegex = /{% (?:topic_)?link_in_list ?\/(.*?) ?%}/g module.exports = async function categoriesForSupportTeam (req, res, next) { - if (req.path !== '/categories.json') return next() - const categories = await generateCategories() - return res.json(categories) -} - -async function generateCategories () { - // get links included in dotcom index page. - // each link corresponds to a dotcom subdirectory - // example: getting-started-with-github - const links = getLinks(await readFileAsync(dotcomIndex, 'utf8')) - - // get links included in each subdir's index page - // these are links to articles - const categories = await Promise.all(links.map(async link => { - const category = {} - const indexPath = getPath(link, 'index') - const indexContents = await readFileAsync(indexPath, 'utf8') - const { data, content } = matter(indexContents) - - // get name from title frontmatter - category.name = data.title - - // get child article links - const articleLinks = getLinks(content) - - category.published_articles = (await Promise.all(articleLinks.map(async articleLink => { - // get title from frontmatter - const articlePath = getPath(link, articleLink) - const articleContents = await readFileAsync(articlePath, 'utf8') - const { data } = matter(articleContents) - - // do not include map topics in list of published articles - if (data.mapTopic) return - - return { - title: data.title, - slug: articleLink - } - }))).filter(Boolean) - - return category - })) - - return categories -} - -function getLinks (contents) { - return contents.match(linkRegex) - .map(link => link.match(linkRegex.source)[1]) -} - -function getPath (link, filename) { - return path.join(dotcomDir, link, `${filename}.md`) + const englishSiteTree = req.context.siteTree.en + + const allCategories = [] + + Object.keys(englishSiteTree).forEach(version => { + const versionedProductsTree = englishSiteTree[version].products + + Object.values(versionedProductsTree).forEach(productObj => { + if (productObj.id === 'early-access') return + if (productObj.external) return + + Object.values(productObj.categories).forEach(categoryObj => { + const articlesArry = [] + + if (categoryObj.maptopics) { + Object.values(categoryObj.maptopics).forEach(maptopicObj => { + Object.values(maptopicObj.articles).forEach(articleObj => { + articlesArry.push({ + title: articleObj.title, + slug: path.basename(articleObj.href) + }) + }) + }) + } + + if (categoryObj.standalone) { + articlesArry.push({ + title: categoryObj.title, + slug: path.basename(categoryObj.href) + }) + } + + if (categoryObj.articles) { + Object.values(categoryObj.articles).forEach(articleObj => { + articlesArry.push({ + title: articleObj.title, + slug: path.basename(articleObj.href) + }) + }) + } + + allCategories.push({ + name: categoryObj.title, + published_articles: articlesArry + }) + }) + }) + }) + + return res.json(allCategories) } diff --git a/middleware/index.js b/middleware/index.js index a97f9e9d21..820888ebf8 100644 --- a/middleware/index.js +++ b/middleware/index.js @@ -97,7 +97,7 @@ module.exports = function (app) { app.use(asyncMiddleware(instrument('./archived-enterprise-versions'))) app.use(instrument('./robots')) app.use(/(\/.*)?\/early-access$/, instrument('./contextualizers/early-access-links')) - app.use(asyncMiddleware(instrument('./categories-for-support-team'))) + app.use('/categories.json', asyncMiddleware(instrument('./categories-for-support-team'))) app.use(instrument('./loaderio-verification')) app.get('/_500', asyncMiddleware(instrument('./trigger-error'))) From 3a2839cb17c44e7ad29ebdf7743fbfdafc9d78df Mon Sep 17 00:00:00 2001 From: Sarah Schneider Date: Wed, 14 Apr 2021 10:35:10 -0400 Subject: [PATCH 02/10] differentiate between title and shortTitle in siteTree --- includes/sidebar-specific-product.html | 14 +++++++----- lib/site-tree-titles.js | 31 +++++++++++++++++--------- lib/site-tree.js | 6 +++-- 3 files changed, 34 insertions(+), 17 deletions(-) diff --git a/includes/sidebar-specific-product.html b/includes/sidebar-specific-product.html index e4684187de..99dc1c4d41 100644 --- a/includes/sidebar-specific-product.html +++ b/includes/sidebar-specific-product.html @@ -19,13 +19,14 @@ {% capture fullPathToCategory %}{{category[1].href}}{% endcapture %}