From f75d8541c863fbf3caae4ef87221bf7d7fe1f427 Mon Sep 17 00:00:00 2001 From: Jeroen Engels Date: Sun, 20 Mar 2016 20:19:19 +0100 Subject: [PATCH] Fix #7: Allow comment tags to be on the same line --- README.md | 18 +++++++----------- lib/generate/index.js | 27 ++++++++++++++++----------- lib/generate/index.test.js | 12 +++--------- 3 files changed, 26 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 495c005..b9822a9 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,6 @@ # all-contributors-cli - -[![All Contributors](https://img.shields.io/badge/all_contributors-2-orange.svg?style=flat-square)](#contributors) - +[![All Contributors](https://img.shields.io/badge/all_contributors-2-orange.svg?style=flat-square)](#contributors) 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 - - + + ``` -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 - - - + 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 ``` diff --git a/lib/generate/index.js b/lib/generate/index.js index 9a0bf8a..7cc0cb7 100644 --- a/lib/generate/index.js +++ b/lib/generate/index.js @@ -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(''; + 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); }; diff --git a/lib/generate/index.test.js b/lib/generate/index.test.js index 4bd450e..8c85946 100644 --- a/lib/generate/index.test.js +++ b/lib/generate/index.test.js @@ -27,9 +27,7 @@ function fixtures() { '', '## Contributors', 'These people contributed to the project:', - '', - '###Some content that will be replaced###', - '', + 'FOO BAR BAZ', '', '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', - '', - '###Some content that will be replaced###', - '', + '', '', '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](https://img.shields.io/badge/all_contributors-1-orange.svg?style=flat-square)](#contributors)', - '', + '[![All Contributors](https://img.shields.io/badge/all_contributors-1-orange.svg?style=flat-square)](#contributors)', '', 'License: MIT' ].join('\n');