UNPKG

2.74 kBJavaScriptView Raw
1/* :: declare type Person = Object; */
2/* :: declare type PersonOptions = {displayCopyright?:boolean; displayYears?:boolean; githubSlug?:string}; */
3export function getGithubSlug(data) {
4 let match = null
5 if (typeof data.repository === 'string') {
6 match = data.repository.match(/^(?:github:)?([^/:]+\/[^/:]+)$/)
7 } else {
8 let url = null
9 if (data.repository && typeof data.repository.url === 'string') {
10 url = data.repository && data.repository.url
11 } else if (typeof data.homepage === 'string') {
12 url = data.homepage
13 } else {
14 return null
15 }
16 match = url.match(/github\.com[/:]([^/:]+\/[^/:]+?)(?:\.git|\/)?$/)
17 }
18 return (match && match[1]) || null
19}
20export function getPeopleHTML(people, opts) {
21 if (people.length === 0) {
22 return ''
23 } else {
24 return (
25 '<ul>' +
26 people
27 .map(function (person) {
28 return '<li>' + person.toHTML(opts) + '</li>'
29 })
30 .join('\n') +
31 '</ul>'
32 )
33 }
34}
35export function getPeopleTextArray(people, opts) {
36 if (people.length === 0) {
37 return []
38 } else {
39 const textArray = []
40 people.forEach(function (person) {
41 if (!person.name || person.name === 'null') {
42 throw new Error(
43 `For some reason the person doesn't have a name: ${JSON.stringify(
44 person,
45 null,
46 ' '
47 )}`
48 )
49 }
50 const text = person.toString(opts)
51 if (text) textArray.push(text)
52 })
53 return textArray
54 }
55}
56export function getFileUrl(data, filename) {
57 if (data.github.slug) {
58 return `https://github.com/${data.github.slug}/blob/master/${filename}#files`
59 } else {
60 throw new Error(
61 'File links are currently only supported for github repositories'
62 )
63 }
64}
65export function getLink({ url, text, title }) {
66 if (!url || !text) {
67 throw new Error('Links must have both a url and text')
68 }
69 if (title) {
70 return `<a href="${url}" title="${title}">${text}</a>`
71 } else {
72 return `<a href="${url}">${text}</a>`
73 }
74}
75// @todo replace this with bevry/ropo
76export function replaceSection(names, source, inject) {
77 let regexName, sectionName
78 if (Array.isArray(names)) {
79 regexName = '(' + names.join('|') + ')'
80 sectionName = names[0]
81 } else {
82 regexName = sectionName = names
83 }
84 sectionName = sectionName.toUpperCase()
85 /* eslint indent:0 */
86 const regex = new RegExp(
87 [
88 '^(',
89 `<!--\\s*${regexName}\\s*-->`,
90 '|',
91 `<!--\\s*${regexName}/\\s*-->`,
92 '[\\s\\S]*?',
93 `<!--\\s*/${regexName}\\s*-->`,
94 ')\\s+',
95 ].join(''),
96 'gim'
97 )
98 function replace() {
99 const result = typeof inject === 'function' ? inject() : inject
100 return `<!-- ${sectionName}/ -->\n\n${result}\n\n<!-- /${sectionName} -->\n\n\n`
101 }
102 const result = source.replace(regex, replace)
103 return result
104}
105export function trim(str) {
106 return str.replace(/^\s+|\s+$/g, '')
107}