Skip to content

Commit c6e12a6

Browse files
committed
add code copying js button
1 parent 32d5650 commit c6e12a6

File tree

3 files changed

+102
-5
lines changed

3 files changed

+102
-5
lines changed

assets/codecopy.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copy button for code block
2+
const codes = document.querySelectorAll('div.code-container');
3+
4+
codes.forEach(item => {
5+
// Container style
6+
item.setAttribute("style", "position: relative;");
7+
8+
// Create button
9+
const copyBtn = document.createElement('button');
10+
copyBtn.textContent = 'Copy';
11+
copyBtn.setAttribute("style", "position: absolute; top: 0; right: 0; z-index: 3; background: var(--background-color); color: var(--main-theme); border: none;");
12+
13+
// Add event
14+
copyBtn.addEventListener("click", () => {
15+
navigator.clipboard.writeText(copyBtn.previousElementSibling.textContent).then(() => {
16+
copyBtn.textContent = "Copied!"
17+
setTimeout(() => {
18+
copyBtn.textContent = "Copy"
19+
}, 1000)
20+
})
21+
});
22+
23+
// Add element
24+
item.appendChild(copyBtn);
25+
})
26+

bw.sh

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ CUSTOM_HTML_HEAD="
4242
<link rel=\"icon\" type=\"image/png\" sizes=\"32x32\" href=\"$BASE_URL/assets/favicon_io/favicon-32x32.png\">
4343
<link rel=\"icon\" type=\"image/png\" sizes=\"16x16\" href=\"$BASE_URL/assets/favicon_io/favicon-16x16.png\">
4444
<link rel=\"manifest\" href=\"$BASE_URL/assets/favicon_io/site.webmanifest\">
45+
46+
<script src=\"$BASE_URL/assets/codecopy.js\" defer></script>
4547
"
4648

4749
### Write your own HTML code.
@@ -333,8 +335,8 @@ MOD=$(echo "$MOD" | sed -E '
333335
s/</\&lt;/g
334336
s/>/\&gt;/g
335337
336-
s/```([a-zA-Z0-9_]+)?\n/<pre><code class="language-\1">\n/
337-
s/```/<\/code><\/pre>/
338+
s/```([a-zA-Z0-9_]+)?\n/<div class="code-container">\n<pre><code class="language-\1">\n/
339+
s/```/<\/code><\/pre>\n<\/div>/
338340
s/(<pre><code class="language-)">/\1plaintext">/
339341
340342
s/`/\\`/g
@@ -373,7 +375,7 @@ MOD=$(echo "$MOD" | sed -E '
373375
s/\^/\&caret;/g
374376
s/^:/\&colon;/
375377
376-
s/^(.*)$/<\1>/
378+
s/^(.*)$/<<\1>>/
377379
}
378380
')
379381

@@ -632,8 +634,8 @@ MOD=$(echo "$MOD" | sed -E '
632634

633635
# fixing codeblock for p
634636
MOD=$(echo "$MOD" | sed -E '
635-
/^<<pre>/,/<\/pre>>$/ {
636-
s/^<(.*)>$/\1/
637+
/^<<<pre>/,/<\/pre>>>$/ {
638+
s/^<<(.*)>>$/\1/
637639
}
638640
')
639641

write/codecopy.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
---
2+
title: Code copy JS
3+
description: Add simple code copy button with js
4+
date: 2025-04-28
5+
tags: js tutorial sample
6+
---
7+
8+
```
9+
sample
10+
code
11+
block
12+
```
13+
14+
## 1. Add a \<script\> tag inside the `CUSTOM\_HTML\_HEAD` variable
15+
16+
*bw.sh*
17+
18+
```md
19+
CUSTOM_HTML_HEAD="
20+
<script src=\"$BASE_URL/assets/codecopy.js\" defer></script>
21+
"
22+
```
23+
24+
## 2. Create `codecopy.js` file in the *assets/*
25+
26+
```
27+
your-blog/
28+
├─ assets/
29+
│ ├─ codecopy.js <<< New!
30+
├─ write/
31+
├─ bw.sh
32+
```
33+
34+
## 3. Wirte codes in `codecopy.js`
35+
36+
*assets/codecopy.js*
37+
38+
```js
39+
// Copy button for code block
40+
const codes = document.querySelectorAll('div.code-container');
41+
42+
codes.forEach(item => {
43+
// Container style
44+
item.setAttribute("style", "position: relative;");
45+
46+
// Create button
47+
const copyBtn = document.createElement('button');
48+
copyBtn.textContent = 'Copy';
49+
copyBtn.setAttribute("style", "position: absolute; top: 0; right: 0; z-index: 3; background: var(--background-color); color: var(--main-theme); border: none;");
50+
51+
// Add event
52+
copyBtn.addEventListener("click", () => {
53+
navigator.clipboard.writeText(copyBtn.previousElementSibling.textContent).then(()
54+
=> {
55+
copyBtn.textContent = "Copied!"
56+
setTimeout(() => {
57+
copyBtn.textContent = "Copy"
58+
}, 1000)
59+
})
60+
});
61+
62+
// Add element
63+
item.appendChild(copyBtn);
64+
})
65+
```
66+
67+
## 4. Save the file and run `./bw.sh b`
68+
69+
Fin!

0 commit comments

Comments
 (0)