@@ -5,123 +5,104 @@ import { useRoute } from 'vue-router'
5
5
import { useSiteData } from '../composables/index.js'
6
6
import { RouteLink } from './RouteLink.js'
7
7
8
- export interface AutoLinkConfig {
8
+ export interface AutoLinkProps {
9
9
/**
10
- * Text of item
11
- *
12
- * 项目文字
10
+ * Pattern to determine if the link should be active, which has higher priority than `exact`
13
11
*/
14
- text : string
12
+ activeMatch ? : string | RegExp
15
13
16
14
/**
17
- * Aria label of item
18
- *
19
- * 项目无障碍标签
15
+ * The `aria-label` attribute
20
16
*/
21
17
ariaLabel ?: string
22
18
23
19
/**
24
- * Link of item
25
- *
26
- * 当前页面链接
20
+ * Whether the link should be active only if the url is an exact match
21
+ */
22
+ exact ?: boolean
23
+
24
+ /**
25
+ * URL of the auto link
27
26
*/
28
27
link : string
29
28
30
29
/**
31
- * Rel of `<a>` tag
32
- *
33
- * `<a>` 标签的 `rel` 属性
30
+ * The `rel` attribute
34
31
*/
35
32
rel ?: string
36
33
37
34
/**
38
- * Target of `<a>` tag
39
- *
40
- * `<a>` 标签的 `target` 属性
35
+ * The `target` attribute
41
36
*/
42
37
target ?: string
43
38
44
39
/**
45
- * Regexp mode to be active
46
- *
47
- * 匹配激活的正则表达式
40
+ * Text of the auto link
48
41
*/
49
- activeMatch ? : string
42
+ text : string
50
43
}
51
44
45
+ /**
46
+ * Component to render a link automatically according to the link type
47
+ *
48
+ * - If the link is internal, it will be rendered as a `<RouteLink>`
49
+ * - If the link is external, it will be rendered as a normal `<a>` tag
50
+ */
52
51
export const AutoLink = defineComponent ( {
53
52
name : 'AutoLink' ,
54
53
55
54
props : {
56
55
/**
57
- * Text of item
58
- *
59
- * 项目文字
56
+ * Pattern to determine if the link should be active, which has higher priority than `exact`
60
57
*/
61
- text : {
62
- type : String ,
63
- required : true ,
58
+ activeMatch : {
59
+ type : [ String , RegExp ] ,
60
+ default : '' ,
64
61
} ,
65
62
66
63
/**
67
- * Link of item
68
- *
69
- * 当前页面链接
64
+ * The `aria-label` attribute
70
65
*/
71
- link : {
66
+ ariaLabel : {
72
67
type : String ,
73
- required : true ,
68
+ default : '' ,
74
69
} ,
75
70
76
71
/**
77
- * Aria label of item
78
- *
79
- * 项目无障碍标签
72
+ * Whether the link should be active only if the url is an exact match
80
73
*/
81
- ariaLabel : {
74
+ exact : Boolean ,
75
+
76
+ /**
77
+ * URL of the auto link
78
+ */
79
+ link : {
82
80
type : String ,
83
- default : '' ,
81
+ required : true ,
84
82
} ,
85
83
86
84
/**
87
- * Rel of `<a>` tag
88
- *
89
- * `<a>` 标签的 `rel` 属性
85
+ * The `rel` attribute
90
86
*/
91
87
rel : {
92
88
type : String ,
93
89
default : '' ,
94
90
} ,
95
91
96
92
/**
97
- * Target of `<a>` tag
98
- *
99
- * `<a>` 标签的 `target` 属性
93
+ * The `target` attribute
100
94
*/
101
95
target : {
102
96
type : String ,
103
97
default : '' ,
104
98
} ,
105
99
106
100
/**
107
- * Whether it's active only when exact match
108
- *
109
- * 是否当恰好匹配时激活
110
- */
111
- exact : Boolean ,
112
-
113
- /**
114
- * Regexp mode to be active
115
- *
116
- * @description has higher priority than exact
117
- *
118
- * 匹配激活的正则表达式
119
- *
120
- * @description 比 exact 的优先级更高
101
+ * Text of the auto link
121
102
*/
122
- activeMatch : {
123
- type : [ String , RegExp ] ,
124
- default : '' ,
103
+ text : {
104
+ type : String ,
105
+ required : true ,
125
106
} ,
126
107
} ,
127
108
@@ -177,28 +158,26 @@ export const AutoLink = defineComponent({
177
158
const isActive = computed ( ( ) => {
178
159
if ( ! isInternal . value ) return false
179
160
180
- if ( props . activeMatch )
161
+ if ( props . activeMatch ) {
181
162
return (
182
163
props . activeMatch instanceof RegExp
183
164
? props . activeMatch
184
165
: new RegExp ( props . activeMatch , 'u' )
185
166
) . test ( route . path )
167
+ }
186
168
187
169
// If this link is active in subpath
188
- if ( shouldBeActiveInSubpath . value )
170
+ if ( shouldBeActiveInSubpath . value ) {
189
171
return route . path . startsWith ( props . link )
172
+ }
190
173
191
174
return route . path === props . link
192
175
} )
193
176
194
- return ( ) : VNode => {
177
+ return ( ) => {
195
178
const { before, after, default : defaultSlot } = slots
196
179
197
- const content = defaultSlot ?.( ) || [
198
- before ? before ( ) : null ,
199
- props . text ,
200
- after ?.( ) ,
201
- ]
180
+ const content = defaultSlot ?.( ) || [ before ?.( ) , props . text , after ?.( ) ]
202
181
203
182
return isInternal . value
204
183
? h (
@@ -216,9 +195,9 @@ export const AutoLink = defineComponent({
216
195
{
217
196
'class' : 'auto-link external-link' ,
218
197
'href' : props . link ,
198
+ 'aria-label' : linkAriaLabel . value ,
219
199
'rel' : linkRel . value ,
220
200
'target' : linkTarget . value ,
221
- 'aria-label' : linkAriaLabel . value ,
222
201
} ,
223
202
content ,
224
203
)
0 commit comments