-
Notifications
You must be signed in to change notification settings - Fork 2
/
smart-link.coffee
53 lines (44 loc) · 1.16 KB
/
smart-link.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# Render a nuxt-link if an internal link or a v-parse-anchor wrapped a if not.
# This is so that link pre-fetching works.
import {
isInternal,
makeRouterPath,
shouldOpenInNewWindow,
addTrailingSlash
} from './index'
export default
name: 'SmartLink'
functional: true
props:
to: String # The URL gets passed here
# Destructure the props and data we care about
render: (create, {
props: { to }
data
listeners
children
parent
}) ->
# If no "to", wrap children in a span so that children are nested
# consistently
if !to then return create 'span', data, children
# Add trailing slashes if configured to
if parent?.$config?.anchorParser?.addTrailingSlashToInternal
then to = addTrailingSlash to
# Test if an internal link
if isInternal to
# Render a nuxt-link
then create 'nuxt-link', {
...data
nativeOn: listeners # nuxt-link doesn't forward events on it's own
props: to: makeRouterPath to, { router: parent?.$router }
}, children
# Make a standard link that opens in a new window
else create 'a', {
...data
attrs: {
...data.attrs
href: to
target: '_blank' if shouldOpenInNewWindow to
}
}, children