-
Notifications
You must be signed in to change notification settings - Fork 479
/
gatsby-browser.tsx
59 lines (51 loc) · 2.11 KB
/
gatsby-browser.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
// organize-imports-ignore
// note that reordering the css file imports will break some styles
import { wrapRootElement as wrap } from './root-wrapper';
import 'katex/dist/katex.min.css';
import './src/styles/main.css';
import { inject } from '@vercel/analytics';
import { injectSpeedInsights } from '@vercel/speed-insights';
// import './build.css';
export const wrapRootElement = wrap;
export const onClientEntry = () => {
// Source (modified): https://github.com/KaTeX/KaTeX/blob/master/contrib/copy-tex/copy-tex.js
// Global copy handler to modify behavior on .katex elements.
document.addEventListener('copy', function (event) {
const selection = window.getSelection();
if (selection.isCollapsed) {
return; // default action OK if selection is empty
}
const fragment = selection.getRangeAt(0).cloneContents();
if (fragment.querySelectorAll('[data-latex]').length === 0) {
return; // the following code breaks copy-pasting of code blocks; see #464
}
// Preserve usual HTML copy/paste behavior.
const html = [];
for (let i = 0; i < fragment.childNodes.length; i++) {
html.push((fragment.childNodes[i] as HTMLElement).outerHTML);
}
event.clipboardData.setData('text/html', html.join(''));
const katexElements = fragment.querySelectorAll('[data-latex]');
for (let i = 0; i < katexElements.length; i++) {
const element = katexElements[i];
element.innerHTML = (element as any).dataset.latex;
}
const displayElements = fragment.querySelectorAll('.katex-display');
for (let i = 0; i < displayElements.length; i++) {
const element = displayElements[i];
// fromCharCode(13) = newline
element.innerHTML =
String.fromCharCode(13) + element.innerHTML + String.fromCharCode(13);
}
const plaintext = Array.from(fragment.childNodes)
.map(node => node.textContent)
.join(String.fromCharCode(13));
// Rewrite plain-text version.
event.clipboardData.setData('text/plain', plaintext);
// Prevent normal copy handling.
event.preventDefault();
});
// vercel analytics
inject();
injectSpeedInsights();
};