mirror of
https://github.com/all-contributors/cli.git
synced 2025-01-09 13:36:29 +00:00
fix: using a custom badge template breaks badge replacement (#210)
This commit is contained in:
parent
97878f496f
commit
72de75abc5
4 changed files with 38 additions and 12 deletions
|
@ -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-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 -->',
|
||||
'[![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-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-BADGE:END -->',
|
||||
'[![version](https://img.shields.io/npm/v/all-contributors-cli.svg?style=flat-square)](http://npm.im/all-contributors-cli)',
|
||||
].join(''),
|
||||
'',
|
||||
|
|
|
@ -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 = '<!-- ALL-CONTRIBUTORS-LIST:'
|
||||
const tagToLookFor = `<!-- ALL-CONTRIBUTORS-LIST:`
|
||||
const closingTag = '-->'
|
||||
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 = `<!-- ALL-CONTRIBUTORS-BADGE:`
|
||||
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.slice(0, regexResult.index) +
|
||||
newContent +
|
||||
previousContent.slice(regexResult.index + regexResult[0].length)
|
||||
)
|
||||
return [
|
||||
previousContent.slice(0, endOfOpeningTagIndex + closingTag.length),
|
||||
newContent,
|
||||
previousContent.slice(startOfClosingTagIndex),
|
||||
].join('')
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -4,7 +4,9 @@ test('insert badge under title', () => {
|
|||
const content = ['# project', '', 'Description', '', 'Foo bar'].join('\n')
|
||||
const expected = [
|
||||
'# 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-BADGE:END -->',
|
||||
'',
|
||||
'Description',
|
||||
'',
|
||||
|
@ -20,7 +22,9 @@ test('add badge if content is empty', () => {
|
|||
const content = ''
|
||||
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-BADGE:END -->',
|
||||
].join('\n')
|
||||
|
||||
const result = addBadge(content)
|
||||
|
|
|
@ -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-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 =
|
||||
'Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):'
|
||||
const listContent = [
|
||||
|
|
Loading…
Reference in a new issue