| 8 | |
| 9 | // return an array of objects like { type: 'category|maptopic|article', href: 'path' } |
| 10 | export default function getTocItems(page) { |
| 11 | // only process product and category tocs |
| 12 | if (!page.relativePath.endsWith('index.md')) return |
| 13 | if (page.relativePath === 'index.md') return |
| 14 | |
| 15 | // ignore content above Table of Contents heading |
| 16 | const pageContent = page.markdown.replace(/[\s\S]*?# Table of contents\n/im, '') |
| 17 | |
| 18 | // find array of TOC link strings |
| 19 | const rawItems = pageContent.match(linksArray) |
| 20 | |
| 21 | // return an empty array if this is a localized page |
| 22 | if (!rawItems) { |
| 23 | return [] |
| 24 | } |
| 25 | |
| 26 | return rawItems.map((item) => { |
| 27 | const tocItem = {} |
| 28 | |
| 29 | // a product's toc items are always categories |
| 30 | // whereas a category's toc items can be either maptopics or articles |
| 31 | tocItem.type = productTOCs.includes(page.relativePath) |
| 32 | ? 'category' |
| 33 | : item.includes('topic_') |
| 34 | ? 'maptopic' |
| 35 | : 'article' |
| 36 | |
| 37 | tocItem.href = item.match(linkString)[1] |
| 38 | |
| 39 | return tocItem |
| 40 | }) |
| 41 | } |