-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathhelpers.js
82 lines (70 loc) · 2.23 KB
/
helpers.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
81
82
const { decode, encode } = require('html-entities')
const meta = require('./content/meta.json')
// configure markdown-it
const transformer = require('jstransformer')
const { _tr: mdTransformer } = transformer(require('jstransformer-markdown-it'))
const config = {
typographer: true,
html: true
}
// monkey-patch render function to pass custom options
const { render: renderMd } = mdTransformer
mdTransformer.render = str => renderMd(str, config)
// replacements
const replacements = str => {
return str && str.replace(/<\/?u>/g, '').replace(meta.tallycoinUrl, meta.shoutoutUrl)
}
const stripHTML = str => {
return str && encode(decode(str.replace(/(<([^>]+)>)/ig, '').trim().replace(/\n\s*/g, '\n')), { level: 'xml' })
}
// meetups
const toMeetupMapInfo = m => {
return {
name: m.name,
latLng: [m.latitude, m.longitude],
url: m.url,
city: m.city,
portalUrl: m.portalLink,
websiteUrl: m.websiteUrl,
twitter: m.twitter_username,
event: m.next_event,
style: {
fill: m.name.startsWith('Einundzwanzig') || m.name.includes('Einezwänzg') || m.name.includes('Eenanzwanzeg') || m.name.includes('Yirmibir') ? 'var(--color-accent)' : 'var(--color-neutral-50)'
}
}
}
// slug
const slugify = str => str.toLowerCase()
.replace(/ä/g, 'ae').replace(/ö/g, 'oe').replace(/ü/g, 'ue')
.replace(/\s+/g, '-').replace(/[^\w\-]+/g, '')
.replace(/\-\-+/g, '-').replace(/^-+/, '').replace(/-+$/, '')
const truncate = (str, wordCount) => {
const words = str.trim().split(/\s(?![^\[]*\])/g)
const head = words.splice(0, wordCount).join(' ')
const tail = words.join(' ')
return [head, tail]
}
// team
const teamWithAliases = team => {
const withAliases = {}
Object.entries(team).forEach(([id, member]) => {
withAliases[id] = member
const aliases = (member.aliases || []).concat(member.name.toLowerCase())
aliases.forEach(alias => {
const aliasId = alias.toLowerCase()
if (!withAliases[aliasId]) withAliases[aliasId] = member
})
})
return withAliases
}
const participantToId = p => p.replace(/\(.*?\)/, '').trim().toLowerCase()
module.exports = {
markdown: mdTransformer.render,
replacements,
slugify,
stripHTML,
truncate,
teamWithAliases,
participantToId,
toMeetupMapInfo
}