UNPKG

3.44 kBPlain TextView Raw
1import { getPeopleHTML, getFileUrl, getLink } from './util.js'
2import { getBadgesInCategory } from './badge.js'
3import type { Github, FilenamesForReadmeFiles } from './types'
4import type Fellow from 'fellow'
5import type { BadgesField } from 'badges'
6
7function getSponsorsText(data: {
8 badges: BadgesField
9 sponsors: Fellow[]
10 github: Github
11}): string {
12 let result = ''
13
14 if (data.sponsors.length === 0) {
15 // ignore
16 result +=
17 'No sponsors yet! Will you be the first?\n\n' +
18 getBadgesInCategory('funding', data)
19 } else {
20 result +=
21 'These amazing people have contributed finances to this project:\n\n' +
22 getPeopleHTML(data.sponsors) +
23 `\n\nBecome a sponsor!\n\n${getBadgesInCategory('funding', data)}`
24 }
25
26 return result
27}
28
29function getMaintainersText(data: {
30 maintainers: Fellow[]
31 github: Github
32}): string {
33 let result = ''
34
35 if (data.maintainers.length === 0) {
36 // ignore
37 result += 'No maintainers yet! Will you be the first?'
38 } else {
39 result +=
40 'These amazing people are maintaining this project:\n\n' +
41 getPeopleHTML(data.maintainers, {
42 displayContributions: true,
43 githubRepoSlug: data.github.slug,
44 })
45 }
46
47 return result
48}
49
50function getContributeLink(
51 data: { filenamesForReadmeFiles: FilenamesForReadmeFiles; github: Github },
52 optional = false
53): string {
54 // Prepare
55 const file = data.filenamesForReadmeFiles.contributing
56 if (!file) {
57 if (optional) {
58 return ''
59 } else {
60 throw new Error(
61 'Contributing section requires a CONTRIBUTING file to exist'
62 )
63 }
64 }
65 const url = getFileUrl(data, file)
66 const text = `Discover how you can contribute by heading on over to the <code>${file}</code> file.`
67
68 // Return
69 return getLink({ url, text })
70}
71
72function getContributorsText(data: {
73 filenamesForReadmeFiles: FilenamesForReadmeFiles
74 contributors: Fellow[]
75 github: Github
76}): string {
77 let result = ''
78
79 if (data.contributors.length === 0) {
80 // ignore
81 result +=
82 'No contributors yet! Will you be the first?' +
83 `\n\n${getContributeLink(data, true)}`
84 } else {
85 result +=
86 'These amazing people have contributed code to this project:\n\n' +
87 getPeopleHTML(data.contributors, {
88 displayContributions: true,
89 githubRepoSlug: data.github.slug,
90 }) +
91 `\n\n${getContributeLink(data, true)}`
92 }
93
94 return result
95}
96
97interface BackerOptions {
98 filenamesForReadmeFiles: FilenamesForReadmeFiles
99 badges: BadgesField
100 maintainers: Fellow[]
101 sponsors: Fellow[]
102 contributors: Fellow[]
103 github: Github
104}
105export function getBackerSection(data: BackerOptions): string {
106 // Prepare
107 const result = [
108 '<h2>Backers</h2>',
109 '',
110 '<h3>Maintainers</h3>',
111 '',
112 getMaintainersText(data),
113 '',
114 '<h3>Sponsors</h3>',
115 '',
116 getSponsorsText(data),
117 '',
118 '<h3>Contributors</h3>',
119 '',
120 getContributorsText(data),
121 ].join('\n')
122
123 // Return
124 return result
125}
126
127export function getBackerFile(data: BackerOptions): string {
128 // Prepare
129 const result = [
130 '<h1>Backers</h1>',
131 '',
132 '<h2>Maintainers</h2>',
133 '',
134 getMaintainersText(data),
135 '',
136 '<h2>Sponsors</h2>',
137 '',
138 getSponsorsText(data),
139 '',
140 '<h2>Contributors</h2>',
141 '',
142 getContributorsText(data),
143 ].join('\n')
144
145 // Return
146 return result
147}
148
149export function getContributeSection(data: {
150 filenamesForReadmeFiles: FilenamesForReadmeFiles
151 github: Github
152}): string {
153 // Prepare
154 const result = ['<h2>Contribute</h2>', '', getContributeLink(data)].join('\n')
155
156 // Return
157 return result
158}