forked from Einundzwanzig-Podcast/einundzwanzig.space
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.js
45 lines (36 loc) · 1.15 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
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]
}
module.exports = {
markdown: mdTransformer.render,
replacements,
slugify,
stripHTML,
truncate
}