Skip to content

markup: improve code block readability and isolate copy button #34009

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Apr 19, 2025

Conversation

bigdeejay
Copy link
Contributor

@bigdeejay bigdeejay commented Mar 25, 2025

Fix #33197


Summary

This PR improves the rendering of code blocks in markdown content (e.g., README.md) for better readability and UI stability across screen sizes.

Problem

  • Code blocks previously used soft wrapping (pre-wrap), which caused line breaks on narrow or mobile displays.
  • This significantly reduced code readability.
  • The copy button overlapped or misaligned when horizontal scrolling was applied, due to its placement inside the scrolling container.

Changes

  • Updated HTML structure and CSS to:
    • Enable horizontal scrolling (overflow-x: auto) for code blocks
    • Prevent forced line-wrapping by setting white-space: pre
    • Move the copy button into a separate absolutely-positioned container, outside the scrollable area

Result

  • Code remains readable on all screen sizes via scroll instead of wrapping
  • Copy button is visually stable and no longer affected by code block overflow

Affected Files

  • modules/highlight/highlight.go — changed rendered HTML structure
  • modules/markup/markdown/markdown.go — passed format options
  • web_src/css/markup/codecopy.css — copy button isolation
  • web_src/css/markup/content.css — horizontal scroll and white-space handling
  • web_src/js/markup/codecopy.ts — adjusted to handle isolated copy area

Notes

This is a UI/UX refinement that improves the usability of code blocks, especially for developers reading source code on mobile devices or narrow displays.

Animation

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.
@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Mar 25, 2025
@github-actions github-actions bot added modifies/go Pull requests that update Go code modifies/frontend labels Mar 25, 2025
@kyeongkwan
Copy link

Awesome

@silverwind
Copy link
Member

silverwind commented Mar 25, 2025

There are people who prefer to always wrap code (me), and ones who don't. I think instead of this, we need a per-user preference that controls whether we wrap code everywhere or not. Singling out these inline blocks to not wrap seems a bad solution to me, we should either wrap everything or nothing.

PS: Reason I prefer to always wrap is because I think vertical scrollbars are bad UX.

@bigdeejay
Copy link
Contributor Author

To be clear, are you saying we should apply code wrapping consistently—either everywhere or nowhere—based on a user preference?

@wxiaoguang
Copy link
Contributor

I can see indeed there are bugs in current layout and this PR's approach is overall right. However we need to improve more details to make the markup layout work for all cases, will try to figure out the details and make some changes here.

# Conflicts:
#	web_src/css/markup/content.css
#	web_src/js/markup/codecopy.ts
@github-actions github-actions bot added the modifies/templates This PR modifies the template files label Apr 16, 2025
@wxiaoguang
Copy link
Contributor

wxiaoguang commented Apr 16, 2025

Old buggy result:

image

Let's clearly define all layouts:

image

@wxiaoguang wxiaoguang added this to the 1.24.0 milestone Apr 16, 2025
@wxiaoguang wxiaoguang added type/bug type/enhancement An improvement of existing functionality and removed type/bug labels Apr 16, 2025
@GiteaBot GiteaBot removed the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Apr 16, 2025
@GiteaBot GiteaBot added the lgtm/need 1 This PR needs approval from one additional maintainer to be merged. label Apr 16, 2025
@silverwind
Copy link
Member

silverwind commented Apr 16, 2025

I guess we can have these scrolling for now, but in the future, we definitely want one or two per-user options:

  • Wrap code in code views (is enabled)
  • Wrap code in code blocks (was enabled, is disabled with this PR)

Personally, I prefer to always wrap, e.g. both options enabled.

@wxiaoguang
Copy link
Contributor

wxiaoguang commented Apr 16, 2025

I guess we can have these scrolling for now, but in the future, we definitely want one or two per-user options:

That's why I use different CSS classes for wrap/scroll, it's flexible enough and could be configurable in the future.


At the moment, default to "scroll" should be good enough for most users, because it is the GitHub's behavior.

@silverwind
Copy link
Member

Did you test mermaid blocks? Loading state and rendering.

@wxiaoguang
Copy link
Contributor

Did you test mermaid blocks? Loading state and rendering.

image

@wxiaoguang
Copy link
Contributor

wxiaoguang commented Apr 17, 2025

I noticed that a similar fix has already been merged into the main branch through another commit, which seems to reflect the ideas from this PR.

Which one? Maybe because you are working on the main branch in your fork, while Gitea repo's main branch doesn't contain any fix yet?


I didn't see any related commit on the upstream's main branch. When this PR would be merged (there will be a squash merge), you are the "author", not just only "co-author" 😄

@bigdeejay
Copy link
Contributor Author

Thanks, that makes sense — I misunderstood the situation.
Sorry if that caused any confusion! 😅

@lunny lunny added type/bug topic/ui Change the appearance of the Gitea UI labels Apr 18, 2025
@GiteaBot GiteaBot added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Apr 18, 2025
@wxiaoguang wxiaoguang enabled auto-merge (squash) April 19, 2025 00:14
@lunny lunny added the reviewed/wait-merge This pull request is part of the merge queue. It will be merged soon. label Apr 19, 2025
@wxiaoguang wxiaoguang merged commit eda6d65 into go-gitea:main Apr 19, 2025
26 checks passed
@GiteaBot GiteaBot removed the reviewed/wait-merge This pull request is part of the merge queue. It will be merged soon. label Apr 19, 2025
zjjhot added a commit to zjjhot/gitea that referenced this pull request Apr 21, 2025
* giteaofficial/main:
  Add API routes to lock and unlock issues (go-gitea#34165)
  Make ROOT_URL support using request Host header (go-gitea#32564)
  Valid email address should only start with alphanumeric (go-gitea#28174)
  Fix notify watch failure when the content is too long (go-gitea#34233)
  Add "--fullname" arg to gitea admin user create (go-gitea#34241)
  Fix various UI problems (go-gitea#34243)
  markup: improve code block readability and isolate copy button (go-gitea#34009)
  Don't assume the default wiki branch is master in the wiki API (go-gitea#34244)
  [skip ci] Updated translations via Crowdin
  Optimize the calling code of queryElems (go-gitea#34235)
  Actions Runner rest api (go-gitea#33873)
  Fix some trivial problems (go-gitea#34237)
  Swift files can be passed either as file or as form value (go-gitea#34068)

# Conflicts:
#	templates/repo/wiki/revision.tmpl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. modifies/frontend modifies/go Pull requests that update Go code modifies/templates This PR modifies the template files topic/ui Change the appearance of the Gitea UI type/bug type/enhancement An improvement of existing functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Package description in pre tag might cause text go out of borders
7 participants