-
Notifications
You must be signed in to change notification settings - Fork 161
Expand file tree
/
Copy pathfsdocs-copy-button.js
More file actions
75 lines (68 loc) · 2.61 KB
/
fsdocs-copy-button.js
File metadata and controls
75 lines (68 loc) · 2.61 KB
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
// Adds a "Copy" button to every code block so readers can easily copy snippets.
function createCopyButton() {
const button = document.createElement('button')
button.className = 'copy-code-button'
button.setAttribute('aria-label', 'Copy code to clipboard')
button.textContent = 'Copy'
return button
}
function attachCopyHandler(button, getText) {
button.addEventListener('click', function () {
const text = getText()
if (navigator.clipboard && navigator.clipboard.writeText) {
navigator.clipboard.writeText(text).then(
function () {
button.textContent = 'Copied!'
setTimeout(function () {
button.textContent = 'Copy'
}, 2000)
},
function () {
button.textContent = 'Failed'
setTimeout(function () {
button.textContent = 'Copy'
}, 2000)
}
)
} else {
// Fallback for non-HTTPS environments
const el = document.createElement('textarea')
el.value = text
document.body.appendChild(el)
el.select()
document.execCommand('copy')
document.body.removeChild(el)
button.textContent = 'Copied!'
setTimeout(function () {
button.textContent = 'Copy'
}, 2000)
}
})
}
document.addEventListener('DOMContentLoaded', function () {
// table.pre blocks (F# highlighted code, sometimes with line numbers)
document.querySelectorAll('table.pre').forEach(function (table) {
const wrapper = document.createElement('div')
wrapper.className = 'code-block-wrapper'
table.parentNode.insertBefore(wrapper, table)
wrapper.appendChild(table)
const button = createCopyButton()
wrapper.appendChild(button)
const snippet = table.querySelector('.snippet pre')
attachCopyHandler(button, function () {
return (snippet || table).innerText
})
})
// Standard pre > code blocks (Markdown fenced code, standalone fssnip, etc.)
// Skip those already handled inside table.pre above.
document.querySelectorAll('pre > code').forEach(function (code) {
if (code.closest('table.pre')) return
const pre = code.parentElement
pre.classList.add('has-copy-button')
const button = createCopyButton()
pre.appendChild(button)
attachCopyHandler(button, function () {
return code.innerText
})
})
})