diff --git a/package.json b/package.json index 17c7cb1bbb48..7fcc03c99bc6 100644 --- a/package.json +++ b/package.json @@ -100,6 +100,7 @@ "focus-trap": "^7.5.4", "mark.js": "8.11.1", "minisearch": "^6.2.0", + "mrmime": "^1.0.1", "shiki": "^0.14.5", "vite": "^5.0.0", "vue": "^3.3.8" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 69d322340f3b..d06e2f1136ac 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1,9 +1,5 @@ lockfileVersion: '6.0' -settings: - autoInstallPeers: false - excludeLinksFromLockfile: false - overrides: ora>string-width: ^5 @@ -41,6 +37,9 @@ importers: minisearch: specifier: ^6.2.0 version: 6.2.0 + mrmime: + specifier: ^1.0.1 + version: 1.0.1 shiki: specifier: ^0.14.5 version: 0.14.5 @@ -3325,7 +3324,6 @@ packages: /mrmime@1.0.1: resolution: {integrity: sha512-hzzEagAgDyoU1Q6yg5uI+AorQgdvMCur3FcKf7NhMKWsaYg+RnbTyHRa/9IlLF9rf455MOCtcqqrQQ83pPP7Uw==} engines: {node: '>=10'} - dev: true /ms@2.0.0: resolution: {integrity: sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A==} @@ -4717,3 +4715,7 @@ packages: resolution: {integrity: sha512-9bnSc/HEW2uRy67wc+T8UwauLuPJVn28jb+GtJY16iiKWyvmYJRXVT4UamsAEGQfPohgr2q4Tq0sQbQlxTfi1g==} engines: {node: '>=12.20'} dev: true + +settings: + autoInstallPeers: false + excludeLinksFromLockfile: false diff --git a/src/client/app/router.ts b/src/client/app/router.ts index c81a1de58097..f48af9645017 100644 --- a/src/client/app/router.ts +++ b/src/client/app/router.ts @@ -1,5 +1,6 @@ import { reactive, inject, markRaw, nextTick, readonly } from 'vue' import type { Component, InjectionKey } from 'vue' +import { lookup } from 'mrmime' import { notFoundPageData } from '../shared' import type { PageData, PageDataPayload, Awaitable } from '../shared' import { inBrowser, withBase } from './utils' @@ -181,7 +182,6 @@ export function createRouter( link.baseURI ) const currentUrl = window.location - const extMatch = pathname.match(/\.\w+$/) // only intercept inbound links if ( !e.ctrlKey && @@ -191,7 +191,9 @@ export function createRouter( !target && origin === currentUrl.origin && // don't intercept if non-html extension is present - !(extMatch && extMatch[0] !== '.html') + !(siteDataRef.value.cleanUrls + ? lookup(pathname) + : lookup(pathname) !== 'text/html') ) { e.preventDefault() if ( diff --git a/src/client/theme-default/support/utils.ts b/src/client/theme-default/support/utils.ts index 1cd168cdf1a8..f63e855a7700 100644 --- a/src/client/theme-default/support/utils.ts +++ b/src/client/theme-default/support/utils.ts @@ -1,4 +1,5 @@ import { withBase } from 'vitepress' +import { lookup } from 'mrmime' import { useData } from '../composables/data' import { isExternal } from '../../shared' @@ -27,7 +28,7 @@ export function normalizeLink(url: string): string { isExternal(url) || url.startsWith('#') || !protocol.startsWith('http') || - /\.(?!html|md)\w+($|\?)/i.test(url) + (/\.(?!html|md)\w+($|\?)/i.test(url) && lookup(url)) ) return url