clean urls in next/previous and sidebar

pull/869/head
Georges Gomes 3 years ago
parent c7c4d1a782
commit fb3069fe25

@ -8,7 +8,7 @@ import VPIconEdit from './icons/VPIconEdit.vue'
import VPLink from './VPLink.vue'
import VPDocFooterLastUpdated from './VPDocFooterLastUpdated.vue'
const { theme, page, frontmatter } = useData()
const { site, theme, page, frontmatter } = useData()
const editLink = useEditLink()
const control = usePrevNext()
@ -35,13 +35,13 @@ const hasLastUpdated = computed(() => {
<div v-if="control.prev || control.next" class="prev-next">
<div class="pager">
<a v-if="control.prev" class="pager-link prev" :href="normalizeLink(control.prev.link)">
<a v-if="control.prev" class="pager-link prev" :href="normalizeLink(control.prev.link, site.cleanUrls)">
<span class="desc">Previous page</span>
<span class="title">{{ control.prev.text }} </span>
</a>
</div>
<div class="pager" :class="{ 'has-prev': control.prev }">
<a v-if="control.next" class="pager-link next" :href="normalizeLink(control.next.link)">
<a v-if="control.next" class="pager-link next" :href="normalizeLink(control.next.link, site.cleanUrls)">
<span class="desc">Next page</span>
<span class="title">{{ control.next.text }}</span>
</a>

@ -1,8 +1,11 @@
<script lang="ts" setup>
import { computed } from 'vue'
import { useData } from 'vitepress'
import { normalizeLink } from '../support/utils'
import VPIconExternalLink from './icons/VPIconExternalLink.vue'
const { site } = useData()
const props = defineProps<{
href?: string
noIcon?: boolean
@ -16,7 +19,7 @@ const isExternal = computed(() => props.href && /^[a-z]+:/i.test(props.href))
:is="href ? 'a' : 'span'"
class="VPLink"
:class="{ link: href }"
:href="href ? normalizeLink(href) : undefined"
:href="href ? normalizeLink(href, site.cleanUrls) : undefined"
:target="isExternal ? '_blank' : undefined"
:rel="isExternal ? 'noopener noreferrer' : undefined"
>

@ -69,17 +69,24 @@ export function normalize(path: string): string {
return decodeURI(path).replace(HASH_RE, '').replace(EXT_RE, '')
}
export function normalizeLink(url: string): string {
export function normalizeLink(url: string, cleanUrls: boolean = false): string {
if (isExternal(url)) {
return url
}
const { pathname, search, hash } = new URL(url, 'http://example.com')
const normalizedPath =
pathname.endsWith('/') || pathname.endsWith('.html')
? url
: `${pathname.replace(/(\.md)?$/, '.html')}${search}${hash}`
let normalizedPath = url
if (cleanUrls) {
normalizedPath = `${pathname.replace(/(\.md)?$/, '')}${search}${hash}`
} else {
if (!pathname.endsWith('/') && !pathname.endsWith('.html')) {
normalizedPath = `${pathname.replace(
/(\.md)?$/,
'.html'
)}${search}${hash}`
}
}
return withBase(normalizedPath)
}

Loading…
Cancel
Save