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

site-tree.js 6.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
  1. const path = require('path')
  2. const { productMap } = require('./all-products')
  3. const languageCodes = Object.keys(require('./languages'))
  4. const addTitlesToTree = require('./site-tree-titles')
  5. const allVersions = Object.keys(require('./all-versions'))
  6. const { getPathWithoutVersion } = require('./path-utils')
  7. const getApplicableVersions = require('./get-applicable-versions')
  8. const findPage = require('./find-page')
  9. const removeFPTFromPath = require('./remove-fpt-from-path')
  10. // This module builds a localized tree of every page on the site
  11. // It includes single-source pages that have different variants
  12. // for different product versions, e.g. dotcom vs GHES
  13. //
  14. // siteTree[languageCode][version].products[productHref].categories[categoryHref].maptopics[maptopicHref].articles
  15. // or if a category has direct child articles:
  16. // siteTree[languageCode][version].products[productHref].categories[categoryHref].articles
  17. module.exports = async function buildSiteTree (pageMap, site, redirects) {
  18. const siteTree = {}
  19. languageCodes.forEach(languageCode => {
  20. siteTree[languageCode] = {}
  21. allVersions.forEach(version => {
  22. siteTree[languageCode][version] = {}
  23. const productTree = {}
  24. Object.values(productMap).forEach(item => {
  25. const product = { title: item.name }
  26. // return early if the product has external docs, like Atom
  27. if (item.external) {
  28. product.href = item.href
  29. product.external = true
  30. productTree[item.id] = product
  31. return
  32. }
  33. // find the product TOC page so we have access to the TOC items
  34. const page = findPage(item.href, pageMap, redirects)
  35. // skip if page can't be found in this version
  36. if (!page) return
  37. if (!getApplicableVersions(page.versions).includes(version)) return
  38. // item.hrefs have a default version via lib/all-products, so update to the current version
  39. product.href = removeFPTFromPath(path.join('/', languageCode, version, getPathWithoutVersion(item.href)))
  40. product.categories = buildCategoriesTree(page.tocItems, product.href, pageMap, redirects, version)
  41. productTree[item.id] = product
  42. return null
  43. })
  44. siteTree[languageCode][version].products = productTree
  45. })
  46. })
  47. await addTitlesToTree(siteTree, site)
  48. return siteTree
  49. }
  50. function buildCategoriesTree (tocItems, versionedProductHref, pageMap, redirects, version) {
  51. const categoryTree = {}
  52. // for every category in a product TOC...
  53. tocItems.forEach(item => {
  54. const category = {}
  55. category.href = path.posix.join(versionedProductHref, item.href)
  56. // find the category TOC page and get its TOC items
  57. const page = findPage(category.href, pageMap, redirects)
  58. // skip if page can't be found in this version
  59. if (!page) return
  60. if (!getApplicableVersions(page.versions).includes(version)) return
  61. category.title = page.title
  62. category.shortTitle = page.shortTitle
  63. // support standalone pages at the category level, like actions/quickstart.md
  64. if (!page.tocItems) {
  65. category.standalone = true
  66. }
  67. // TOC items can be maptopics and/or articles
  68. if (page.tocItems) {
  69. const hasMaptopics = page.tocItems.some(item => item.type === 'maptopic')
  70. // if TOC contains maptopics, build a maptopics tree
  71. // otherwise build an articles tree
  72. if (hasMaptopics) {
  73. category.maptopics = buildMaptopicsTree(page.tocItems, category.href, pageMap, redirects, version)
  74. } else {
  75. category.articles = buildArticlesTree(page.tocItems, category.href, pageMap, redirects, version)
  76. }
  77. }
  78. categoryTree[category.href] = category
  79. })
  80. return categoryTree
  81. }
  82. function buildMaptopicsTree (tocItems, versionedCategoryHref, pageMap, redirects, version) {
  83. const maptopicTree = {}
  84. // for every maptopic in a category TOC...
  85. tocItems
  86. .filter(item => item.type === 'maptopic')
  87. .forEach(item => {
  88. const maptopic = {}
  89. maptopic.href = path.posix.join(versionedCategoryHref, item.href)
  90. // find the category TOC page and get its TOC items
  91. const page = findPage(maptopic.href, pageMap, redirects)
  92. // skip if page can't be found in this version
  93. if (!page) return
  94. if (!getApplicableVersions(page.versions).includes(version)) return
  95. // if this is not a maptopic, return early
  96. if (!page.mapTopic) return
  97. maptopic.title = page.title
  98. maptopic.shortTitle = page.shortTitle
  99. maptopic.hidden = page.hidden
  100. // make the child articles accessible to the page object for maptopic rendering
  101. maptopic.childArticles = getChildArticles(tocItems, item.href)
  102. maptopic.articles = buildArticlesTree(maptopic.childArticles, versionedCategoryHref, pageMap, redirects, version)
  103. maptopicTree[maptopic.href] = maptopic
  104. })
  105. return maptopicTree
  106. }
  107. function buildArticlesTree (tocItems, versionedCategoryHref, pageMap, redirects, version) {
  108. const articleTree = {}
  109. // REST categories may not have TOC items
  110. if (!tocItems) return articleTree
  111. // for every article in a maptopic (or category) TOC...
  112. tocItems.forEach(item => {
  113. const article = {}
  114. article.href = path.posix.join(versionedCategoryHref, item.href)
  115. // find the category TOC page and get its TOC items
  116. const page = findPage(article.href, pageMap, redirects)
  117. // skip if page can't be found in this version
  118. if (!page) return
  119. if (!getApplicableVersions(page.versions).includes(version)) return
  120. article.title = page.title
  121. article.shortTitle = page.shortTitle
  122. article.hidden = page.hidden
  123. articleTree[article.href] = article
  124. })
  125. return articleTree
  126. }
  127. // in a category TOC, get the {% link_in_list %} items under the current {% topic_link_in_list %}
  128. function getChildArticles (tocItems, maptopicPath) {
  129. let withinMaptopic = false
  130. return tocItems.filter(item => {
  131. if (item.type === 'maptopic') {
  132. if (item.href === maptopicPath) {
  133. withinMaptopic = true
  134. } else {
  135. withinMaptopic = false
  136. }
  137. } else {
  138. if (withinMaptopic) return item.href
  139. }
  140. return false
  141. })
  142. }
Tip!

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

Comments

Loading...