Skip to content

Commit

Permalink
feat: breadcrumbs
Browse files Browse the repository at this point in the history
  • Loading branch information
holloway committed Nov 25, 2024
1 parent 4402178 commit e509f7d
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 22 deletions.
48 changes: 48 additions & 0 deletions client/components/ContentBreadcrumbs.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<template>
<Breadcrumbs :breadcrumb-items="breadcrumbItems" />
</template>

<script setup lang="ts">
import type { NavItem } from '@nuxt/content'
import type { BreadcrumbItem } from './BreadcrumbsTypes'
// import type { BreadcrumbItem } from '~/components/BreadcrumbsTypes'
const { data: navigation } = await useAsyncData('navigation', () =>
fetchContentNavigation()
)
const route = useRoute()
function shallowNavitems(navItems: NavItem[] | null | undefined): NavItem[] {
if (!navItems) return []
return [
...navItems,
...navItems.map((navItem) => shallowNavitems(navItem.children))
].flat(10)
}
function navItemToBreadcrumbItem(navItem: NavItem): BreadcrumbItem {
return {
url: navItem._path,
label: navItem.title
}
}
const breadcrumbItems = shallowNavitems(navigation.value)
.filter((item) => route.path.substring(0, item._path.length) === item._path)
.sort((a, b) => {
return a._path.length - b._path.length
})
.map(navItemToBreadcrumbItem)
// function navigationToBreadcrumbs(navigation: Navigation): BreadcrumbItem[] {
// return navigation.map(
// (item): BreadcrumbItem => ({
// url: item._path,
// label: item.title
// })
// )
// }
</script>
4 changes: 4 additions & 0 deletions client/content/about.md
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
---
title: About
---

# Hello Content
4 changes: 4 additions & 0 deletions client/content/about/subpub_stats.md
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
---
title: Subpub Statistics
---

# Subpub Stats
5 changes: 5 additions & 0 deletions client/content/test.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: Test Is Not In Path
---

# my title
20 changes: 3 additions & 17 deletions client/pages/[...slug].vue
Original file line number Diff line number Diff line change
@@ -1,31 +1,17 @@
<template>
<div class="min-h-[100vh]">
<div class="container mx-auto">
<Breadcrumbs :breadcrumb-items="breadcrumbItems" />
<ContentBreadcrumbs />
<ContentDoc />
<ContentDocLastUpdated />
</div>
</div>
</template>

<script setup lang="ts">
import { startCase } from 'lodash-es'
import _contentMetadata from '../content-metadata.json'
import type { BreadcrumbItem } from '~/components/BreadcrumbsTypes'
const route = useRoute()
const { data: page } = await useAsyncData('my-page', queryContent('/').findOne)
const pathParts = route.path.substring(1).split('/')
// const { data: navigation } = await useAsyncData('navigation', () =>
// fetchContentNavigation()
// )
const breadcrumbItems: BreadcrumbItem[] = [
{ url: '/', label: 'Home' },
...pathParts.map((pathPart, index, arr) => ({
url: `/${arr.slice(0, index + 1).join('/')}`,
label: startCase(pathPart)
}))
]
useContentHead(page)
</script>
11 changes: 6 additions & 5 deletions client/scripts/generate-content-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,32 @@ export type ContentMetadata = Record<
string, // path within content directory
{
mtime: string // timestamp ISO 8601
}
} | undefined
>

const markdownMetadataArray = await Promise.all(
contentMarkdownPaths.map(
(contentMarkdownPath) =>
new Promise<ContentMetadata>((resolve, reject) => {
new Promise<ContentMetadata>((resolve) => {
git
.log({
file: contentMarkdownPath,
maxCount: 1,
strictDate: true
})
.then((gitLog) => {
if (gitLog.latest?.date) {
const relativePath = contentMarkdownPath
const relativePath = contentMarkdownPath
.substring(contentPath.length)
.replace(/\.md$/, '')
if (gitLog.latest?.date) {
resolve({
[relativePath]: { mtime: gitLog.latest?.date }
})
} else {
reject(
console.warn(
`Unable to extract latest Git log time from path ${contentMarkdownPath}`
)
resolve({ [relativePath]: undefined })
}
})
})
Expand Down

0 comments on commit e509f7d

Please sign in to comment.