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

generic-toc.js 2.7 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
  1. const findPageInSiteTree = require('../../lib/find-page-in-site-tree')
  2. // This module adds either flatTocItems or nestedTocItems to the context object for
  3. // product, categorie, and map topic TOCs that don't have other layouts specified.
  4. // They are rendered by includes/generic-toc-flat.html or inclueds/generic-toc-nested.html.
  5. module.exports = async function genericToc (req, res, next) {
  6. if (!req.context.page) return next()
  7. if (req.context.currentLayoutName !== 'default') return next()
  8. // This middleware can only run on product, category, and map topics.
  9. if (req.context.page.documentType === 'homepage' || req.context.page.documentType === 'article') return next()
  10. // This one product TOC is weird.
  11. const isOneOffProductToc = req.context.page.relativePath === 'github/index.md'
  12. // There are different types of TOC depending on the document type.
  13. const tocTypes = {
  14. product: 'flat',
  15. category: 'nested',
  16. mapTopic: 'flat'
  17. }
  18. // Find the current TOC type based on the current document type.
  19. const currentTocType = tocTypes[req.context.page.documentType]
  20. // Find the part of the site tree that corresponds to the current path.
  21. const treePage = findPageInSiteTree(req.context.currentProductTree, req.context.currentEnglishTree, req.pagePath)
  22. // Conditionally run getTocItems() recursively.
  23. let isRecursive
  24. // Conditionally render intros.
  25. let renderIntros
  26. // Get an array of child links with intros and add it to the context object.
  27. if (currentTocType === 'flat' && !isOneOffProductToc) {
  28. isRecursive = false
  29. renderIntros = true
  30. req.context.genericTocFlat = await getTocItems(treePage.childPages, req.context, isRecursive, renderIntros)
  31. }
  32. // Get an array of child map topics and their child articles and add it to the context object.
  33. if (currentTocType === 'nested' || isOneOffProductToc) {
  34. isRecursive = !isOneOffProductToc
  35. renderIntros = false
  36. req.context.genericTocNested = await getTocItems(treePage.childPages, req.context, isRecursive, renderIntros)
  37. }
  38. return next()
  39. }
  40. async function getTocItems (pagesArray, context, isRecursive, renderIntros) {
  41. return (await Promise.all(pagesArray.map(async (child) => {
  42. // Do not include hidden child items on a TOC page
  43. if (child.page.hidden && !context.currentPath.includes('/early-access/')) return
  44. return {
  45. title: child.renderedFullTitle,
  46. fullPath: child.href,
  47. // renderProp is the most expensive part of this function.
  48. intro: renderIntros ? await child.page.renderProp('intro', context, { unwrap: true }) : null,
  49. childTocItems: isRecursive && child.childPages ? getTocItems(child.childPages, context, isRecursive, renderIntros) : null
  50. }
  51. })))
  52. .filter(Boolean)
  53. }
Tip!

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

Comments

Loading...