-
Notifications
You must be signed in to change notification settings - Fork 71
/
mdx-components.tsx
61 lines (54 loc) · 1.7 KB
/
mdx-components.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
50
51
52
53
54
55
56
57
58
59
60
61
import Link from "next/link"
import React from "react"
import { createElement } from "react"
import Balancer from "react-wrap-balancer"
const balanceHeadings =
(type: string) => (props: React.HTMLAttributes<HTMLHeadingElement>) => {
let newProps = { ...props }
newProps.children = <Balancer>{props.children}</Balancer>
return createElement(type, newProps)
}
function shouldBeConverted(href: string): boolean {
return (
href.startsWith("http://vertx.io") ||
href.startsWith("https://vertx.io") ||
href.startsWith("/")
)
}
function makeRelative(href: string): string {
href = href.replace(/https?:\/\/vertx.io/, "")
if (href === "") {
href = "/"
}
return href
}
// this file is required to make MDX custom components work with SSR
export function useMDXComponents(components: {
component: Record<string, React.ComponentType>
}) {
return {
// replace markdown links with "next/link"
a: (props: React.AnchorHTMLAttributes<HTMLAnchorElement>) =>
props.href !== undefined ? (
<Link href={props.href} {...props} />
) : (
<a {...props} />
),
// automatically add basePath to image source
img: (props: React.ImgHTMLAttributes<HTMLImageElement>) => {
if (props.src !== undefined && shouldBeConverted(props.src)) {
let src = makeRelative(props.src)
src = process.env.basePath + props.src
// eslint-disable-next-line jsx-a11y/alt-text
return <img {...props} src={src} />
}
// eslint-disable-next-line jsx-a11y/alt-text
return <img {...props} />
},
// balance headings
h2: balanceHeadings("h2"),
h3: balanceHeadings("h3"),
h4: balanceHeadings("h4"),
...components,
}
}