mirror of
https://github.com/Einundzwanzig-Podcast/einundzwanzig.space.git
synced 2025-12-13 16:26:50 +00:00
Refactor helpers and improve feed
This commit is contained in:
36
helpers.js
36
helpers.js
@@ -15,14 +15,13 @@ 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)
|
||||
}
|
||||
// constants
|
||||
const IS_DEV = process.env.NODE_ENV === 'development'
|
||||
const HOST = IS_DEV ? 'http://localhost:3000' : 'https://einundzwanzig.space'
|
||||
|
||||
const stripHTML = str => {
|
||||
return str && encode(decode(str.replace(/(<([^>]+)>)/ig, '').trim().replace(/\n\s*/g, '\n')), { level: 'xml' })
|
||||
}
|
||||
// replacements
|
||||
const replacements = str => str && str.replace(/<\/?u>/g, '').replace(meta.tallycoinUrl, meta.shoutoutUrl)
|
||||
const stripHTML = str => str && encode(decode(str.replace(/(<([^>]+)>)/ig, '').trim().replace(/\n\s*/g, '\n')), { level: 'xml' })
|
||||
|
||||
// meetups
|
||||
const toMeetupMapInfo = m => {
|
||||
@@ -69,9 +68,32 @@ const participantsWithAliases = participants => {
|
||||
}
|
||||
|
||||
const participantToId = p => p.replace(/\(.*?\)/, '').trim().toLowerCase()
|
||||
const random = max => Math.floor(Math.random() * Math.floor(max))
|
||||
const shuffle = arr => { for (let i = arr.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * i); const temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; }; return arr }
|
||||
const formatDate = date => (new Date(date)).toISOString().replace(/T.*/, '').split('-').reverse().join('.')
|
||||
const linkTarget = url => url.startsWith('http') ? '_blank' : null
|
||||
const assetPath = path => {
|
||||
if (path.startsWith('http')) return path
|
||||
let revs
|
||||
try { revs = require('./generated/rev.json') } catch (error) { }
|
||||
return `${(revs && revs[path]) || path}`
|
||||
}
|
||||
const assetUrl = (path, protocol = 'https') => {
|
||||
if (IS_DEV && !path.startsWith('http')) protocol = 'http'
|
||||
const base = path.startsWith('http') ? '' : HOST
|
||||
let url = `${base}${assetPath(path)}`
|
||||
if (!url.startsWith(`${protocol}:`)) url = url.replace(/^.*:/, `${protocol}:`)
|
||||
return url
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
markdown: mdTransformer.render,
|
||||
random,
|
||||
shuffle,
|
||||
assetUrl,
|
||||
assetPath,
|
||||
formatDate,
|
||||
linkTarget,
|
||||
replacements,
|
||||
slugify,
|
||||
stripHTML,
|
||||
|
||||
Reference in New Issue
Block a user