Skip to content

Commit

Permalink
fix: using a custom badge template breaks badge replacement (#210)
Browse files Browse the repository at this point in the history
  • Loading branch information
kharaone authored and Berkmann18 committed Oct 19, 2019
1 parent 97878f4 commit 72de75a
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 12 deletions.
4 changes: 4 additions & 0 deletions src/generate/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(''),
'',
Expand All @@ -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(''),
'',
Expand Down
34 changes: 24 additions & 10 deletions src/generate/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down Expand Up @@ -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('')
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/init/__tests__/add-badge.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
'',
Expand All @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions src/init/init-content.js
Original file line number Diff line number Diff line change
@@ -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 = [
Expand Down

0 comments on commit 72de75a

Please sign in to comment.