Skip to content

Commit 15ff2e2

Browse files
committed
update categories-for-support-team middleware
1 parent dbb4f92 commit 15ff2e2

2 files changed

Lines changed: 51 additions & 62 deletions

File tree

Lines changed: 50 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,54 @@
1-
// This middleware serves a file that's used by the GitHub support team
2-
// to quickly search for Help articles by title and insert the link to
3-
// the article into a reply to a customer.
41
const path = require('path')
5-
const matter = require('gray-matter')
6-
const readFileAsync = require('../lib/readfile-async')
7-
const dotcomDir = path.join(__dirname, '../content/github')
8-
const dotcomIndex = path.join(dotcomDir, 'index.md')
9-
const linkRegex = /{% (?:topic_)?link_in_list ?\/(.*?) ?%}/g
102

113
module.exports = async function categoriesForSupportTeam (req, res, next) {
12-
if (req.path !== '/categories.json') return next()
13-
const categories = await generateCategories()
14-
return res.json(categories)
15-
}
16-
17-
async function generateCategories () {
18-
// get links included in dotcom index page.
19-
// each link corresponds to a dotcom subdirectory
20-
// example: getting-started-with-github
21-
const links = getLinks(await readFileAsync(dotcomIndex, 'utf8'))
22-
23-
// get links included in each subdir's index page
24-
// these are links to articles
25-
const categories = await Promise.all(links.map(async link => {
26-
const category = {}
27-
const indexPath = getPath(link, 'index')
28-
const indexContents = await readFileAsync(indexPath, 'utf8')
29-
const { data, content } = matter(indexContents)
30-
31-
// get name from title frontmatter
32-
category.name = data.title
33-
34-
// get child article links
35-
const articleLinks = getLinks(content)
36-
37-
category.published_articles = (await Promise.all(articleLinks.map(async articleLink => {
38-
// get title from frontmatter
39-
const articlePath = getPath(link, articleLink)
40-
const articleContents = await readFileAsync(articlePath, 'utf8')
41-
const { data } = matter(articleContents)
42-
43-
// do not include map topics in list of published articles
44-
if (data.mapTopic) return
45-
46-
return {
47-
title: data.title,
48-
slug: articleLink
49-
}
50-
}))).filter(Boolean)
51-
52-
return category
53-
}))
54-
55-
return categories
56-
}
57-
58-
function getLinks (contents) {
59-
return contents.match(linkRegex)
60-
.map(link => link.match(linkRegex.source)[1])
61-
}
62-
63-
function getPath (link, filename) {
64-
return path.join(dotcomDir, link, `${filename}.md`)
4+
const englishSiteTree = req.context.siteTree.en
5+
6+
const allCategories = []
7+
8+
Object.keys(englishSiteTree).forEach(version => {
9+
const versionedProductsTree = englishSiteTree[version].products
10+
11+
Object.values(versionedProductsTree).forEach(productObj => {
12+
if (productObj.id === 'early-access') return
13+
if (productObj.external) return
14+
15+
Object.values(productObj.categories).forEach(categoryObj => {
16+
const articlesArry = []
17+
18+
if (categoryObj.maptopics) {
19+
Object.values(categoryObj.maptopics).forEach(maptopicObj => {
20+
Object.values(maptopicObj.articles).forEach(articleObj => {
21+
articlesArry.push({
22+
title: articleObj.title,
23+
slug: path.basename(articleObj.href)
24+
})
25+
})
26+
})
27+
}
28+
29+
if (categoryObj.standalone) {
30+
articlesArry.push({
31+
title: categoryObj.title,
32+
slug: path.basename(categoryObj.href)
33+
})
34+
}
35+
36+
if (categoryObj.articles) {
37+
Object.values(categoryObj.articles).forEach(articleObj => {
38+
articlesArry.push({
39+
title: articleObj.title,
40+
slug: path.basename(articleObj.href)
41+
})
42+
})
43+
}
44+
45+
allCategories.push({
46+
name: categoryObj.title,
47+
published_articles: articlesArry
48+
})
49+
})
50+
})
51+
})
52+
53+
return res.json(allCategories)
6554
}

middleware/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ module.exports = function (app) {
9797
app.use(asyncMiddleware(instrument('./archived-enterprise-versions')))
9898
app.use(instrument('./robots'))
9999
app.use(/(\/.*)?\/early-access$/, instrument('./contextualizers/early-access-links'))
100-
app.use(asyncMiddleware(instrument('./categories-for-support-team')))
100+
app.use('/categories.json', asyncMiddleware(instrument('./categories-for-support-team')))
101101
app.use(instrument('./loaderio-verification'))
102102
app.get('/_500', asyncMiddleware(instrument('./trigger-error')))
103103

0 commit comments

Comments
 (0)