Skip to content

[fix] Made dark syntax highlighting follow the theme toggle#285

Merged
nemesifier merged 1 commit into
masterfrom
fix-pygments-theme
Jun 16, 2026
Merged

[fix] Made dark syntax highlighting follow the theme toggle#285
nemesifier merged 1 commit into
masterfrom
fix-pygments-theme

Conversation

@nemesifier

Copy link
Copy Markdown
Member

Sphinx 9 and sphinxawesome-theme 6 gate the dark Pygments palette on @media (prefers-color-scheme: dark), both in the block appended to pygments.css and in the separately media-linked pygments_dark.css.

The theme, however, toggles dark mode with a .dark class on that is persisted in localStorage and can diverge from the OS preference (for example the site is kept dark while the OS turns light at dawn). The code blocks therefore switched palette independently of the rest of the page, producing low-contrast, unreadable syntax highlighting.

Re-scope the dark palette under .dark so the syntax colors track the same toggle as the rest of the theme.

@coderabbitai

coderabbitai Bot commented Jun 16, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The write_pygments_dark_css function in conf.py is modified to change how dark-mode syntax highlighting is applied. Previously the function relied on an OS-level @media (prefers-color-scheme: dark) block inside pygments.css. It now reads pygments.css, strips that media block to keep only the light palette, appends the dark formatter's CSS scoped under .dark .highlight, and overwrites pygments_dark.css with empty content to prevent it from independently applying the dark palette. The function's docstring is updated to reflect this behavior.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes


Caution

Pre-merge checks failed

Please resolve all errors before merging. Addressing warnings is optional.

  • Ignore

❌ Failed checks (1 error)

Check name Status Explanation Resolution
Bug Fixes ❌ Error The PR fixes the root cause (re-scoping CSS under .dark instead of media query) but lacks regression tests. No tests reproduce the bug scenario, verify the CSS modification, or ensure the functio... Add unit tests for write_pygments_dark_css: verify CSS re-scoping, test missing pygments.css handling, and add the missing file existence check (if not pygments_css.exists(): return).
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title uses the correct [fix] prefix and clearly describes the main change: making dark syntax highlighting follow the theme toggle instead of OS preference.
Description check ✅ Passed The description is directly related to the changeset, explaining the problem (syntax highlighting mismatch) and the solution (re-scoping dark palette under .dark class).
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix-pygments-theme

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@conf.py`:
- Around line 527-533: The code on line 527 calls pygments_css.read_text()
without first checking if the file exists, which will raise FileNotFoundError if
Sphinx hasn't generated _static/pygments.css. Add an existence guard before
attempting to read the pygments_css file, using the early return pattern already
established in lines 519-523 to skip this block if the file is not found.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 840f86cd-3fcc-4ed0-a0b5-e4a87500e98d

📥 Commits

Reviewing files that changed from the base of the PR and between 8edf7ab and 850d9d7.

📒 Files selected for processing (1)
  • conf.py
📜 Review details
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: build
🧰 Additional context used
📓 Path-based instructions (1)
**/*.{py,html}

📄 CodeRabbit inference engine (Custom checks)

For Django pull requests: ensure all user-facing strings are marked as translatable using the Django i18n framework

Files:

  • conf.py
🔇 Additional comments (1)
conf.py (1)

511-517: LGTM!

Comment thread conf.py
@github-project-automation github-project-automation Bot moved this from Backlog to In progress in 26.06 Release Jun 16, 2026
@nemesifier nemesifier force-pushed the fix-pygments-theme branch from 850d9d7 to c744820 Compare June 16, 2026 19:01
Sphinx 9 and sphinxawesome-theme 6 gate the dark Pygments palette on
`@media (prefers-color-scheme: dark)`, both in the block appended to
pygments.css and in the separately media-linked pygments_dark.css.

The theme, however, toggles dark mode with a `.dark` class on <html>
that is persisted in localStorage and can diverge from the OS
preference (for example the site is kept dark while the OS turns
light at dawn). The code blocks therefore switched palette
independently of the rest of the page, producing low-contrast,
unreadable syntax highlighting.

Re-scope the dark palette under `.dark` so the syntax colors track
the same toggle as the rest of the theme.
@nemesifier nemesifier force-pushed the fix-pygments-theme branch from c744820 to 2befc94 Compare June 16, 2026 19:14
@nemesifier nemesifier merged commit a622bba into master Jun 16, 2026
5 checks passed
@nemesifier nemesifier deleted the fix-pygments-theme branch June 16, 2026 19:30
@github-project-automation github-project-automation Bot moved this from In progress to Done in 26.06 Release Jun 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant