From 1c86ac5740e7d3d3b89286a09d76e201c56396a7 Mon Sep 17 00:00:00 2001 From: Heiko Joerg Schick Date: Sat, 21 Mar 2026 15:01:54 +0100 Subject: [PATCH] Add "Remove Unicode Formatting" command to clean Unicode text back to ASCII Co-Authored-By: Claude Sonnet 4.6 --- src/commands.ts | 12 +++++++++++- src/formatter.ts | 6 +++++- src/unicode-maps.ts | 7 +++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/commands.ts b/src/commands.ts index 88c5cca..17b2c14 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -1,5 +1,5 @@ import { Plugin } from "obsidian"; -import { transformText, FormatStyle } from "./formatter"; +import { transformText, cleanText, FormatStyle } from "./formatter"; function addFormatCommand(plugin: Plugin, style: FormatStyle, name: string) { plugin.addCommand({ @@ -18,4 +18,14 @@ export function registerCommands(plugin: Plugin): void { addFormatCommand(plugin, "bold", "Format as Unicode Bold"); addFormatCommand(plugin, "italic", "Format as Unicode Italic"); addFormatCommand(plugin, "bold-italic", "Format as Unicode Bold Italic"); + plugin.addCommand({ + id: "unicode-formatter:clean", + name: "Remove Unicode Formatting", + editorCallback: (editor) => { + const selection = editor.getSelection(); + if (selection) { + editor.replaceSelection(cleanText(selection)); + } + }, + }); } diff --git a/src/formatter.ts b/src/formatter.ts index 36a5908..5b6f1c0 100644 --- a/src/formatter.ts +++ b/src/formatter.ts @@ -1,4 +1,4 @@ -import { BOLD_MAP, ITALIC_MAP, BOLD_ITALIC_MAP } from "./unicode-maps"; +import { BOLD_MAP, ITALIC_MAP, BOLD_ITALIC_MAP, UNICODE_TO_ASCII_MAP } from "./unicode-maps"; export type FormatStyle = "bold" | "italic" | "bold-italic"; @@ -12,3 +12,7 @@ export function transformText(text: string, style: FormatStyle): string { const map = STYLE_MAPS[style]; return [...text].map(ch => map[ch] ?? ch).join(""); } + +export function cleanText(text: string): string { + return [...text].map(ch => UNICODE_TO_ASCII_MAP.get(ch) ?? ch).join(""); +} diff --git a/src/unicode-maps.ts b/src/unicode-maps.ts index d3b5796..3d973db 100644 --- a/src/unicode-maps.ts +++ b/src/unicode-maps.ts @@ -24,3 +24,10 @@ export const ITALIC_MAP: Record = buildMap(0x1D608, 0x1D622); // Sans-Serif Bold-Italic: U+1D63C–U+1D655 (upper), U+1D656–U+1D66F (lower) export const BOLD_ITALIC_MAP: Record = buildMap(0x1D63C, 0x1D656); + +// Reverse map: Unicode symbol → ASCII character +export const UNICODE_TO_ASCII_MAP: Map = new Map( + [BOLD_MAP, ITALIC_MAP, BOLD_ITALIC_MAP].flatMap(m => + Object.entries(m).map(([ascii, unicode]) => [unicode, ascii] as [string, string]) + ) +);