Add "Remove Unicode Formatting" command to clean Unicode text back to ASCII

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-21 15:01:54 +01:00
parent e7bcc6f789
commit 1c86ac5740
3 changed files with 23 additions and 2 deletions
+11 -1
View File
@@ -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));
}
},
});
}
+5 -1
View File
@@ -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("");
}
+7
View File
@@ -24,3 +24,10 @@ export const ITALIC_MAP: Record<string, string> = buildMap(0x1D608, 0x1D622);
// Sans-Serif Bold-Italic: U+1D63CU+1D655 (upper), U+1D656U+1D66F (lower)
export const BOLD_ITALIC_MAP: Record<string, string> = buildMap(0x1D63C, 0x1D656);
// Reverse map: Unicode symbol → ASCII character
export const UNICODE_TO_ASCII_MAP: Map<string, string> = new Map(
[BOLD_MAP, ITALIC_MAP, BOLD_ITALIC_MAP].flatMap(m =>
Object.entries(m).map(([ascii, unicode]) => [unicode, ascii] as [string, string])
)
);