once again change how blog-list is structured

This commit is contained in:
lightling 2024-06-26 17:31:32 -04:00
parent ca91d9fcb5
commit e4da36a23c
6 changed files with 100 additions and 56 deletions

View file

@ -1,11 +1,10 @@
import DOMPurify from 'dompurify'
import { marked } from 'marked'
import yaml from 'js-yaml'
import type {
EntryWithConfig,
EntryWithContent,
} from '@goldenwere/mackenzii-types/src/content/templates/shared'
ListEntry,
ListWithEntries,
} from '@goldenwere/mackenzii-types'
/**
* Config used for DOMPurify.
@ -129,33 +128,57 @@ export const fetchAndParseMarkdown = async (path: string) => {
}
/**
* Fetches content for an entry
* @param entry the entry whose content should be fetched
* @returns the markdown content for the entry
* Fetches all the entry configs from a given list
* @param list the list to fetch configs from
* @returns the resolved configs
*/
export const fetchContent = async (entry: EntryWithContent) => {
if (!!entry.content) {
return entry.content
} else if (!!entry.contentUrl) {
return fetchAndParseMarkdown(entry.contentUrl)
}
export const fetchConfigsFromList = async <T>(list: ListWithEntries<T>): Promise<{
ids: string[]
entries: ListEntry<T>
}> => {
return new Promise(async (resolve, reject) => {
let ids: string[] = []
let entries: ListEntry<T> = {}
if (!!list.entries) {
ids = ids.concat(Object.keys(list.entries))
const allEntries = await Promise.all(ids.map(async id => ({
entry: await fetchAndParseYaml<T>(list.entries[id]),
id,
})))
allEntries.forEach((entry) => {
entries[entry.id] = entry.entry
})
}
if (!!list.embeddedEntries) {
ids = ids.concat(Object.keys(list.embeddedEntries))
entries = {
...entries,
...list.embeddedEntries,
}
}
return ''
resolve({
entries,
ids,
})
})
}
/**
* Fetches config for an entry
* @param entry the entry whose config should be fetched
* @returns the config object for the entry
* Fetches the config for a given id from the given list
* @param list the list to query from
* @param id the id to query for
* @returns the resolved config
*/
export const fetchConfig = async <T>(entry: EntryWithConfig<T>) => {
if (!!entry.config) {
return entry.config as T
} else if (!!entry.configUrl) {
return fetchAndParseYaml(entry.configUrl) as T
}
return {} as T
export const fetchConfigByIdFromList = async <T>(list: ListWithEntries<T>, id: string): Promise<T> => {
return new Promise(async (resolve, reject) => {
if (!!list.embeddedEntries && list.embeddedEntries[id]) {
resolve(list.embeddedEntries[id])
} else if (!!list.entries && list.entries[id]) {
const config = await fetchAndParseYaml<T>(list.entries[id])
resolve(config)
}
})
}
/**

View file

@ -6,7 +6,7 @@ import type {
BlogListDefinition,
} from '@goldenwere/mackenzii-types'
import { fetchAndParseYaml, fetchConfig } from 'src/utilities/fetch'
import { fetchAndParseYaml, fetchConfigsFromList } from 'src/utilities/fetch'
import { getCurrentRoute } from 'src/utilities/vuetils'
import { useRouteStore } from 'src/routes'
@ -51,12 +51,11 @@ const viewPath = computed(() => `${currentRoute.path}/view`)
}
onMounted(async () => {
config.value = await fetchAndParseYaml<BlogList>(routeConfig.config)
entryIds.value = Object.keys(config.value.entries)
for (let i = 0; i < entryIds.value.length; ++i) {
const id = entryIds.value[i]
entries.value[id] = await fetchConfig(config.value.entries[id])
}
let listConfig = await fetchAndParseYaml<BlogList>(routeConfig.config)
const list = await fetchConfigsFromList<BlogEntry>(listConfig)
config.value = listConfig
entryIds.value = list.ids
entries.value = list.entries
document.title = routeConfig.fullTitle
ready.value = true
})

View file

@ -7,7 +7,7 @@ import type {
RoutedWindow,
} from '@goldenwere/mackenzii-types'
import { fetchAndParseYaml, fetchConfig, fetchContent } from 'src/utilities/fetch'
import { fetchAndParseMarkdown, fetchAndParseYaml, fetchConfigByIdFromList } from 'src/utilities/fetch'
import { getCurrentRoute } from 'src/utilities/vuetils'
import { useRouteStore } from 'src/routes'
@ -29,8 +29,8 @@ const routeSubConfig = routeStore._routes[currentRoute.path]
onMounted(async () => {
const config = await fetchAndParseYaml<BlogList>(routeConfig.config)
info.value = await fetchConfig(config.entries[currentRoute.query.id as string])
const md = await fetchContent(config.entries[currentRoute.query.id as string])
info.value = await fetchConfigByIdFromList(config, currentRoute.query.id as string)
const md = await fetchAndParseMarkdown(info.value.url)
content.value = md
document.title = routeSubConfig.fullTitle?.replace('$ENTRY', info.value.title)
routeStore.setBreadcrumbs(currentRoute, info.value.title)