forked from cpojer/copy-as-markdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.tsx
78 lines (73 loc) · 2.39 KB
/
index.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import {useCallback, useRef} from 'react';
import type TurndownService from 'turndown';
type CopyRefType = {
element: HTMLElement;
listener: (event: ClipboardEvent) => void;
};
type CallbackFn = (element: HTMLElement) => void;
function getSelectedInnerHTML(
containerNode: HTMLElement,
processNode: (element: HTMLElement) => HTMLElement = (element) => element,
) {
const selection = window.getSelection();
if (selection && selection.rangeCount) {
const container = document.createElement('div');
for (let i = 0; i < selection.rangeCount; i++) {
let parent: Node | null = selection.getRangeAt(i).commonAncestorContainer;
// Let the regular copy-paste behavior take place if the user only selects
// content within a `code` tag to avoid escape characters from being added.
while (parent && parent !== containerNode) {
if (
parent &&
parent.nodeType === 1 &&
(parent as HTMLElement).tagName.toLowerCase() === 'code'
) {
return null;
}
parent = parent.parentElement;
}
container.appendChild(selection.getRangeAt(i).cloneContents());
}
return processNode(container).innerHTML;
}
return null;
}
export default function useCopyAsMarkdown(
markdownOptions?: TurndownService.Options | null,
options?: {
processNode?: (element: HTMLElement) => HTMLElement;
},
): CallbackFn {
const ref = useRef<CopyRefType | null>(null);
return useCallback((element: HTMLElement) => {
if (ref.current) {
ref.current.element.removeEventListener('copy', ref.current.listener);
ref.current = null;
}
if (element) {
const listener = (event: ClipboardEvent) => {
// Lazy-require `turndown` only when needed.
const TurndownService =
require('turndown').default || require('turndown');
const html = getSelectedInnerHTML(element, options?.processNode);
const markdown =
html &&
new TurndownService({
codeBlockStyle: 'fenced',
headingStyle: 'atx',
hr: '---',
...markdownOptions,
}).turndown(html);
if (markdown) {
event.preventDefault();
event.clipboardData?.setData('text/plain', markdown);
}
};
element.addEventListener('copy', listener);
ref.current = {
element,
listener,
};
}
}, []);
}