mirror of
https://github.com/changesets/action
synced 2026-10-26 16:39:56 +08:00
Fix a bug with getting the highest bump type
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
import unified from "unified";
|
||||
import remarkParse from "remark-parse";
|
||||
import remarkStringify from "remark-stringify";
|
||||
// @ts-ignore
|
||||
import mdastToString from "mdast-util-to-string";
|
||||
|
||||
export const BumpLevels = {
|
||||
dep: 0,
|
||||
patch: 1,
|
||||
minor: 2,
|
||||
major: 3
|
||||
} as const;
|
||||
|
||||
export function getChangelogEntry(changelog: string, version: string) {
|
||||
let ast = unified()
|
||||
.use(remarkParse)
|
||||
.parse(changelog);
|
||||
|
||||
let highestLevel: number = BumpLevels.dep;
|
||||
|
||||
let nodes = ast.children as Array<any>;
|
||||
let headingStartInfo:
|
||||
| {
|
||||
index: number;
|
||||
depth: number;
|
||||
}
|
||||
| undefined;
|
||||
let endIndex: number | undefined;
|
||||
|
||||
for (let i = 0; i < nodes.length; i++) {
|
||||
let node = nodes[i];
|
||||
if (node.type === "heading") {
|
||||
let stringified: string = mdastToString(node);
|
||||
let match = stringified.toLowerCase().match(/(major|minor|patch)/);
|
||||
if (match !== null) {
|
||||
let level = BumpLevels[match[0] as "major" | "minor" | "patch"];
|
||||
highestLevel = Math.max(level, highestLevel);
|
||||
}
|
||||
if (headingStartInfo === undefined && stringified === version) {
|
||||
headingStartInfo = {
|
||||
index: i,
|
||||
depth: node.depth
|
||||
};
|
||||
continue;
|
||||
}
|
||||
if (
|
||||
endIndex === undefined &&
|
||||
headingStartInfo !== undefined &&
|
||||
headingStartInfo.depth === node.depth
|
||||
) {
|
||||
endIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (headingStartInfo) {
|
||||
ast.children = (ast.children as any).slice(
|
||||
headingStartInfo.index + 1,
|
||||
endIndex
|
||||
);
|
||||
}
|
||||
return {
|
||||
content: unified()
|
||||
.use(remarkStringify)
|
||||
.stringify(ast),
|
||||
highestLevel: highestLevel
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user