Skip to content

Fix: Update markdown image for dark/light mode#36922

Open
mykh-hailo wants to merge 2 commits intogo-gitea:mainfrom
mykh-hailo:fix/markdown-dark-light-mode
Open

Fix: Update markdown image for dark/light mode#36922
mykh-hailo wants to merge 2 commits intogo-gitea:mainfrom
mykh-hailo:fix/markdown-dark-light-mode

Conversation

@mykh-hailo
Copy link

Description

This PR improves how Gitea renders Markdown images for dark/light mode to match GitHub behavior more closely.

Closes: #35545

Markdown image dark/light switching

  • Enhanced the Markdown HTML sanitizer to allow GitHub’s / elements and required attributes (media, srcset).

  • Updated the Markdown post-processing so that:

    • The fallback inside remains a direct child (so browsers correctly apply <source media="(prefers-color-scheme: ...)" ...> selection).

    • <source srcset="..."> URLs are rewritten/resolved consistently with how <img src="..."> URLs are handled.

CSS behavior

Added the prefers-color-scheme rules for #gh-dark-mode-only / #gh-light-mode-only so the correct image is shown automatically in dark vs light mode.

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Mar 18, 2026
@github-actions github-actions bot added modifies/go Pull requests that update Go code modifies/frontend labels Mar 18, 2026
@bircni
Copy link
Contributor

bircni commented Mar 18, 2026

The prefers-color-scheme responds to OS preferences. If a user has Gitea explicitly set to dark mode while their OS is in light mode, #gh-dark-mode-only images will be hidden.

@mykh-hailo
Copy link
Author

@bircni I updated my code for you comments.
Appreciate for you all kind comments and feedback.

@mykh-hailo
Copy link
Author

@wxiaoguang can you check my for this issue please?

@wxiaoguang
Copy link
Contributor

wxiaoguang commented Mar 19, 2026

  1. it needs tests.
  2. users can have custom themes, so the selector data-theme^="gitea-auto" still doesn't really work.

@wxiaoguang
Copy link
Contributor

One of the feasible approaches is "calling isDarkTheme() in JS and add some class/attribute to the HTML body, then use it as selector", and you also need to watch the change of "prefers-color-scheme".

@wxiaoguang wxiaoguang marked this pull request as draft March 19, 2026 11:11
@wxiaoguang
Copy link
Contributor

By the way, this PR is unnecessarily complex.

You can simply allow the media attribute, and also make #gh-dark-mode-only work.

I don't understand why you need to do these: extract the "picture" node / parse the attributes / transform to other layouts.

@mykh-hailo
Copy link
Author

@wxiaoguang My approach works like this:

The <picture> element chooses which image source to use based on the prefers-color-scheme media query.
To make it follow a selected theme (instead of the system theme), I update the prefers-color-scheme condition so it matches the theme I want to use.
For example, I modify the media attribute to include both (prefers-color-scheme: dark) and (prefers-color-scheme: light), depending on the selected theme. This ensures the correct image source is chosen for that specific theme—similar to how GitHub handles it.

@wxiaoguang
Copy link
Contributor

I think we shouldn't touch picture transforming. The syntax is very complex https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/picture. We never supported that, so no need to introduce a new support for it.

The only thing need to do is to support #gh-....-mode-only correctly at the moment.

@mykh-hailo mykh-hailo force-pushed the fix/markdown-dark-light-mode branch from 93bd290 to 5217897 Compare March 19, 2026 22:47
@github-actions github-actions bot removed the modifies/go Pull requests that update Go code label Mar 19, 2026
@mykh-hailo mykh-hailo force-pushed the fix/markdown-dark-light-mode branch from 5217897 to 1b5c9f6 Compare March 19, 2026 22:52
@mykh-hailo
Copy link
Author

@wxiaoguang thank you for the quick update.
I updated my code for your comments.
Could you possibly review it please?
Thank you?

@mykh-hailo mykh-hailo marked this pull request as ready for review March 19, 2026 22:57
@wxiaoguang
Copy link
Contributor

I think it can be like this e1156fe

@mykh-hailo
Copy link
Author

@wxiaoguang I'd appreciate it if you can give me the ETA for the merge please.
Thank you.

@wxiaoguang
Copy link
Contributor

Wait for 2 maintainer's approvals.

By the way, please update the PR's description

@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Mar 20, 2026
@lunny lunny added the type/enhancement An improvement of existing functionality label Mar 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lgtm/need 1 This PR needs approval from one additional maintainer to be merged. modifies/frontend type/enhancement An improvement of existing functionality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support for dark mode alternate images in markdown

5 participants