-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
formatters.js
80 lines (71 loc) · 2.24 KB
/
formatters.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
* @typedef {import('mdast').PhrasingContent} PhrasingContent
* @typedef {import('#get-contributors-from-package').Contributor} Contributor
*/
/**
* @callback Format
* Format a field.
* @param {unknown} value
* Value of a field in a contributor.
* @param {string} key
* Name of a field in a contributor.
* @param {Contributor} contributor
* Whole contributor.
* @returns {Array<PhrasingContent> | PhrasingContent | string | null | undefined}
* Content.
*
* @typedef FormatterObject
* How to format a field.
* @property {boolean | null | undefined} [exclude=false]
* Whether to ignore these fields (default: `false`).
* @property {Format | null | undefined} [format]
* How to format the cell (optional).
* @property {string | null | undefined} [label]
* Text in the header row that labels the column for this field (optional).
*
* @typedef {Record<string, FormatterObject>} FormatterObjects
* Map of fields found in `contributors` to formatter objects.
*/
/** @type {FormatterObjects} */
export const defaultFormatters = {
email: {exclude: true},
github: {format: profile, label: 'GitHub'},
name: {
format(value) {
return {type: 'strong', children: [{type: 'text', value: String(value)}]}
},
label: 'Name'
},
twitter: {format: profile, label: 'Twitter'},
url: {label: 'Website'}
}
/** @type {Format} */
function profile(raw, key) {
let value = String(raw)
let pos = value.toLowerCase().indexOf('.com/')
// Automatically strip URL's in values from GitHub and Twitter
// So we can display just the username alone
if (pos !== -1) {
value = value.slice(pos + '.com/'.length)
}
pos = value.indexOf('/')
// Strip out the string without trailing slash if it has one
// so we just get the username back from the value entered
if (pos !== -1) {
value = value.slice(0, pos)
}
// Remove "@" from the URL's if there are any
if (value.indexOf('@') === 0) {
value = value.slice(1)
}
// Prevent empty links
if (!value) {
// To do: investigate? should return `undefined`?
return {type: 'text', value: ''}
}
return {
type: 'link',
url: 'https://' + key + '.com/' + value,
children: [{type: 'strong', children: [{type: 'text', value: '@' + value}]}]
}
}