diff --git a/src/template.js b/src/template.js index c014aad7..3b4bb2b2 100644 --- a/src/template.js +++ b/src/template.js @@ -1,6 +1,6 @@ import { join } from 'path' import Handlebars from 'handlebars' -import { removeIndentation, readFile, fileExists } from './utils' +import { readFile, fileExists } from './utils' const TEMPLATES_DIR = join(__dirname, '..', 'templates') @@ -38,7 +38,7 @@ Handlebars.registerHelper('commit-list', function (context, options) { return '' } - return `${options.hash.heading}\n${list}` + return `${options.hash.heading}\n\n${list}` }) Handlebars.registerHelper('matches', function (val, pattern, options) { @@ -57,10 +57,20 @@ async function getTemplate (template) { return readFile(path) } +function cleanTemplate (template) { + return template + // Remove indentation + .replace(/\n +/g, '\n') + .replace(/^ +/, '') + // Fix multiple blank lines + .replace(/\n\n\n+/g, '\n\n') + .replace(/\n\n$/, '\n') +} + export async function compileTemplate (template, data) { const compile = Handlebars.compile(await getTemplate(template)) if (template === 'json') { return compile(data) } - return removeIndentation(compile(data)) + return cleanTemplate(compile(data)) } diff --git a/src/utils.js b/src/utils.js index a3f3e2bb..faf8fd7c 100644 --- a/src/utils.js +++ b/src/utils.js @@ -30,12 +30,6 @@ export function niceDate (string) { return `${day} ${month} ${year}` } -export function removeIndentation (string) { - return string - .replace(/\n +/g, '\n') - .replace(/^ +/, '') -} - export function isLink (string) { return /^http/.test(string) } diff --git a/templates/compact.hbs b/templates/compact.hbs index 52b587b3..a394ca98 100644 --- a/templates/compact.hbs +++ b/templates/compact.hbs @@ -1,21 +1,20 @@ ### Changelog + All notable changes to this project will be documented in this file. Dates are displayed in UTC. Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog). {{#each releases}} {{#if href}} - {{#if major}} - ### [{{title}}]({{href}}) - {{else}} - #### [{{title}}]({{href}}) - {{/if}} + ###{{#unless major}}#{{/unless}} [{{title}}]({{href}}) {{else}} #### {{title}} {{/if}} + {{#if tag}} > {{niceDate}} {{/if}} + {{#each merges}} - {{{message}}}{{#if href}} [`#{{id}}`]({{href}}){{/if}} {{/each}} diff --git a/templates/keepachangelog.hbs b/templates/keepachangelog.hbs index 6c91cea2..9c96bede 100644 --- a/templates/keepachangelog.hbs +++ b/templates/keepachangelog.hbs @@ -1,4 +1,5 @@ # Changelog + All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) @@ -12,20 +13,23 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog). {{else}} ## {{title}}{{#if tag}} - {{isoDate}}{{/if}} {{/if}} + {{#if merges}} ### Merged + {{#each merges}} - {{{message}}} {{#if href}}[`#{{id}}`]({{href}}){{/if}} {{/each}} - {{/if}} + {{#if fixes}} ### Fixed + {{#each fixes}} - {{{commit.subject}}}{{#each fixes}} {{#if href}}[`#{{id}}`]({{href}}){{/if}}{{/each}} {{/each}} - {{/if}} + {{#commit-list commits heading='### Commits'}} - {{#if breaking}}**Breaking change:** {{/if}}{{{subject}}} {{#if href}}[`{{shorthash}}`]({{href}}){{/if}} {{/commit-list}} diff --git a/test/commit-list-helper.js b/test/commit-list-helper.js index ca3dda1f..03194e8e 100644 --- a/test/commit-list-helper.js +++ b/test/commit-list-helper.js @@ -26,7 +26,7 @@ describe('commit-list helper', () => { '{{/commit-list}}' ) const expected = - '# Heading\n' + + '# Heading\n\n' + '- Commit 1\n' + '- Commit 2\n' + '- feat: Commit 3\n' @@ -40,7 +40,7 @@ describe('commit-list helper', () => { '{{/commit-list}}' ) const expected = - '# Heading\n' + + '# Heading\n\n' + '- feat: Commit 3\n' expect(compile({ commits })).to.equal(expected) }) @@ -52,7 +52,7 @@ describe('commit-list helper', () => { '{{/commit-list}}' ) const expected = - '# Breaking Changes\n' + + '# Breaking Changes\n\n' + '- Commit 2\n' expect(compile({ commits })).to.equal(expected) }) @@ -64,7 +64,7 @@ describe('commit-list helper', () => { '{{/commit-list}}' ) const expected = - '# Heading\n' + + '# Heading\n\n' + '- Commit 1\n' + '- feat: Commit 3\n' expect(compile({ commits })).to.equal(expected) diff --git a/test/data/template-compact-no-remote.md b/test/data/template-compact-no-remote.md index 398b6104..c03b4a97 100644 --- a/test/data/template-compact-no-remote.md +++ b/test/data/template-compact-no-remote.md @@ -1,25 +1,33 @@ ### Changelog + All notable changes to this project will be documented in this file. Dates are displayed in UTC. Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog). #### v1.0.0 + > 15 December 2015 + - Some breaking change #### v0.1.0 + > 29 December 2015 + - Commit that fixes nothing - Another commit that fixes nothing but with less changes #### v0.0.2 + > 28 December 2015 + - Should not parse #4 in PR title - Commit 4 fixes #4 in the subject #### v0.0.1 + > 15 December 2015 + - Third commit with same name as PR - Second commit - First commit - diff --git a/test/data/template-compact.md b/test/data/template-compact.md index f0aaed0b..d7804c5c 100644 --- a/test/data/template-compact.md +++ b/test/data/template-compact.md @@ -1,25 +1,33 @@ ### Changelog + All notable changes to this project will be documented in this file. Dates are displayed in UTC. Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog). ### [v1.0.0](https://github.com/user/repo/compare/v0.1.0...v1.0.0) + > 15 December 2015 + - Some breaking change [`b0b3040`](https://github.com/user/repo/commit/b0b304049847d9568585bc11399fa6cfa4cab5dc) #### [v0.1.0](https://github.com/user/repo/compare/v0.0.2...v0.1.0) + > 29 December 2015 + - Commit that fixes nothing [`12c0624`](https://github.com/user/repo/commit/12c0624e7e419a70bd5f3b403d7e0bd8f23ec617) - Another commit that fixes nothing but with less changes [`e9a43b2`](https://github.com/user/repo/commit/e9a43b2bf50449fc0d84465308e6008cc1597bb3) #### [v0.0.2](https://github.com/user/repo/compare/v0.0.1...v0.0.2) + > 28 December 2015 + - Should not parse #4 in PR title [`#5`](https://github.com/user/repo/pull/5) - Commit 4 fixes #4 in the subject [`#4`](https://github.com/user/repo/issues/4) #### v0.0.1 + > 15 December 2015 + - Third commit with same name as PR [`#3`](https://github.com/user/repo/pull/3) - Second commit [`#1`](https://github.com/user/repo/issues/1) [`#2`](https://github.com/user/repo/issues/2) - First commit [`158fdde`](https://github.com/user/repo/commit/158fdde54b6188c9f9ca3034e9cb5bcc3fe3ff69) - diff --git a/test/data/template-keepachangelog.md b/test/data/template-keepachangelog.md index 95b80da9..ea8e9a5b 100644 --- a/test/data/template-keepachangelog.md +++ b/test/data/template-keepachangelog.md @@ -1,4 +1,5 @@ # Changelog + All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) @@ -7,29 +8,38 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog). ## [v1.0.0](https://github.com/user/repo/compare/v0.1.0...v1.0.0) - 2015-12-15 + ### Commits + - Some breaking change [`b0b3040`](https://github.com/user/repo/commit/b0b304049847d9568585bc11399fa6cfa4cab5dc) ## [v0.1.0](https://github.com/user/repo/compare/v0.0.2...v0.1.0) - 2015-12-29 + ### Commits + - Commit that fixes nothing [`12c0624`](https://github.com/user/repo/commit/12c0624e7e419a70bd5f3b403d7e0bd8f23ec617) - Another commit that fixes nothing but with less changes [`e9a43b2`](https://github.com/user/repo/commit/e9a43b2bf50449fc0d84465308e6008cc1597bb3) ## [v0.0.2](https://github.com/user/repo/compare/v0.0.1...v0.0.2) - 2015-12-28 + ### Merged + - Should not parse #4 in PR title [`#5`](https://github.com/user/repo/pull/5) ### Fixed -- Commit 4 fixes #4 in the subject [`#4`](https://github.com/user/repo/issues/4) +- Commit 4 fixes #4 in the subject [`#4`](https://github.com/user/repo/issues/4) ## v0.0.1 - 2015-12-15 + ### Merged + - Third commit with same name as PR [`#3`](https://github.com/user/repo/pull/3) ### Fixed + - Second commit [`#1`](https://github.com/user/repo/issues/1) [`#2`](https://github.com/user/repo/issues/2) ### Commits -- First commit [`158fdde`](https://github.com/user/repo/commit/158fdde54b6188c9f9ca3034e9cb5bcc3fe3ff69) +- First commit [`158fdde`](https://github.com/user/repo/commit/158fdde54b6188c9f9ca3034e9cb5bcc3fe3ff69) diff --git a/test/utils.js b/test/utils.js index 76b835a0..dbcb2bc4 100644 --- a/test/utils.js +++ b/test/utils.js @@ -3,7 +3,6 @@ import { expect } from 'chai' import { cmd, niceDate, - removeIndentation, isLink, getGitVersion, __Rewire__ as mock, @@ -29,14 +28,6 @@ describe('niceDate', () => { }) }) -describe('removeIndentation', () => { - it('removes indentation', () => { - const input = ' some\n indented\n text' - const expected = 'some\nindented\ntext' - expect(removeIndentation(input)).to.equal(expected) - }) -}) - describe('isLink', () => { it('returns true for links', () => { expect(isLink('http://test.com')).to.equal(true)