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

get-toc-items.js 1.3 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
  1. const { productMap } = require('./all-products')
  2. const productTOCs = Object.values(productMap)
  3. .filter(product => !product.external)
  4. .map(product => product.toc.replace('content/', ''))
  5. const linkString = /{% [^}]*?link.*? \/(.*?) ?%}/m
  6. const linksArray = new RegExp(linkString.source, 'gm')
  7. // return an array of objects like { type: 'category|maptopic|article', href: 'path' }
  8. module.exports = function getTocItems (page) {
  9. // only process product and category tocs
  10. if (!page.relativePath.endsWith('index.md')) return
  11. if (page.relativePath === 'index.md') return
  12. // ignore content above Table of Contents heading
  13. const pageContent = page.markdown.replace(/[\s\S]*?# Table of contents\n/im, '')
  14. // find array of TOC link strings
  15. const rawItems = pageContent.match(linksArray)
  16. // return an empty array if this is a localized page
  17. if (!rawItems) {
  18. return []
  19. }
  20. return rawItems.map(item => {
  21. const tocItem = {}
  22. // a product's toc items are always categories
  23. // whereas a category's toc items can be either maptopics or articles
  24. tocItem.type = productTOCs.includes(page.relativePath)
  25. ? 'category'
  26. : item.includes('topic_') ? 'maptopic' : 'article'
  27. tocItem.href = item.match(linkString)[1]
  28. return tocItem
  29. })
  30. }
Tip!

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

Comments

Loading...