Skip to content

Commit eda6d65

Browse files
markup: improve code block readability and isolate copy button (#34009)
Fix #33197 Improve the rendering of code blocks in markdown content for better readability and UI stability across screen sizes. Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
1 parent f0544db commit eda6d65

File tree

5 files changed

+95
-25
lines changed

5 files changed

+95
-25
lines changed

Diff for: modules/markup/markdown/markdown.go

+2-7
Original file line numberDiff line numberDiff line change
@@ -86,20 +86,15 @@ func (r *GlodmarkRender) highlightingRenderer(w util.BufWriter, c highlighting.C
8686
preClasses += " is-loading"
8787
}
8888

89-
err := r.ctx.RenderInternal.FormatWithSafeAttrs(w, `<pre class="%s">`, preClasses)
90-
if err != nil {
91-
return
92-
}
93-
9489
// include language-x class as part of commonmark spec, "chroma" class is used to highlight the code
9590
// the "display" class is used by "js/markup/math.ts" to render the code element as a block
9691
// the "math.ts" strictly depends on the structure: <pre class="code-block is-loading"><code class="language-math display">...</code></pre>
97-
err = r.ctx.RenderInternal.FormatWithSafeAttrs(w, `<code class="chroma language-%s display">`, languageStr)
92+
err := r.ctx.RenderInternal.FormatWithSafeAttrs(w, `<div class="code-block-container code-overflow-scroll"><pre class="%s"><code class="chroma language-%s display">`, preClasses, languageStr)
9893
if err != nil {
9994
return
10095
}
10196
} else {
102-
_, err := w.WriteString("</code></pre>")
97+
_, err := w.WriteString("</code></pre></div>")
10398
if err != nil {
10499
return
105100
}

Diff for: templates/devtest/markup-render.tmpl

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
{{template "devtest/devtest-header"}}
2+
<div class="page-content devtest ui container">
3+
{{$longCode := "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"}}
4+
<div class="tw-flex">
5+
<div class="tw-w-[50%] tw-p-4">
6+
<div class="markup render-content">
7+
Inline <code>code</code> content
8+
</div>
9+
10+
<div class="divider"></div>
11+
12+
<div class="markup render-content">
13+
<p>content before</p>
14+
<pre><code>Very long line with no code block or container: {{$longCode}}</code></pre>
15+
<p>content after</p>
16+
</div>
17+
18+
<div class="divider"></div>
19+
20+
<div class="markup render-content">
21+
<p>content before</p>
22+
<div class="code-block-container code-overflow-wrap">
23+
<pre class="code-block"><code>Very long line with wrap: {{$longCode}}</code></pre>
24+
</div>
25+
<p>content after</p>
26+
</div>
27+
28+
<div class="divider"></div>
29+
30+
<div class="markup render-content">
31+
<p>content before</p>
32+
<div class="code-block-container code-overflow-scroll">
33+
<pre class="code-block"><code>Short line in scroll container</code></pre>
34+
</div>
35+
<div class="code-block-container code-overflow-scroll">
36+
<pre class="code-block"><code>Very long line with scroll: {{$longCode}}</code></pre>
37+
</div>
38+
<p>content after</p>
39+
</div>
40+
</div>
41+
42+
<div class="tw-w-[50%] tw-p-4">
43+
<div class="markup render-content">
44+
<p>content before</p>
45+
<div class="code-block-container">
46+
<pre class="code-block"><code class="language-math">
47+
\lim\limits_{n\rightarrow\infty}{\left(1+\frac{1}{n}\right)^n}
48+
</code></pre>
49+
</div>
50+
<p>content after</p>
51+
</div>
52+
53+
<div class="divider"></div>
54+
55+
<div class="markup render-content">
56+
<p>content before</p>
57+
<div class="code-block-container">
58+
<pre class="code-block"><code class="language-mermaid is-loading">
59+
graph LR
60+
A[Square Rect] -- Link text --> B((Circle))
61+
A --> C(Round Rect)
62+
B --> D{Rhombus}
63+
C --> D
64+
</code></pre>
65+
</div>
66+
<p>content after</p>
67+
</div>
68+
</div>
69+
</div>
70+
</div>
71+
{{template "devtest/devtest-footer"}}

Diff for: web_src/css/markup/codecopy.css

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
.markup .code-block,
2-
.markup .mermaid-block {
3-
position: relative;
4-
}
5-
61
.markup .code-copy {
72
position: absolute;
83
top: 8px;
@@ -28,8 +23,8 @@
2823
background: var(--color-secondary-dark-1) !important;
2924
}
3025

31-
.markup .code-block:hover .code-copy,
32-
.markup .mermaid-block:hover .code-copy {
26+
.markup .code-block-container:hover .code-copy,
27+
.markup .code-block:hover .code-copy {
3328
visibility: visible;
3429
animation: fadein 0.2s both;
3530
}

Diff for: web_src/css/markup/content.css

+17-10
Original file line numberDiff line numberDiff line change
@@ -443,13 +443,25 @@
443443
}
444444

445445
.markup pre > code {
446-
padding: 0;
447-
margin: 0;
448446
font-size: 100%;
447+
}
448+
449+
.markup .code-block,
450+
.markup .code-block-container {
451+
position: relative;
452+
}
453+
454+
.markup .code-block-container.code-overflow-wrap pre > code {
449455
white-space: pre-wrap;
450-
overflow-wrap: anywhere;
451-
background: transparent;
452-
border: 0;
456+
}
457+
458+
.markup .code-block-container.code-overflow-scroll pre {
459+
overflow-x: auto;
460+
}
461+
462+
.markup .code-block-container.code-overflow-scroll pre > code {
463+
white-space: pre;
464+
overflow-wrap: normal;
453465
}
454466

455467
.markup .highlight {
@@ -470,16 +482,11 @@
470482
word-break: normal;
471483
}
472484

473-
.markup pre {
474-
word-wrap: normal;
475-
}
476-
477485
.markup pre code,
478486
.markup pre tt {
479487
display: inline;
480488
padding: 0;
481489
line-height: inherit;
482-
word-wrap: normal;
483490
background-color: transparent;
484491
border: 0;
485492
}

Diff for: web_src/js/markup/codecopy.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ export function initMarkupCodeCopy(elMarkup: HTMLElement): void {
1515
const btn = makeCodeCopyButton();
1616
// remove final trailing newline introduced during HTML rendering
1717
btn.setAttribute('data-clipboard-text', el.textContent.replace(/\r?\n$/, ''));
18-
el.after(btn);
18+
// we only want to use `.code-block-container` if it exists, no matter `.code-block` exists or not.
19+
const btnContainer = el.closest('.code-block-container') ?? el.closest('.code-block');
20+
btnContainer.append(btn);
1921
});
2022
}

0 commit comments

Comments
 (0)