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.js 2.0 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
  1. const path = require('path')
  2. const renderOpts = { textOnly: true, encodeEntities: true }
  3. // This middleware exposes a list of all categories and child articles at /categories.json.
  4. // GitHub Support uses this for internal ZenDesk search functionality.
  5. module.exports = async function categoriesForSupport (req, res, next) {
  6. const englishSiteTree = req.context.siteTree.en
  7. const allCategories = []
  8. await Promise.all(Object.keys(englishSiteTree).map(async (version) => {
  9. await Promise.all(englishSiteTree[version].childPages.map(async (productPage) => {
  10. if (productPage.page.relativePath.startsWith('early-access')) return
  11. if (!productPage.childPages) return
  12. await Promise.all(productPage.childPages.map(async (categoryPage) => {
  13. // We can't get the rendered titles from middleware/render-tree-titles
  14. // here because that middleware only runs on the current version, and this
  15. // middleware processes all versions.
  16. const name = categoryPage.page.title.includes('{')
  17. ? await categoryPage.page.renderProp('title', req.context, renderOpts)
  18. : categoryPage.page.title
  19. allCategories.push({
  20. name,
  21. published_articles: await findArticlesPerCategory(categoryPage, [], req.context)
  22. })
  23. }))
  24. }))
  25. }))
  26. return res.json(allCategories)
  27. }
  28. async function findArticlesPerCategory (currentPage, articlesArray, context) {
  29. if (currentPage.page.documentType === 'article') {
  30. const title = currentPage.page.title.includes('{')
  31. ? await currentPage.page.renderProp('title', context, renderOpts)
  32. : currentPage.page.title
  33. articlesArray.push({
  34. title,
  35. slug: path.basename(currentPage.href)
  36. })
  37. }
  38. if (!currentPage.childPages) return articlesArray
  39. // Run recursively to find any articles deeper in the tree.
  40. await Promise.all(currentPage.childPages.map(async (childPage) => {
  41. await findArticlesPerCategory(childPage, articlesArray, context)
  42. }))
  43. return articlesArray
  44. }
Tip!

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

Comments

Loading...