Skip to content

Build your personal knowledge base with Trilium Notes

feat(appearance): add a setting to turn off programming ligatures in code

Elian Doranc9dec8e2cd7d
diff --git a/apps/client/src/stylesheets/style.css b/apps/client/src/stylesheets/style.css
index f0dc786ce3..e7ec4f025c 100644
--- a/apps/client/src/stylesheets/style.css
+++ b/apps/client/src/stylesheets/style.css
@@ -284,6 +284,20 @@ samp {
font-family: var(--monospace-font-family) !important;
}
+/*
+ * Programming ligatures: the bundled JetBrains Mono renders `!=` as `≠`, `->` as `→` and so on,
+ * which reads as the characters having been silently replaced (see #2851). The `monospaceLigatures-
+ * Enabled` option gates this; the class is applied by RootContainer.
+ *
+ * Scoped to the monospace surfaces rather than set on `body`: `font-variant-ligatures` is inherited,
+ * so a blanket rule would also disable `fi`/`fl` in prose, which is a typographic regression in note
+ * content and not what anyone is asking for. Inheritance is what makes `.cm-editor` enough to cover
+ * CodeMirror's internals.
+ */
+body.monospace-ligatures-disabled :is(code, kbd, pre, samp, .cm-editor, .ck-mermaid__editing-view) {
+ font-variant-ligatures: none;
+}
+
.badge {
--bs-badge-color: var(--muted-text-color);
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index d60bb0e209..ee59f07eff 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -1773,6 +1773,8 @@
"note_detail_font": "Document text",
"monospace_font": "Monospace text",
"monospace_font_description": "Used for code notes and code blocks",
+ "monospace_ligatures": "Programming ligatures",
+ "monospace_ligatures_description": "Render character pairs such as != and -> as single symbols (≠, →), if the monospace font supports it. The text itself is unchanged.",
"size_relative_to_general": "Size is relative to the general font size",
"not_all_fonts_available": "Not all listed fonts may be available on your system",
"apply_changes": "Reload to apply changes",
diff --git a/apps/client/src/widgets/containers/root_container.ts b/apps/client/src/widgets/containers/root_container.ts
index e0a5c0855b..314f1ec392 100644
--- a/apps/client/src/widgets/containers/root_container.ts
+++ b/apps/client/src/widgets/containers/root_container.ts
@@ -52,6 +52,7 @@ export default class RootContainer extends FlexContainer<BasicWidget> {
this.#setMotion();
this.#setShadows();
this.#setBackdropEffects();
+ this.#setMonospaceLigatures();
updateThemeCapabilities();
this.#setLocaleAndDirection(options.get("locale"));
this.#setExperimentalFeatures();
@@ -81,6 +82,10 @@ export default class RootContainer extends FlexContainer<BasicWidget> {
this.#setBackdropEffects();
}
+ if (loadResults.isOptionReloaded("monospaceLigaturesEnabled")) {
+ this.#setMonospaceLigatures();
+ }
+
if (loadResults.isOptionReloaded("maxContentWidth")
|| loadResults.isOptionReloaded("centerContent")) {
@@ -133,6 +138,16 @@ export default class RootContainer extends FlexContainer<BasicWidget> {
document.body.classList.toggle("backdrop-effects-disabled", !enabled);
}
+ /**
+ * Ligatures are a property of how the monospace font renders, not of which font is selected, so
+ * this rides a body class rather than the server-generated fonts stylesheet — no round-trip to
+ * `api/fonts` is needed to flip it.
+ */
+ #setMonospaceLigatures() {
+ const enabled = options.is("monospaceLigaturesEnabled");
+ document.body.classList.toggle("monospace-ligatures-disabled", !enabled);
+ }
+
#setExperimentalFeatures() {
for (const featureId of getEnabledExperimentalFeatureIds()) {
document.body.classList.add(`experimental-feature-${featureId}`);
diff --git a/apps/client/src/widgets/type_widgets/options/appearance.tsx b/apps/client/src/widgets/type_widgets/options/appearance.tsx
index b93026dd99..2d94e4870b 100644
--- a/apps/client/src/widgets/type_widgets/options/appearance.tsx
+++ b/apps/client/src/widgets/type_widgets/options/appearance.tsx
@@ -404,6 +404,7 @@ function OrientationIllustration({ orientation }: { orientation: "vertical" | "h
function Fonts() {
const [ overrideThemeFonts, setOverrideThemeFonts ] = useTriliumOptionBool("overrideThemeFonts");
+ const [ ligaturesEnabled, setLigaturesEnabled ] = useTriliumOptionBool("monospaceLigaturesEnabled");
const isEnabled = overrideThemeFonts === true;
return (
@@ -420,6 +421,19 @@ function Fonts() {
<Font label={t("fonts.note_tree_font")} sizeDescription={t("fonts.size_relative_to_general")} fontFamilyOption="treeFontFamily" fontSizeOption="treeFontSize" disabled={!isEnabled} />
<Font label={t("fonts.note_detail_font")} sizeDescription={t("fonts.size_relative_to_general")} fontFamilyOption="detailFontFamily" fontSizeOption="detailFontSize" disabled={!isEnabled} />
<Font label={t("fonts.monospace_font")} description={t("fonts.monospace_font_description")} fontFamilyOption="monospaceFontFamily" fontSizeOption="monospaceFontSize" disabled={!isEnabled} isMonospace />
+
+ {/*
+ * Deliberately not gated behind `overrideThemeFonts` like the rows above: the ligatures
+ * come from the *theme's* monospace font, so the setting is needed exactly when custom
+ * fonts are off. Gating it would put it out of reach of everyone affected.
+ */}
+ <OptionsRowWithToggle
+ name="monospace-ligatures-enabled"
+ label={t("fonts.monospace_ligatures")}
+ description={t("fonts.monospace_ligatures_description")}
+ currentValue={ligaturesEnabled}
+ onChange={setLigaturesEnabled}
+ />
</OptionsSection>
);
}
diff --git a/packages/commons/src/lib/options_interface.ts b/packages/commons/src/lib/options_interface.ts
index 973f936448..b2ee7c0165 100644
--- a/packages/commons/src/lib/options_interface.ts
+++ b/packages/commons/src/lib/options_interface.ts
@@ -177,6 +177,12 @@ export interface OptionDefinitions extends KeyboardShortcutsOptions<KeyboardActi
hasUserBackendScripts: boolean;
isPasswordSet: boolean;
overrideThemeFonts: boolean;
+ /**
+ * Whether the monospace font's programming ligatures (`!=` rendered as `≠`, `->` as `→`) are
+ * left on. Off by default: the bundled JetBrains Mono ships them enabled, so users get them
+ * without choosing them and repeatedly mistake them for their characters having been replaced.
+ */
+ monospaceLigaturesEnabled: boolean;
spellCheckEnabled: boolean;
autoFixConsistencyIssues: boolean;
vimKeymapEnabled: boolean;
diff --git a/packages/trilium-core/src/routes/api/options.ts b/packages/trilium-core/src/routes/api/options.ts
index 784e6addce..f52816d53c 100644
--- a/packages/trilium-core/src/routes/api/options.ts
+++ b/packages/trilium-core/src/routes/api/options.ts
@@ -58,6 +58,7 @@ const ALLOWED_OPTIONS = new Set<OptionNames>([
"detailFontFamily",
"monospaceFontSize",
"monospaceFontFamily",
+ "monospaceLigaturesEnabled",
"openNoteContexts",
"vimKeymapEnabled",
"codeLineWrapEnabled",
diff --git a/packages/trilium-core/src/services/options_init.ts b/packages/trilium-core/src/services/options_init.ts
index 81ea77289d..fa92f5c42c 100644
--- a/packages/trilium-core/src/services/options_init.ts
+++ b/packages/trilium-core/src/services/options_init.ts
@@ -162,6 +162,10 @@ const defaultOptions: DefaultOption[] = [
{ name: "detailFontSize", value: "110", isSynced: false },
{ name: "monospaceFontFamily", value: "theme", isSynced: false },
{ name: "monospaceFontSize", value: "110", isSynced: false },
+ // Off rather than on: the theme default is JetBrains Mono, whose ligatures nobody opted into,
+ // and which have been reported as characters being replaced often enough to be worth defaulting
+ // away from (#2851, #6224). Not synced, matching the font options it sits with in the UI.
+ { name: "monospaceLigaturesEnabled", value: "false", isSynced: false },
{ name: "spellCheckEnabled", value: "true", isSynced: false },
{ name: "spellCheckLanguageCode", value: "en-US", isSynced: false },
{ name: "imageMaxWidthHeight", value: "2000", isSynced: true },