fix: encode caused by vue-router hash, close #285

This commit is contained in:
YunYouJun 2023-10-29 00:12:55 +08:00
parent e717b89ee7
commit a1a2590828
11 changed files with 334 additions and 290 deletions

View File

@ -25,7 +25,7 @@
},
"devDependencies": {
"nodemon": "^3.0.1",
"vite-plugin-inspect": "^0.7.40",
"vite-plugin-pwa": "^0.16.5"
"vite-plugin-inspect": "^0.7.41",
"vite-plugin-pwa": "^0.16.6"
}
}

View File

@ -20,10 +20,10 @@
"valaxy-theme-press": "link:../packages/valaxy-theme-press"
},
"devDependencies": {
"@iconify-json/simple-icons": "^1.1.75",
"@iconify-json/simple-icons": "^1.1.76",
"nodemon": "^3.0.1",
"vite": "^4.5.0",
"vitepress": "1.0.0-rc.23",
"vue": "^3.3.6"
"vue": "^3.3.7"
}
}

View File

@ -3,7 +3,7 @@
"type": "module",
"version": "0.15.10",
"private": true,
"packageManager": "pnpm@8.9.2",
"packageManager": "pnpm@8.10.0",
"description": "📄 Vite & Vue powered static blog generator.",
"author": {
"email": "me@yunyoujun.cn",
@ -50,18 +50,18 @@
"devDependencies": {
"@antfu/eslint-config": "1.0.0-beta.27",
"@iconify-json/logos": "^1.1.37",
"@iconify-json/vscode-icons": "^1.1.28",
"@iconify-json/vscode-icons": "^1.1.29",
"@microsoft/api-extractor": "^7.38.0",
"@types/debug": "^4.1.10",
"@types/markdown-it-attrs": "^4.1.2",
"@types/markdown-it-container": "^2.0.8",
"@types/markdown-it-emoji": "^2.0.3",
"@types/node": "^20.8.7",
"@types/node": "^20.8.9",
"@types/prompts": "^2.4.7",
"@types/resolve": "^1.20.4",
"@types/semver": "^7.5.4",
"cross-env": "^7.0.3",
"cypress": "^13.3.2",
"cypress": "^13.3.3",
"eslint": "^8.52.0",
"eslint-plugin-cypress": "^2.15.1",
"https-localhost": "^4.7.1",

View File

@ -1 +1 @@
export const targetPadding = -40
export const targetPadding = 0

View File

@ -13,8 +13,14 @@ const { locale } = useI18n()
<template>
<ul :class="root ? 'root' : 'nested'" class="va-toc css-i18n-toc">
<li v-for="{ children, link, title, lang } in headers" :key="link" class="va-toc-item" :lang="lang || locale">
<a class="outline-link" :href="link" @click="onClick">{{ title }}</a>
<li
v-for="{ children, link, title, lang } in headers"
:key="link" class="va-toc-item"
:lang="lang || locale"
>
<RouterLink class="outline-link" :to="link" @click="onClick">
{{ title }}
</RouterLink>
<template v-if="children?.length">
<PressOutlineItem :headers="children" :on-click="onClick" />
</template>

View File

@ -12,57 +12,11 @@ import 'vitepress/dist/client/theme-default/styles/components/custom-block.css'
// import 'vitepress/dist/client/theme-default/styles/components/vp-sponsor.css'
import { targetPadding } from '../client'
export default defineAppSetup((ctx) => {
const { router, isClient } = ctx
if (!isClient)
return
window.addEventListener(
'click',
async (e) => {
const link = (e.target as Element).closest('a')
if (link) {
const { protocol, hostname, pathname, hash, target } = link
const currentUrl = window.location
const extMatch = pathname.match(/\.\w+$/)
// only intercept inbound links
if (
!e.ctrlKey
&& !e.shiftKey
&& !e.altKey
&& !e.metaKey
&& target !== '_blank'
&& protocol === currentUrl.protocol
&& hostname === currentUrl.hostname
&& !(extMatch && extMatch[0] !== '.html')
) {
if (pathname === currentUrl.pathname) {
e.preventDefault()
// scroll between hash anchors in the same page
if (hash && hash !== currentUrl.hash) {
await router.push({ hash })
history.replaceState({ ...history.state }, '')
// still emit the event so we can listen to it in themes
window.dispatchEvent(new Event('hashchange'))
// use smooth scroll when clicking on header anchor links
scrollTo(link, hash, {
smooth: link.classList.contains('header-anchor'),
})
}
}
}
}
},
{ capture: true },
)
window.addEventListener('hashchange', (e) => {
e.preventDefault()
})
router.beforeEach((to, from) => {
if (to.path !== from.path)
return
@ -70,7 +24,6 @@ export default defineAppSetup((ctx) => {
nextTick(() => {
scrollTo(document.body, to.hash, {
smooth: true,
targetPadding,
})
})
})

View File

@ -56,11 +56,8 @@ onContentUpdated(() => {
e.preventDefault()
// scroll between hash anchors in the same page
if (hash && hash !== currentUrl.hash) {
await router.push({ hash })
history.replaceState({ ...history.state }, '')
await router.push({ hash: decodeURIComponent(hash) })
// still emit the event so we can listen to it in themes
window.dispatchEvent(new Event('hashchange'))
// use smooth scroll when clicking on header anchor links
scrollTo(link, hash, link.classList.contains('header-anchor'))
}

View File

@ -19,7 +19,7 @@
"dependencies": {
"@explosions/fireworks": "^0.0.2",
"@iconify-json/ant-design": "^1.1.10",
"@iconify-json/simple-icons": "^1.1.75",
"@iconify-json/simple-icons": "^1.1.76",
"animejs": "^3.2.1"
},
"devDependencies": {

View File

@ -65,7 +65,7 @@ useVanillaLazyLoad()
<slot v-if="frontmatter.end !== undefined" name="end">
<div m="y-4" class="end flex justify-center items-center">
<hr class="line inline-flex" w="full" m="!y-2">
<span p="x-4" font="serif bold" class="whitespace-nowrap">
<span p="x-4" font="bold" class="whitespace-nowrap">
{{ frontmatter.end ? 'Q.E.D.' : 'To Be Continued.' }}
</span>
<hr class="line inline-flex" w="full" m="!y-2">

View File

@ -65,9 +65,9 @@
"@iconify-json/ri": "^1.1.12",
"@intlify/unplugin-vue-i18n": "^1.4.0",
"@types/body-scroll-lock": "^3.1.1",
"@unhead/addons": "^1.7.4",
"@unhead/schema-org": "^1.7.4",
"@unhead/vue": "^1.7.4",
"@unhead/addons": "^1.8.0",
"@unhead/schema-org": "^1.8.0",
"@unhead/vue": "^1.8.0",
"@vitejs/plugin-vue": "^4.4.0",
"@vueuse/core": "^10.5.0",
"@vueuse/integrations": "^10.5.0",
@ -77,7 +77,7 @@
"cross-spawn": "^7.0.3",
"css-i18n": "^0.0.2",
"dayjs": "^1.11.10",
"defu": "^6.1.2",
"defu": "^6.1.3",
"ejs": "^3.1.9",
"escape-html": "^1.0.3",
"feed": "^4.2.2",
@ -106,7 +106,7 @@
"pascalcase": "^2.0.0",
"pinia": "^2.1.7",
"qrcode": "^1.5.3",
"sass": "^1.69.4",
"sass": "^1.69.5",
"shiki": "^0.14.5",
"star-markdown-css": "^0.4.2",
"unconfig": "^0.3.11",
@ -118,8 +118,8 @@
"vite-plugin-vue-layouts": "^0.8.0",
"vite-ssg": "0.23.3",
"vite-ssg-sitemap": "0.6.0",
"vue": "^3.3.6",
"vue-i18n": "^9.5.0",
"vue": "^3.3.7",
"vue-i18n": "^9.6.1",
"vue-router": "^4.2.5",
"yargs": "^17.7.2"
},
@ -135,7 +135,7 @@
"@types/ejs": "^3.1.4",
"@types/html-to-text": "^9.0.3",
"@types/katex": "^0.16.5",
"@types/markdown-it": "^13.0.4",
"@types/markdown-it": "^13.0.5",
"@types/nprogress": "^0.2.2",
"@types/pascalcase": "^1.0.2",
"@types/qrcode": "^1.5.4",

File diff suppressed because it is too large Load Diff