forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenterprise-release-notes.js
65 lines (52 loc) · 2.23 KB
/
enterprise-release-notes.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
const renderContent = require('../../lib/render-content')
const patterns = require('../../lib/patterns')
module.exports = async (req, res, next) => {
// The `/release-notes` sub-path
if (!req.path.endsWith('/release-notes')) return next()
// ignore paths that don't have an enterprise version number
if (!patterns.getEnterpriseServerNumber.test(req.path)) return next()
// extract enterprise version from path, e.g. 2.16
const requestedVersion = req.path.match(patterns.getEnterpriseServerNumber)[1]
const versionString = `${requestedVersion.replace(/\./g, '-')}`
const allReleaseNotes = req.context.site.data['release-notes']
// This version doesn't have any release notes - let's be helpful and redirect
// to the notes on `enterprise.github.com`
if (!allReleaseNotes || !allReleaseNotes[versionString]) {
return res.redirect(`https://enterprise.github.com/releases/${requestedVersion}.0/notes`)
}
const releaseNotes = allReleaseNotes[versionString]
const keys = Object.keys(releaseNotes)
// Turn { [key]: { notes, intro, date } }
// into [{ version, notes, intro, date }]
const patches = keys
.sort((a, b) => {
if (a > b) return -1
if (a < b) return 1
return 0
})
.map(key => ({ version: `${requestedVersion}.${key}`, ...releaseNotes[key] }))
const renderedPatches = await Promise.all(patches.map(async patch => {
// Render the intro block, it might contain markdown formatting
patch.intro = await renderContent(patch.intro, req.context)
// Run the notes through the markdown rendering pipeline
patch.notes = await Promise.all(patch.notes.map(async note => {
if (note.note) note.note = await renderContent(note.note, req.context)
return note
}))
// Sort the notes into sections
// Takes an array of notes: Array<{ note, type }>
// Turns it into { [type]: [{ note }] }
patch.sortedNotes = patch.notes.reduce((prev, curr) => {
const existingObj = prev.find(o => o.title === curr.type)
if (!existingObj) {
prev.push({ title: curr.type, notes: [curr] })
} else {
existingObj.notes.push(curr)
}
return prev
}, [])
return patch
}))
req.context.releaseNotes = renderedPatches
return next()
}