Fix #7: Allow comment tags to be on the same line

This commit is contained in:
Jeroen Engels 2016-03-20 20:19:19 +01:00
parent 9059b75d46
commit f75d8541c8
3 changed files with 26 additions and 31 deletions

View file

@ -1,8 +1,6 @@
# all-contributors-cli # all-contributors-cli
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section --> <!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->[![All Contributors](https://img.shields.io/badge/all_contributors-2-orange.svg?style=flat-square)](#contributors)<!-- ALL-CONTRIBUTORS-BADGE:END -->
[![All Contributors](https://img.shields.io/badge/all_contributors-2-orange.svg?style=flat-square)](#contributors)
<!-- ALL-CONTRIBUTORS-BADGE:END -->
This is a tool to help automate adding contributor acknowledgements according to the [all-contributors](https://github.com/kentcdodds/all-contributors) specification. This is a tool to help automate adding contributor acknowledgements according to the [all-contributors](https://github.com/kentcdodds/all-contributors) specification.
@ -47,22 +45,20 @@ These are the keys you can specify:
### Add contributors section ### Add contributors section
If you don't already have a Contributors section in a Markdown file, create one. Then add the comment tags section below to it. Don't worry, they're visible only to those that read the raw file. The tags **must** be at the beginning of the line, and each on their separate line. If you don't already have a Contributors section in a Markdown file, create one. Then add the comment tags section below to it. Don't worry, they're visible only to those that read the raw file.
```md ```md
## Contributors ## Contributors
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section --> <!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
<!-- ALL-CONTRIBUTORS-LIST:END --> <!-- ALL-CONTRIBUTORS-LIST:END -->
``` ```
If you wish to add a badge ( [![All Contributors](https://img.shields.io/badge/all_contributors-14-orange.svg?style=flat-square)](#contributors) ) indicating the number of collaborators, add the following tags (again, at the beginning of the line and each on their separate line): If you wish to add a badge ( [![All Contributors](https://img.shields.io/badge/all_contributors-14-orange.svg?style=flat-square)](#contributors) ) indicating the number of collaborators, add the following comment tags section:
```md ```md
some-badge some-badge
<!-- ALL-CONTRIBUTORS-BADGE:START --><!-- ALL-CONTRIBUTORS-BADGE:END -->
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
<!-- ALL-CONTRIBUTORS-BADGE:END -->
some-other-badge some-other-badge
``` ```
@ -72,7 +68,7 @@ some-other-badge
Use `generate` to generate the contributors list and inject it into your contributors file. Contributors will be read from your configuration file. Use `generate` to generate the contributors list and inject it into your contributors file. Contributors will be read from your configuration file.
``` ```console
all-contributors generate all-contributors generate
``` ```

View file

@ -12,16 +12,21 @@ function injectContentBetween(lines, content, startIndex, endIndex) {
); );
} }
var injectBetweenTags = _.curry(function (tag, newContent, previousContent) { function injectBetweenTags(tag, newContent) {
var lines = previousContent.split('\n'); return function (previousContent) {
var openingTagIndex = _.findIndex(_.startsWith('<!-- ALL-CONTRIBUTORS-' + tag + ':START '), lines); var tagToLookFor = '<!-- ALL-CONTRIBUTORS-' + tag + ':';
var closingTagIndex = _.findIndex(_.startsWith('<!-- ALL-CONTRIBUTORS-' + tag + ':END '), lines); var closingTag = '-->';
if (openingTagIndex === -1 || closingTagIndex === -1) { var startOfOpeningTagIndex = previousContent.indexOf(tagToLookFor + 'START');
var endOfOpeningTagIndex = previousContent.indexOf(closingTag, startOfOpeningTagIndex);
var startOfClosingTagIndex = previousContent.indexOf(tagToLookFor + 'END', endOfOpeningTagIndex);
if (startOfOpeningTagIndex === -1 || endOfOpeningTagIndex === -1 || startOfClosingTagIndex === -1) {
return previousContent; return previousContent;
} }
return injectContentBetween(lines, newContent, openingTagIndex + 1, closingTagIndex) return previousContent.slice(0, endOfOpeningTagIndex + closingTag.length) +
.join('\n'); newContent +
}); previousContent.slice(startOfClosingTagIndex);
};
}
function formatLine(contributors) { function formatLine(contributors) {
return '| ' + contributors.join(' | ') + ' |'; return '| ' + contributors.join(' | ') + ' |';
@ -51,7 +56,7 @@ module.exports = function generate(options, contributors, fileContent) {
var contributorsList = generateContributorsList(options, contributors); var contributorsList = generateContributorsList(options, contributors);
var badge = formatBadge(options, contributors); var badge = formatBadge(options, contributors);
return _.flow( return _.flow(
injectBetweenTags('LIST', contributorsList), injectBetweenTags('LIST', '\n' + contributorsList + '\n'),
injectBetweenTags('BADGE', badge) injectBetweenTags('BADGE', badge)
)(fileContent); )(fileContent);
}; };

View file

@ -27,9 +27,7 @@ function fixtures() {
'', '',
'## Contributors', '## Contributors',
'These people contributed to the project:', 'These people contributed to the project:',
'<!-- ALL-CONTRIBUTORS-LIST:START -->', '<!-- ALL-CONTRIBUTORS-LIST:START -->FOO BAR BAZ<!-- ALL-CONTRIBUTORS-LIST:END -->',
'###Some content that will be replaced###',
'<!-- ALL-CONTRIBUTORS-LIST:END -->',
'', '',
'Thanks a lot guys!' 'Thanks a lot guys!'
].join('\n'); ].join('\n');
@ -146,9 +144,7 @@ test('should inject badge if the ALL-CONTRIBUTORS-BADGE tag is present', t => {
'# project', '# project',
'', '',
'Badges', 'Badges',
'<!-- ALL-CONTRIBUTORS-BADGE:START -->', '<!-- ALL-CONTRIBUTORS-BADGE:START --><!-- ALL-CONTRIBUTORS-BADGE:END -->',
'###Some content that will be replaced###',
'<!-- ALL-CONTRIBUTORS-BADGE:END -->',
'', '',
'License: MIT' 'License: MIT'
].join('\n'); ].join('\n');
@ -156,9 +152,7 @@ test('should inject badge if the ALL-CONTRIBUTORS-BADGE tag is present', t => {
'# project', '# project',
'', '',
'Badges', 'Badges',
'<!-- ALL-CONTRIBUTORS-BADGE:START -->', '<!-- ALL-CONTRIBUTORS-BADGE:START -->[![All Contributors](https://img.shields.io/badge/all_contributors-1-orange.svg?style=flat-square)](#contributors)<!-- ALL-CONTRIBUTORS-BADGE:END -->',
'[![All Contributors](https://img.shields.io/badge/all_contributors-1-orange.svg?style=flat-square)](#contributors)',
'<!-- ALL-CONTRIBUTORS-BADGE:END -->',
'', '',
'License: MIT' 'License: MIT'
].join('\n'); ].join('\n');