|
| 1 | +'use strict' |
| 2 | + |
| 3 | +module.exports = (numOfItems, { data }) => { |
| 4 | + const { contentCatalog, site } = data.root |
| 5 | + if (!contentCatalog) return |
| 6 | + const rawPages = getDatedReleaseNotesRawPages(contentCatalog) |
| 7 | + const pageUiModels = turnRawPagesIntoPageUiModels(site, rawPages, contentCatalog) |
| 8 | + return getMostRecentlyUpdatedPages(pageUiModels, numOfItems) |
| 9 | +} |
| 10 | + |
| 11 | +let buildPageUiModel |
| 12 | + |
| 13 | +function getDatedReleaseNotesRawPages (contentCatalog) { |
| 14 | + return contentCatalog.getPages(({ asciidoc, out }) => { |
| 15 | + if (!asciidoc || !out) return |
| 16 | + return getReleaseNotesWithRevdate(asciidoc) |
| 17 | + }) |
| 18 | +} |
| 19 | + |
| 20 | +function getReleaseNotesWithRevdate (asciidoc) { |
| 21 | + const attributes = asciidoc.attributes |
| 22 | + return asciidoc.attributes && isReleaseNotes(attributes) && hasRevDate(attributes) |
| 23 | +} |
| 24 | + |
| 25 | +function isReleaseNotes (attributes) { |
| 26 | + return attributes['page-component-name'] === 'release-notes' |
| 27 | +} |
| 28 | + |
| 29 | +function hasRevDate (attributes) { |
| 30 | + return 'page-revdate' in attributes |
| 31 | +} |
| 32 | + |
| 33 | +function turnRawPagesIntoPageUiModels (site, pages, contentCatalog) { |
| 34 | + buildPageUiModel ??= module.parent.require('@antora/page-composer/build-ui-model').buildPageUiModel |
| 35 | + return pages |
| 36 | + .map((page) => buildPageUiModel(site, page, contentCatalog)) |
| 37 | + .filter((page) => isValidDate(page.attributes?.revdate)) |
| 38 | + .sort(sortByRevDate) |
| 39 | +} |
| 40 | + |
| 41 | +function isValidDate (dateStr) { |
| 42 | + return !isNaN(Date.parse(dateStr)) |
| 43 | +} |
| 44 | + |
| 45 | +function sortByRevDate (a, b) { |
| 46 | + return new Date(b.attributes.revdate) - new Date(a.attributes.revdate) |
| 47 | +} |
| 48 | + |
| 49 | +function getMostRecentlyUpdatedPages (pageUiModels, numOfItems) { |
| 50 | + return getResultList(pageUiModels, Math.min(pageUiModels.length, numOfItems)) |
| 51 | +} |
| 52 | + |
| 53 | +function getResultList (pageUiModels, maxNumberOfPages) { |
| 54 | + const resultList = [] |
| 55 | + for (let i = 0; i < maxNumberOfPages; i++) { |
| 56 | + const page = pageUiModels[i] |
| 57 | + if (page.attributes?.revdate) resultList.push(getSelectedAttributes(page)) |
| 58 | + } |
| 59 | + return resultList |
| 60 | +} |
| 61 | + |
| 62 | +function getSelectedAttributes (page) { |
| 63 | + const latestVersion = getLatestVersion(page.contents.toString()) |
| 64 | + return { |
| 65 | + latestVersionAnchor: latestVersion?.anchor, |
| 66 | + latestVersionName: latestVersion?.innerText, |
| 67 | + revdateWithoutYear: removeYear(page.attributes?.revdate), |
| 68 | + title: cleanTitle(page.title), |
| 69 | + url: page.url, |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +function getLatestVersion (contentsStr) { |
| 74 | + const firstVersion = contentsStr.match(/<h2 id="([^"]+)">(.+?)<\/h2>/) |
| 75 | + if (!firstVersion) return |
| 76 | + const result = { anchor: firstVersion[1] } |
| 77 | + if (isVersion(firstVersion[2])) result.innerText = firstVersion[2] |
| 78 | + return result |
| 79 | +} |
| 80 | + |
| 81 | +function isVersion (versionText) { |
| 82 | + return /^[0-9]+\.[0-9]+(?:\.[0-9]+)?/.test(versionText) |
| 83 | +} |
| 84 | + |
| 85 | +function removeYear (dateStr) { |
| 86 | + if (!isValidDate(dateStr)) return |
| 87 | + const dateObj = new Date(dateStr) |
| 88 | + return `${dateObj.toLocaleString('default', { month: 'short' })} ${dateObj.getDate()}` |
| 89 | +} |
| 90 | + |
| 91 | +function cleanTitle (title) { |
| 92 | + return title.split('Release Notes')[0].trim() |
| 93 | +} |
0 commit comments