mirror of
https://github.com/ipshing/obsidian-text-transform.git
synced 2026-07-22 08:30:29 +00:00
Improve title case
This commit is contained in:
parent
95e1122a6f
commit
33217ab15c
2 changed files with 40 additions and 44 deletions
10
src/main.ts
10
src/main.ts
|
|
@ -1,5 +1,5 @@
|
|||
import { Editor, Plugin } from "obsidian";
|
||||
import { toTitleCase } from "./text";
|
||||
import { convertToTitleCase } from "./text";
|
||||
import { TextTransformSettingsTab } from "./settings";
|
||||
|
||||
interface TextTransformSettings {
|
||||
|
|
@ -46,7 +46,13 @@ export default class TextTransform extends Plugin {
|
|||
this.addCommand({
|
||||
id: "title-case",
|
||||
name: "Transform to Title Case",
|
||||
editorCallback: (editor) => toTitleCase(editor, this),
|
||||
editorCallback: (editor) => {
|
||||
if (editor.hasFocus()) {
|
||||
let selection = editor.getSelection();
|
||||
selection = convertToTitleCase(selection, this.settings.wordBoundaryChars, this.settings.titleCaseIgnore);
|
||||
this.replaceSelection(editor, selection);
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
console.log("Text Transform plugin loaded");
|
||||
|
|
|
|||
74
src/text.ts
74
src/text.ts
|
|
@ -1,47 +1,37 @@
|
|||
import { Editor } from "obsidian";
|
||||
import TextTransform from "./main";
|
||||
export function convertToTitleCase(text: string, wordBoundaryChars: string[], ignore: string[]): string {
|
||||
// Add space and tab to wordBoundaryChars
|
||||
wordBoundaryChars.push(" ", "\t");
|
||||
|
||||
export function toTitleCase(editor: Editor, plugin: TextTransform) {
|
||||
// Get the selected text
|
||||
const from = editor.getCursor("from");
|
||||
const to = editor.getCursor("to");
|
||||
const selection = editor.getSelection();
|
||||
|
||||
// First put the string into lower case
|
||||
let transformed = selection.toLowerCase();
|
||||
// Replace all chars in wordBoundaryChars with spaces
|
||||
for (const char of plugin.settings.wordBoundaryChars) {
|
||||
transformed = transformed.replace(char, " ");
|
||||
}
|
||||
// Split using space as separator
|
||||
const words = transformed.split(" ");
|
||||
// Transform words
|
||||
transformed = "";
|
||||
for (const word of words) {
|
||||
// Check for ignored words
|
||||
if (plugin.settings.titleCaseIgnore.includes(word)) {
|
||||
transformed += word + " ";
|
||||
continue;
|
||||
}
|
||||
// Transform first character
|
||||
if (word.length > 0) {
|
||||
transformed += word[0].toUpperCase();
|
||||
}
|
||||
// Add in remaining characters
|
||||
if (word.length > 1) {
|
||||
transformed += word.slice(1);
|
||||
}
|
||||
// Add separator (space)
|
||||
transformed += " ";
|
||||
}
|
||||
// Replace spaces with original characters
|
||||
for (const [i, char] of [...selection].entries()) {
|
||||
if (plugin.settings.wordBoundaryChars.includes(char)) {
|
||||
transformed = transformed.slice(0, i) + char + transformed.slice(i + 1);
|
||||
let transformed = "";
|
||||
let word = "";
|
||||
// Iterate the characters and build each word
|
||||
for (const char of text) {
|
||||
if (wordBoundaryChars.includes(char)) {
|
||||
// push 'word' to 'transformed'
|
||||
transformed += convertWord(word, ignore);
|
||||
// push 'char' to 'transformed'
|
||||
transformed += char;
|
||||
// clear 'word'
|
||||
word = "";
|
||||
} else {
|
||||
// push 'char' to 'word'
|
||||
word += char;
|
||||
}
|
||||
}
|
||||
// Push 'word' one last time
|
||||
transformed += convertWord(word, ignore);
|
||||
|
||||
// Replace the selection and re-highlight
|
||||
editor.replaceSelection(transformed.trim());
|
||||
editor.setSelection(from, to);
|
||||
return transformed;
|
||||
}
|
||||
|
||||
function convertWord(word: string, ignore: string[]): string {
|
||||
if (word.length > 0) {
|
||||
// convert to lowercase
|
||||
word = word.toLocaleLowerCase();
|
||||
// only capitalize if not in 'ignore' list
|
||||
if (!ignore.includes(word)) {
|
||||
word = word[0].toLocaleUpperCase() + word.slice(1);
|
||||
}
|
||||
}
|
||||
return word;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue