UNPKG

npm-fetch-changelog

Version:

fetch the changelog for an npm package from GitHub

22 lines 777 B
const versionRx = `\\[?v?\\d+\\.\\d+(\\.\\d+(-[-a-z0-9.]+)?)?\\]?`; export default function parseChangelog(text) { const result = {}; const versionHeaderRx = new RegExp(`^#+\\s+(${versionRx})(.*)$|^(${versionRx})(.*)(\r\n?|\n)(=+|-+)`, 'mg'); let match; let start = 0; let release; while (match = versionHeaderRx.exec(text)) { const rawVersion = match[1] || match[5]; let version = rawVersion.replace(/\[|\]/g, ''); if (!match[2] && !match[6]) version += '.0'; if (release) release.body = text.substring(start, match.index).trim(); release = { version, header: match[0] }; result[version] = release; start = match.index + match[0].length; } if (release) release.body = text.substring(start).trim(); return result; }