This repository has been archived by the owner on Nov 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.ts
71 lines (52 loc) · 1.77 KB
/
main.ts
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
import './styles.scss'
import { App, Plugin, PluginManifest, MarkdownView } from "obsidian";
const excludeLangs = [
"todoist"
];
export default class CMSyntaxHighlightPlugin extends Plugin {
constructor(app: App, pluginManifest: PluginManifest) {
super(app, pluginManifest);
}
// all I need to do is import the modes.
async onload() {
this.registerInterval(
window.setInterval(this.injectButtons.bind(this), 1000)
);
}
injectButtons() {
this.addCopyButtons(navigator.clipboard);
}
addCopyButtons(clipboard:any) {
document.querySelectorAll('pre > code').forEach(function (codeBlock) {
var pre = codeBlock.parentNode;
// check for excluded langs
for ( let lang of excludeLangs ){
if (pre.classList.contains( `language-${lang}` ))
return;
}
// Dont add more than once
if (pre.parentNode.classList.contains('has-copy-button')) {
return;
}
pre.parentNode.classList.add( 'has-copy-button' );
var button = document.createElement('button');
button.className = 'copy-code-button';
button.type = 'button';
button.innerText = 'Copy';
button.addEventListener('click', function () {
clipboard.writeText(codeBlock.innerText).then(function () {
/* Chrome doesn't seem to blur automatically,
leaving the button in a focused state. */
button.blur();
button.innerText = 'copied!';
setTimeout(function () {
button.innerText = 'copy';
}, 2000);
}, function (error) {
button.innerText = 'Error';
});
});
pre.appendChild(button);
});
}
}