|
1 | 1 | 'use strict'; |
2 | 2 |
|
| 3 | +import { coerce } from 'semver'; |
| 4 | + |
| 5 | +import { loadFromURL } from '../utils/parser.mjs'; |
3 | 6 | import createQueries from '../utils/queries/index.mjs'; |
4 | 7 | import { getRemark } from '../utils/remark.mjs'; |
5 | 8 |
|
| 9 | +// A ReGeX for retrieving Node.js version headers from the CHANGELOG.md |
| 10 | +const NODE_VERSIONS_REGEX = /\* \[Node\.js ([0-9.]+)\]\S+ (.*)\r?\n/g; |
| 11 | + |
| 12 | +// A ReGeX for retrieving the list items in the index document |
| 13 | +const LIST_ITEM_REGEX = /\* \[(.*?)\]\((.*?)\.md\)/g; |
| 14 | + |
| 15 | +// A ReGeX for checking if a Node.js version is an LTS release |
| 16 | +const NODE_LTS_VERSION_REGEX = /Long Term Support/i; |
| 17 | + |
6 | 18 | /** |
7 | 19 | * Creates an API doc parser for a given Markdown API doc file |
8 | 20 | * |
@@ -58,4 +70,35 @@ const createParser = linter => { |
58 | 70 | return { parseApiDocs, parseApiDoc }; |
59 | 71 | }; |
60 | 72 |
|
| 73 | +/** |
| 74 | + * Retrieves all Node.js major versions from the provided CHANGELOG.md file |
| 75 | + * and returns an array of objects containing the version and LTS status. |
| 76 | + * @param {string|URL} path Path to changelog |
| 77 | + * @returns {Promise<Array<ApiDocReleaseEntry>>} |
| 78 | + */ |
| 79 | +export const parseChangelog = async path => { |
| 80 | + const changelog = await loadFromURL(path); |
| 81 | + |
| 82 | + const nodeMajors = Array.from(changelog.matchAll(NODE_VERSIONS_REGEX)); |
| 83 | + |
| 84 | + return nodeMajors.map(match => ({ |
| 85 | + version: coerce(match[1]), |
| 86 | + isLts: NODE_LTS_VERSION_REGEX.test(match[2]), |
| 87 | + })); |
| 88 | +}; |
| 89 | + |
| 90 | +/** |
| 91 | + * Retrieves all the document titles for sidebar generation. |
| 92 | + * |
| 93 | + * @param {string|URL} path Path to changelog |
| 94 | + * @returns {Promise<Array<{ section: string, api: string }>>} |
| 95 | + */ |
| 96 | +export const parseIndex = async path => { |
| 97 | + const index = await loadFromURL(path); |
| 98 | + |
| 99 | + const items = Array.from(index.matchAll(LIST_ITEM_REGEX)); |
| 100 | + |
| 101 | + return items.map(([, section, api]) => ({ section, api })); |
| 102 | +}; |
| 103 | + |
61 | 104 | export default createParser; |
0 commit comments