chore: cleanup

This commit is contained in:
Cory Dransfeldt 2024-09-23 14:10:12 -07:00
parent 3f48399c09
commit 4696b0c383
No known key found for this signature in database
6 changed files with 72 additions and 75 deletions

View file

@ -32,11 +32,23 @@ export const processContent = (collection) => {
const parseDate = (date) => {
if (!date) return null
let parsedDate = DateTime.fromISO(date)
if (!parsedDate.isValid) parsedDate = DateTime.fromFormat(date, 'yyyy-MM-dd')
if (!parsedDate.isValid) parsedDate = DateTime.fromFormat(date, 'MM/dd/yyyy')
if (!parsedDate.isValid) parsedDate = DateTime.fromFormat(date, 'dd-MM-yyyy')
return parsedDate.isValid ? parsedDate : null
const formats = [
{ method: 'fromISO' },
{ method: 'fromFormat', format: 'yyyy-MM-dd' },
{ method: 'fromFormat', format: 'MM/dd/yyyy' },
{ method: 'fromFormat', format: 'dd-MM-yyyy' },
]
for (const { method, format } of formats) {
const parsedDate = format
? DateTime[method](date, format)
: DateTime[method](date)
if (parsedDate.isValid) return parsedDate
}
return null
}
const addSiteMapContent = (items, getTitle, getDate) => {

View file

@ -12,79 +12,67 @@ export default {
const posts = []
const mdGenerator = () => {
const md = markdownIt({ html: true, linkify: true })
md.use(markdownItAnchor, {
level: [1, 2],
permalink: markdownItAnchor.permalink.headerLink({
safariReaderFix: true
})
permalink: markdownItAnchor.permalink.headerLink({ safariReaderFix: true })
})
md.use(markdownItFootnote)
md.renderer.rules.footnote_ref = (tokens, idx) => {
const id = tokens[idx].meta.id + 1
return `<sup>${id}</sup>`
}
md.renderer.rules.footnote_block_open = () => (
'<hr class="footnotes-sep">\n<section class="footnotes">\n<ol class="footnotes-list">\n'
)
md.renderer.rules.footnote_open = (tokens, idx) => {
const id = tokens[idx].meta.id + 1
return `<li id="fn${id}" class="footnote-item"> `
}
md.renderer.rules.footnote_ref = (tokens, idx) => `<sup>${tokens[idx].meta.id + 1}</sup>`
md.renderer.rules.footnote_block_open = () => '<hr class="footnotes-sep">\n<section class="footnotes">\n<ol class="footnotes-list">\n'
md.renderer.rules.footnote_open = (tokens, idx) => `<li id="fn${tokens[idx].meta.id + 1}" class="footnote-item"> `
md.renderer.rules.footnote_anchor = () => ''
return md
}
const entryData = limit ? entries.slice(0, limit) : entries
const entryData = limit ? entries.slice(0, limit) : entries
entryData.forEach((entry) => {
const md = mdGenerator()
const dateKey = Object.keys(entry).find(key => key.includes('date'))
let { artist, authors, backdrop, content, description, image, link, rating, review, slug, title, url, tags, type } = entry
const {
artist, authors, backdrop, content, description, image, link, rating, review,
slug, title, url, tags, type
} = entry
const processedEntry = {
title: title.trim(),
date: new Date(entry[dateKey]),
content: description || ''
}
const feedNote = '<hr/><p>This is a full text feed, but not all content can be rendered perfectly within the feed. If something looks off, feel free to <a href="https://coryd.dev">visit my site</a> for the original post.</p>'
const processedEntry = { title: title.trim(), date: new Date(entry[dateKey]), content: description }
if (url?.includes('http')) processedEntry['url'] = url
if (!url?.includes('http')) processedEntry['url'] = new URL(url, BASE_URL).toString()
if (slug) processedEntry['url'] = new URL(slug, BASE_URL).toString()
processedEntry.url = (url?.includes('http')) ? url : new URL(slug || url, BASE_URL).toString()
if (link) {
processedEntry['title'] = `${title} via ${authors['name']}`
processedEntry['url'] = link,
processedEntry['author'] = {
name: authors['name'],
url: authors['url'],
mastodon: authors?.['mastodon'] || '',
rss: authors?.['rss_feed'] || ''
},
processedEntry['excerpt'] = sanitizeHtml(`${md.render(description)}`)
}
if (description) processedEntry['excerpt'] = description
if (['book', 'movie'].includes(type) && review) {
processedEntry['excerpt'] = sanitizeHtml(`${md.render(review)}`)
processedEntry.title = `${title} via ${authors?.name || 'Unknown'}`
processedEntry.url = link
processedEntry.author = {
name: authors?.name || 'Unknown',
url: authors?.url || '',
mastodon: authors?.mastodon || '',
rss: authors?.rss_feed || ''
}
processedEntry.excerpt = sanitizeHtml(md.render(description || ''))
} else if (['book', 'movie'].includes(type)) {
processedEntry['excerpt'] = sanitizeHtml(`${md.render(description)}`)
processedEntry.excerpt = sanitizeHtml(md.render(review || description || ''))
} else if (type === 'album-release') {
let sanitizedDescription = sanitizeHtml(md.render(description || ''))
let truncatedDescription = truncate(sanitizedDescription, { length: 500, reserveLastWord: true, ellipsis: '...' })
if (sanitizedDescription.length > 500) truncatedDescription += ` <a href="${artist?.url}">Read more about ${artist?.name}</a>`
processedEntry.excerpt = truncatedDescription
} else if (slug && content) {
processedEntry.excerpt = sanitizeHtml(md.render(content) + feedNote, { disallowedTagsMode: 'completelyDiscard' })
} else if (description) {
processedEntry.excerpt = description
}
if (type === 'album-release') {
let sanitizedDescription = sanitizeHtml(`${md.render(description)}`)
let truncatedDescription = truncate(sanitizedDescription, {
length: 500,
reserveLastWord: true,
ellipsis: '...'
})
if (sanitizedDescription.length > 500) truncatedDescription += ` <a href="${entry['artist']['url']}">Read more about ${entry['artist']['name']}</a>`
processedEntry['excerpt'] = truncatedDescription
}
if (slug && content) processedEntry['excerpt'] = sanitizeHtml(`${md.render(content)}${feedNote}`, {
disallowedTagsMode: 'completelyDiscard'
})
processedEntry['image'] = backdrop || image
processedEntry.image = backdrop || image
if (rating) processedEntry['rating'] = rating
if (tags) processedEntry['tags'] = tags
if (type === 'album-release' && artist) processedEntry['title'] = `${title} by ${artist}`
if (rating) processedEntry.rating = rating
if (tags) processedEntry.tags = tags
if (type === 'album-release' && artist) processedEntry.title = `${title} by ${artist}`
if (entry) posts.push(processedEntry)
posts.push(processedEntry)
})
return posts