fix: using a custom badge template breaks badge replacement (#210)

This commit is contained in:
kharaone 2019-10-20 01:45:00 +02:00 committed by Maximilian Berkmann
parent 97878f496f
commit 72de75abc5
4 changed files with 38 additions and 12 deletions

View file

@ -143,7 +143,9 @@ test('replace all-contributors badge if present', () => {
'Badges', 'Badges',
[ [
'[![version](https://img.shields.io/npm/v/all-contributors-cli.svg?style=flat-square)](http://npm.im/all-contributors-cli)', '[![version](https://img.shields.io/npm/v/all-contributors-cli.svg?style=flat-square)](http://npm.im/all-contributors-cli)',
'<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->',
'[![All Contributors](https://img.shields.io/badge/all_contributors-0-orange.svg?style=flat-square)](#contributors-)', '[![All Contributors](https://img.shields.io/badge/all_contributors-0-orange.svg?style=flat-square)](#contributors-)',
'<!-- ALL-CONTRIBUTORS-BADGE:END -->',
'[![version](https://img.shields.io/npm/v/all-contributors-cli.svg?style=flat-square)](http://npm.im/all-contributors-cli)', '[![version](https://img.shields.io/npm/v/all-contributors-cli.svg?style=flat-square)](http://npm.im/all-contributors-cli)',
].join(''), ].join(''),
'', '',
@ -155,7 +157,9 @@ test('replace all-contributors badge if present', () => {
'Badges', 'Badges',
[ [
'[![version](https://img.shields.io/npm/v/all-contributors-cli.svg?style=flat-square)](http://npm.im/all-contributors-cli)', '[![version](https://img.shields.io/npm/v/all-contributors-cli.svg?style=flat-square)](http://npm.im/all-contributors-cli)',
'<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->',
'[![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-)',
'<!-- ALL-CONTRIBUTORS-BADGE:END -->',
'[![version](https://img.shields.io/npm/v/all-contributors-cli.svg?style=flat-square)](http://npm.im/all-contributors-cli)', '[![version](https://img.shields.io/npm/v/all-contributors-cli.svg?style=flat-square)](http://npm.im/all-contributors-cli)',
].join(''), ].join(''),
'', '',

View file

@ -2,11 +2,9 @@ const _ = require('lodash/fp')
const formatBadge = require('./format-badge') const formatBadge = require('./format-badge')
const formatContributor = require('./format-contributor') const formatContributor = require('./format-contributor')
const badgeRegex = /\[!\[All Contributors\]\([a-zA-Z0-9\-./_:?=]+\)\]\(#[\w-]+\)/
function injectListBetweenTags(newContent) { function injectListBetweenTags(newContent) {
return function(previousContent) { return function(previousContent) {
const tagToLookFor = '<!-- ALL-CONTRIBUTORS-LIST:' const tagToLookFor = `<!-- ALL-CONTRIBUTORS-LIST:`
const closingTag = '-->' const closingTag = '-->'
const startOfOpeningTagIndex = previousContent.indexOf( const startOfOpeningTagIndex = previousContent.indexOf(
`${tagToLookFor}START`, `${tagToLookFor}START`,
@ -61,15 +59,31 @@ function generateContributorsList(options, contributors) {
function replaceBadge(newContent) { function replaceBadge(newContent) {
return function(previousContent) { return function(previousContent) {
const regexResult = badgeRegex.exec(previousContent) const tagToLookFor = `<!-- ALL-CONTRIBUTORS-BADGE:`
if (!regexResult) { const closingTag = '-->'
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
} }
return ( return [
previousContent.slice(0, regexResult.index) + previousContent.slice(0, endOfOpeningTagIndex + closingTag.length),
newContent + newContent,
previousContent.slice(regexResult.index + regexResult[0].length) previousContent.slice(startOfClosingTagIndex),
) ].join('')
} }
} }

View file

@ -4,7 +4,9 @@ test('insert badge under title', () => {
const content = ['# project', '', 'Description', '', 'Foo bar'].join('\n') const content = ['# project', '', 'Description', '', 'Foo bar'].join('\n')
const expected = [ const expected = [
'# project', '# project',
'<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->',
'[![All Contributors](https://img.shields.io/badge/all_contributors-0-orange.svg?style=flat-square)](#contributors-)', '[![All Contributors](https://img.shields.io/badge/all_contributors-0-orange.svg?style=flat-square)](#contributors-)',
'<!-- ALL-CONTRIBUTORS-BADGE:END -->',
'', '',
'Description', 'Description',
'', '',
@ -20,7 +22,9 @@ test('add badge if content is empty', () => {
const content = '' const content = ''
const expected = [ const expected = [
'', '',
'<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->',
'[![All Contributors](https://img.shields.io/badge/all_contributors-0-orange.svg?style=flat-square)](#contributors-)', '[![All Contributors](https://img.shields.io/badge/all_contributors-0-orange.svg?style=flat-square)](#contributors-)',
'<!-- ALL-CONTRIBUTORS-BADGE:END -->',
].join('\n') ].join('\n')
const result = addBadge(content) const result = addBadge(content)

View file

@ -1,8 +1,12 @@
const _ = require('lodash/fp') const _ = require('lodash/fp')
const injectContentBetween = require('../util').markdown.injectContentBetween const injectContentBetween = require('../util').markdown.injectContentBetween
const badgeContent = const badgeContent = [
'[![All Contributors](https://img.shields.io/badge/all_contributors-0-orange.svg?style=flat-square)](#contributors-)' '<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->',
'[![All Contributors](https://img.shields.io/badge/all_contributors-0-orange.svg?style=flat-square)](#contributors-)',
'<!-- ALL-CONTRIBUTORS-BADGE:END -->',
].join('\n')
const headerContent = const headerContent =
'Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):' 'Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):'
const listContent = [ const listContent = [