|
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. |
4 | 1 | 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 |
10 | 2 |
|
11 | 3 | 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) |
65 | 54 | } |
0 commit comments