1
-
2
1
/**
3
2
* @license Apache-2.0
4
3
*
24
23
var filter = require ( '@stdlib/array/base/filter' ) ;
25
24
var trim = require ( '@stdlib/string/trim' ) ;
26
25
var map = require ( '@stdlib/utils/map' ) ;
26
+ var reEOL = require ( '@stdlib/regexp/eol' ) ;
27
27
var collectField = require ( './collect_field.js' ) ;
28
28
var sectionStart = require ( './section_start.js' ) ;
29
29
var sectionEnd = require ( './section_end.js' ) ;
@@ -33,10 +33,51 @@ var heading = require( './heading.js' );
33
33
// VARIABLES //
34
34
35
35
var STDLIB_GITHUB_URL = 'https://github.com/stdlib-js/stdlib/commit' ;
36
+ var RE_YAML_DELIMITER = / ^ \s * - - - \s * $ / ;
36
37
37
38
38
39
// FUNCTIONS //
39
40
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
+
40
81
/**
41
82
* Formats a breaking change.
42
83
*
@@ -54,13 +95,26 @@ var STDLIB_GITHUB_URL = 'https://github.com/stdlib-js/stdlib/commit';
54
95
* // returns '- [`abcdef1`](https://github.com/stdlib-js/stdlib/commit/abcdef1234567890): beep'
55
96
*/
56
97
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 ' ) ;
64
118
out += '\n' ;
65
119
}
66
120
return out ;
@@ -88,18 +142,18 @@ function isBreakingChange( note ) {
88
142
* @returns {string } list of breaking changes
89
143
*/
90
144
function breakingChanges ( commits ) {
91
- var breakingChanges ;
145
+ var changes ;
92
146
var notes ;
93
147
var out ;
94
148
95
149
notes = collectField ( commits , 'notes' ) ;
96
- breakingChanges = filter ( notes , isBreakingChange ) ;
97
- if ( breakingChanges . length === 0 ) {
150
+ changes = filter ( notes , isBreakingChange ) ;
151
+ if ( changes . length === 0 ) {
98
152
return '' ;
99
153
}
100
154
out = sectionStart ( 'breaking-changes' ) ;
101
155
out += heading ( 'BREAKING CHANGES' , 3 ) ;
102
- out += map ( breakingChanges , formatBreakingChange ) . join ( '\n' ) ;
156
+ out += map ( changes , formatBreakingChange ) . join ( '\n' ) ;
103
157
out += sectionEnd ( 'breaking-changes' ) ;
104
158
return out ;
105
159
}
0 commit comments