@@ -10,7 +10,7 @@ import { parseArgs } from "node:util";
1010
1111import { getErrorMessage } from "../src/util" ;
1212
13- import { NO_CHANGES_STR } from "./changelog" ;
13+ import { NO_CHANGES_STR , parseChangelog } from "./changelog" ;
1414import { CHANGELOG_FILE } from "./config" ;
1515
1616/**
@@ -22,28 +22,15 @@ import { CHANGELOG_FILE } from "./config";
2222 */
2323export 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