Skip to content

Commit

Permalink
Fix changelog spacing
Browse files Browse the repository at this point in the history
  • Loading branch information
cookpete committed Jun 28, 2018
1 parent 5086228 commit 5e8ffe5
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 33 deletions.
16 changes: 13 additions & 3 deletions 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')

Expand Down Expand Up @@ -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) {
Expand All @@ -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))
}
6 changes: 0 additions & 6 deletions src/utils.js
Expand Up @@ -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)
}
Expand Down
9 changes: 4 additions & 5 deletions 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}}
Expand Down
8 changes: 6 additions & 2 deletions 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/)
Expand All @@ -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}}
Expand Down
8 changes: 4 additions & 4 deletions test/commit-list-helper.js
Expand Up @@ -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'
Expand All @@ -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)
})
Expand All @@ -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)
})
Expand All @@ -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)
Expand Down
10 changes: 9 additions & 1 deletion 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

10 changes: 9 additions & 1 deletion 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)

14 changes: 12 additions & 2 deletions 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/)
Expand All @@ -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)
9 changes: 0 additions & 9 deletions test/utils.js
Expand Up @@ -3,7 +3,6 @@ import { expect } from 'chai'
import {
cmd,
niceDate,
removeIndentation,
isLink,
getGitVersion,
__Rewire__ as mock,
Expand All @@ -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)
Expand Down

0 comments on commit 5e8ffe5

Please sign in to comment.