Skip to content

Commit 71c1d88

Browse files
committed
chore: tweaks
1 parent fdc75f6 commit 71c1d88

File tree

6 files changed

+90
-11
lines changed

6 files changed

+90
-11
lines changed

e2e/docs/components/auto-link.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# AutoLink
2+
3+
<div id="route-link">
4+
<AutoLink v-for="item in routeLinksConfig" :config="item" />
5+
</div>
6+
7+
<div id="anchor-link">
8+
<AutoLink v-for="item in anchorLinksConfig" :config="item" />
9+
</div>
10+
11+
<div id="aria-label">
12+
<AutoLink :config="{ text: 'text', link: '/', ariaLabel: 'label' }" />
13+
</div>
14+
15+
<script setup lang="ts">
16+
import { AutoLink } from 'vuepress/client'
17+
18+
const routeLinks = [
19+
'/',
20+
'/README.md',
21+
'/index.html',
22+
'/non-existent',
23+
'/non-existent.md',
24+
'/non-existent.html',
25+
'/routes/non-ascii-paths/中文目录名/中文文件名',
26+
'/routes/non-ascii-paths/中文目录名/中文文件名.md',
27+
'/routes/non-ascii-paths/中文目录名/中文文件名.html',
28+
'/README.md#hash',
29+
'/README.md?query',
30+
'/README.md?query#hash',
31+
'/#hash',
32+
'/?query',
33+
'/?query#hash',
34+
'#hash',
35+
'?query',
36+
'?query#hash',
37+
'route-link',
38+
'route-link.md',
39+
'route-link.html',
40+
'not-existent',
41+
'not-existent.md',
42+
'not-existent.html',
43+
'../',
44+
'../README.md',
45+
'../404.md',
46+
'../404.html',
47+
]
48+
49+
const routeLinksConfig = routeLinks.map((link) => ({ link, text: 'text' }))
50+
51+
const anchorLinks = [
52+
'//example.com',
53+
'http://example.com',
54+
'https://example.com',
55+
56+
'tel:+1234567890',
57+
]
58+
59+
const anchorLinksConfig = anchorLinks.map((link) => ({ link, text: 'text' }))
60+
</script>

e2e/tests/components/auto-link.cy.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
it('AutoLink', () => {
2+
cy.visit('/components/auto-link.html')
3+
4+
cy.get(`.e2e-theme-content #route-link a`).each((el) => {
5+
cy.wrap(el)
6+
.should('have.attr', 'class')
7+
.and('match', /route-link/)
8+
})
9+
10+
cy.get(`.e2e-theme-content #anchor-link a`).each((el) => {
11+
cy.wrap(el)
12+
.should('have.attr', 'class')
13+
.and('match', /anchor-link/)
14+
})
15+
16+
cy.get(`.e2e-theme-content #aria-label a`).each((el) => {
17+
cy.wrap(el).should('have.attr', 'aria-label').and('eq', 'label')
18+
})
19+
})

e2e/tests/components/route-link.cy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ it('RouteLink', () => {
7878
cy.wrap(el).within(() => {
7979
cy.get('a')
8080
.children()
81-
.should('have.lengthOf', index + 1)
81+
.should('have.lengthOf', (index + 1) % 2)
8282
.each((el) => {
8383
cy.wrap(el).contains('span', 'text')
8484
})

packages/client/src/components/AutoLink.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ export const AutoLink = defineComponent({
9090
// If the `target` attr is "_blank"
9191
const isBlankTarget = computed(() => linkTarget.value === '_blank')
9292

93-
// Render `<RouteLink>` or not
94-
const renderRouteLink = computed(
93+
// Whether the link is internal
94+
const isInternal = computed(
9595
() => !withProtocol.value && !isBlankTarget.value,
9696
)
9797

@@ -122,15 +122,15 @@ export const AutoLink = defineComponent({
122122
})
123123

124124
// If this link is active
125-
const isActive = computed(() =>
126-
renderRouteLink.value
127-
? config.value.activeMatch
125+
const isActive = computed(
126+
() =>
127+
isInternal.value &&
128+
(config.value.activeMatch
128129
? new RegExp(config.value.activeMatch, 'u').test(route.path)
129130
: // If this link is active in subpath
130131
shouldBeActiveInSubpath.value
131132
? route.path.startsWith(config.value.link)
132-
: route.path === config.value.link
133-
: false,
133+
: route.path === config.value.link),
134134
)
135135

136136
return (): VNode => {
@@ -143,15 +143,14 @@ export const AutoLink = defineComponent({
143143
after?.(),
144144
]
145145

146-
return renderRouteLink.value
146+
return isInternal.value
147147
? h(
148148
RouteLink,
149149
{
150150
'class': 'auto-link',
151151
'to': link,
152152
'active': isActive.value,
153153
'aria-label': linkAriaLabel.value,
154-
// Class needs to be merged manually
155154
},
156155
() => content,
157156
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export * from './AutoLink.js'
12
export * from './ClientOnly.js'
23
export * from './Content.js'
34
export * from './RouteLink.js'

packages/shared/src/utils/normalizeRoutePath.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const FAKE_HOST = 'http://.'
22

3-
const inferRoutePath = (path: string): string => {
3+
export const inferRoutePath = (path: string): string => {
44
// if the pathname is empty or ends with `/`, return as is
55
if (!path || path.endsWith('/')) return path
66

0 commit comments

Comments
 (0)