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-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-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 -->
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
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
## Contributors
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
<!-- ALL-CONTRIBUTORS-LIST:END -->
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
<!-- 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
some-badge
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
<!-- ALL-CONTRIBUTORS-BADGE:END -->
<!-- ALL-CONTRIBUTORS-BADGE:START --><!-- ALL-CONTRIBUTORS-BADGE:END -->
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.
```
```console
all-contributors generate
```

View file

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

View file

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