diff --git a/src/components/Navbar.astro b/src/components/Navbar.astro index bb88dbc..d25efe3 100644 --- a/src/components/Navbar.astro +++ b/src/components/Navbar.astro @@ -12,7 +12,7 @@ const isTagActive = isTag const isAboutActive = isAbout function getNavItemClass(isActive: boolean) { - return isActive ? 'font-bold c-primary' : 'hover:c-primary hover:font-bold' + return isActive ? 'font-bold c-primary' : 'hover:(c-primary font-bold) transition-all' } const navItems = [ diff --git a/src/content/config.ts b/src/content/config.ts index 17387ee..cce7946 100644 --- a/src/content/config.ts +++ b/src/content/config.ts @@ -2,11 +2,13 @@ import { defineCollection, z } from 'astro:content' const postsCollection = defineCollection({ schema: z.object({ + // Basic title: z.string(), published: z.date(), updated: z.date().optional(), tags: z.array(z.string()).optional().default([]), + // Advanced pin: z.number().int().min(0).max(99).optional().default(0), draft: z.boolean().optional().default(false), @@ -16,6 +18,7 @@ const postsCollection = defineCollection({ abbrlink => !abbrlink || /^[a-z0-9\-]*$/.test(abbrlink), { message: 'Abbrlink can only contain lowercase letters, numbers and hyphens' }, ), + // Auto-generated description: z.string().optional().default(''), image: z.string().optional().default(''), diff --git a/src/utils/path.ts b/src/utils/path.ts index 7bf506a..700dca8 100644 --- a/src/utils/path.ts +++ b/src/utils/path.ts @@ -3,39 +3,47 @@ import themeConfig from '@/config' const defaultLocale = themeConfig.global.locale const moreLocales = themeConfig.global.moreLocale -// Get the language code from the path -export function getLangFromPath(path: string) { - const lang = path.split('/')[1] - return moreLocales.includes(lang) ? lang : defaultLocale +export function cleanPath(path: string) { + return path.replace(/^\/|\/$/g, '') } -// Get the localized path -export function getLocalizedPath(path: string) { - const pathWithoutSlashes = path.replace(/^\/+|\/+$/g, '') - const lang = getLangFromPath(path) +export function getLangFromPath(path: string) { + const secondaryLang = moreLocales.find( + lang => + path.startsWith(`/${lang}/`), + ) + return secondaryLang || defaultLocale +} - if (pathWithoutSlashes === '') { +export function getLocalizedPath(path: string, currentLang?: string) { + const clean = cleanPath(path) + const lang = currentLang || getLangFromPath(path) + + if (clean === '') { return lang === defaultLocale ? '/' : `/${lang}/` } - return lang === defaultLocale ? `/${pathWithoutSlashes}/` : `/${lang}/${pathWithoutSlashes}/` + return lang === defaultLocale ? `/${clean}/` : `/${lang}/${clean}/` } -// Checking the current path to the page export function isHomePage(path: string) { - return path === '/' || moreLocales.some(lang => path === `/${lang}/`) + const clean = cleanPath(path) + return clean === '' || moreLocales.includes(clean) } export function isPostPage(path: string) { - return path.includes('/posts/') + const clean = cleanPath(path) + return clean.startsWith('posts') || moreLocales.some(lang => clean.startsWith(`${lang}/posts`)) } export function isTagPage(path: string) { - return path.includes('/tags/') + const clean = cleanPath(path) + return clean.startsWith('tags') || moreLocales.some(lang => clean.startsWith(`${lang}/tags`)) } export function isAboutPage(path: string) { - return path.includes('/about/') + const clean = cleanPath(path) + return clean.startsWith('about') || moreLocales.some(lang => clean.startsWith(`${lang}/about`)) } export function getPagePath(path: string) { @@ -47,6 +55,6 @@ export function getPagePath(path: string) { isPost: isPostPage(path), isTag: isTagPage(path), isAbout: isAboutPage(path), - getLocalizedPath: (targetPath: string) => getLocalizedPath(targetPath), + getLocalizedPath: (targetPath: string) => getLocalizedPath(targetPath, currentLang), } }