Skip to content

Commit

Permalink
Merge branch 'route-link' into auto-link
Browse files Browse the repository at this point in the history
  • Loading branch information
Mister-Hope committed Apr 17, 2024
2 parents 5a4700a + 0f82b53 commit 923619c
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 204 deletions.
3 changes: 1 addition & 2 deletions packages/client/src/components/RouteLink.ts
@@ -1,4 +1,3 @@
import { removeLeadingSlash } from '@vuepress/shared'
import { computed, defineComponent, h } from 'vue'
import type { SlotsType, VNode } from 'vue'
import { useRoute, useRouter } from 'vue-router'
Expand Down Expand Up @@ -92,7 +91,7 @@ export const RouteLink = defineComponent({
const path = computed(() =>
props.to.startsWith('#') || props.to.startsWith('?')
? props.to
: `${__VUEPRESS_BASE__}${removeLeadingSlash(resolveRoutePath(props.to, route.path))}`,
: `${__VUEPRESS_BASE__}${resolveRoutePath(props.to, route.path).substring(1)}`,
)

return () =>
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/src/utils/index.ts
Expand Up @@ -2,11 +2,11 @@ export * from './dedupeHead.js'
export * from './ensureLeadingSlash.js'
export * from './ensureEndingSlash.js'
export * from './formatDateString.js'
export * from './inferRoutePath.js'
export * from './isLinkExternal.js'
export * from './isLinkHttp.js'
export * from './isLinkWithProtocol.js'
export * from './isPlainObject.js'
export * from './inferRoutePath.js'
export * from './normalizeRoutePath.js'
export * from './omit.js'
export * from './removeEndingSlash.js'
Expand Down
3 changes: 3 additions & 0 deletions packages/shared/src/utils/inferRoutePath.ts
@@ -1,3 +1,6 @@
/**
* Infer route path according to the given (markdown file) path
*/
export const inferRoutePath = (path: string): string => {
// if the pathname is empty or ends with `/`, return as is
if (!path || path.endsWith('/')) return path
Expand Down
20 changes: 5 additions & 15 deletions packages/shared/src/utils/isLinkExternal.ts
@@ -1,23 +1,13 @@
import { isLinkHttp } from './isLinkHttp.js'
import { isLinkWithProtocol } from './isLinkWithProtocol.js'

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

/**
* Determine a link is external or not
*/
export const isLinkExternal = (link: string, base = '/'): boolean => {
if (isLinkHttp(link)) {
return true
}

export const isLinkExternal = (link: string, base = '/'): boolean =>
isLinkWithProtocol(link) ||
// absolute link that does not start with `base` and does not end with `.md`
if (
link.startsWith('/') &&
(link.startsWith('/') &&
!link.startsWith(base) &&
!markdownLinkRegexp.test(link)
) {
return true
}

return false
}
!markdownLinkRegexp.test(link))
14 changes: 7 additions & 7 deletions packages/shared/tests/isLinkExternal.spec.ts
Expand Up @@ -20,13 +20,13 @@ const testCases: [
[['//foobar.com/base/README.md', '/base/'], true],

// links with other protocols
[['mailto:foobar', '/base/'], false],
[['tel:foobar', '/base/'], false],
[['ftp://foobar.com'], false],
[['ftp://foobar.com', '/base/'], false],
[['ftp://foobar.com/base/README.md'], false],
[['ftp://foobar.com/base/README.md', '/base/'], false],
[['ms-windows-store://home', '/base/'], false],
[['mailto:foobar', '/base/'], true],
[['tel:foobar', '/base/'], true],
[['ftp://foobar.com'], true],
[['ftp://foobar.com', '/base/'], true],
[['ftp://foobar.com/base/README.md'], true],
[['ftp://foobar.com/base/README.md', '/base/'], true],
[['ms-windows-store://home', '/base/'], true],

// absolute links
[['/foo/bar'], false],
Expand Down

0 comments on commit 923619c

Please sign in to comment.