feat: resolve timing with producing follow feed

This commit is contained in:
Cory Dransfeldt 2024-05-17 11:59:00 -07:00
parent ca31cd4f0e
commit c37fc2f1a3
No known key found for this signature in database
10 changed files with 133 additions and 193 deletions

View file

@ -1,37 +1,79 @@
import tagAliases from '../data/tag-aliases.js'
import { DateTime } from 'luxon'
export const searchIndex = (collection) => {
const searchIndex = []
let id = 0
const collectionData = collection.getAll()[0]
const posts = collectionData.data.collections.posts
const links = collectionData.data.links
if (posts) {
posts.forEach((post) => {
const url = post.url.includes('http') ? post.url : `https://coryd.dev${post.url}`
searchIndex.push({
id,
url,
title: `📝: ${post.data.title}`,
tags: post.data.tags.filter((tag) => tag !== 'posts'),
const { data } = collectionData
const { collections: { posts, links } } = data
const addItemToIndex = (items, icon, getUrl, getTitle, getTags) => {
if (items) {
items.forEach((item) => {
searchIndex.push({
id,
url: getUrl(item),
title: `${icon}: ${getTitle(item)}`,
tags: getTags(item),
})
id++
})
id++;
})
}
if (links) {
links.forEach((link) => {
searchIndex.push({
id,
url: link.url,
title: `🔗: ${link.title}`,
tags: link.tags,
})
id++;
})
}
}
addItemToIndex(posts, '📝', item => item.url.includes('http') ? item.url : `https://coryd.dev${item.url}`, item => item.data.title, item => item.data.tags.filter(tag => tag !== 'posts'))
addItemToIndex(links, '🔗', item => item.data.link, item => item.data.title, item => item.data.tags)
return searchIndex
}
export const followContent = (collection) => {
const aggregateContent = []
const collectionData = collection.getAll()[0]
const { data } = collectionData
const {
collections: { posts, links },
books,
movies: { movies },
weeklyArtistChart
} = data
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.toISO() : null
}
const addContent = (items, icon, getTitle, getDate) => {
if (items) {
items.forEach(item => {
const content = {
url: item.url.includes('http') ? item.url : `https://coryd.dev${item.url}`,
title: `${icon}: ${getTitle(item)}`
}
if (item.data?.link) content.url = item.data?.link
const date = getDate ? parseDate(getDate(item)) : null
if (date) content.date = date
aggregateContent.push(content)
})
}
}
addContent(posts, '📝', item => item.data.title, item => item.data.date)
addContent(links, '🔗', item => item.data.title, item => item.data.date)
addContent(books.filter(book => book.status === 'started'), '📖', item => item.title, item => item.date)
addContent(movies, '🎥', item => item.title, item => item.lastWatched)
addContent(weeklyArtistChart, '🎧', item => item.title, item => item.date)
return aggregateContent.sort((a, b) => {
const dateA = a.date ? DateTime.fromISO(a.date) : DateTime.fromMillis(0)
const dateB = b.date ? DateTime.fromISO(b.date) : DateTime.fromMillis(0)
return dateB - dateA
})
}
export const tagList = (collection) => {
const tagsSet = new Set()
collection.getAll().forEach((item) => {
@ -46,48 +88,40 @@ export const tagList = (collection) => {
export const tagMap = (collection) => {
const tags = {}
const collectionData = collection.getAll()[0]
const posts = collectionData.data.collections.posts
const links = collectionData.data.collections.links
const books = collectionData.data.books
const { data } = collectionData
const { collections: { posts, links }, books } = data
const processItems = (items, getUrl, getTags) => {
if (items) {
items.forEach((item) => {
const url = getUrl(item)
const tagString = [...new Set(getTags(item).map(tag => tagAliases[tag.toLowerCase()]))]
.join(' ')
.trim()
.replace(/\s+/g, ' ')
if (tagString) tags[url] = tagString
})
}
}
if (posts) posts.forEach((post) => {
const url = post.url.includes('http') ? post.url : `https://coryd.dev${post.url}`
const tagString = [...new Set(post.data.tags?.map((tag) => tagAliases[tag.toLowerCase()]))]
.join(' ')
.trim()
if (tagString) tags[url] = tagString.replace(/\s+/g,' ')
})
if (links) links.forEach((link) => {
const url = link.data.link
const tagString = [...new Set(link.data.tags?.map((tag) => tagAliases[tag.toLowerCase()]))]
.join(' ')
.trim()
if (tagString) tags[url] = tagString.replace(/\s+/g,' ')
})
if (books) books.forEach((book) => {
const tagString = book['tags']?.map((tag) => tagAliases[tag.toLowerCase()])
.join(' ')
.trim()
if (tagString) tags[book.url] = tagString.replace(/\s+/g,' ')
})
processItems(posts, item => item.url.includes('http') ? item.url : `https://coryd.dev${item.url}`, item => item.data.tags || [])
processItems(links, item => item.data.link, item => item.data.tags || [])
processItems(books, item => item.tags || [], item => item.tags || [])
return tags
}
export const tagsSortedByCount = (collection) => {
const tagStats = {};
const tagStats = {}
collection.getFilteredByGlob('src/posts/**/*.*').forEach((item) => {
if (!item.data.tags) return;
if (!item.data.tags) return
item.data.tags
.filter((tag) => !['posts', 'all', 'politics', 'net neutrality'].includes(tag))
.forEach((tag) => {
if (!tagStats[tag]) tagStats[tag] = 1;
if (tagStats[tag]) tagStats[tag] = tagStats[tag] + 1;
});
});
return Object.entries(tagStats).sort((a, b) => b[1] - a[1]).map(([key, value]) => `${key}`);
if (!tagStats[tag]) tagStats[tag] = 1
if (tagStats[tag]) tagStats[tag] = tagStats[tag] + 1
})
})
return Object.entries(tagStats).sort((a, b) => b[1] - a[1]).map(([key, value]) => `${key}`)
}
export const links = (collection) => collection.getFilteredByGlob('src/links/**/*.*').reverse()