vuejs / vitepress

Vite & Vue powered static site generator.
https://vitepress.dev
MIT License
12.62k stars 2.05k forks source link

The link in VPSidebarItem is forced to increase config.base #2207

Closed yangdan8 closed 1 year ago

yangdan8 commented 1 year ago

Describe the bug

The link in VPSidebarItem is forced to increase config.base. If a path starting with https://is used, it will open in a new page and cannot add an active class name to the current menu.

Reproduction

As described above

Expected behavior

Page is normal

System Info

window11,chrome112

Additional context

No response

Validations

yangdan8 commented 1 year ago

// VPSidebarItem.vue

  <VPLink v-if="item.link" :tag="linkTag" class="link" :href="item.link">
        <component :is="textTag" class="text" v-html="item.text" />
   </VPLink>

// VPLink.vue

<template>
  <component
    :is="tag"
    class="VPLink"
    :class="{ link: href }"
    :href="href ? normalizeLink(href) : undefined"
    :target="target || (isExternal ? '_blank' : undefined)"
    :rel="rel || (isExternal ? 'noreferrer' : undefined)"
  >
    <slot />
    <VPIconExternalLink v-if="isExternal && !noIcon" class="icon" />
  </component>
</template>

// support/utils.ts

export function normalizeLink(url: string): string {
  if (isExternal(url)) {
    return url.replace(PATHNAME_PROTOCOL_RE, '')
  }
  const { site } = useData()
  const { pathname, search, hash } = new URL(url, 'http://example.com')
  const normalizedPath =
    pathname.endsWith('/') || pathname.endsWith('.html')
      ? url
      : url.replace(
          /(?:(^\.+)\/)?.*$/,
          `$1${pathname.replace(
            /(\.md)?$/,
            site.value.cleanUrls ? '' : '.html'
          )}${search}${hash}`
        )
  return withBase(normalizedPath)
}

// app/utils.ts

export function withBase(path: string) {
  return EXTERNAL_URL_RE.test(path) || path.startsWith('.')
    ? path
    : joinPath(siteDataRef.value.base, path)
}
brc-dd commented 1 year ago

What's the expected behavior and use-case here? We treat urls having scheme as external. If that's internal then remove that scheme. And if it's external, then why should we add active class to that sidebar item?

yangdan8 commented 1 year ago

What's the expected behavior and use-case here? We treat urls having scheme as external. If that's internal then remove that scheme. And if it's external, then why should we add active class to that sidebar item?

My HTML file comes from a.xxx.com, while other files come from CDN, such as b.cdn.com

brc-dd commented 1 year ago

That still doesn't answer my question. What are you trying to do? Having any link relative to the domain should work fine. If you're using external link, that will open up in a new tab. How does CDN comes into play here and what base have you set?