mirror of
https://github.com/reonokiy/blog.nokiy.net.git
synced 2025-06-16 03:32:51 +02:00
fix: navbar path error
This commit is contained in:
parent
a2bf9ee3c5
commit
a133dfd7f2
3 changed files with 28 additions and 17 deletions
|
@ -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 = [
|
||||
|
|
|
@ -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(''),
|
||||
|
|
|
@ -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),
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue