UNPKG

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