Skip to content

build: strip YAML frontmatter from breaking changes in changelog #6100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 21, 2025
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

/**
* @license Apache-2.0
*
Expand All @@ -24,6 +23,7 @@
var filter = require( '@stdlib/array/base/filter' );
var trim = require( '@stdlib/string/trim' );
var map = require( '@stdlib/utils/map' );
var reEOL = require( '@stdlib/regexp/eol' );
var collectField = require( './collect_field.js' );
var sectionStart = require( './section_start.js' );
var sectionEnd = require( './section_end.js' );
Expand All @@ -33,10 +33,42 @@ var heading = require( './heading.js' );
// VARIABLES //

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


// FUNCTIONS //

/**
* Strips YAML frontmatter blocks from text lines.
*
* @private
* @param {Array<string>} lines - text lines to process
* @returns {Array<string>} text lines without YAML blocks
*/
function stripYamlBlocks( lines ) {
var result;
var inYaml;
var i;

result = [];
inYaml = false;

for ( i = 0; i < lines.length; i++ ) {
// Check for YAML delimiter (---) with optional whitespace:
if ( RE_YAML_DELIMITER.test( lines[i] ) ) {
inYaml = !inYaml; // Toggle YAML block state
continue;
}

// Only include lines that are not part of YAML blocks:
if ( !inYaml ) {
result.push( lines[i] );
}
}

return result;
}

/**
* Formats a breaking change.
*
Expand All @@ -54,13 +86,39 @@ var STDLIB_GITHUB_URL = 'https://github.com/stdlib-js/stdlib/commit';
* // returns '- [`abcdef1`](https://github.com/stdlib-js/stdlib/commit/abcdef1234567890): beep'
*/
function formatBreakingChange( note ) {
var parts = note.text.split( '\n' );
var hash = trim( note.hash );
var out = '- [`'+hash.substring( 0, 7 )+'`]('+STDLIB_GITHUB_URL+'/'+hash+'): '+parts[ 0 ];
if ( parts.length > 1 ) {
out +='\n\n';
out += ' - ';
out += parts.slice( 1 ).join( '\n ' );
var contentLines;
var trimmedLine;
var firstLine;
var restLines;
var textLines;
var hash;
var out;
var i;

textLines = note.text.split( reEOL.REGEXP );
contentLines = stripYamlBlocks( textLines );
firstLine = '';
restLines = [];
hash = trim( note.hash );

for ( i = 0; i < contentLines.length; i++ ) {
trimmedLine = trim( contentLines[i] );
if ( trimmedLine !== '' ) {
// If this is our first non-empty line, it's the firstLine...
if ( firstLine === '' ) {
firstLine = trimmedLine;
} else {
restLines.push( trimmedLine );
}
}
}

// Construct output string - use a single string concatenation when possible
out = '- [`' + hash.substring( 0, 7 ) + '`](' + STDLIB_GITHUB_URL + '/' + hash + '): ' + firstLine;

if ( restLines.length > 0 ) {
out += '\n\n';
out += ' - ' + restLines.join( '\n ' );
out += '\n';
}
return out;
Expand Down