Skip to content

Commit

Permalink
refactor: rename to isLinkWithProtocol
Browse files Browse the repository at this point in the history
  • Loading branch information
meteorlxy committed Nov 14, 2023
1 parent d7e9283 commit 7eba693
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 12 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 { hasProtocol, isLinkHttp } from '@vuepress/shared'
import { isLinkHttp, isLinkWithProtocol } 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(
() => !hasHttpProtocol.value && hasProtocol(item.value.link),
() => !hasHttpProtocol.value && isLinkWithProtocol(item.value.link),
)
// resolve the `target` attr
const linkTarget = computed(() => {
Expand Down
5 changes: 0 additions & 5 deletions packages/shared/src/utils/hasProtocol.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/shared/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +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 './isLinkHttp.js'
export * from './isLinkWithProtocol.js'
export * from './isPlainObject.js'
export * from './omit.js'
export * from './removeEndingSlash.js'
Expand Down
5 changes: 5 additions & 0 deletions packages/shared/src/utils/isLinkWithProtocol.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**
* Determine a link has protocol or not
*/
export const isLinkWithProtocol = (link: string): boolean =>
/^[a-z][a-z0-9+.-]*:/.test(link)
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, expect, it } from 'vitest'
import { hasProtocol } from '../src/index.js'
import { isLinkWithProtocol } from '../src/index.js'

const testCases: [string, ReturnType<typeof hasProtocol>][] = [
const testCases: [string, ReturnType<typeof isLinkWithProtocol>][] = [
['ftp://foobar.com', true],
['ms-windows-store://home', true],
['mailto:foobar', true],
Expand All @@ -14,11 +14,11 @@ const testCases: [string, ReturnType<typeof hasProtocol>][] = [
['//foobar.com', false],
]

describe('shared > hasProtocol', () => {
describe('shared > isLinkWithProtocol', () => {
describe('should determine link with protocol correctly', () => {
testCases.forEach(([source, expected]) => {
it(`link: ${source}`, () => {
expect(hasProtocol(source)).toBe(expected)
expect(isLinkWithProtocol(source)).toBe(expected)
})
})
})
Expand Down

0 comments on commit 7eba693

Please sign in to comment.