File tree Expand file tree Collapse file tree 3 files changed +102
-5
lines changed Expand file tree Collapse file tree 3 files changed +102
-5
lines changed Original file line number Diff line number Diff line change
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
+
Original file line number Diff line number Diff line change @@ -42,6 +42,8 @@ CUSTOM_HTML_HEAD="
42
42
<link rel=\" icon\" type=\" image/png\" sizes=\" 32x32\" href=\" $BASE_URL /assets/favicon_io/favicon-32x32.png\" >
43
43
<link rel=\" icon\" type=\" image/png\" sizes=\" 16x16\" href=\" $BASE_URL /assets/favicon_io/favicon-16x16.png\" >
44
44
<link rel=\" manifest\" href=\" $BASE_URL /assets/favicon_io/site.webmanifest\" >
45
+
46
+ <script src=\" $BASE_URL /assets/codecopy.js\" defer></script>
45
47
"
46
48
47
49
# ## Write your own HTML code.
@@ -333,8 +335,8 @@ MOD=$(echo "$MOD" | sed -E '
333
335
s/</\</g
334
336
s/>/\>/g
335
337
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> /
338
340
s/(<pre><code class="language-)">/\1plaintext">/
339
341
340
342
s/`/\\`/g
@@ -373,7 +375,7 @@ MOD=$(echo "$MOD" | sed -E '
373
375
s/\^/\⁁/g
374
376
s/^:/\:/
375
377
376
- s/^(.*)$/<\1 >/
378
+ s/^(.*)$/<<\1> >/
377
379
}
378
380
' )
379
381
@@ -632,8 +634,8 @@ MOD=$(echo "$MOD" | sed -E '
632
634
633
635
# fixing codeblock for p
634
636
MOD=$( echo " $MOD " | sed -E '
635
- /^<<pre>/,/<\/pre>>$/ {
636
- s/^<(.*)>$/\1/
637
+ /^<<< pre>/,/<\/pre> >>$/ {
638
+ s/^<< (.*)> >$/\1/
637
639
}
638
640
' )
639
641
Original file line number Diff line number Diff line change
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!
You can’t perform that action at this time.
0 commit comments