Files
obsidian-unicode-formatter/src/formatter.ts
T
2026-03-21 15:46:13 +01:00

49 lines
1.5 KiB
TypeScript

import { BOLD_MAP, ITALIC_MAP, BOLD_ITALIC_MAP, UNICODE_TO_ASCII_MAP } from "./unicode-maps";
export type FormatStyle = "bold" | "italic" | "bold-italic";
const STYLE_MAPS: Record<FormatStyle, Record<string, string>> = {
"bold": BOLD_MAP,
"italic": ITALIC_MAP,
"bold-italic": BOLD_ITALIC_MAP,
};
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("");
}
export function bulletToEmdash(text: string): string {
return text.split("\n").map(line =>
line.startsWith("* ") ? "— " + line.slice(2) : "— " + line
).join("\n");
}
export function bulletToArrow(text: string): string {
return text.split("\n").map(line =>
line.startsWith("* ") ? "→ " + line.slice(2) : "→ " + line
).join("\n");
}
export function numberedListSlash(text: string): string {
let n = 0;
return text.split("\n").map(line => {
n++;
const content = line.startsWith("* ") ? line.slice(2) : line;
return `${n}/ ${content}`;
}).join("\n");
}
export function numberedListParens(text: string): string {
let n = 0;
return text.split("\n").map(line => {
n++;
const content = line.startsWith("* ") ? line.slice(2) : line;
return `(${n}) ${content}`;
}).join("\n");
}