Skip to content

Commit 9b8dc48

Browse files
committed
Update extractChangelogSnippet to use parseChangelog
1 parent d3dd097 commit 9b8dc48

1 file changed

Lines changed: 7 additions & 20 deletions

File tree

‎pr-checks/prepare-changelog.ts‎

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { parseArgs } from "node:util";
1010

1111
import { getErrorMessage } from "../src/util";
1212

13-
import { NO_CHANGES_STR } from "./changelog";
13+
import { NO_CHANGES_STR, parseChangelog } from "./changelog";
1414
import { CHANGELOG_FILE } from "./config";
1515

1616
/**
@@ -22,28 +22,15 @@ import { CHANGELOG_FILE } from "./config";
2222
*/
2323
export function extractChangelogSnippet(changelogPath: string) {
2424
try {
25-
const lines = fs.readFileSync(changelogPath, "utf-8").split("\n");
26-
const output: string[] = [];
27-
let foundFirstSection = false;
25+
const content = fs.readFileSync(changelogPath, "utf-8");
26+
const changelog = parseChangelog(content);
2827

29-
// Extract the body of the first section in the changelog file.
30-
for (const line of lines) {
31-
if (line.startsWith("## ")) {
32-
if (foundFirstSection) {
33-
// This is the second section header we have found, which means that we have
34-
// captured all lines in the first section in `output`. We can stop here.
35-
break;
36-
}
37-
38-
// We have discovered the first section header.
39-
foundFirstSection = true;
40-
} else if (foundFirstSection) {
41-
// Add lines between the first section header (if any) and the next to the output.
42-
output.push(line);
43-
}
28+
// Return an empty string if we couldn't find the first section.
29+
if (changelog.sections.length === 0) {
30+
return "";
4431
}
4532

46-
return output.join("\n").trim();
33+
return changelog.sections[0].bodyLines.join("\n").trim();
4734
} catch (err) {
4835
if (err instanceof Error && "code" in err && err.code === "ENOENT") {
4936
console.error(`Changelog file at '${changelogPath}' does not exist.`);

0 commit comments

Comments
 (0)