mirror of
https://github.com/Einundzwanzig-Podcast/einundzwanzig.space.git
synced 2025-12-13 16:26:50 +00:00
47 lines
1.2 KiB
JavaScript
47 lines
1.2 KiB
JavaScript
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' })
|
|
}
|
|
|
|
// 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
|
|
}
|