Skip to content

Commit b39390c

Browse files
committed
Add mdx plugins
1 parent 5c00da6 commit b39390c

File tree

5 files changed

+74
-2
lines changed

5 files changed

+74
-2
lines changed

next.config.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1-
const withMDX = require("@next/mdx")()
1+
const withMDX = require("@next/mdx")({
2+
options: {
3+
remarkPlugins: [
4+
require("@silvenon/remark-smartypants"),
5+
],
6+
rehypePlugins: [],
7+
},
8+
})
29

310
module.exports = withMDX()

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"@mdx-js/loader": "^1.6.5",
99
"@mdx-js/react": "^1.0.6",
1010
"@next/mdx": "^9.4.4",
11+
"@silvenon/remark-smartypants": "^1.0.0",
1112
"next": "^9.5.3",
1213
"raw-loader": "^4.0.1",
1314
"react": "^16.13.1",

src/headings.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import React from "react"
2+
3+
export const headings = {
4+
h2: heading("h2"),
5+
h3: heading("h3"),
6+
h4: heading("h4"),
7+
h5: heading("h5"),
8+
}
9+
10+
function heading(type) {
11+
return function Heading({ children }) {
12+
const match = /^.+(\s*\{#([a-z0-9\-_]+?)\}\s*)$/.exec(
13+
children
14+
)
15+
16+
const id = match[2]
17+
18+
return React.createElement(
19+
type,
20+
{
21+
id,
22+
className: "heading",
23+
},
24+
<a href={`#${id}`} aria-hidden>
25+
<svg
26+
aria-hidden="true"
27+
height="16"
28+
version="1.1"
29+
viewBox="0 0 16 16"
30+
width="16"
31+
>
32+
<path
33+
fillRule="evenodd"
34+
d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"
35+
></path>
36+
</svg>
37+
</a>,
38+
children.replace(match[1], "")
39+
)
40+
}
41+
}

src/page.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import s from "./page.module.css"
22
import Content from "./tutorial.mdx"
3+
import { MDXProvider } from "@mdx-js/react"
4+
import { headings } from "./headings"
35

46
export { Page }
57

@@ -10,8 +12,14 @@ function Page() {
1012
<h1>Tutorial: Intro to React</h1>
1113
</header>
1214
<main>
13-
<Content />
15+
<MDXProvider components={components}>
16+
<Content />
17+
</MDXProvider>
1418
</main>
1519
</article>
1620
)
1721
}
22+
23+
const components = {
24+
...headings,
25+
}

src/page.module.css

+15
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,18 @@
128128
background: rgba(255, 229, 100, 0.2);
129129
color: #1a1a1a;
130130
}
131+
132+
.article :global(.heading) a {
133+
float: left;
134+
padding-right: 4px;
135+
margin-left: -20px;
136+
visibility: hidden;
137+
background-color: inherit;
138+
border-bottom: inherit;
139+
color: inherit;
140+
}
141+
.article :global(.heading):hover a {
142+
visibility: visible;
143+
background-color: inherit;
144+
border-bottom-color: inherit;
145+
}

0 commit comments

Comments
 (0)