Skip to content

Commit

Permalink
feat: add wrapperTemplate option (#348)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalrymple committed Apr 26, 2023
1 parent 96534ec commit 2c07af8
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
26 changes: 26 additions & 0 deletions src/generate/__tests__/__snapshots__/index.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`replace the content between the ALL-CONTRIBUTORS-LIST tags by a custom wrapper around the list of contributors contained in the "bodyContent" tag 1`] = `
"# project
Description
## Contributors
These people contributed to the project:
<!-- ALL-CONTRIBUTORS-LIST:START -->
<!-- prettier-ignore-start -->
<!-- markdownlint-disable -->
<p>
<tr>
<td align=\\"center\\" valign=\\"top\\" width=\\"20%\\">Kent C. Dodds is awesome!</td>
<td align=\\"center\\" valign=\\"top\\" width=\\"20%\\">Divjot Singh is awesome!</td>
<td align=\\"center\\" valign=\\"top\\" width=\\"20%\\">Jeroen Engels is awesome!</td>
</tr>
</p>
<!-- markdownlint-restore -->
<!-- prettier-ignore-end -->
<!-- ALL-CONTRIBUTORS-LIST:END -->
Thanks a lot everyone!"
`;
exports[`replace the content between the ALL-CONTRIBUTORS-LIST tags by a table of contributors 1`] = `
"# project
Expand Down
13 changes: 13 additions & 0 deletions src/generate/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ test('replace the content between the ALL-CONTRIBUTORS-LIST tags by a table of c
expect(result).toMatchSnapshot()
})

test('replace the content between the ALL-CONTRIBUTORS-LIST tags by a custom wrapper around the list of contributors contained in the "bodyContent" tag', () => {
const {kentcdodds, bogas04} = contributors
const {options, jfmengels, content} = fixtures()
const contributorList = [kentcdodds, bogas04, jfmengels]
const result = generate(
Object.assign(options, { wrapperTemplate: '<p><%= bodyContent %></p>'}),
contributorList,
content,
)

expect(result).toMatchSnapshot()
})

test('split contributors into multiples lines when there are too many', () => {
const {kentcdodds} = contributors
const {options, content} = fixtures()
Expand Down
12 changes: 10 additions & 2 deletions src/generate/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ function formatFooter(options) {

function generateContributorsList(options, contributors) {
const tableFooter = formatFooter(options)
const defaultWrapperTemplate = _.template('\n<table>\n <tbody><%= bodyContent %> </tbody>\n<%= tableFooterContent %></table>\n\n')
const wrapperTemplate = options.wrapperTemplate ? _.template(`\n${options.wrapperTemplate}\n\n`) : defaultWrapperTemplate
const seperator = options.wrapperTemplate ? '\n </tr><br />\n <tr>\n ' : '\n </tr>\n <tr>\n '

let tableFooterContent = ''

return _.flow(
Expand All @@ -81,12 +85,15 @@ function generateContributorsList(options, contributors) {
_.map((currentLineContributors) => formatLine(
options, currentLineContributors
)),
_.join('\n </tr>\n <tr>\n '),
_.join(seperator),
newContent => {
if (options.linkToUsage) {
tableFooterContent = ` <tfoot>\n ${tableFooter}\n </tfoot>\n`
}
return `\n<table>\n <tbody>\n <tr>\n ${newContent}\n </tr>\n </tbody>\n${tableFooterContent}</table>\n\n`

const bodyContent = `\n <tr>\n ${newContent}\n </tr>\n`

return wrapperTemplate({ bodyContent, tableFooterContent})
},
)(contributors)
}
Expand Down Expand Up @@ -135,6 +142,7 @@ module.exports = function generate(options, contributors, fileContent) {
? '\n'
: generateContributorsList(options, contributors)
const badge = formatBadge(options, contributors)

return _.flow(
injectListBetweenTags(contributorsList),
replaceBadge(badge),
Expand Down

0 comments on commit 2c07af8

Please sign in to comment.