Skip to content

Commit 3320a60

Browse files
committed
Update md fmt and add copy btn on pre
1 parent 3831b5e commit 3320a60

File tree

4 files changed

+151
-28
lines changed

4 files changed

+151
-28
lines changed

static/css/main.css

+28
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,34 @@ h1.br-nav, h2.br-nav, h3.br-nav, h4.br-nav {color:#777;font-size: 1em;font-weigh
121121
.right {float: right;}
122122
.c {clear: both}
123123

124+
.pre-container {
125+
position: relative;
126+
background: #f5f5f5;
127+
padding: .3em;
128+
border-radius: 4px;
129+
}
130+
.copy-btn {
131+
position: absolute;
132+
top: 8px;
133+
right: 8px;
134+
padding: 4px 8px;
135+
background: #4CAF50;
136+
color: white;
137+
border: none;
138+
border-radius: 3px;
139+
cursor: pointer;
140+
transition: opacity 0.3s;
141+
font-size: .7rem;
142+
}
143+
144+
.copy-btn:hover {
145+
opacity: 0.9;
146+
}
147+
148+
.copy-btn.copied {
149+
background: #2196F3;
150+
}
151+
124152
.button-success,
125153
.button-error,
126154
.button-warning,

util/content_fmt.go

+2-15
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,10 @@ func HasCodeBlock(s string) (has bool) {
5454

5555
// 代码表格
5656
func tableCode(text, lang string) string {
57-
text = strings.TrimSpace(text)
58-
var codes []string
59-
var lines []string
60-
for i, line := range StringSplit(text, "\n") {
61-
lines = append(lines, fmt.Sprintf(`<span class="line-number">%d</span>`, i+1))
62-
codes = append(codes, fmt.Sprintf(`<span class="line">%s</span>`, line))
63-
}
64-
6557
return fmt.Sprintf(`
6658
<div class="highlight highlight-%s">
67-
<table><tbody><tr>
68-
<td class="gutter"><pre class="line-numbers">%s</pre></td>
69-
<td class="code"><pre><code class="%s">%s</code></pre></td>
70-
</tr></tbody></table></div>`, lang, strings.Join(lines, "\n"), lang, strings.Join(codes, "\n"))
59+
<pre><code class="%s">%s</code></pre>
60+
</div>`, lang, lang, text)
7161
}
7262

7363
// TrimPreTag 去除 pre 标签
@@ -94,9 +84,6 @@ var mdp = goldmark.New(
9484
// 注意首行与末行,前均无空格
9585

9686
func ContentFmt(input string) string {
97-
//if strings.Contains(input, "&") {
98-
// input = htmlStd.UnescapeString(input)
99-
//}
10087
// 代码块处理,后端代码高亮
10188
codeRawMap := map[string]string{} // 代码块
10289
if HasCodeBlock(input) {

views/ybs/topic_detail.qtpl

+50
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,58 @@ if(audioLst.length>1){
323323
});
324324
}
325325

326+
function setCopyBtn(){
327+
// 遍历所有pre元素
328+
document.querySelectorAll('pre').forEach(pre => {
329+
// 创建容器包裹pre元素
330+
const container = document.createElement('div');
331+
container.className = 'pre-container';
332+
pre.parentNode.insertBefore(container, pre);
333+
container.appendChild(pre);
334+
335+
// 创建复制按钮
336+
const btn = document.createElement('button');
337+
btn.className = 'copy-btn';
338+
btn.textContent = '复制';
339+
340+
// 添加点击事件
341+
{% if p.CurrentUser.ID > 0 %}
342+
btn.addEventListener('click', async () => {
343+
try {
344+
// 获取纯文本内容(自动去除HTML标签)
345+
const text = pre.textContent;
346+
347+
// 现代浏览器API
348+
await navigator.clipboard.writeText(text);
349+
350+
// 反馈效果
351+
btn.textContent = '✓ 已复制';
352+
btn.classList.add('copied');
353+
setTimeout(() => {
354+
btn.textContent = '复制';
355+
btn.classList.remove('copied');
356+
}, 1500);
357+
} catch (err) {
358+
// 兼容旧浏览器方案
359+
const textarea = document.createElement('textarea');
360+
textarea.value = pre.textContent;
361+
document.body.appendChild(textarea);
362+
textarea.select();
363+
document.execCommand('copy');
364+
document.body.removeChild(textarea);
365+
btn.textContent = '✓ 已复制';
366+
setTimeout(() => btn.textContent = '复制', 1500);
367+
}
368+
});
369+
{% endif %}
370+
371+
container.appendChild(btn);
372+
});
373+
}
374+
326375
docReady(function() {
327376
getContentLinkCount();
377+
setCopyBtn();
328378
});
329379

330380
</script>

views/ybs/topic_detail.qtpl.go

+71-13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)