Skip to content

Commit 923619c

Browse files
committed
Merge branch 'route-link' into auto-link
2 parents 5a4700a + 0f82b53 commit 923619c

File tree

6 files changed

+193
-204
lines changed

6 files changed

+193
-204
lines changed

packages/client/src/components/RouteLink.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { removeLeadingSlash } from '@vuepress/shared'
21
import { computed, defineComponent, h } from 'vue'
32
import type { SlotsType, VNode } from 'vue'
43
import { useRoute, useRouter } from 'vue-router'
@@ -92,7 +91,7 @@ export const RouteLink = defineComponent({
9291
const path = computed(() =>
9392
props.to.startsWith('#') || props.to.startsWith('?')
9493
? props.to
95-
: `${__VUEPRESS_BASE__}${removeLeadingSlash(resolveRoutePath(props.to, route.path))}`,
94+
: `${__VUEPRESS_BASE__}${resolveRoutePath(props.to, route.path).substring(1)}`,
9695
)
9796

9897
return () =>

packages/shared/src/utils/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ export * from './dedupeHead.js'
22
export * from './ensureLeadingSlash.js'
33
export * from './ensureEndingSlash.js'
44
export * from './formatDateString.js'
5+
export * from './inferRoutePath.js'
56
export * from './isLinkExternal.js'
67
export * from './isLinkHttp.js'
78
export * from './isLinkWithProtocol.js'
89
export * from './isPlainObject.js'
9-
export * from './inferRoutePath.js'
1010
export * from './normalizeRoutePath.js'
1111
export * from './omit.js'
1212
export * from './removeEndingSlash.js'

packages/shared/src/utils/inferRoutePath.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/**
2+
* Infer route path according to the given (markdown file) path
3+
*/
14
export const inferRoutePath = (path: string): string => {
25
// if the pathname is empty or ends with `/`, return as is
36
if (!path || path.endsWith('/')) return path
Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,13 @@
1-
import { isLinkHttp } from './isLinkHttp.js'
1+
import { isLinkWithProtocol } from './isLinkWithProtocol.js'
22

33
const markdownLinkRegexp = /.md((\?|#).*)?$/
44

55
/**
66
* Determine a link is external or not
77
*/
8-
export const isLinkExternal = (link: string, base = '/'): boolean => {
9-
if (isLinkHttp(link)) {
10-
return true
11-
}
12-
8+
export const isLinkExternal = (link: string, base = '/'): boolean =>
9+
isLinkWithProtocol(link) ||
1310
// absolute link that does not start with `base` and does not end with `.md`
14-
if (
15-
link.startsWith('/') &&
11+
(link.startsWith('/') &&
1612
!link.startsWith(base) &&
17-
!markdownLinkRegexp.test(link)
18-
) {
19-
return true
20-
}
21-
22-
return false
23-
}
13+
!markdownLinkRegexp.test(link))

packages/shared/tests/isLinkExternal.spec.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ const testCases: [
2020
[['//foobar.com/base/README.md', '/base/'], true],
2121

2222
// links with other protocols
23-
[['mailto:foobar', '/base/'], false],
24-
[['tel:foobar', '/base/'], false],
25-
[['ftp://foobar.com'], false],
26-
[['ftp://foobar.com', '/base/'], false],
27-
[['ftp://foobar.com/base/README.md'], false],
28-
[['ftp://foobar.com/base/README.md', '/base/'], false],
29-
[['ms-windows-store://home', '/base/'], false],
23+
[['mailto:foobar', '/base/'], true],
24+
[['tel:foobar', '/base/'], true],
25+
[['ftp://foobar.com'], true],
26+
[['ftp://foobar.com', '/base/'], true],
27+
[['ftp://foobar.com/base/README.md'], true],
28+
[['ftp://foobar.com/base/README.md', '/base/'], true],
29+
[['ms-windows-store://home', '/base/'], true],
3030

3131
// absolute links
3232
[['/foo/bar'], false],

0 commit comments

Comments
 (0)