Skip to content

Commit

Permalink
fix: add support for custom protocol, close #1404
Browse files Browse the repository at this point in the history
  • Loading branch information
Mister-Hope committed Oct 10, 2023
1 parent f342a86 commit f417ba2
Show file tree
Hide file tree
Showing 12 changed files with 38 additions and 92 deletions.
4 changes: 2 additions & 2 deletions ecosystem/theme-default/src/client/components/AutoLink.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default defineComponent({

<script setup lang="ts">
import { useSiteData } from '@vuepress/client'
import { isLinkHttp, isLinkMailto, isLinkTel } from '@vuepress/shared'
import { hasProtocol, isLinkHttp } from '@vuepress/shared'
import { computed, toRefs } from 'vue'
import type { PropType } from 'vue'
import { useRoute } from 'vue-router'
Expand All @@ -36,7 +36,7 @@ const { item } = toRefs(props)
const hasHttpProtocol = computed(() => isLinkHttp(item.value.link))
// if the link has non-http protocol
const hasNonHttpProtocol = computed(
() => isLinkMailto(item.value.link) || isLinkTel(item.value.link),
() => !hasHttpProtocol.value && hasProtocol(item.value.link),
)
// resolve the `target` attr
const linkTarget = computed(() => {
Expand Down
5 changes: 5 additions & 0 deletions packages/shared/src/utils/hasProtocol.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**
* Whether the link has a protocol
*/
export const hasProtocol = (link: string): boolean =>
/^[a-z][a-z0-9+.-]*:/.test(link)
4 changes: 1 addition & 3 deletions packages/shared/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ export * from './dedupeHead.js'
export * from './ensureLeadingSlash.js'
export * from './ensureEndingSlash.js'
export * from './formatDateString.js'
export * from './hasProtocol.js'
export * from './isLinkExternal.js'
export * from './isLinkFtp.js'
export * from './isLinkHttp.js'
export * from './isLinkMailto.js'
export * from './isLinkTel.js'
export * from './isPlainObject.js'
export * from './omit.js'
export * from './removeEndingSlash.js'
Expand Down
4 changes: 1 addition & 3 deletions packages/shared/src/utils/isLinkExternal.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { isLinkFtp } from './isLinkFtp.js'
import { isLinkHttp } from './isLinkHttp.js'

const markdownLinkRegexp = /.md((\?|#).*)?$/
Expand All @@ -7,8 +6,7 @@ const markdownLinkRegexp = /.md((\?|#).*)?$/
* Determine a link is external or not
*/
export const isLinkExternal = (link: string, base = '/'): boolean => {
// http link or ftp link
if (isLinkHttp(link) || isLinkFtp(link)) {
if (isLinkHttp(link)) {
return true
}

Expand Down
4 changes: 0 additions & 4 deletions packages/shared/src/utils/isLinkFtp.ts

This file was deleted.

4 changes: 0 additions & 4 deletions packages/shared/src/utils/isLinkMailto.ts

This file was deleted.

4 changes: 0 additions & 4 deletions packages/shared/src/utils/isLinkTel.ts

This file was deleted.

24 changes: 24 additions & 0 deletions packages/shared/tests/hasProtocal.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { describe, expect, it } from 'vitest'
import { hasProtocol } from '../src/index.js'

const testCases: [string, ReturnType<typeof hasProtocol>][] = [
['ftp://foobar.com', true],
['mailto:foobar', true],
['tel:foobar', true],
['https://foobar.com', true],
['http://foobar.com', true],
['foobar.com', false],
['/foo/bar', false],
['../foo/bar', false],
['//foobar.com', false],
]

describe('shared > hasProtocol', () => {
describe('should determine link with protocol correctly', () => {
testCases.forEach(([source, expected]) => {
it(`link: ${source}`, () => {
expect(hasProtocol(source)).toBe(expected)
})
})
})
})
10 changes: 5 additions & 5 deletions packages/shared/tests/isLinkExternal.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,27 @@ const testCases: [
Parameters<typeof isLinkExternal>,
ReturnType<typeof isLinkExternal>,
][] = [
// http & ftp links
// http links
[['https://foobar.com'], true],
[['https://foobar.com', '/base/'], true],
[['http://foobar.com'], true],
[['http://foobar.com', '/base/'], true],
[['//foobar.com'], true],
[['//foobar.com', '/base/'], true],
[['ftp://foobar.com'], true],
[['ftp://foobar.com', '/base/'], true],
[['https://foobar.com/base/README.md'], true],
[['https://foobar.com/base/README.md', '/base/'], true],
[['http://foobar.com/base/README.md'], true],
[['http://foobar.com/base/README.md', '/base/'], true],
[['//foobar.com/base/README.md'], true],
[['//foobar.com/base/README.md', '/base/'], true],
[['ftp://foobar.com/base/README.md'], true],
[['ftp://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],

// absolute links
[['/foo/bar'], false],
Expand Down
21 changes: 0 additions & 21 deletions packages/shared/tests/isLinkFtp.spec.ts

This file was deleted.

23 changes: 0 additions & 23 deletions packages/shared/tests/isLinkMailto.spec.ts

This file was deleted.

23 changes: 0 additions & 23 deletions packages/shared/tests/isLinkTel.spec.ts

This file was deleted.

0 comments on commit f417ba2

Please sign in to comment.