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

page-data.js 5.1 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
  1. const path = require('path')
  2. const languages = require('./languages')
  3. const versions = Object.keys(require('./all-versions'))
  4. const enterpriseServerVersions = versions.filter(v => v.startsWith('enterprise-server@'))
  5. const createTree = require('./create-tree')
  6. const renderContent = require('./render-content')
  7. const loadSiteData = require('./site-data')
  8. const nonEnterpriseDefaultVersion = require('./non-enterprise-default-version')
  9. const englishPath = path.posix.join(__dirname, '..', 'content')
  10. const renderOpts = { textOnly: true, encodeEntities: true }
  11. /**
  12. * We only need to initialize pages _once per language_ since pages don't change per version. So we do that
  13. * first since it's the most expensive work. This gets us a nested object with pages attached that we can use
  14. * as the basis for the siteTree after we do some versioning. We can also use it to derive the pageList.
  15. */
  16. async function loadUnversionedTree () {
  17. const unversionedTree = {}
  18. await Promise.all(Object.values(languages)
  19. .map(async (langObj) => {
  20. unversionedTree[langObj.code] = await createTree(englishPath, langObj)
  21. }))
  22. return unversionedTree
  23. }
  24. /**
  25. * The siteTree is a nested object with pages for every language and version, useful for nav because it
  26. * contains parent, child, and sibling relationships:
  27. *
  28. * siteTree[languageCode][version].childPages[<array of pages>].childPages[<array of pages>] (etc...)
  29. * Given an unversioned tree of all pages per language, we can walk it for each version and do a couple operations:
  30. * 1. Add a versioned href to every item, where the href is the relevant permalink for the current version.
  31. * 2. Drop any child pages that are not available in the current version.
  32. *
  33. * Order of languages and versions doesn't matter, but order of child page arrays DOES matter (for navigation).
  34. */
  35. async function loadSiteTree (unversionedTree, siteData) {
  36. const site = siteData || loadSiteData()
  37. const rawTree = Object.assign({}, (unversionedTree || await loadUnversionedTree()))
  38. const siteTree = {}
  39. // For every language...
  40. await Promise.all(Object.keys(languages).map(async (langCode) => {
  41. const treePerVersion = {}
  42. // in every version...
  43. await Promise.all(versions.map(async (version) => {
  44. // "version" the pages.
  45. treePerVersion[version] = await versionPages(Object.assign({}, rawTree[langCode]), version, langCode, site)
  46. }))
  47. siteTree[langCode] = treePerVersion
  48. }))
  49. return siteTree
  50. }
  51. async function versionPages (obj, version, langCode, site) {
  52. // Add a versioned href as a convenience for use in layouts.
  53. obj.href = obj.page.permalinks
  54. .find(pl => pl.pageVersion === version || (pl.pageVersion === 'homepage' && version === nonEnterpriseDefaultVersion))
  55. .href
  56. const context = {
  57. currentLanguage: langCode,
  58. currentVersion: version,
  59. enterpriseServerVersions,
  60. site: site[langCode].site
  61. }
  62. // The Liquid parseAndRender method is MUCH faster than renderContent or renderProp.
  63. // This only works for titles and short titles, which have no other Markdown that needs
  64. // to be converted to HTML, so we can get away with _only_ parsing Liquid.
  65. obj.renderedFullTitle = obj.page.title.includes('{')
  66. ? await renderContent.liquid.parseAndRender(obj.page.title, context, renderOpts)
  67. : obj.page.title
  68. if (obj.page.shortTitle) {
  69. obj.renderedShortTitle = obj.page.shortTitle.includes('{')
  70. ? await renderContent.liquid.parseAndRender(obj.page.shortTitle, context, renderOpts)
  71. : obj.page.shortTitle
  72. }
  73. if (!obj.childPages) return obj
  74. const versionedChildPages = await Promise.all(obj.childPages
  75. // Drop child pages that do not apply to the current version.
  76. .filter(childPage => childPage.page.applicableVersions.includes(version))
  77. // Version the child pages recursively.
  78. .map(childPage => versionPages(Object.assign({}, childPage), version, langCode, site)))
  79. obj.childPages = [...versionedChildPages]
  80. return obj
  81. }
  82. // Derive a flat array of Page objects in all languages.
  83. async function loadPageList (unversionedTree) {
  84. const rawTree = unversionedTree || await loadUnversionedTree()
  85. const pageList = []
  86. await Promise.all(Object.keys(languages).map(async (langCode) => {
  87. await addToCollection(rawTree[langCode], pageList)
  88. }))
  89. async function addToCollection (item, collection) {
  90. if (!item.page) return
  91. collection.push(item.page)
  92. if (!item.childPages) return
  93. await Promise.all(item.childPages.map(async (childPage) => await addToCollection(childPage, collection)))
  94. }
  95. return pageList
  96. }
  97. // Create an object from the list of all pages with permalinks as keys for fast lookup.
  98. function createMapFromArray (pageList) {
  99. const pageMap =
  100. pageList.reduce(
  101. (pageMap, page) => {
  102. for (const permalink of page.permalinks) {
  103. pageMap[permalink.href] = page
  104. }
  105. return pageMap
  106. },
  107. {}
  108. )
  109. return pageMap
  110. }
  111. async function loadPageMap (pageList) {
  112. const pages = pageList || await loadPageList()
  113. return createMapFromArray(pages)
  114. }
  115. module.exports = {
  116. loadUnversionedTree,
  117. loadSiteTree,
  118. loadPages: loadPageList,
  119. loadPageMap
  120. }
Tip!

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

Comments

Loading...