fix: navbar path error

This commit is contained in:
radishzzz 2025-03-12 03:41:07 +00:00
parent a2bf9ee3c5
commit a133dfd7f2
3 changed files with 28 additions and 17 deletions

View file

@ -12,7 +12,7 @@ const isTagActive = isTag
const isAboutActive = isAbout const isAboutActive = isAbout
function getNavItemClass(isActive: boolean) { 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 = [ const navItems = [

View file

@ -2,11 +2,13 @@ import { defineCollection, z } from 'astro:content'
const postsCollection = defineCollection({ const postsCollection = defineCollection({
schema: z.object({ schema: z.object({
// Basic // Basic
title: z.string(), title: z.string(),
published: z.date(), published: z.date(),
updated: z.date().optional(), updated: z.date().optional(),
tags: z.array(z.string()).optional().default([]), tags: z.array(z.string()).optional().default([]),
// Advanced // Advanced
pin: z.number().int().min(0).max(99).optional().default(0), pin: z.number().int().min(0).max(99).optional().default(0),
draft: z.boolean().optional().default(false), draft: z.boolean().optional().default(false),
@ -16,6 +18,7 @@ const postsCollection = defineCollection({
abbrlink => !abbrlink || /^[a-z0-9\-]*$/.test(abbrlink), abbrlink => !abbrlink || /^[a-z0-9\-]*$/.test(abbrlink),
{ message: 'Abbrlink can only contain lowercase letters, numbers and hyphens' }, { message: 'Abbrlink can only contain lowercase letters, numbers and hyphens' },
), ),
// Auto-generated // Auto-generated
description: z.string().optional().default(''), description: z.string().optional().default(''),
image: z.string().optional().default(''), image: z.string().optional().default(''),

View file

@ -3,39 +3,47 @@ import themeConfig from '@/config'
const defaultLocale = themeConfig.global.locale const defaultLocale = themeConfig.global.locale
const moreLocales = themeConfig.global.moreLocale const moreLocales = themeConfig.global.moreLocale
// Get the language code from the path export function cleanPath(path: string) {
export function getLangFromPath(path: string) { return path.replace(/^\/|\/$/g, '')
const lang = path.split('/')[1]
return moreLocales.includes(lang) ? lang : defaultLocale
} }
// Get the localized path export function getLangFromPath(path: string) {
export function getLocalizedPath(path: string) { const secondaryLang = moreLocales.find(
const pathWithoutSlashes = path.replace(/^\/+|\/+$/g, '') lang =>
const lang = getLangFromPath(path) 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 ? '/' : `/${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) { 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) { 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) { 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) { 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) { export function getPagePath(path: string) {
@ -47,6 +55,6 @@ export function getPagePath(path: string) {
isPost: isPostPage(path), isPost: isPostPage(path),
isTag: isTagPage(path), isTag: isTagPage(path),
isAbout: isAboutPage(path), isAbout: isAboutPage(path),
getLocalizedPath: (targetPath: string) => getLocalizedPath(targetPath), getLocalizedPath: (targetPath: string) => getLocalizedPath(targetPath, currentLang),
} }
} }