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

@ -1,8 +1,11 @@
<script lang="ts" setup> <script lang="ts" setup>
import { computed } from 'vue' import { computed } from 'vue'
import { useData } from 'vitepress'
import { normalizeLink } from '../support/utils' import { normalizeLink } from '../support/utils'
import VPIconExternalLink from './icons/VPIconExternalLink.vue' import VPIconExternalLink from './icons/VPIconExternalLink.vue'
const { site } = useData()
const props = defineProps<{ const props = defineProps<{
href?: string href?: string
noIcon?: boolean noIcon?: boolean
@ -16,7 +19,7 @@ const isExternal = computed(() => props.href && /^[a-z]+:/i.test(props.href))
:is="href ? 'a' : 'span'" :is="href ? 'a' : 'span'"
class="VPLink" class="VPLink"
:class="{ link: href }" :class="{ link: href }"
:href="href ? normalizeLink(href) : undefined" :href="href ? normalizeLink(href, site.cleanUrls) : undefined"
:target="isExternal ? '_blank' : undefined" :target="isExternal ? '_blank' : undefined"
:rel="isExternal ? 'noopener noreferrer' : 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, '') 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)) { if (isExternal(url)) {
return url return url
} }
const { pathname, search, hash } = new URL(url, 'http://example.com') const { pathname, search, hash } = new URL(url, 'http://example.com')
const normalizedPath = let normalizedPath = url
pathname.endsWith('/') || pathname.endsWith('.html') if (cleanUrls) {
? url normalizedPath = `${pathname.replace(/(\.md)?$/, '')}${search}${hash}`
: `${pathname.replace(/(\.md)?$/, '.html')}${search}${hash}` } else {
if (!pathname.endsWith('/') && !pathname.endsWith('.html')) {
normalizedPath = `${pathname.replace(
/(\.md)?$/,
'.html'
)}${search}${hash}`
}
}
return withBase(normalizedPath) return withBase(normalizedPath)
} }

Loading…
Cancel
Save