diff --git a/src/generate/__tests__/index.js b/src/generate/__tests__/index.js index f4ec338..c75a883 100644 --- a/src/generate/__tests__/index.js +++ b/src/generate/__tests__/index.js @@ -143,7 +143,9 @@ test('replace all-contributors badge if present', () => { 'Badges', [ '[![version](https://img.shields.io/npm/v/all-contributors-cli.svg?style=flat-square)](http://npm.im/all-contributors-cli)', + '', '[![All Contributors](https://img.shields.io/badge/all_contributors-0-orange.svg?style=flat-square)](#contributors-)', + '', '[![version](https://img.shields.io/npm/v/all-contributors-cli.svg?style=flat-square)](http://npm.im/all-contributors-cli)', ].join(''), '', @@ -155,7 +157,9 @@ test('replace all-contributors badge if present', () => { 'Badges', [ '[![version](https://img.shields.io/npm/v/all-contributors-cli.svg?style=flat-square)](http://npm.im/all-contributors-cli)', + '', '[![All Contributors](https://img.shields.io/badge/all_contributors-1-orange.svg?style=flat-square)](#contributors-)', + '', '[![version](https://img.shields.io/npm/v/all-contributors-cli.svg?style=flat-square)](http://npm.im/all-contributors-cli)', ].join(''), '', diff --git a/src/generate/index.js b/src/generate/index.js index 314e294..3a2c899 100644 --- a/src/generate/index.js +++ b/src/generate/index.js @@ -2,11 +2,9 @@ const _ = require('lodash/fp') const formatBadge = require('./format-badge') const formatContributor = require('./format-contributor') -const badgeRegex = /\[!\[All Contributors\]\([a-zA-Z0-9\-./_:?=]+\)\]\(#[\w-]+\)/ - function injectListBetweenTags(newContent) { return function(previousContent) { - const tagToLookFor = '' const startOfOpeningTagIndex = previousContent.indexOf( `${tagToLookFor}START`, @@ -61,15 +59,31 @@ function generateContributorsList(options, contributors) { function replaceBadge(newContent) { return function(previousContent) { - const regexResult = badgeRegex.exec(previousContent) - if (!regexResult) { + const tagToLookFor = `' + const startOfOpeningTagIndex = previousContent.indexOf( + `${tagToLookFor}START`, + ) + const endOfOpeningTagIndex = previousContent.indexOf( + closingTag, + startOfOpeningTagIndex, + ) + const startOfClosingTagIndex = previousContent.indexOf( + `${tagToLookFor}END`, + endOfOpeningTagIndex, + ) + if ( + startOfOpeningTagIndex === -1 || + endOfOpeningTagIndex === -1 || + startOfClosingTagIndex === -1 + ) { return previousContent } - return ( - previousContent.slice(0, regexResult.index) + - newContent + - previousContent.slice(regexResult.index + regexResult[0].length) - ) + return [ + previousContent.slice(0, endOfOpeningTagIndex + closingTag.length), + newContent, + previousContent.slice(startOfClosingTagIndex), + ].join('') } } diff --git a/src/init/__tests__/add-badge.js b/src/init/__tests__/add-badge.js index a093210..78b6d0d 100644 --- a/src/init/__tests__/add-badge.js +++ b/src/init/__tests__/add-badge.js @@ -4,7 +4,9 @@ test('insert badge under title', () => { const content = ['# project', '', 'Description', '', 'Foo bar'].join('\n') const expected = [ '# project', + '', '[![All Contributors](https://img.shields.io/badge/all_contributors-0-orange.svg?style=flat-square)](#contributors-)', + '', '', 'Description', '', @@ -20,7 +22,9 @@ test('add badge if content is empty', () => { const content = '' const expected = [ '', + '', '[![All Contributors](https://img.shields.io/badge/all_contributors-0-orange.svg?style=flat-square)](#contributors-)', + '', ].join('\n') const result = addBadge(content) diff --git a/src/init/init-content.js b/src/init/init-content.js index 93b9543..f772eb3 100644 --- a/src/init/init-content.js +++ b/src/init/init-content.js @@ -1,8 +1,12 @@ const _ = require('lodash/fp') const injectContentBetween = require('../util').markdown.injectContentBetween -const badgeContent = - '[![All Contributors](https://img.shields.io/badge/all_contributors-0-orange.svg?style=flat-square)](#contributors-)' +const badgeContent = [ + '', + '[![All Contributors](https://img.shields.io/badge/all_contributors-0-orange.svg?style=flat-square)](#contributors-)', + '', +].join('\n') + const headerContent = 'Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):' const listContent = [