Skip to content

Commit 10be97a

Browse files
Planeshifterkgryte
andauthored
build: strip YAML frontmatter from breaking changes in changelog
PR-URL: #6100 Closes: stdlib-js/metr-issue-tracker#53 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent 4b8eae7 commit 10be97a

File tree

1 file changed

+66
-12
lines changed

1 file changed

+66
-12
lines changed

lib/node_modules/@stdlib/_tools/changelog/generate/lib/breaking_changes.js

Lines changed: 66 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
/**
32
* @license Apache-2.0
43
*
@@ -24,6 +23,7 @@
2423
var filter = require( '@stdlib/array/base/filter' );
2524
var trim = require( '@stdlib/string/trim' );
2625
var map = require( '@stdlib/utils/map' );
26+
var reEOL = require( '@stdlib/regexp/eol' );
2727
var collectField = require( './collect_field.js' );
2828
var sectionStart = require( './section_start.js' );
2929
var sectionEnd = require( './section_end.js' );
@@ -33,10 +33,51 @@ var heading = require( './heading.js' );
3333
// VARIABLES //
3434

3535
var STDLIB_GITHUB_URL = 'https://github.com/stdlib-js/stdlib/commit';
36+
var RE_YAML_DELIMITER = /^\s*---\s*$/;
3637

3738

3839
// FUNCTIONS //
3940

41+
/**
42+
* Processes commit message text by removing YAML blocks and squashing multiple empty lines into a single empty line.
43+
*
44+
* @private
45+
* @param {Array<string>} lines - text lines to process
46+
* @returns {Array<string>} processed commit message lines
47+
*/
48+
function stripYamlBlocks( lines ) {
49+
var previousLineEmpty;
50+
var inYaml;
51+
var out;
52+
var i;
53+
54+
previousLineEmpty = false;
55+
inYaml = false;
56+
out = [];
57+
for ( i = 0; i < lines.length; i++ ) {
58+
// Check for YAML delimiter (---) with optional whitespace:
59+
if ( RE_YAML_DELIMITER.test( lines[ i ] ) ) {
60+
inYaml = !inYaml; // Toggle YAML block state
61+
continue;
62+
}
63+
// Only include lines that are not part of YAML blocks:
64+
if ( inYaml ) {
65+
continue;
66+
}
67+
if ( trim( lines[ i ] ) === '' ) {
68+
// Only add an empty line if the previous line wasn't empty:
69+
if ( !previousLineEmpty && out.length > 0 ) {
70+
previousLineEmpty = true;
71+
out.push( '' );
72+
}
73+
} else {
74+
previousLineEmpty = false;
75+
out.push( lines[ i ] ); // Preserve original line formatting
76+
}
77+
}
78+
return out;
79+
}
80+
4081
/**
4182
* Formats a breaking change.
4283
*
@@ -54,13 +95,26 @@ var STDLIB_GITHUB_URL = 'https://github.com/stdlib-js/stdlib/commit';
5495
* // returns '- [`abcdef1`](https://github.com/stdlib-js/stdlib/commit/abcdef1234567890): beep'
5596
*/
5697
function formatBreakingChange( note ) {
57-
var parts = note.text.split( '\n' );
58-
var hash = trim( note.hash );
59-
var out = '- [`'+hash.substring( 0, 7 )+'`]('+STDLIB_GITHUB_URL+'/'+hash+'): '+parts[ 0 ];
60-
if ( parts.length > 1 ) {
61-
out +='\n\n';
62-
out += ' - ';
63-
out += parts.slice( 1 ).join( '\n ' );
98+
var contentLines;
99+
var textLines;
100+
var firstLine;
101+
var restLines;
102+
var hash;
103+
var out;
104+
105+
textLines = note.text.split( reEOL.REGEXP );
106+
contentLines = stripYamlBlocks( textLines );
107+
108+
// If we have content, first element is the first line:
109+
firstLine = contentLines[ 0 ] || '';
110+
restLines = contentLines.slice( 1 );
111+
hash = trim( note.hash );
112+
113+
// Construct output string:
114+
out = '- [`' + hash.substring( 0, 7 ) + '`](' + STDLIB_GITHUB_URL + '/' + hash + '): ' + firstLine;
115+
if ( restLines.length > 0 ) {
116+
out += '\n\n';
117+
out += ' - ' + restLines.join( '\n ' );
64118
out += '\n';
65119
}
66120
return out;
@@ -88,18 +142,18 @@ function isBreakingChange( note ) {
88142
* @returns {string} list of breaking changes
89143
*/
90144
function breakingChanges( commits ) {
91-
var breakingChanges;
145+
var changes;
92146
var notes;
93147
var out;
94148

95149
notes = collectField( commits, 'notes' );
96-
breakingChanges = filter( notes, isBreakingChange );
97-
if ( breakingChanges.length === 0 ) {
150+
changes = filter( notes, isBreakingChange );
151+
if ( changes.length === 0 ) {
98152
return '';
99153
}
100154
out = sectionStart( 'breaking-changes' );
101155
out += heading( 'BREAKING CHANGES', 3 );
102-
out += map( breakingChanges, formatBreakingChange ).join( '\n' );
156+
out += map( changes, formatBreakingChange ).join( '\n' );
103157
out += sectionEnd( 'breaking-changes' );
104158
return out;
105159
}

0 commit comments

Comments
 (0)