Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Oct 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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