Skip to content

Commit 41de145

Browse files
(DEPLOY) Add Clipboard Button to Code Blocks
1 parent a619047 commit 41de145

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

app/assets/stylesheets/application.css

+28
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,13 @@ blockquote {
116116
border-left: 3px solid #9ca3af;
117117
}
118118

119+
.code-block {
120+
position: relative;
121+
}
122+
119123
.highlight {
124+
padding-right: 40px;
125+
120126
background-color: rgba(var(--color-light-gray), 0.5);
121127
border: 1px solid rgba(var(--color-light-gray), 1);
122128
border-radius: 2px;
@@ -132,6 +138,27 @@ blockquote {
132138
width: fit-content;
133139
}
134140

141+
.clipboard-button {
142+
position: absolute;
143+
top: 8px;
144+
right: 8px;
145+
background: none;
146+
border: 1px solid #cacfd2;
147+
border-radius: 4px;
148+
width: 24px;
149+
height: 24px;
150+
151+
padding: 0;
152+
line-height: 24px;
153+
154+
cursor: pointer;
155+
transition: 0.25s all ease;
156+
}
157+
158+
.clipboard-button:hover {
159+
background-color: #cacfd2;
160+
}
161+
135162
/* SIDEBAR */
136163
.logo-container {
137164
width: 100%;
@@ -345,6 +372,7 @@ h1.centered {
345372
transform: translateX(100%);
346373
opacity: 0;
347374
transition: 0.25s all ease;
375+
z-index: 2;
348376
}
349377

350378
.mobile-sidebar.visible {

app/controllers/articles_controller.rb

+12-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,18 @@
66
class ArticlesController < ApplicationController
77
class HTMLwithPygments < Redcarpet::Render::HTML
88
def block_code(code, language)
9-
Pygments.highlight(code, lexer: language)
9+
puts code
10+
result = Pygments.highlight(code, lexer: language)
11+
result = "<div class=\"code-block\">
12+
#{result}
13+
<button class=\"clipboard-button\" onclick=\"
14+
parent = this.parentElement;
15+
content = parent.getElementsByClassName('highlight')[0].textContent;
16+
navigator.clipboard.writeText(content);
17+
\">📋</button>
18+
</div>"
19+
result
20+
1021
end
1122
end
1223

0 commit comments

Comments
 (0)