Skip to content

Commit

Permalink
chore: tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Mister-Hope committed Apr 11, 2024
1 parent fdc75f6 commit 71c1d88
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 11 deletions.
60 changes: 60 additions & 0 deletions e2e/docs/components/auto-link.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# AutoLink

<div id="route-link">
<AutoLink v-for="item in routeLinksConfig" :config="item" />
</div>

<div id="anchor-link">
<AutoLink v-for="item in anchorLinksConfig" :config="item" />
</div>

<div id="aria-label">
<AutoLink :config="{ text: 'text', link: '/', ariaLabel: 'label' }" />
</div>

<script setup lang="ts">
import { AutoLink } from 'vuepress/client'

const routeLinks = [
'/',
'/README.md',
'/index.html',
'/non-existent',
'/non-existent.md',
'/non-existent.html',
'/routes/non-ascii-paths/中文目录名/中文文件名',
'/routes/non-ascii-paths/中文目录名/中文文件名.md',
'/routes/non-ascii-paths/中文目录名/中文文件名.html',
'/README.md#hash',
'/README.md?query',
'/README.md?query#hash',
'/#hash',
'/?query',
'/?query#hash',
'#hash',
'?query',
'?query#hash',
'route-link',
'route-link.md',
'route-link.html',
'not-existent',
'not-existent.md',
'not-existent.html',
'../',
'../README.md',
'../404.md',
'../404.html',
]

const routeLinksConfig = routeLinks.map((link) => ({ link, text: 'text' }))

const anchorLinks = [
'//example.com',
'http://example.com',
'https://example.com',
'mailto:example@example.com',
'tel:+1234567890',
]

const anchorLinksConfig = anchorLinks.map((link) => ({ link, text: 'text' }))
</script>
19 changes: 19 additions & 0 deletions e2e/tests/components/auto-link.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
it('AutoLink', () => {
cy.visit('/components/auto-link.html')

cy.get(`.e2e-theme-content #route-link a`).each((el) => {
cy.wrap(el)
.should('have.attr', 'class')
.and('match', /route-link/)
})

cy.get(`.e2e-theme-content #anchor-link a`).each((el) => {
cy.wrap(el)
.should('have.attr', 'class')
.and('match', /anchor-link/)
})

cy.get(`.e2e-theme-content #aria-label a`).each((el) => {
cy.wrap(el).should('have.attr', 'aria-label').and('eq', 'label')
})
})
2 changes: 1 addition & 1 deletion e2e/tests/components/route-link.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ it('RouteLink', () => {
cy.wrap(el).within(() => {
cy.get('a')
.children()
.should('have.lengthOf', index + 1)
.should('have.lengthOf', (index + 1) % 2)
.each((el) => {
cy.wrap(el).contains('span', 'text')
})
Expand Down
17 changes: 8 additions & 9 deletions packages/client/src/components/AutoLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ export const AutoLink = defineComponent({
// If the `target` attr is "_blank"
const isBlankTarget = computed(() => linkTarget.value === '_blank')

// Render `<RouteLink>` or not
const renderRouteLink = computed(
// Whether the link is internal
const isInternal = computed(
() => !withProtocol.value && !isBlankTarget.value,
)

Expand Down Expand Up @@ -122,15 +122,15 @@ export const AutoLink = defineComponent({
})

// If this link is active
const isActive = computed(() =>
renderRouteLink.value
? config.value.activeMatch
const isActive = computed(
() =>
isInternal.value &&
(config.value.activeMatch
? new RegExp(config.value.activeMatch, 'u').test(route.path)
: // If this link is active in subpath
shouldBeActiveInSubpath.value
? route.path.startsWith(config.value.link)
: route.path === config.value.link
: false,
: route.path === config.value.link),
)

return (): VNode => {
Expand All @@ -143,15 +143,14 @@ export const AutoLink = defineComponent({
after?.(),
]

return renderRouteLink.value
return isInternal.value
? h(
RouteLink,
{
'class': 'auto-link',
'to': link,
'active': isActive.value,
'aria-label': linkAriaLabel.value,
// Class needs to be merged manually
},
() => content,
)
Expand Down
1 change: 1 addition & 0 deletions packages/client/src/components/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './AutoLink.js'
export * from './ClientOnly.js'
export * from './Content.js'
export * from './RouteLink.js'
2 changes: 1 addition & 1 deletion packages/shared/src/utils/normalizeRoutePath.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const FAKE_HOST = 'http://.'

const inferRoutePath = (path: string): string => {
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

0 comments on commit 71c1d88

Please sign in to comment.