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:
+11
-1
@@ -1,5 +1,5 @@
|
|||||||
import { Plugin } from "obsidian";
|
import { Plugin } from "obsidian";
|
||||||
import { transformText, FormatStyle } from "./formatter";
|
import { transformText, cleanText, FormatStyle } from "./formatter";
|
||||||
|
|
||||||
function addFormatCommand(plugin: Plugin, style: FormatStyle, name: string) {
|
function addFormatCommand(plugin: Plugin, style: FormatStyle, name: string) {
|
||||||
plugin.addCommand({
|
plugin.addCommand({
|
||||||
@@ -18,4 +18,14 @@ export function registerCommands(plugin: Plugin): void {
|
|||||||
addFormatCommand(plugin, "bold", "Format as Unicode Bold");
|
addFormatCommand(plugin, "bold", "Format as Unicode Bold");
|
||||||
addFormatCommand(plugin, "italic", "Format as Unicode Italic");
|
addFormatCommand(plugin, "italic", "Format as Unicode Italic");
|
||||||
addFormatCommand(plugin, "bold-italic", "Format as Unicode Bold 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
@@ -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";
|
export type FormatStyle = "bold" | "italic" | "bold-italic";
|
||||||
|
|
||||||
@@ -12,3 +12,7 @@ export function transformText(text: string, style: FormatStyle): string {
|
|||||||
const map = STYLE_MAPS[style];
|
const map = STYLE_MAPS[style];
|
||||||
return [...text].map(ch => map[ch] ?? ch).join("");
|
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("");
|
||||||
|
}
|
||||||
|
|||||||
@@ -24,3 +24,10 @@ export const ITALIC_MAP: Record<string, string> = buildMap(0x1D608, 0x1D622);
|
|||||||
|
|
||||||
// Sans-Serif Bold-Italic: U+1D63C–U+1D655 (upper), U+1D656–U+1D66F (lower)
|
// Sans-Serif Bold-Italic: U+1D63C–U+1D655 (upper), U+1D656–U+1D66F (lower)
|
||||||
export const BOLD_ITALIC_MAP: Record<string, string> = buildMap(0x1D63C, 0x1D656);
|
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])
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user