-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Link.tsx
49 lines (42 loc) · 1.13 KB
/
Link.tsx
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
import * as React from 'react'
import { useMemo, SFC } from 'react'
import { useConfig, useDocs } from 'docz'
import styled from 'styled-components'
import { get } from '@utils/theme'
export const LinkStyled = styled.a<any>`
&,
&:visited,
&:active {
text-decoration: none;
color: ${get('colors.link')};
}
&:hover {
color: ${get('colors.link')};
}
${get('styles.link')};
`
type LinkProps = React.AnchorHTMLAttributes<any>
export const Link: SFC<LinkProps> = ({ href, ...props }) => {
const { separator, linkComponent: Link } = useConfig()
const docs = useDocs()
const toCheck = useMemo(
() =>
[
location.pathname
.split(separator)
.slice(0, -1)
.join(separator)
.slice(1),
(href || '').replace(/^(?:\.\/)+/gi, ''),
].join('/'),
[separator]
)
const matched = docs && docs.find(doc => doc.filepath === toCheck)
const nHref = matched ? matched.route : href
const isInternal = nHref && nHref.startsWith('/')
return isInternal ? (
<LinkStyled as={Link} {...props} to={nHref} />
) : (
<LinkStyled {...props} href={nHref} />
)
}