Skip to content

Build your personal knowledge base with Trilium Notes

feat(editor): let each group of automatic replacements be turned off

Elian Doran494f0e607a4c
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 625204d903..02bcdb7194 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -3153,6 +3153,18 @@
"continue_anyway": "Continue Anyway",
"dont_show_again": "Don't show this warning again"
},
+ "automatic_replacements": {
+ "title": "Automatic replacements",
+ "description": "As you type, some sequences of characters are replaced with the typographic symbol they stand for. This never happens inside a code block or inline code, and pasted text is left alone.",
+ "quotes": "Quotation marks",
+ "quotes_description": "Turns straight quotes into the curved ones of the note's language: \"quote\" becomes “quote” in English, „quote“ in German and « quote » in French.",
+ "punctuation": "Punctuation",
+ "punctuation_description": "Turns ... into an ellipsis (…), and -- and --- surrounded by spaces into an en dash (–) and an em dash (—).",
+ "math": "Mathematical symbols",
+ "math_description": "Turns fractions such as 1/2 into ½, and the operators <=, >=, !=, <- and -> into ≤, ≥, ≠, ← and →.",
+ "symbols": "Copyright and trademark",
+ "symbols_description": "Turns (c), (r) and (tm) into ©, ® and ™."
+ },
"editorfeatures": {
"title": "Features",
"emoji_completion_enabled": "Emoji auto-completion",
diff --git a/apps/client/src/widgets/type_widgets/options/text_notes.tsx b/apps/client/src/widgets/type_widgets/options/text_notes.tsx
index b8acbc679e..0cb5d5c991 100644
--- a/apps/client/src/widgets/type_widgets/options/text_notes.tsx
+++ b/apps/client/src/widgets/type_widgets/options/text_notes.tsx
@@ -33,6 +33,7 @@ export default function TextNoteSettings() {
<OptionsPageHeader />
<FormattingToolbar />
<EditorFeatures />
+ <AutomaticReplacements />
<Editor />
<CodeBlockStyle />
<TableOfContent />
@@ -177,6 +178,54 @@ function EditorFeatures() {
);
}
+/**
+ * The as-you-type replacements, grouped the way CKEditor groups them. Each description names what
+ * the group actually does to your text: the complaints these settings answer were as much about the
+ * behaviour being undocumented as about it being unwanted, so the examples are the point.
+ */
+function AutomaticReplacements() {
+ const [quotesEnabled, setQuotesEnabled] = useTriliumOptionBool("textNoteQuoteReplacementsEnabled");
+ const [punctuationEnabled, setPunctuationEnabled] = useTriliumOptionBool("textNotePunctuationReplacementsEnabled");
+ const [mathEnabled, setMathEnabled] = useTriliumOptionBool("textNoteMathReplacementsEnabled");
+ const [symbolsEnabled, setSymbolsEnabled] = useTriliumOptionBool("textNoteSymbolReplacementsEnabled");
+
+ return (
+ <OptionsSection title={t("automatic_replacements.title")} description={t("automatic_replacements.description")}>
+ <OptionsRowWithToggle
+ name="quote-replacements-enabled"
+ label={t("automatic_replacements.quotes")}
+ description={t("automatic_replacements.quotes_description")}
+ currentValue={quotesEnabled}
+ onChange={setQuotesEnabled}
+ />
+
+ <OptionsRowWithToggle
+ name="punctuation-replacements-enabled"
+ label={t("automatic_replacements.punctuation")}
+ description={t("automatic_replacements.punctuation_description")}
+ currentValue={punctuationEnabled}
+ onChange={setPunctuationEnabled}
+ />
+
+ <OptionsRowWithToggle
+ name="math-replacements-enabled"
+ label={t("automatic_replacements.math")}
+ description={t("automatic_replacements.math_description")}
+ currentValue={mathEnabled}
+ onChange={setMathEnabled}
+ />
+
+ <OptionsRowWithToggle
+ name="symbol-replacements-enabled"
+ label={t("automatic_replacements.symbols")}
+ description={t("automatic_replacements.symbols_description")}
+ currentValue={symbolsEnabled}
+ onChange={setSymbolsEnabled}
+ />
+ </OptionsSection>
+ );
+}
+
function Editor() {
const [headingStyle, setHeadingStyle] = useTriliumOption("headingStyle");
const [autoReadonlySize, setAutoReadonlySize] = useTriliumOption("autoReadonlySizeText");
diff --git a/apps/client/src/widgets/type_widgets/text/config.spec.ts b/apps/client/src/widgets/type_widgets/text/config.spec.ts
index cefebf8044..891b89e03f 100644
--- a/apps/client/src/widgets/type_widgets/text/config.spec.ts
+++ b/apps/client/src/widgets/type_widgets/text/config.spec.ts
@@ -35,6 +35,9 @@ vi.mock("../../../services/options.js", () => ({
get(name: string) {
if (name in optionsState.map) return optionsState.map[name];
if (name === "allowedHtmlTags") return "[]";
+ // The replacement groups ship on, so an unset option here has to answer the way the
+ // real defaults do — otherwise every test would silently run with them all disabled.
+ if (name.startsWith("textNote") && name.endsWith("ReplacementsEnabled")) return "true";
return undefined;
},
getJson(name: string) {
@@ -275,19 +278,40 @@ describe("CK config - language & emoji", () => {
]);
});
- it("leaves CKEditor's own transformations alone for a locale with no mapping", async () => {
+ it("leaves CKEditor's own quotes in force for a locale with no mapping", async () => {
const config = await buildConfig(baseOpts({ contentLanguage: "ku" }));
- // No `typing` override at all, so the default `quotes` group stays in force.
- expect(config.typing).toBeUndefined();
+ // Not removed, so the default `quotes` group still runs — better than a locale we have no
+ // pair for losing quote replacement altogether.
+ expect(config.typing?.transformations.remove).not.toContain("quotes");
+ expect(config.typing?.transformations.extra).toEqual([]);
// The language itself still applies, so right-to-left text lays out correctly regardless.
expect(config.language).toMatchObject({ content: "ku" });
});
- it("keeps the non-quote transformation groups when overriding the quotes", async () => {
+ it("expresses the enabled groups as deltas, leaving CKEditor's defaults to upstream", async () => {
+ // All four groups on: only the quotes are taken over, and only because we supply our own.
+ const allOn = await buildConfig(baseOpts({ contentLanguage: "de" }));
+ expect(allOn.typing?.transformations.remove).toEqual(["quotes"]);
+ expect(allOn.typing?.transformations.extra).toHaveLength(2);
+
+ // Each toggle removes its group by name, so the patterns behind the dashes and fractions
+ // stay upstream's rather than being restated here.
+ optionsState.map.textNotePunctuationReplacementsEnabled = "false";
+ optionsState.map.textNoteMathReplacementsEnabled = "false";
+ optionsState.map.textNoteSymbolReplacementsEnabled = "false";
+ const allOff = await buildConfig(baseOpts({ contentLanguage: "de" }));
+ expect(allOff.typing?.transformations.remove).toEqual(["typography", "mathematical", "symbols", "quotes"]);
+ });
+
+ it("drops the quote replacements entirely when their toggle is off", async () => {
+ optionsState.map.textNoteQuoteReplacementsEnabled = "false";
+
const config = await buildConfig(baseOpts({ contentLanguage: "de" }));
- expect(config.typing?.transformations.include).toEqual(["symbols", "mathematical", "typography"]);
+ // Removed and not re-supplied, so a straight quote stays straight whatever the language.
+ expect(config.typing?.transformations.remove).toContain("quotes");
+ expect(config.typing?.transformations.extra).toEqual([]);
});
it("prefixes the emoji definitions URL with the page origin in dev mode", async () => {
diff --git a/apps/client/src/widgets/type_widgets/text/config.ts b/apps/client/src/widgets/type_widgets/text/config.ts
index b067b4e842..e0e4522097 100644
--- a/apps/client/src/widgets/type_widgets/text/config.ts
+++ b/apps/client/src/widgets/type_widgets/text/config.ts
@@ -1,4 +1,4 @@
-import { type EditorConfig, getCkLocale, SnippetDefinition } from "@triliumnext/ckeditor5";
+import { type EditorConfig, getCkLocale, SnippetDefinition, type TextTransformationConfig } from "@triliumnext/ckeditor5";
import emojiDefinitionsUrl from "@triliumnext/ckeditor5/src/emoji_definitions/en.json?url";
import { ALLOWED_PROTOCOLS, DISPLAYABLE_LOCALE_IDS, formatShortcut, IMAGE_UPLOAD_SUBTYPES, joinShortcut, KATEX_MACROS, MIME_TYPE_AUTO, normalizeMimeTypeForCKEditor } from "@triliumnext/commons";
import i18next from "i18next";
@@ -260,22 +260,7 @@ export async function buildConfig(opts: BuildEditorOptions): Promise<EditorConfi
};
}
- // CKEditor knows quote conventions for three locales and applies the US pair to everything else,
- // which is wrong rather than merely unstyled for most of the languages Trilium ships — a German
- // writer wants „…“, a French one « … ». Resolved here rather than pushed into a live editor
- // because a language change already rebuilds it (see the effect deps in `CKEditorWithWatchdog`).
- const quoteStyle = resolveQuoteStyle(contentLanguage);
- if (quoteStyle) {
- config.typing = {
- transformations: {
- // Upstream's default groups minus `quotes`, whose two transformations are replaced
- // by the locale-specific pair below. Restated rather than filtered with `remove`
- // because the config type requires `include`.
- include: ["symbols", "mathematical", "typography"],
- extra: buildQuoteTransformations(quoteStyle)
- }
- };
- }
+ config.typing = { transformations: buildTransformationsConfig(contentLanguage) };
// Mention customisation.
if (options.get("textNoteCompletionEnabled") === "true") {
@@ -321,6 +306,38 @@ export async function buildConfig(opts: BuildEditorOptions): Promise<EditorConfi
};
}
+/**
+ * Which as-you-type replacements the editor runs, expressed as deltas against CKEditor's own default
+ * set rather than by restating it.
+ *
+ * That is deliberate: the dashes and the fractions are boundary-sensitive regexes upstream — ` -- `
+ * needs its surrounding spaces, and `1/2` needs the guards that stop `11/2` becoming `1½` — and
+ * those are exactly the definitions that break subtly when copied by hand. Naming a group in
+ * `remove` disables it without us ever holding its pattern.
+ *
+ * Quotes are the one exception we do own, because which marks they produce depends on the note's
+ * language and upstream has data for three locales. They are removed and re-supplied — but only when
+ * we actually have a pair for the language, so an unmapped locale keeps falling through to
+ * CKEditor's default rather than losing quote replacement altogether.
+ */
+function buildTransformationsConfig(contentLanguage: string | null): TextTransformationConfig {
+ const remove: string[] = [];
+ if (options.get("textNotePunctuationReplacementsEnabled") !== "true") remove.push("typography");
+ if (options.get("textNoteMathReplacementsEnabled") !== "true") remove.push("mathematical");
+ if (options.get("textNoteSymbolReplacementsEnabled") !== "true") remove.push("symbols");
+
+ const quotesEnabled = options.get("textNoteQuoteReplacementsEnabled") === "true";
+ const quoteStyle = quotesEnabled ? resolveQuoteStyle(contentLanguage) : null;
+ if (!quotesEnabled || quoteStyle) {
+ remove.push("quotes");
+ }
+
+ // `include` is typed as required even though upstream's own documented examples pass `remove`
+ // alone, and the plugin defines the default set in its constructor. Narrowed here rather than
+ // restating the default groups, so that a group upstream adds later keeps working.
+ return { remove, extra: quoteStyle ? buildQuoteTransformations(quoteStyle) : [] } as TextTransformationConfig;
+}
+
/**
* The English editor messages, i.e. the `text-editor.ck` section of the English catalog, mapping
* each derived key to the English text that plugins pass to `editor.t()`. This section is the
diff --git a/packages/ckeditor5/src/index.ts b/packages/ckeditor5/src/index.ts
index 6d318c0e12..4048ce4c19 100644
--- a/packages/ckeditor5/src/index.ts
+++ b/packages/ckeditor5/src/index.ts
@@ -10,7 +10,7 @@ import { COMMON_PLUGINS, CORE_PLUGINS, POPUP_EDITOR_PLUGINS } from "./plugins.js
import { BalloonEditor, DecoupledEditor, FindAndReplaceEditing, FindCommand } from "ckeditor5";
export { default as EditorWatchdog } from "./custom_watchdog";
export { CHAT_INPUT_PLUGINS, MEMO_PLUGINS } from "./plugins.js";
-export type { EditorConfig, MentionFeed, MentionFeedObjectItem, ModelNode, ModelPosition, ModelElement, ModelText, TextTypingTransformationDescription, WatchdogConfig, WatchdogState } from "ckeditor5";
+export type { EditorConfig, MentionFeed, MentionFeedObjectItem, ModelNode, ModelPosition, ModelElement, ModelText, TextTransformationConfig, TextTypingTransformationDescription, WatchdogConfig, WatchdogState } from "ckeditor5";
export type { ClipboardImageEmbedConfig } from "./plugins/clipboard_image_embed.js";
export type { SlashCommandConfig, SlashCommandDefinition } from "./plugins/mention/slash_commands.js";
export type { TriliumMentionFeed } from "./plugins/mention/types.js";
diff --git a/packages/commons/src/lib/options_interface.ts b/packages/commons/src/lib/options_interface.ts
index 38b4965799..61e8c2f009 100644
--- a/packages/commons/src/lib/options_interface.ts
+++ b/packages/commons/src/lib/options_interface.ts
@@ -241,6 +241,17 @@ export interface OptionDefinitions extends KeyboardShortcutsOptions<KeyboardActi
codeNoteTabWidth: number;
codeNoteIndentWithTabs: boolean;
textNoteEditorMultilineToolbar: boolean;
+ /**
+ * Whether a typed `"…"` becomes the typographic quotes of the note's language (`“…”`, `„…“`,
+ * `« … »`). Which marks are produced is decided by the content language, not by this flag.
+ */
+ textNoteQuoteReplacementsEnabled: boolean;
+ /** Whether `...` becomes `…`, and ` -- ` / ` --- ` become an en/em dash, as they are typed. */
+ textNotePunctuationReplacementsEnabled: boolean;
+ /** Whether `1/2`, `!=`, `->` and their kin become `½`, `≠`, `→` as they are typed. */
+ textNoteMathReplacementsEnabled: boolean;
+ /** Whether `(c)`, `(r)` and `(tm)` become `©`, `®` and `™` as they are typed. */
+ textNoteSymbolReplacementsEnabled: boolean;
/** Whether keyboard auto-completion for emojis is triggered when typing `:`. */
textNoteEmojiCompletionEnabled: boolean;
/** Whether keyboard auto-completion for notes is triggered when typing `@` in text notes (attribute editing is not affected). */
diff --git a/packages/trilium-core/src/routes/api/options.ts b/packages/trilium-core/src/routes/api/options.ts
index cfca46b3fd..ba201d8499 100644
--- a/packages/trilium-core/src/routes/api/options.ts
+++ b/packages/trilium-core/src/routes/api/options.ts
@@ -124,6 +124,10 @@ const ALLOWED_OPTIONS = new Set<OptionNames>([
"languages",
"textNoteEditorType",
"textNoteEditorMultilineToolbar",
+ "textNoteQuoteReplacementsEnabled",
+ "textNotePunctuationReplacementsEnabled",
+ "textNoteMathReplacementsEnabled",
+ "textNoteSymbolReplacementsEnabled",
"textNoteEmojiCompletionEnabled",
"textNoteCompletionEnabled",
"textNoteSlashCommandsEnabled",
diff --git a/packages/trilium-core/src/services/options_init.ts b/packages/trilium-core/src/services/options_init.ts
index 941ed35dd6..993c718a0b 100644
--- a/packages/trilium-core/src/services/options_init.ts
+++ b/packages/trilium-core/src/services/options_init.ts
@@ -305,6 +305,13 @@ const defaultOptions: DefaultOption[] = [
// Text note configuration
{ name: "textNoteEditorType", value: "ckeditor-balloon", isSynced: true },
{ name: "textNoteEditorMultilineToolbar", value: "false", isSynced: true },
+ // The four groups of as-you-type replacements. All on, which is how the editor behaved before
+ // they could be turned off; the point of the setting is that the behaviour is now visible and
+ // refusable, not that it changes for anyone who leaves it alone.
+ { name: "textNoteQuoteReplacementsEnabled", value: "true", isSynced: true },
+ { name: "textNotePunctuationReplacementsEnabled", value: "true", isSynced: true },
+ { name: "textNoteMathReplacementsEnabled", value: "true", isSynced: true },
+ { name: "textNoteSymbolReplacementsEnabled", value: "true", isSynced: true },
{ name: "textNoteEmojiCompletionEnabled", value: "true", isSynced: true },
{ name: "textNoteCompletionEnabled", value: "true", isSynced: true },
{ name: "textNoteSlashCommandsEnabled", value: "true", isSynced: true },