handle storing route config

This commit is contained in:
lightling 2024-03-12 00:31:19 -04:00
parent dca70d7028
commit 6e4be6ca3e
6 changed files with 57 additions and 10 deletions

1
.gitignore vendored
View file

@ -11,6 +11,7 @@ node_modules
dist
dist-ssr
*.local
.vite-ssg-temp
/content
# Editor directories and files

View file

@ -2,10 +2,12 @@ declare module 'content/routes.js' {
type Template =
| 'markdown'
type Routes = { [key: string]: {
type RouteDefinition = {
id: string
template: Template
}}
}
const routes: Routes
type RouteCollection = { [key: string]: RouteDefinition }
const routes: RouteCollection
}

View file

@ -1,7 +1,10 @@
import { type RouteRecordRaw } from 'vue-router'
import { ViteSSG } from 'vite-ssg'
import './style.css'
import main from './main.vue'
import { createRoutes } from './routes'
import { createPinia } from 'pinia'
import { createRoutes, useRouteStore } from './routes'
import { routes as appRoutes, type RouteDefinition } from 'content/routes.js'
export const createApp = ViteSSG(
// the root component
@ -9,7 +12,14 @@ export const createApp = ViteSSG(
// vue-router options
{ routes: createRoutes() },
// function to have custom setups
// ({ app, router, routes, isClient, initialState }) => {
// // install plugins etc.
// },
({ app, router, routes, isClient, initialState }) => {
app.use(createPinia())
const routeStore = useRouteStore()
Object.keys(appRoutes).forEach(route =>
routeStore._routes[route] = {
...routes.find(other => other.path === route) as RouteRecordRaw,
...appRoutes[route] as RouteDefinition,
}
)
},
)

View file

@ -1,5 +1,6 @@
import { defineStore } from 'pinia'
import { type RouteRecordRaw } from 'vue-router'
import { routes, type Template } from 'content/routes.js'
import { routes, type RouteDefinition, type Template } from 'content/routes.js'
const markdownBody = () => import ('./views/markdown.vue')
@ -20,3 +21,18 @@ export const createRoutes = (): RouteRecordRaw[] => {
return routeRecord
}
export const useRouteStore = defineStore('routeStore', {
state: () => ({
_routes: {} as Record<string, RouteRecordRaw & RouteDefinition>,
}),
actions: {
},
})
export type RouteStoreDefinition = Omit<
ReturnType<typeof useRouteStore>,
keyof ReturnType<typeof defineStore>
>

18
src/views/markdown.vue Normal file
View file

@ -0,0 +1,18 @@
<script setup lang="ts">
import { useRoute } from 'vue-router'
import { useRouteStore } from 'src/routes'
const currentRoute = useRoute()
const routeStore = useRouteStore()
console.log(routeStore._routes)
console.log(routeStore._routes[currentRoute.path])
</script>
<template lang="pug">
p Marked works
</template>
<style scoped lang="sass">
</style>

View file

@ -22,8 +22,8 @@
/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noUnusedLocals": false,
"noUnusedParameters": false,
"noFallthroughCasesInSwitch": true
},
"include": ["src/**/*.ts", "src/**/*.tsx", "src/**/*.vue"],