Skip to content

Commit

Permalink
feat(theme): improve link parsing (#382)
Browse files Browse the repository at this point in the history
* feat(theme): improve link parsing

* chore: tweak
  • Loading branch information
pengzhanbo authored Dec 21, 2024
1 parent 164e884 commit 055bf61
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions theme/src/client/composables/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { computed, type MaybeRefOrGetter, toValue } from 'vue'
import { resolveRouteFullPath, useRoute } from 'vuepress/client'
import { useData } from './data.js'

const SEARCH_RE = /\.md(?:(?:#|\?).*)?$/
const ENDING_SLASH = /(?:\/|\.(?:md|html))$/i

export function useLink(
href: MaybeRefOrGetter<string | undefined>,
Expand All @@ -12,23 +12,25 @@ export function useLink(
const route = useRoute()
const { page } = useData()

const isExternal = computed(
() => {
const link = toValue(href)
const rawTarget = toValue(target)
return (link && isLinkExternal(link)) || rawTarget === '_blank'
},
)
const isExternal = computed(() => {
const link = toValue(href)
const rawTarget = toValue(target)
if (!link)
return false
if (rawTarget === '_blank' || isLinkExternal(link))
return true
const pathname = link.split(/[#?]/)[0]
return !ENDING_SLASH.test(pathname)
})

const link = computed(() => {
const link = toValue(href)
if (!link)
return undefined
if (isExternal.value)
return link
const currentPath = link.startsWith('./') && SEARCH_RE.test(link)
? `/${page.value.filePathRelative!}`
: route.path

const currentPath = page.value.filePathRelative ? `/${page.value.filePathRelative}` : undefined
const path = resolveRouteFullPath(link, currentPath)
if (path.includes('#')) {
if (path.slice(0, path.indexOf('#')) === route.path) {
Expand Down

0 comments on commit 055bf61

Please sign in to comment.