|
| 1 | +const renderContent = require('../../lib/render-content') |
| 2 | +const patterns = require('../../lib/patterns') |
| 3 | + |
| 4 | +module.exports = async (req, res, next) => { |
| 5 | + // The `/release-notes` sub-path |
| 6 | + if (!req.path.endsWith('/release-notes')) return next() |
| 7 | + |
| 8 | + // ignore paths that don't have an enterprise version number |
| 9 | + if (!patterns.getEnterpriseServerNumber.test(req.path)) return next() |
| 10 | + |
| 11 | + // extract enterprise version from path, e.g. 2.16 |
| 12 | + const requestedVersion = req.path.match(patterns.getEnterpriseServerNumber)[1] |
| 13 | + |
| 14 | + const versionString = `${requestedVersion.replace(/\./g, '-')}` |
| 15 | + |
| 16 | + const allReleaseNotes = req.context.site.data['release-notes'] |
| 17 | + |
| 18 | + // This version doesn't have any release notes - let's be helpful and redirect |
| 19 | + // to the notes on `enterprise.github.com` |
| 20 | + if (!allReleaseNotes || !allReleaseNotes[versionString]) { |
| 21 | + return res.redirect(`https://enterprise.github.com/releases/${requestedVersion}.0/notes`) |
| 22 | + } |
| 23 | + |
| 24 | + const releaseNotes = allReleaseNotes[versionString] |
| 25 | + const keys = Object.keys(releaseNotes) |
| 26 | + // Turn { [key]: { notes, intro, date } } |
| 27 | + // into [{ version, notes, intro, date }] |
| 28 | + const patches = keys |
| 29 | + .sort((a, b) => { |
| 30 | + if (a > b) return -1 |
| 31 | + if (a < b) return 1 |
| 32 | + return 0 |
| 33 | + }) |
| 34 | + .map(key => ({ version: `${requestedVersion}.${key}`, ...releaseNotes[key] })) |
| 35 | + |
| 36 | + const renderedPatches = await Promise.all(patches.map(async patch => { |
| 37 | + // Render the intro block, it might contain markdown formatting |
| 38 | + patch.intro = await renderContent(patch.intro, req.context) |
| 39 | + |
| 40 | + // Run the notes through the markdown rendering pipeline |
| 41 | + patch.notes = await Promise.all(patch.notes.map(async note => { |
| 42 | + if (note.note) note.note = await renderContent(note.note, req.context) |
| 43 | + return note |
| 44 | + })) |
| 45 | + |
| 46 | + // Sort the notes into sections |
| 47 | + // Takes an array of notes: Array<{ note, type }> |
| 48 | + // Turns it into { [type]: [{ note }] } |
| 49 | + patch.sortedNotes = patch.notes.reduce((prev, curr) => { |
| 50 | + const existingObj = prev.find(o => o.title === curr.type) |
| 51 | + if (!existingObj) { |
| 52 | + prev.push({ title: curr.type, notes: [curr] }) |
| 53 | + } else { |
| 54 | + existingObj.notes.push(curr) |
| 55 | + } |
| 56 | + return prev |
| 57 | + }, []) |
| 58 | + |
| 59 | + return patch |
| 60 | + })) |
| 61 | + |
| 62 | + req.context.releaseNotes = renderedPatches |
| 63 | + |
| 64 | + return next() |
| 65 | +} |
0 commit comments