chore: add script to format GitHub changelog (#12158)

This commit is contained in:
neverland 2023-08-06 21:33:41 +08:00 committed by GitHub
parent 1f12af7e83
commit c2bef6cdf0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -0,0 +1,60 @@
/**
* This script is used to format changelog which is generated by GitHub
* because we need to paste it to the changelog page of Vant website.
*/
// paste changelog here
const changelog = ``;
changelog.split('\n').map((line) => {
// Skip unused lines
if (
line.startsWith('> Please refer to') ||
line.startsWith('release:') ||
line.includes("What's Changed") ||
line.includes('Full Changelog') ||
line.includes('docs(changelog)')
) {
return;
}
if (line.startsWith('<!-- Release notes generated')) {
const version = line.match(/v\d+\.\d+\.\d+/)[0];
if (version) {
console.log(`### ${version}`);
// log the current date
const now = new Date();
const padZero = (num) => (num < 10 ? `0${num}` : num);
console.log(
`\`${now.getFullYear()}-${padZero(now.getMonth() + 1)}-${padZero(
now.getDate(),
)}\``,
);
}
return;
}
// format title
if (line.startsWith('### ')) {
line = line.replace('### ', '#### ');
} else if (line.startsWith('## ')) {
line = line.replace('## ', '#### ');
}
// format PR link
const regex = /https:\/\/github\.com\/(\w+\/\w+)\/pull\/(\d+)/;
const match = line.match(regex);
const repoName = match ? match[1] : null;
const pullRequestNumber = match ? match[2] : null;
if (repoName && pullRequestNumber) {
line = line.replace(
regex,
`[#${pullRequestNumber}](https://github.com/${repoName}/pull/${pullRequestNumber})`,
);
}
// format author
line = line.replace(/@([\w-]+)/, '[$1](https://github.com/$1)');
console.log(line);
});