Skip to content

Commit 1b5c9f6

Browse files
committed
Fix: Update markdown image for dark/light mode
1 parent 068d7a5 commit 1b5c9f6

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

web_src/css/markup/content.css

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,26 @@ In markup content, we always use bottom margin for all elements */
316316
box-sizing: initial;
317317
}
318318

319+
/* GitHub-style dark/light image switching.
320+
These fragments are kept in the rendered HTML as part of the `src` attribute.
321+
We decide which set to show via `data-gitea-theme` on `<html>`, which is
322+
mirrored from `--is-dark-theme` in JS (so it also works with custom themes).
323+
*/
324+
.markup img[src*="#gh-dark-mode-only"],
325+
.markup img[src*="#gh-light-mode-only"] {
326+
display: none;
327+
}
328+
329+
html[data-gitea-theme="dark"] .markup img[src*="#gh-dark-mode-only"] {
330+
display: inline;
331+
}
332+
333+
html[data-gitea-theme="light"] .markup img[src*="#gh-light-mode-only"] {
334+
display: inline;
335+
}
336+
337+
/* `data-gitea-theme` is managed by JS using `isDarkTheme()` (respects explicit theme + auto). */
338+
319339
.file-view.markup {
320340
padding: 1em 2em;
321341
font-size: 16px;

web_src/js/features/common-page.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {showGlobalErrorMessage} from '../bootstrap.ts';
33
import {fomanticQuery} from '../modules/fomantic/base.ts';
44
import {addDelegatedEventListener, queryElems} from '../utils/dom.ts';
55
import {registerGlobalInitFunc, registerGlobalSelectorFunc} from '../modules/observer.ts';
6+
import {isDarkTheme} from '../utils.ts';
67
import {initAvatarUploaderWithCropper} from './comp/Cropper.ts';
78
import {initCompSearchRepoBox} from './comp/SearchRepoBox.ts';
89

@@ -45,10 +46,27 @@ function initFooterThemeSelector() {
4546
});
4647
}
4748

49+
function initMarkdownDarkLightImages() {
50+
// Mirror Gitea's current "darkness" into a dataset so markup/CSS can
51+
// switch `#gh-dark-mode-only` / `#gh-light-mode-only` images correctly
52+
// for both explicit themes and "auto" themes.
53+
const root = document.documentElement;
54+
const sync = () => {
55+
root.setAttribute('data-gitea-theme', isDarkTheme() ? 'dark' : 'light');
56+
};
57+
sync();
58+
59+
// Track system theme changes in case Gitea is set to "auto".
60+
const mql = window.matchMedia('(prefers-color-scheme: dark)');
61+
const onChange = () => sync();
62+
mql.addEventListener('change', onChange);
63+
}
64+
4865
export function initCommmPageComponents() {
4966
initHeadNavbarContentToggle();
5067
initFooterLanguageMenu();
5168
initFooterThemeSelector();
69+
initMarkdownDarkLightImages();
5270
}
5371

5472
export function initGlobalDropdown() {

0 commit comments

Comments
 (0)