Skip to content

Commit

Permalink
fix: escape backticks in all contents
Browse files Browse the repository at this point in the history
closes #23
  • Loading branch information
leonardoanalista committed Jan 23, 2016
1 parent 2916bf9 commit 99aa05b
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ build/Release
node_modules
.DS_Store
coverage
.cz-config.js
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ This is a customizable Commitizen plugin. You can specify the commit types, scop
}
}
```

- you should commit your `.cz-config.js` file to your git. Run `cp ./node_modules/cz-customizable/cz-config-EXAMPLE.js ./.cz-config.js` in a project root directory to get a template.

* if you don't provide a config file, this adapter will use the contents of the default file `node_modules/cz-customizable/cz-config-EXAMPLE.js`
Expand All @@ -36,6 +36,23 @@ Hopefully this will help you to have consistent commit messages and have a fully
It prompts for [conventional changelog](https://github.com/ajoslin/conventional-changelog/blob/master/conventions/angular.md) standard.


## GOTCHAS

* backticks
If you wish to have backticks in your content, for example "feat: \`string\`", the commit preview will be "feat: \\\\`string\\\\`".
Don't worry because on your `git log` will be "feat: \`string\`" as desired.

* multiline contents on the body of the message
Body is the only place where you can use a `pipe` to break lines.
E.g.: you type this: `my items are:| - item01| - item 02`, which will become:


`my items are:`
`- item01`
`- item 02`



## CONTRIBUTING

Please refer to the [Contributor Guidelines](https://github.com/angular/angular.js/blob/master/CONTRIBUTING.md) and [Conduct of Code](https://github.com/angular/code-of-conduct/blob/master/CODE_OF_CONDUCT.md) from [AngularJs](https://github.com/angular/angular.js) project.
Expand Down
15 changes: 14 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var path = require('path');
function readConfigFile() {
// this function is replaced in test.
var config = findConfig.require(CZ_CONFIG_NAME, {home: false});

if (!config) {
log.warn('Unable to find a config file "' + CZ_CONFIG_NAME + '". Default configuration would be used.');
log.warn('Copy and use it as template by running in a project root directory:\n "cp '
Expand Down Expand Up @@ -46,6 +47,18 @@ function buildCommit(answers) {
return subject.trim();
}

function escapeSpecialChars(result) {
var specialChars = ['\`'];

specialChars.map(function (item){
// For some strange reason, we have to pass additional '\' slash to commitizen. Total slashes are 4.
// If user types "feat: `string`", the commit preview should show "feat: `\\string\\`".
// Don't worry. The git log will be "feat: `string`"
result = result.replace(new RegExp(item, 'g'), '\\\\`');
});
return result;
}

// Hard limit this line
var head = (answers.type + addScope(answers.scope) + addSubject(answers.subject)).slice(0, maxLineWidth);

Expand All @@ -67,7 +80,7 @@ function buildCommit(answers) {
result += '\n\n' + footer;
}

return result;
return escapeSpecialChars(result);
}

var isNotWip = function(answers) {
Expand Down
13 changes: 13 additions & 0 deletions spec/czCustomizableSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ describe('cz-customizable', function() {
commit = jasmine.createSpy();
});

it('should escape special characters sush as backticks', function() {
module.prompter(cz, commit);
var commitAnswers = cz.prompt.mostRecentCall.args[1];

var answers = {
confirmCommit: 'yes',
type: 'feat',
subject: 'with backticks `here`'
};

commitAnswers(answers);
expect(commit).toHaveBeenCalledWith('feat: with backticks \\\\`here\\\\`');
});

it('should call cz.prompt with questions', function() {
module.prompter(cz, commit);
Expand Down

0 comments on commit 99aa05b

Please sign in to comment.