Skip to content

Commit 5192340

Browse files
committed
feat: enhance routing metadata to include suffix option for titles
1 parent f71d0ac commit 5192340

3 files changed

Lines changed: 37 additions & 8 deletions

File tree

src/main.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@ export const createApp = ViteSSG(
2121
path: "/about",
2222
component: () => import("@views/AboutView.vue"),
2323
meta: {
24-
title: { default: false, composed: false, content: "À propos" },
24+
title: {
25+
default: false,
26+
composed: false,
27+
content: "À propos",
28+
withSuffix: false,
29+
},
2530
description: { default: true },
2631
dontPregenerate: false,
2732
} satisfies CustomRouteMetadata,
@@ -30,7 +35,12 @@ export const createApp = ViteSSG(
3035
path: "/projects",
3136
component: () => import("@views/ProjectsView.vue"),
3237
meta: {
33-
title: { default: false, composed: false, content: "Projets" },
38+
title: {
39+
default: false,
40+
composed: false,
41+
content: "Projets",
42+
withSuffix: false,
43+
},
3444
description: {
3545
default: false,
3646
composed: false,
@@ -51,6 +61,7 @@ export const createApp = ViteSSG(
5161
const params = args as { projectName: string }
5262
return `Projet "${params.projectName}"`
5363
},
64+
withSuffix: false,
5465
},
5566
description: {
5667
default: false,
@@ -67,7 +78,12 @@ export const createApp = ViteSSG(
6778
path: "/404",
6879
component: () => import("@views/NotFoundView.vue"),
6980
meta: {
70-
title: { default: false, composed: false, content: "Page non trouvée" },
81+
title: {
82+
default: false,
83+
composed: false,
84+
content: "Page non trouvée",
85+
withSuffix: false,
86+
},
7187
description: { default: true },
7288
dontPregenerate: false,
7389
} satisfies CustomRouteMetadata,

src/script/routing.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ const DEFAULTS = {
88
description: "Portfolio de Bastien Luben - Développeur",
99
}
1010

11+
const addSuffixIfNeeded = (title: string, withSuffix: boolean) => {
12+
return withSuffix ? `${title} - ${DEFAULTS.title}` : title
13+
}
14+
1115
export function usePageHead(args: unknown = {}) {
1216
const route = useRoute()
1317

@@ -18,8 +22,8 @@ export function usePageHead(args: unknown = {}) {
1822

1923
const t = meta.value.title
2024
if (t.default) return DEFAULTS.title
21-
if (t.composed) return `${t.content(args)} - ${DEFAULTS.title}`
22-
return `${t.content} - ${DEFAULTS.title}`
25+
else if (t.composed) return addSuffixIfNeeded(t.content(args), t.withSuffix)
26+
return addSuffixIfNeeded(t.content, t.withSuffix)
2327
})
2428

2529
const description = computed(() => {

src/types.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,16 @@ export interface ITimeline {
55
dateToString: string
66
}
77

8-
type ComposedOrNotComposed = { composed: true; content: (args: unknown) => string } | { composed: false; content: string }
9-
type DefaultOrCustom = { default: true } | ({ default: false } & ComposedOrNotComposed)
8+
type ComposedOrNotComposed<T extends object = object> =
9+
| ({ composed: true; content: (args: unknown) => string } & T)
10+
| ({ composed: false; content: string } & T)
1011

11-
export type CustomRouteMetadata = { title: DefaultOrCustom, description: DefaultOrCustom, dontPregenerate: boolean }
12+
type ComposedOrNotComposedAndWithWithoutSuffix = ComposedOrNotComposed<{ withSuffix: boolean }>
13+
14+
type DefaultOrCustom<T extends ComposedOrNotComposed> = { default: true } | ({ default: false } & T)
15+
16+
export type CustomRouteMetadata = {
17+
title: DefaultOrCustom<ComposedOrNotComposedAndWithWithoutSuffix>
18+
description: DefaultOrCustom<ComposedOrNotComposed>
19+
dontPregenerate: boolean
20+
}

0 commit comments

Comments
 (0)