chore: consolidate search/all/sitemap collections; filter out future albums from artist pages if they don't have plays
This commit is contained in:
parent
fc9467de1b
commit
a8beefaa4a
4 changed files with 94 additions and 105 deletions
|
@ -23,46 +23,30 @@ const tagsToHashtags = (tags) => {
|
|||
return hashtags.join(' ');
|
||||
}
|
||||
|
||||
export const searchIndex = (collection) => {
|
||||
export const popularPosts = (collection) => {
|
||||
const collectionData = collection.getAll()[0]
|
||||
const { data } = collectionData
|
||||
const { posts, analytics } = data
|
||||
|
||||
return posts
|
||||
.filter((post) => {
|
||||
if (analytics.find((p) => p.page.includes(post.slug))) return true
|
||||
})
|
||||
.sort((a, b) => {
|
||||
const visitors = (page) => analytics.filter((p) => p.page.includes(page.slug)).pop()?.visitors
|
||||
return visitors(b) - visitors(a)
|
||||
})
|
||||
}
|
||||
|
||||
export const processContent = (collection) => {
|
||||
const searchIndex = []
|
||||
const aggregateContent = []
|
||||
const siteMapContent = []
|
||||
let id = 0
|
||||
const collectionData = collection.getAll()[0]
|
||||
const { data } = collectionData
|
||||
const { posts, links, movies, books } = data
|
||||
const movieData = movies['movies'].filter(movie => (movie['review']?.length && movie['review']?.length > 0 && movie['rating']))
|
||||
const bookData = books.all.filter(book => (book['review']?.length && book['review']?.length > 0 && book['rating']))
|
||||
const addItemToIndex = (items, icon, getUrl, getTitle, getTags) => {
|
||||
if (items) {
|
||||
items.forEach((item) => {
|
||||
searchIndex.push({
|
||||
id,
|
||||
url: getUrl(item),
|
||||
title: `${icon}: ${getTitle(item)}`,
|
||||
tags: getTags ? getTags(item) : [],
|
||||
})
|
||||
id++
|
||||
})
|
||||
}
|
||||
}
|
||||
const { posts, links, movies, books, pages, artists, genres, tv } = data
|
||||
|
||||
addItemToIndex(posts, '📝', item => new URL(item['slug'], BASE_URL).toString(), item => item['title'], item => item['tags'])
|
||||
addItemToIndex(links, '🔗', item => item['link'], item => item['title'], item => item['tags'])
|
||||
if (movieData) addItemToIndex(movieData, '🎥', item => item['url'], item => `${item['title']} (${item['rating']})`, item => item['tags'])
|
||||
if (bookData) addItemToIndex(bookData, '📖', item => item['url'], item => `${item['title']} (${item['rating']})`, item => item['tags'])
|
||||
|
||||
return searchIndex
|
||||
}
|
||||
|
||||
export const allContent = (collection) => {
|
||||
const aggregateContent = []
|
||||
const collectionData = collection.getAll()[0]
|
||||
const { data } = collectionData
|
||||
const {
|
||||
posts,
|
||||
links,
|
||||
books,
|
||||
movies: { movies }
|
||||
} = data
|
||||
const parseDate = (date) => {
|
||||
if (!date) return null
|
||||
let parsedDate = DateTime.fromISO(date)
|
||||
|
@ -71,10 +55,25 @@ export const allContent = (collection) => {
|
|||
if (!parsedDate.isValid) parsedDate = DateTime.fromFormat(date, 'dd-MM-yyyy')
|
||||
return parsedDate.isValid ? parsedDate.toISO() : null
|
||||
}
|
||||
|
||||
const addItemToIndex = (items, icon, getUrl, getTitle, getTags) => {
|
||||
if (items) {
|
||||
items.forEach((item) => {
|
||||
searchIndex.push({
|
||||
id,
|
||||
url: getUrl(item),
|
||||
title: `${icon}: ${getTitle(item)}`,
|
||||
tags: getTags ? getTags(item) : []
|
||||
})
|
||||
id++
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const addContent = (items, icon, getTitle, getDate) => {
|
||||
if (items) {
|
||||
items.forEach(item => {
|
||||
let attribution;
|
||||
items.forEach((item) => {
|
||||
let attribution
|
||||
|
||||
if (item?.['authors']?.['mastodon']) {
|
||||
const mastoUrl = new URL(item['authors']['mastodon'])
|
||||
|
@ -102,53 +101,11 @@ export const allContent = (collection) => {
|
|||
}
|
||||
}
|
||||
|
||||
addContent(posts, '📝', item => item['title'], item => item['date'])
|
||||
addContent(links, '🔗', item => item['title'], item => item['date'])
|
||||
addContent(books.all.filter(book => book['status'] === 'finished'), '📖', item => `${item['title']}${item['rating'] ? ' (' + item['rating'] + ')' : ''}`, item => item['date'])
|
||||
addContent(movies, '🎥', item => `${item['title']}${item['rating'] ? ' (' + item['rating'] + ')' : ''}`, item => item['lastWatched'])
|
||||
const addSiteMapContent = (items, getTitle, getDate) => {
|
||||
const addedUrls = new Set()
|
||||
|
||||
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 popularPosts = (collection) => {
|
||||
const collectionData = collection.getAll()[0]
|
||||
const { data } = collectionData
|
||||
const { posts, analytics } = data
|
||||
|
||||
return posts
|
||||
.filter((post) => {
|
||||
if (analytics.find((p) => p.page.includes(post.slug))) return true
|
||||
})
|
||||
.sort((a, b) => {
|
||||
const visitors = (page) => analytics.filter((p) => p.page.includes(page.slug)).pop()?.visitors
|
||||
return visitors(b) - visitors(a)
|
||||
})
|
||||
}
|
||||
|
||||
export const siteMap = (collection) => {
|
||||
const aggregateContent = []
|
||||
const collectionData = collection.getAll()[0]
|
||||
const { data } = collectionData
|
||||
const { posts, pages, artists, genres, movies, tv, books } = 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 addedUrls = new Set()
|
||||
|
||||
const addContent = (items, getTitle, getDate) => {
|
||||
if (items) {
|
||||
items.forEach(item => {
|
||||
items.forEach((item) => {
|
||||
let url
|
||||
if (item?.['url']) url = item['url']
|
||||
if (item?.['permalink']) url = item['permalink']
|
||||
|
@ -160,27 +117,44 @@ export const siteMap = (collection) => {
|
|||
title: getTitle(item),
|
||||
date: getDate ? parseDate(getDate(item)) : null
|
||||
}
|
||||
aggregateContent.push(content)
|
||||
siteMapContent.push(content)
|
||||
addedUrls.add(url)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
if (posts) addContent(posts, item => item.title, item => item.date)
|
||||
if (pages) addContent(pages, item => item.title, item => item.date)
|
||||
if (artists) addContent(artists, item => item.name, item => item.date)
|
||||
if (genres) addContent(genres, item => item.name, item => item.date)
|
||||
if (movies?.['movies']) addContent(movies['movies'], item => item.title, item => item.date)
|
||||
if (books?.['all']) addContent(books['all'], item => item.title, item => item.date)
|
||||
if (tv?.['shows']) addContent(tv['shows'], item => item.title, item => item.date)
|
||||
const movieData = movies['movies'].filter((movie) => movie['review']?.length && movie['rating'])
|
||||
const bookData = books.all.filter((book) => book['review']?.length && book['rating'])
|
||||
|
||||
collection.getAll().forEach(item => {
|
||||
if (item.data.pages) addContent(item.data.pages, item => item.title, item => item.date)
|
||||
})
|
||||
addItemToIndex(posts, '📝', (item) => new URL(item['slug'], BASE_URL).toString(), (item) => item['title'], (item) => item['tags'])
|
||||
addItemToIndex(links, '🔗', (item) => item['link'], (item) => item['title'], (item) => item['tags'])
|
||||
if (movieData) addItemToIndex(movieData, '🎥', (item) => item['url'], (item) => `${item['title']} (${item['rating']})`, (item) => item['tags'])
|
||||
if (bookData) addItemToIndex(bookData, '📖', (item) => item['url'], (item) => `${item['title']} (${item['rating']})`, (item) => item['tags'])
|
||||
|
||||
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
|
||||
})
|
||||
addContent(posts, '📝', (item) => item['title'], (item) => item['date'])
|
||||
addContent(links, '🔗', (item) => item['title'], (item) => item['date'])
|
||||
addContent(books.all.filter((book) => book['status'] === 'finished'), '📖', (item) => `${item['title']}${item['rating'] ? ' (' + item['rating'] + ')' : ''}`, (item) => item['date'])
|
||||
addContent(movies['movies'], '🎥', (item) => `${item['title']}${item['rating'] ? ' (' + item['rating'] + ')' : ''}`, (item) => item['lastWatched'])
|
||||
|
||||
addSiteMapContent(posts, (item) => item.title, (item) => item.date)
|
||||
addSiteMapContent(pages, (item) => item.title, (item) => item.date)
|
||||
addSiteMapContent(artists, (item) => item.name, (item) => item.date)
|
||||
addSiteMapContent(genres, (item) => item.name, (item) => item.date)
|
||||
addSiteMapContent(movies['movies'], (item) => item.title, (item) => item.date)
|
||||
addSiteMapContent(books.all, (item) => item.title, (item) => item.date)
|
||||
addSiteMapContent(tv?.['shows'], (item) => item.title, (item) => item.date)
|
||||
|
||||
return {
|
||||
searchIndex,
|
||||
allContent: 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
|
||||
}),
|
||||
siteMap: siteMapContent.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
|
||||
})
|
||||
}
|
||||
}
|
Reference in a new issue