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

feat: add wrapperTemplate option #348

Merged
merged 2 commits into from
Apr 26, 2023
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
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