Skip to content

Commit

Permalink
fix(changelog): limit lines in changelog to default of 80 characters …
Browse files Browse the repository at this point in the history
…to address markdownlint issue MD013

Refs: #20
  • Loading branch information
da-moon authored and hdevalke committed Aug 26, 2021
1 parent 1a87e67 commit a01e53a
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 9 deletions.
38 changes: 35 additions & 3 deletions src/cmd/changelog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ impl<'a> ChangeLogTransformer<'a> {
commit_parser,
})
}

fn make_notes(&self, footers: &'a [Footer], scope: Option<String>) -> Vec<(String, Note)> {
let changelog_line_length = self.config.changelog_line_length ;
footers
.iter()
.filter(|footer| footer.key.starts_with("BREAKING"))
Expand All @@ -80,7 +80,23 @@ impl<'a> ChangeLogTransformer<'a> {
footer.key.clone(),
Note {
scope: scope.clone(),
text: footer.value.clone(),
text: footer.value.to_owned()
.split_whitespace()
.map(|s| String::from(s))
.fold(Vec::<String>::new(), |mut acc, word|{
let length=acc.len();
if length != 0 {
let last_line = acc.clone().pop().unwrap();
if last_line.len() + word.len() < changelog_line_length {
acc[length -1] = format!("{} {}", last_line,word);
}else{
acc.push(word);
}
}else{
acc.push(word);
}
acc
}),
},
)
})
Expand Down Expand Up @@ -132,7 +148,23 @@ impl<'a> ChangeLogTransformer<'a> {
let hash = commit.id().to_string();
let date = chrono::NaiveDateTime::from_timestamp(commit.time().seconds(), 0).date();
let scope = conv_commit.scope;
let subject = conv_commit.description;
let subject = conv_commit.description.to_owned()
.split_whitespace()
.map(|s| String::from(s))
.fold(Vec::<String>::new(), |mut acc, word|{
let length=acc.len();
if length != 0 {
let last_line = acc.clone().pop().unwrap();
if last_line.len() + word.len() < self.config.changelog_line_length {
acc[length -1] = format!("{} {}", last_line,word);
}else{
acc.push(word);
}
}else{
acc.push(word);
}
acc
});
let body = conv_commit.body;
let short_hash = hash[..7].into();
let mut references = Vec::new();
Expand Down
6 changes: 3 additions & 3 deletions src/conventional/changelog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub(crate) struct Reference<'a> {
#[derive(Debug, Serialize)]
pub(crate) struct Note {
pub(crate) scope: Option<String>,
pub(crate) text: String,
pub(crate) text: Vec<String>,
}

#[derive(Debug, Serialize)]
Expand All @@ -39,7 +39,7 @@ pub(crate) struct NoteGroup {
pub(crate) struct CommitContext<'a> {
pub(crate) hash: String,
pub(crate) date: NaiveDate,
pub(crate) subject: String,
pub(crate) subject: Vec<String>,
pub(crate) body: Option<String>,
pub(crate) scope: Option<String>,
pub(crate) short_hash: String,
Expand Down Expand Up @@ -159,4 +159,4 @@ impl<W: io::Write> ChangelogWriter<W> {
.render_to_write("template", context, writer)?;
Ok(())
}
}
}
3 changes: 2 additions & 1 deletion src/conventional/changelog/commit.hbs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
*{{#if scope}} **{{scope}}:**{{/if}} {{#if subject}}{{subject}}{{else}}{{header}}{{/if}}
*{{#if scope}} **{{scope}}:**{{/if}} {{#if subject}}{{#each subject}}{{this}}
{{/each}}{{else}}{{header}}{{/if}}
{{~#if hash}}
{{~#if @root.linkReferences}} ([{{shortHash}}]({{commitUrlFormat}}))
{{~else}} {{shortHash}}{{/if}}
Expand Down
6 changes: 4 additions & 2 deletions src/conventional/changelog/template.hbs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
{{> header}}
{{#if noteGroups}}{{#each noteGroups}}
### ⚠ ||{{title}}||
### ⚠ {{title}}

{{#each notes}}* {{#if commit.scope}}**{{commit.scope}}:** {{/if}}{{text}}{{/each}}
{{#each notes}}* {{#if commit.scope}}**{{commit.scope}}:** {{/if}}{{#each text}}{{this}}
{{/each}}
{{/each}}
{{/each}}
{{/if}}

Expand Down
11 changes: 11 additions & 0 deletions src/conventional/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ pub(crate) struct Config {
/// Defaults to `"[[:alnum:]]+(?:[-_/][[:alnum:]]+)*"`.
#[serde(default = "default_scope_regex")]
pub(crate) scope_regex: String,
/// default number of characters in a single line of the CHANGELOG.
#[serde(default = "default_changelog_line_length")]
pub(crate) changelog_line_length: usize,

}

impl Default for Config {
Expand All @@ -81,6 +85,7 @@ impl Default for Config {
user_url_format: default_user_url_format(),
release_commit_message_format: default_release_commit_message_format(),
issue_prefixes: default_issue_prefixes(),
changelog_line_length: default_changelog_line_length(),
host: None,
owner: None,
repository: None,
Expand Down Expand Up @@ -168,6 +173,11 @@ fn default_user_url_format() -> String {
fn default_release_commit_message_format() -> String {
"chore(release): {{currentTag}}".into()
}
fn default_changelog_line_length() -> usize {
80
}



fn default_issue_prefixes() -> Vec<String> {
vec!["#".into()]
Expand Down Expand Up @@ -255,6 +265,7 @@ mod tests {
assert_eq!(
value,
Config {
changelog_line_length: 80,
header: "# Changelog\n".to_string(),
types: vec![
Type {
Expand Down

0 comments on commit a01e53a

Please sign in to comment.