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