-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
gatsby-ssr.js
84 lines (78 loc) · 2.15 KB
/
gatsby-ssr.js
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import React from "react"
const pluginDefaults = {
className: `anchor`,
icon: true,
offsetY: 0,
}
exports.onRenderBody = ({ setHeadComponents }, pluginOptions) => {
const { className, icon, offsetY } = Object.assign(
pluginDefaults,
pluginOptions
)
const styles = `
.${className}.before {
position: absolute;
top: 0;
left: 0;
transform: translateX(-100%);
padding-right: 4px;
}
.${className}.after {
display: inline-block;
padding-left: 4px;
}
h1 .${className} svg,
h2 .${className} svg,
h3 .${className} svg,
h4 .${className} svg,
h5 .${className} svg,
h6 .${className} svg {
visibility: hidden;
}
h1:hover .${className} svg,
h2:hover .${className} svg,
h3:hover .${className} svg,
h4:hover .${className} svg,
h5:hover .${className} svg,
h6:hover .${className} svg,
h1 .${className}:focus svg,
h2 .${className}:focus svg,
h3 .${className}:focus svg,
h4 .${className}:focus svg,
h5 .${className}:focus svg,
h6 .${className}:focus svg {
visibility: visible;
}
`
const script = `
document.addEventListener("DOMContentLoaded", function(event) {
var hash = window.decodeURI(location.hash.replace('#', ''))
if (hash !== '') {
var element = document.getElementById(hash)
if (element) {
let scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
let clientTop = document.documentElement.clientTop || document.body.clientTop || 0
var offset = element.getBoundingClientRect().top + scrollTop - clientTop
// Wait for the browser to finish rendering before scrolling.
setTimeout((function() {
window.scrollTo(0, offset - ${offsetY})
}), 0)
}
}
})
`
const style = icon ? (
<style key={`gatsby-remark-autolink-headers-style`} type="text/css">
{styles}
</style>
) : (
undefined
)
return setHeadComponents([
style,
<script
key={`gatsby-remark-autolink-headers-script`}
dangerouslySetInnerHTML={{ __html: script }}
/>,
])
}