forked from Einundzwanzig-Podcast/einundzwanzig.space
-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.js
51 lines (42 loc) · 1.38 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
const { decode, encode } = require('html-entities')
// 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, '')
}
const stripHTML = str => {
return str && encode(decode(str.replace(/(<([^>]+)>)/ig, '').trim().replace(/\n\s*/g, '\n')), { level: 'xml' })
}
// 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]
}
const memberUrl = member => {
if (member.url) return member.url
else if (member.nostr) return `https://snort.social/p/${member.nostr}`
else if (member.twitter) return `https://twitter.com/${member.twitter}`
}
module.exports = {
markdown: mdTransformer.render,
replacements,
slugify,
stripHTML,
truncate,
memberUrl
}