Refactor helpers and improve feed

This commit is contained in:
Dennis Reimann
2025-02-28 12:19:36 +01:00
parent 2bb6b608e7
commit 9572c9dc2c
3 changed files with 36 additions and 38 deletions

View File

@@ -15,14 +15,13 @@ const { render: renderMd } = mdTransformer
mdTransformer.render = str => renderMd(str, config) mdTransformer.render = str => renderMd(str, config)
// replacements // constants
const replacements = str => { const IS_DEV = process.env.NODE_ENV === 'development'
return str && str.replace(/<\/?u>/g, '').replace(meta.tallycoinUrl, meta.shoutoutUrl) const HOST = IS_DEV ? 'http://localhost:3000' : 'https://einundzwanzig.space'
}
const stripHTML = str => { // replacements
return str && encode(decode(str.replace(/(<([^>]+)>)/ig, '').trim().replace(/\n\s*/g, '\n')), { level: 'xml' }) 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 // meetups
const toMeetupMapInfo = m => { const toMeetupMapInfo = m => {
@@ -69,9 +68,32 @@ const participantsWithAliases = participants => {
} }
const participantToId = p => p.replace(/\(.*?\)/, '').trim().toLowerCase() 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 = { module.exports = {
markdown: mdTransformer.render, markdown: mdTransformer.render,
random,
shuffle,
assetUrl,
assetPath,
formatDate,
linkTarget,
replacements, replacements,
slugify, slugify,
stripHTML, stripHTML,

View File

@@ -1,33 +1,7 @@
// initialize markdown rendering // initialize markdown rendering
const helpers = require('./helpers') const helpers = require('./helpers')
const IS_DEV = process.env.NODE_ENV === 'development'
const HOST = IS_DEV ? 'http://localhost:3000' : 'https://einundzwanzig.space'
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 = { module.exports = {
basedir: './src/includes', basedir: './src/includes',
random,
shuffle,
assetUrl,
assetPath,
formatDate,
linkTarget,
...helpers ...helpers
} }

View File

@@ -1,6 +1,6 @@
const { writeFileSync } = require('fs') const { writeFileSync } = require('fs')
const { join, resolve } = require('path') const { join, resolve } = require('path')
const { replacements, slugify, stripHTML, participantsWithAliases, participantToId } = require('../helpers') const { replacements, slugify, stripHTML, participantsWithAliases, participantToId, assetUrl } = require('../helpers')
const { masterFeedUrl, publicFeedUrl, nodeId } = require('../content/meta.json') const { masterFeedUrl, publicFeedUrl, nodeId } = require('../content/meta.json')
const participantsRaw = require('../content/participants.json') const participantsRaw = require('../content/participants.json')
const request = require('sync-request') const request = require('sync-request')
@@ -208,11 +208,13 @@ const parseEpisode = e => {
updated['podcast:person'] = [] updated['podcast:person'] = []
people.forEach(p => { people.forEach(p => {
let href = p.url const __attr = {}
if (!href && p.nostr) href = `https://primal.net/p/${p.nostr}` if (p.image) __attr.img = assetUrl(p.image)
if (!href && p.twitter) href = `https://twitter.com/${p.twitter}` if (p.url) __attr.href = p.url
else if (p.nostr) __attr.href = `https://njump.me/${p.nostr}`
else if (p.twitter) __attr.href = `https://x.com/${p.twitter}`
updated['podcast:person'].push({ updated['podcast:person'].push({
__attr: { href }, __attr,
'#text': p.name '#text': p.name
}) })
}) })