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

category-pages.js 7.5 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
  1. const path = require('path')
  2. const fs = require('fs')
  3. const walk = require('walk-sync')
  4. const matter = require('../../lib/read-frontmatter')
  5. const { zip, difference } = require('lodash')
  6. const GithubSlugger = require('github-slugger')
  7. const { XmlEntities } = require('html-entities')
  8. const readFileAsync = require('../../lib/readfile-async')
  9. const loadSiteData = require('../../lib/site-data')
  10. const renderContent = require('../../lib/render-content')
  11. const getApplicableVersions = require('../../lib/get-applicable-versions')
  12. const slugger = new GithubSlugger()
  13. const entities = new XmlEntities()
  14. const contentDir = path.join(__dirname, '../../content')
  15. describe('category pages', () => {
  16. let siteData
  17. beforeAll(async () => {
  18. // Load the English site data
  19. const allSiteData = await loadSiteData()
  20. siteData = allSiteData.en.site
  21. })
  22. const walkOptions = {
  23. globs: ['*/index.md', 'enterprise/*/index.md'],
  24. ignore: ['{rest,graphql}/**', 'enterprise/index.md', '**/articles/**', 'early-access/**'],
  25. directories: false,
  26. includeBasePath: true
  27. }
  28. const productIndices = walk(contentDir, walkOptions)
  29. const productNames = productIndices.map(index => path.basename(path.dirname(index)))
  30. // Combine those to fit Jest's `.each` usage
  31. const productTuples = zip(productNames, productIndices)
  32. describe.each(productTuples)(
  33. 'product "%s"',
  34. (productName, productIndex) => {
  35. // Get links included in product index page.
  36. // Each link corresponds to a product subdirectory (category).
  37. // Example: "getting-started-with-github"
  38. const contents = fs.readFileSync(productIndex, 'utf8') // TODO move to async
  39. const { data } = matter(contents)
  40. const productDir = path.dirname(productIndex)
  41. const categoryLinks = data.children
  42. // Only include category directories, not standalone category files like content/actions/quickstart.md
  43. .filter(link => fs.existsSync(getPath(productDir, link, 'index')))
  44. // TODO this should move to async, but you can't asynchronously define tests with Jest...
  45. // Map those to the Markdown file paths that represent that category page index
  46. const categoryPaths = categoryLinks.map(link => getPath(productDir, link, 'index'))
  47. // Make them relative for nicer display in test names
  48. const categoryRelativePaths = categoryPaths.map(p => path.relative(contentDir, p))
  49. // Combine those to fit Jest's `.each` usage
  50. const categoryTuples = zip(categoryRelativePaths, categoryPaths, categoryLinks)
  51. if (!categoryTuples.length) return
  52. // TODO rework this for the new site tree structure
  53. describe.each(categoryTuples)(
  54. 'category index "%s"',
  55. (indexRelPath, indexAbsPath, indexLink) => {
  56. let publishedArticlePaths, availableArticlePaths, indexTitle, categoryVersions
  57. const articleVersions = {}
  58. beforeAll(async () => {
  59. const categoryDir = path.dirname(indexAbsPath)
  60. // Get child article links included in each subdir's index page
  61. const indexContents = await readFileAsync(indexAbsPath, 'utf8')
  62. const { data } = matter(indexContents)
  63. categoryVersions = getApplicableVersions(data.versions, indexAbsPath)
  64. const articleLinks = data.children
  65. // Save the index title for later testing
  66. indexTitle = await renderContent(data.title, { site: siteData }, { textOnly: true })
  67. publishedArticlePaths = (await Promise.all(
  68. articleLinks.map(async (articleLink) => {
  69. const articlePath = getPath(productDir, indexLink, articleLink)
  70. const articleContents = await readFileAsync(articlePath, 'utf8')
  71. const { data } = matter(articleContents)
  72. // Do not include map topics in list of published articles
  73. if (data.mapTopic || data.hidden) return null
  74. // ".../content/github/{category}/{article}.md" => "/{article}"
  75. return `/${path.relative(categoryDir, articlePath).replace(/\.md$/, '')}`
  76. })
  77. )).filter(Boolean)
  78. // Get all of the child articles that exist in the subdir
  79. const childEntries = await fs.promises.readdir(categoryDir, { withFileTypes: true })
  80. const childFileEntries = childEntries.filter(ent => ent.isFile() && ent.name !== 'index.md')
  81. const childFilePaths = childFileEntries.map(ent => path.join(categoryDir, ent.name))
  82. availableArticlePaths = (await Promise.all(
  83. childFilePaths.map(async (articlePath) => {
  84. const articleContents = await readFileAsync(articlePath, 'utf8')
  85. const { data } = matter(articleContents)
  86. // Do not include map topics nor hidden pages in list of available articles
  87. if (data.mapTopic || data.hidden) return null
  88. // ".../content/github/{category}/{article}.md" => "/{article}"
  89. return `/${path.relative(categoryDir, articlePath).replace(/\.md$/, '')}`
  90. })
  91. )).filter(Boolean)
  92. await Promise.all(
  93. childFilePaths.map(async (articlePath) => {
  94. const articleContents = await readFileAsync(articlePath, 'utf8')
  95. const { data } = matter(articleContents)
  96. articleVersions[articlePath] = getApplicableVersions(data.versions, articlePath)
  97. })
  98. )
  99. })
  100. // TODO get these tests passing after the new site tree code is in production
  101. test.skip('contains all expected articles', () => {
  102. const missingArticlePaths = difference(availableArticlePaths, publishedArticlePaths)
  103. const errorMessage = formatArticleError('Missing article links:', missingArticlePaths)
  104. expect(missingArticlePaths.length, errorMessage).toBe(0)
  105. })
  106. test.skip('does not any unexpected articles', () => {
  107. const unexpectedArticles = difference(publishedArticlePaths, availableArticlePaths)
  108. const errorMessage = formatArticleError('Unexpected article links:', unexpectedArticles)
  109. expect(unexpectedArticles.length, errorMessage).toBe(0)
  110. })
  111. test.skip('contains only articles and map topics with versions that are also available in the parent category', () => {
  112. Object.entries(articleVersions).forEach(([articleName, articleVersions]) => {
  113. const unexpectedVersions = difference(articleVersions, categoryVersions)
  114. const errorMessage = `${articleName} has versions that are not available in parent category`
  115. expect(unexpectedVersions.length, errorMessage).toBe(0)
  116. })
  117. })
  118. // TODO: Unskip this test once the related script has been executed
  119. test.skip('slugified title matches parent directory name', () => {
  120. // Get the parent directory name
  121. const categoryDirPath = path.dirname(indexAbsPath)
  122. const categoryDirName = path.basename(categoryDirPath)
  123. slugger.reset()
  124. const expectedSlug = slugger.slug(entities.decode(indexTitle))
  125. // Check if the directory name matches the expected slug
  126. expect(categoryDirName).toBe(expectedSlug)
  127. // If this fails, execute "script/reconcile-category-dirs-with-ids.js"
  128. })
  129. }
  130. )
  131. }
  132. )
  133. })
  134. function getPath (productDir, link, filename) {
  135. return path.join(productDir, link, `${filename}.md`)
  136. }
  137. function formatArticleError (message, articles) {
  138. return `${message}\n - ${articles.join('\n - ')}`
  139. }
Tip!

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

Comments

Loading...