Skip to content

feat(theme): import custom editor themes from a folder - #4318

Open
jamesfredley wants to merge 2 commits into
marktext:developfrom
jamesfredley:feat/import-custom-themes
Open

jamesfredley wants to merge 2 commits into
marktext:developfrom
jamesfredley:feat/import-custom-themes

Conversation

@jamesfredley

Copy link
Copy Markdown
Contributor

Closes #174

Summary

Adds support for user-provided editor themes, a long-standing request (#174). A custom theme is a single <id>.theme.css file placed in <userData>/themes/editor/ (a sibling of the existing <userData>/themes/export/ used for export themes). It is auto-discovered and listed under Custom Themes in the Theme menu and in Preferences -> Theme, and applies to the app like a built-in theme. This also enables the previously-disabled "Open themes folder" / "Import Theme" UI.

How it works:

  • The main process reads <userData>/themes/editor/*.theme.css, parses an optional @name / @type header, sanitizes the CSS, and exposes the themes over a typed IPC contract (mt::themes::list-custom / import-custom / open-folder).
  • The renderer keeps a small registry, applies a custom theme through addThemeStyle's default branch (injected via textContent), and merges custom themes into the preview grid, the mode dropdowns, and the native menu. A custom @type dark theme also drives the dark body class and nativeTheme.themeSource.
  • Import Theme copies a chosen .css into the folder and applies it immediately; Open themes folder reveals the folder for manual drops.

Security

The editor renderer runs with webSecurity: false, so untrusted theme CSS is sanitized in the main process before it is applied: @import, @namespace, @font-face, and any url() / image-set() / image() value (remote, file: and data:) are removed. CSS escapes are decoded first so the denylist cannot be bypassed with, for example, \75 rl(...). Custom themes are therefore colors-only (no external images or fonts). This is best-effort hardening, not a sandbox; the existing "Custom CSS" box remains the raw, unsanitized escape hatch.

Type of change

  • Bug fix (non-breaking, fixes an issue)
  • New feature (non-breaking, adds functionality)
  • Breaking change (causes existing functionality to change)
  • Documentation update

Test plan

  • Unit tests: theme metadata parser, the CSS sanitizer (including escape-bypass regression cases), and the renderer registry.
  • E2E (packages/desktop/test/e2e/custom-themes.spec.ts): seeds themes in <userData>/themes/editor, then asserts IPC discovery, that selecting a custom dark theme from the Theme menu applies body.dark, that the sanitized CSS is injected, and that a custom light theme removes body.dark.
  • Manually validated on Linux (Ubuntu): pnpm lint, pnpm typecheck, pnpm test:unit (579 passing), pnpm build, and pnpm test:e2e (77 passing) all green.

Notes for reviewers [optional]

  • The folder is <userData>/themes/editor/, deliberately separate from <userData>/themes/export/ (export/PDF themes).
  • Imported themes apply immediately and appear in Preferences right away; they appear in the native Theme menu when a window is next opened or the menu is rebuilt. A live file-watcher was intentionally left out to keep this PR focused.
  • The theme file format and a complete example are documented in THEMES.md (the "planned for a future release" placeholder is replaced).
  • The new customThemes / importFailed i18n keys were added to every locale with English values for translators to localize later.

By submitting this pull request, I confirm that my contribution is made under the terms of the MIT license.

Copilot AI review requested due to automatic review settings May 29, 2026 20:45
Add support for user-provided editor themes, addressing the long-standing
request in marktext#174 (and the confusion in marktext#3402 / marktext#3873, where users dropped a
theme file in a folder and expected it to appear in the Theme menu).

A custom theme is a single `<id>.theme.css` file in `<userData>/themes/editor`
(a sibling of the existing `themes/export` used for export themes). The main
process reads the folder, parses an optional `@name` / `@type` header,
sanitizes the CSS, and exposes the themes over typed IPC. The renderer
registers them and applies them through addThemeStyle's default branch; they
appear under "Custom Themes" in the Theme menu and in Preferences -> Theme.
The previously disabled "Open themes folder" / "Import Theme" UI is enabled
and wired up.

Security: the editor renderer runs with webSecurity:false, so custom CSS is
sanitized in the main process before it is applied - @import, @font-face and
any url() / image-set() value (remote, file: and data:) are removed. Custom
themes are therefore colors-only and cannot load external resources.

Adds unit tests (metadata parser, sanitizer, renderer registry), an e2e test
that seeds a theme and verifies discovery + application, and end-user docs
documenting the file format with a complete example.

Closes marktext#174
…istry

Address review feedback on the custom editor themes feature:

- importCustomTheme: remove the copied file when the imported theme cannot
  be read back, so an unreadable file is not left behind to fail every
  future scan or block re-importing the same id.
- loadCustomThemes: clear the renderer registry when a refresh fails, so
  getCustomTheme/getCustomThemes never serve stale descriptors after an
  IPC error (callers treat the empty result as "no custom themes").
- Fix doc comments to consistently reference <userData>/themes/editor.
- Add a regression test asserting the registry is cleared on failed refresh.
@Jocs
Jocs force-pushed the feat/import-custom-themes branch from ac3b38b to af8e1cb Compare October 23, 2026 17:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support customize theme(css styles) in Mark Text

1 participant