Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

categories-for-support-team.js 2.1 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
  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. const path = require('path')
  5. const fs = require('fs').promises
  6. const matter = require('gray-matter')
  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. module.exports = async (req, res, next) => {
  11. if (req.path !== '/categories.json') return next()
  12. const categories = await generateCategories()
  13. return res.json(categories)
  14. }
  15. async function generateCategories () {
  16. // get links included in dotcom index page.
  17. // each link corresponds to a dotcom subdirectory
  18. // example: getting-started-with-github
  19. const links = getLinks(await fs.readFile(dotcomIndex, 'utf8'))
  20. // get links included in each subdir's index page
  21. // these are links to articles
  22. const categories = await Promise.all(links.map(async link => {
  23. const category = {}
  24. const indexPath = getPath(link, 'index')
  25. const indexContents = await fs.readFile(indexPath, 'utf8')
  26. const { data, content } = matter(indexContents)
  27. // get name from title frontmatter
  28. category.name = data.title
  29. // get child article links
  30. const articleLinks = getLinks(content)
  31. category.published_articles = (await Promise.all(articleLinks.map(async articleLink => {
  32. // get title from frontmatter
  33. const articlePath = getPath(link, articleLink)
  34. const articleContents = await fs.readFile(articlePath, 'utf8')
  35. const { data } = matter(articleContents)
  36. // do not include map topics in list of published articles
  37. if (data.mapTopic) return
  38. return {
  39. title: data.title,
  40. slug: articleLink
  41. }
  42. }))).filter(Boolean)
  43. return category
  44. }))
  45. return categories
  46. }
  47. function getLinks (contents) {
  48. return contents.match(linkRegex)
  49. .map(link => link.match(linkRegex.source)[1])
  50. }
  51. function getPath (link, filename) {
  52. return path.join(dotcomDir, link, `${filename}.md`)
  53. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...