forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
page.js
303 lines (260 loc) · 10 KB
/
page.js
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import assert from 'assert'
import path from 'path'
import cheerio from 'cheerio'
import patterns from './patterns.js'
import getApplicableVersions from './get-applicable-versions.js'
import generateRedirectsForPermalinks from './redirects/permalinks.js'
import getEnglishHeadings from './get-english-headings.js'
import getTocItems from './get-toc-items.js'
import pathUtils from './path-utils.js'
import Permalink from './permalink.js'
import languages from './languages.js'
import renderContent from './render-content/index.js'
import processLearningTracks from './process-learning-tracks.js'
import { productMap } from './all-products.js'
import slash from 'slash'
import statsd from './statsd.js'
import readFileContents from './read-file-contents.js'
import getLinkData from './get-link-data.js'
import getDocumentType from './get-document-type.js'
import { union } from 'lodash-es'
class Page {
static async init(opts) {
opts = await Page.read(opts)
if (!opts) return
return new Page(opts)
}
static async read(opts) {
assert(opts.languageCode, 'languageCode is required')
assert(opts.relativePath, 'relativePath is required')
assert(opts.basePath, 'basePath is required')
const relativePath = slash(opts.relativePath)
const fullPath = slash(path.join(opts.basePath, relativePath))
// Per https://nodejs.org/api/fs.html#fs_fs_exists_path_callback
// its better to read and handle errors than to check access/stats first
try {
const {
data,
content,
errors: frontmatterErrors,
} = await readFileContents(fullPath, opts.languageCode)
return {
...opts,
relativePath,
fullPath,
...data,
markdown: content,
frontmatterErrors,
}
} catch (err) {
if (err.code === 'ENOENT') return false
console.error(err)
}
}
constructor(opts) {
Object.assign(this, { ...opts })
if (this.frontmatterErrors.length) {
throw new Error(JSON.stringify(this.frontmatterErrors, null, 2))
}
// Store raw data so we can cache parsed versions
this.rawIntro = this.intro
this.rawTitle = this.title
this.rawShortTitle = this.shortTitle
this.rawProduct = this.product
this.rawPermissions = this.permissions
this.rawLearningTracks = this.learningTracks
this.rawIncludeGuides = this.includeGuides
this.raw_product_video = this.product_video
if (this.introLinks) {
this.introLinks.rawQuickstart = this.introLinks.quickstart
this.introLinks.rawReference = this.introLinks.reference
this.introLinks.rawOverview = this.introLinks.overview
}
// Is this the Homepage or a Product, Category, Topic, or Article?
this.documentType = getDocumentType(this.relativePath)
// Get array of versions that the page is available in for fast lookup
this.applicableVersions = getApplicableVersions(this.versions, this.fullPath)
// a page should only be available in versions that its parent product is available in
const versionsParentProductIsNotAvailableIn = this.applicableVersions
// only the homepage will not have this.parentProduct
.filter(
(availableVersion) =>
this.parentProduct && !this.parentProduct.versions.includes(availableVersion)
)
if (versionsParentProductIsNotAvailableIn.length) {
throw new Error(
`\`versions\` frontmatter in ${this.fullPath} contains ${versionsParentProductIsNotAvailableIn}, which ${this.parentProduct.id} product is not available in!`
)
}
// derive array of Permalink objects
this.permalinks = Permalink.derive(
this.languageCode,
this.relativePath,
this.title,
this.applicableVersions
)
if (this.relativePath.endsWith('index.md')) {
// get an array of linked items in product and category TOCs
this.tocItems = getTocItems(this)
}
// if this is an article and it doesn't have showMiniToc = false, set mini TOC to true
if (!this.relativePath.endsWith('index.md')) {
this.showMiniToc = this.showMiniToc === false ? this.showMiniToc : true
}
// Instrument the `_render` method, so externally we call #render
// but it's wrapped in a timer that reports to Datadog
this.render = statsd.asyncTimer(this._render.bind(this), 'page.render')
return this
}
buildRedirects() {
// create backwards-compatible old paths for page permalinks and frontmatter redirects
this.redirects = generateRedirectsForPermalinks(this.permalinks, this.redirect_from)
return this.redirects
}
// Infer the parent product ID from the page's relative file path
get parentProductId() {
// Each page's top-level content directory matches its product ID
const id = this.relativePath.split('/')[0]
// ignore top-level content/index.md
if (id === 'index.md') return null
// make sure the ID is valid
if (process.env.NODE_ENV !== 'test') {
assert(
Object.keys(productMap).includes(id),
`page ${this.fullPath} has an invalid product ID: ${id}`
)
}
return id
}
get parentProduct() {
return productMap[this.parentProductId]
}
async renderTitle(context, opts = { preferShort: true }) {
return opts.preferShort && this.shortTitle
? this.renderProp('shortTitle', context, opts)
: this.renderProp('title', context, opts)
}
async _render(context) {
// use English IDs/anchors for translated headings, so links don't break (see #8572)
if (this.languageCode !== 'en') {
const englishHeadings = getEnglishHeadings(this, context)
context.englishHeadings = englishHeadings
}
this.intro = await renderContent(this.rawIntro, context)
this.introPlainText = await renderContent(this.rawIntro, context, { textOnly: true })
this.title = await renderContent(this.rawTitle, context, {
textOnly: true,
encodeEntities: true,
})
this.titlePlainText = await renderContent(this.rawTitle, context, { textOnly: true })
this.shortTitle = await renderContent(this.shortTitle, context, {
textOnly: true,
encodeEntities: true,
})
this.product_video = await renderContent(this.raw_product_video, context, { textOnly: true })
if (this.introLinks) {
this.introLinks.quickstart = await renderContent(this.introLinks.rawQuickstart, context, {
textOnly: true,
})
this.introLinks.reference = await renderContent(this.introLinks.rawReference, context, {
textOnly: true,
})
this.introLinks.overview = await renderContent(this.introLinks.rawOverview, context, {
textOnly: true,
})
}
context.relativePath = this.relativePath
const html = await renderContent(this.markdown, context)
// Adding communityRedirect for Discussions, Sponsors, and Codespaces - request from Product
if (
this.parentProduct &&
(this.parentProduct.id === 'discussions' ||
this.parentProduct.id === 'sponsors' ||
this.parentProduct.id === 'codespaces')
) {
this.communityRedirect = {
name: 'Provide GitHub Feedback',
href: `https://github.com/github/feedback/discussions/categories/${this.parentProduct.id}-feedback`,
}
}
// product frontmatter may contain liquid
if (this.product) {
this.product = await renderContent(this.rawProduct, context)
}
// permissions frontmatter may contain liquid
if (this.permissions) {
this.permissions = await renderContent(this.rawPermissions, context)
}
// Learning tracks may contain Liquid and need to have versioning processed.
if (this.learningTracks) {
const { featuredTrack, learningTracks } = await processLearningTracks(
this.rawLearningTracks,
context
)
this.featuredTrack = featuredTrack
this.learningTracks = learningTracks
}
if (this.rawIncludeGuides) {
this.allTopics = []
this.includeGuides = await getLinkData(this.rawIncludeGuides, context)
this.includeGuides.map((guide) => {
const { page } = guide
guide.type = page.type
if (page.topics) {
this.allTopics = union(this.allTopics, page.topics).sort((a, b) =>
a.localeCompare(b, page.languageCode)
)
guide.topics = page.topics
}
delete guide.page
return guide
})
}
// set a flag so layout knows whether to render a mac/windows/linux switcher element
this.includesPlatformSpecificContent =
html.includes('extended-markdown mac') ||
html.includes('extended-markdown windows') ||
html.includes('extended-markdown linux')
return html
}
// Allow other modules (like custom liquid tags) to make one-off requests
// for a page's rendered properties like `title` and `intro`
async renderProp(propName, context, opts = { unwrap: false }) {
let prop
if (propName === 'title') {
prop = this.rawTitle
} else if (propName === 'shortTitle') {
prop = this.rawShortTitle || this.rawTitle // fall back to title
} else if (propName === 'intro') {
prop = this.rawIntro
} else {
prop = this[propName]
}
const html = await renderContent(prop, context, opts)
if (!opts.unwrap) return html
// The unwrap option removes surrounding tags from a string, preserving any inner HTML
const $ = cheerio.load(html, { xmlMode: true })
return $.root().contents().html()
}
// infer current page's corresponding homepage
// /en/articles/foo -> /en
// /en/enterprise/2.14/user/articles/foo -> /en/enterprise/2.14/user
static getHomepage(requestPath) {
return requestPath.replace(/\/articles.*/, '')
}
// given a page path, return an array of objects containing hrefs
// for that page in all languages
static getLanguageVariants(href) {
const suffix = pathUtils.getPathWithoutLanguage(href)
return Object.values(languages).map(({ name, code, hreflang }) => {
// eslint-disable-line
return {
name,
code,
hreflang,
href: `/${code}${suffix}`.replace(patterns.trailingSlash, '$1'),
}
})
}
}
export default Page