meta updates

This commit is contained in:
Cory Dransfeldt 2023-03-11 18:03:54 -08:00
parent 70c52e6a37
commit 2809018dc1
No known key found for this signature in database
6 changed files with 154 additions and 55 deletions

62
config/filters.js Normal file
View file

@ -0,0 +1,62 @@
const sanitizeHTML = require('sanitize-html')
module.exports = {
trim: (string, limit) => {
return string.length <= limit ? string : `${string.slice(0, limit)}...`
},
postPath: (path) => {
if (path.includes('micro/')) return path
return `/micro/${path}`
},
stripIndex: (path) => {
return path.replace('/index.html', '/')
},
getFirstAttachment: (post) => {
if (post && post.attachments && post.attachments.length > 0) {
return post.attachments[0].url ? post.attachments[0].url : post.attachments[0]
}
return '/assets/img/social-card.png'
},
webmentionsByUrl: (webmentions, url) => {
const allowedTypes = ['mention-of', 'in-reply-to', 'like-of', 'repost-of']
const data = {
'like-of': [],
'repost-of': [],
'in-reply-to': [],
}
const hasRequiredFields = (entry) => {
const { author, published, content } = entry
return author.name && published && content
}
const filtered = webmentions
.filter((entry) => entry['wm-target'] === `https://coryd.dev${url}`)
.filter((entry) => allowedTypes.includes(entry['wm-property']))
filtered.forEach((m) => {
if (data[m['wm-property']]) {
const isReply = m['wm-property'] === 'in-reply-to'
const isValidReply = isReply && hasRequiredFields(m)
if (isReply) {
if (isValidReply) {
m.sanitized = sanitizeHTML(m.content.html)
data[m['wm-property']].unshift(m)
}
return
}
data[m['wm-property']].unshift(m)
}
})
data['in-reply-to'].sort((a, b) =>
a.published > b.published ? 1 : b.published > a.published ? -1 : 0
)
return data
},
}