Skip to content

Commit ae828d8

Browse files
committed
markup: add horizontal scroll to code blocks and isolate copy button
Previously, code blocks did not support horizontal scrolling, leading to poor readability on mobile or narrow displays due to forced line wrapping. This patch updates the HTML structure and CSS so that: - Code blocks now scroll horizontally instead of wrapping lines - The copy button is placed in a separate wrapper to avoid layout interference These changes improve overall accessibility and usability for markdown-rendered content in the Gitea interface.
1 parent 41c946a commit ae828d8

File tree

5 files changed

+19
-12
lines changed

5 files changed

+19
-12
lines changed

modules/highlight/highlight.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func CodeFromLexer(lexer chroma.Lexer, code string) template.HTML {
131131

132132
_ = htmlw.Flush()
133133
// Chroma will add newlines for certain lexers in order to highlight them properly
134-
// Once highlighted, strip them here, so they don't cause copy/paste trouble in HTML output
134+
// // Once highlighted, strip them here, so they don't cause copy/paste trouble in HTML output
135135
return template.HTML(strings.TrimSuffix(htmlbuf.String(), "\n"))
136136
}
137137

modules/markup/markdown/markdown.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ 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)
89+
err := r.ctx.RenderInternal.FormatWithSafeAttrs(w, `<div class="code-wrapper"><pre class="%s">`, preClasses)
9090
if err != nil {
9191
return
9292
}
@@ -99,7 +99,7 @@ func (r *GlodmarkRender) highlightingRenderer(w util.BufWriter, c highlighting.C
9999
return
100100
}
101101
} else {
102-
_, err := w.WriteString("</code></pre>")
102+
_, err := w.WriteString("</code></pre></div>")
103103
if err != nil {
104104
return
105105
}
@@ -109,7 +109,7 @@ func (r *GlodmarkRender) highlightingRenderer(w util.BufWriter, c highlighting.C
109109
// SpecializedMarkdown sets up the Gitea specific markdown extensions
110110
func SpecializedMarkdown(ctx *markup.RenderContext) *GlodmarkRender {
111111
// TODO: it could use a pool to cache the renderers to reuse them with different contexts
112-
// at the moment it is fast enough (see the benchmarks)
112+
// at the moment it is fast enough (see the benchmarks
113113
r := &GlodmarkRender{ctx: ctx}
114114
r.goldmarkMarkdown = goldmark.New(
115115
goldmark.WithExtensions(

web_src/css/markup/codecopy.css

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
1-
.markup .code-block,
1+
.markup .code-wrapper,
22
.markup .mermaid-block {
33
position: relative;
44
}
55

6+
.code-wrapper {
7+
background-color: var(--color-markup-code-block);
8+
border-radius: var(--border-radius);
9+
}
10+
11+
.code-block {
12+
margin-right: 56px;
13+
}
14+
615
.markup .code-copy {
716
position: absolute;
817
top: 8px;
918
right: 6px;
1019
padding: 9px;
11-
visibility: hidden;
20+
/*visibility: hidden;*/
1221
animation: fadeout 0.2s both;
1322
}
1423

@@ -28,7 +37,7 @@
2837
background: var(--color-secondary-dark-1) !important;
2938
}
3039

31-
.markup .code-block:hover .code-copy,
40+
.markup .code-wrapper:hover .code-copy,
3241
.markup .mermaid-block:hover .code-copy {
3342
visibility: visible;
3443
animation: fadein 0.2s both;

web_src/css/markup/content.css

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@
455455
padding: 0;
456456
margin: 0;
457457
font-size: 100%;
458-
white-space: pre-wrap;
458+
white-space: pre;
459459
word-break: break-all;
460460
overflow-wrap: break-word;
461461
background: transparent;
@@ -471,8 +471,6 @@
471471
padding: 16px;
472472
font-size: 85%;
473473
line-height: 1.45;
474-
background-color: var(--color-markup-code-block);
475-
border-radius: var(--border-radius);
476474
}
477475

478476
.markup .highlight pre {

web_src/js/markup/codecopy.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ export function makeCodeCopyButton(): HTMLButtonElement {
88
}
99

1010
export function initMarkupCodeCopy(elMarkup: HTMLElement): void {
11-
const el = elMarkup.querySelector('.code-block code'); // .markup .code-block code
11+
const el = elMarkup.querySelector('.code-wrapper'); // .markup .code-block code
1212
if (!el || !el.textContent) return;
1313

1414
const btn = makeCodeCopyButton();
1515
// remove final trailing newline introduced during HTML rendering
1616
btn.setAttribute('data-clipboard-text', el.textContent.replace(/\r?\n$/, ''));
17-
el.after(btn);
17+
el.append(btn);
1818
}

0 commit comments

Comments
 (0)