fix internal paths

This commit is contained in:
kakkokari-gtyih 2023-11-26 01:48:16 +09:00
parent e2a753bef5
commit c812abe139
6 changed files with 25 additions and 8 deletions

View File

@ -43,6 +43,11 @@ export function isLocalPath(link: string, base?: string): boolean {
return (!url.host || rootDomain.host === url.host);
}
export function sanitizeInternalPath(path: string): string {
const runtimeConfig = useRuntimeConfig();
return path.replace(new RegExp(`^(\/(${runtimeConfig.public.locales.map((l) => l.code).join('|')})\/?){2,}(.*)$`, 'g'), '$1$2');
}
/**
* Objectを合致する条件まで深掘り
* @param obj Object

View File

@ -1,6 +1,7 @@
<script setup lang="ts">
import ExtIco from 'bi/box-arrow-up-right.svg';
import { $URL, isRelative, joinURL } from 'ufo';
import { isLocalPath, sanitizeInternalPath } from '@/assets/js/misc';
const runtimeConfig = useRuntimeConfig();
const rootDomain = new $URL(runtimeConfig.public.baseUrl);
@ -23,12 +24,12 @@ const realHref = ref(props.href);
const realTarget = ref(props.target);
const url = new $URL(realHref.value);
if (url.host === '' || rootDomain.host === url.host) {
if (isLocalPath(realHref.value)) {
//
const route = resolve(realHref.value);
if (route.name && !route.name.toString().includes('___')) {
//
realHref.value = localePath(url.fullpath);
realHref.value = sanitizeInternalPath(localePath(url.fullpath));
}
// trailing slash

View File

@ -9,7 +9,7 @@
<script setup lang="ts">
import { cleanDoubleSlashes, withTrailingSlash } from 'ufo';
import { isLocalPath } from '@/assets/js/misc';
import { isLocalPath, sanitizeInternalPath } from '@/assets/js/misc';
import type { RouteLocationRaw } from '#vue-router';
/**
@ -29,7 +29,7 @@ const realHref = computed(() => {
if (rhf && typeof rhf === 'string') {
if (isLocalPath(rhf)) {
return withTrailingSlash(cleanDoubleSlashes(rhf), true);
return withTrailingSlash(cleanDoubleSlashes(sanitizeInternalPath(rhf)), true);
}
return rhf;

View File

@ -8,8 +8,19 @@ useHead({
},
});
const route = useRoute();
const { locale } = useI18n();
const { data: navigation } = await useAsyncData(`navigation_${locale.value}`, () => fetchContentNavigation(queryContent(`/${locale.value}/docs/`)));
const navigation = ref();
const asideNavKey = ref(0);
const { data } = await useAsyncData(`navigation_${locale.value}`, () => fetchContentNavigation(queryContent(`/${locale.value}/docs/`)));
navigation.value = data.value;
watch(locale, async (to) => {
console.log('locale changed');
const { data } = await useAsyncData(`navigation_${to}`, () => fetchContentNavigation(queryContent(`/${to}/docs/`)));
navigation.value = data.value;
asideNavKey.value++;
});
</script>
<template>
@ -22,7 +33,7 @@ const { data: navigation } = await useAsyncData(`navigation_${locale.value}`, ()
:class="isAsideNavOpen ? 'translate-x-0' : '-translate-x-64'"
>
<div class="lg:sticky lg:top-16 h-[calc(100vh-7.25rem)] lg:h-[calc(100vh-4rem)] overflow-y-scroll border-r border-slate-200 dark:border-slate-700 py-6 pr-3">
<DocsAsideNav :links="navigation ?? []" />
<DocsAsideNav :links="navigation ?? []" :key="asideNavKey" />
</div>
</div>
<div class="relative">

View File

@ -29,9 +29,9 @@ export const locales = localesConst as unknown as LocaleObject[];
export default defineNuxtConfig({
runtimeConfig: {
locales,
public: {
baseUrl,
locales,
}
},
css: [

View File

@ -14,7 +14,7 @@ export default defineNitroPlugin((nitroApp) => {
}
const runtimeConfig: MiHubRuntimeConfig = useRuntimeConfig();
if (!event.path.match(new RegExp(`^/(${runtimeConfig.locales.map((l) => l.code).join('|')})/`))) {
if (!event.path.match(new RegExp(`^/(${runtimeConfig.public.locales.map((l) => l.code).join('|')})/`))) {
const links = runtimeConfig.locales.map((l) => {
const url = withTrailingSlash(`/${l.code}${event.path.replace(/\.html$/g, '/')}`);
return `<a href="${url}">${l.name}</a>`;