Vue
Vite + Vue with the Content instance plugin and content HMR.
Setting up your own app? Follow the Vite integration guide step by step.
App.vue
<script lang="ts" setup>
import { ref, onMounted, onUnmounted, watch } from 'vue'
import { MarkdownDocument } from '@comark/vue'
import type { ContentFile } from 'comark-content'
import { content, version } from './composables/useContent'
type Item = Awaited<ReturnType<typeof content.list>>[number]
const path = ref(window.location.pathname)
const items = ref<Item[]>([])
const page = ref<ContentFile | null>(null)
const pending = ref(true)
const error = ref<string | null>(null)
function navigate(e: MouseEvent, to: string) {
e.preventDefault()
if (to === path.value) return
history.pushState({}, '', to)
path.value = to
}
function syncPath() {
path.value = window.location.pathname
}
onMounted(() => window.addEventListener('popstate', syncPath))
onUnmounted(() => window.removeEventListener('popstate', syncPath))
let token = 0
async function load() {
const myToken = ++token
pending.value = true
error.value = null
try {
const [tr, list] = await Promise.all([content.get(path.value), content.list()])
if (myToken !== token) return
page.value = tr ?? null
items.value = list
if (!tr) error.value = 'Not found'
} catch (err) {
if (myToken !== token) return
error.value = (err as Error).message
} finally {
if (myToken === token) pending.value = false
}
}
watch([path, version], load, { immediate: true })
</script>
<template>
<div class="layout">
<aside>
<h2>Pages</h2>
<nav>
<a
v-for="item in items"
:key="item.path"
:href="item.path"
:class="{ active: path === item.path }"
@click="navigate($event, item.path)"
>
{{ item.data?.title ?? item.meta.stem }}
</a>
</nav>
</aside>
<main>
<p v-if="pending">Loading…</p>
<Suspense v-else-if="page">
<MarkdownDocument :value="page" />
<template #fallback><p>Rendering…</p></template>
</Suspense>
<p v-else>{{ error ?? 'Not found' }}</p>
</main>
</div>
</template>
<style>
:root {
font-family: system-ui, sans-serif;
color-scheme: light dark;
}
body {
margin: 0;
}
.layout {
display: grid;
grid-template-columns: 220px 1fr;
min-height: 100vh;
}
aside {
padding: 1.5rem 1rem;
border-right: 1px solid #8884;
}
aside h2 {
font-size: 0.8rem;
text-transform: uppercase;
letter-spacing: 0.08em;
opacity: 0.6;
}
aside nav {
display: flex;
flex-direction: column;
gap: 0.25rem;
}
aside a {
padding: 0.25rem 0.5rem;
border-radius: 4px;
text-decoration: none;
color: inherit;
cursor: pointer;
}
aside a.active {
background: #8882;
}
main {
padding: 2rem 2.5rem;
max-width: 70ch;
}
</style>