diff --git a/main.ts b/main.ts index f72ccf1..864b374 100644 --- a/main.ts +++ b/main.ts @@ -5,7 +5,7 @@ import { import { removeWikiLink, removeUrlLink, url2WikiLink, convertWikiLinkToMarkdown } from "src/link"; import { TextFormatSettingTab } from "src/settings/settingTab"; import { FormatSettings, DEFAULT_SETTINGS, CalloutTypeDecider, CustomReplaceBuiltIn } from "src/settings/types"; -import { array2markdown, table2bullet, capitalizeWord, capitalizeSentence, removeAllSpaces, zoteroNote, textWrapper, replaceLigature, ankiSelection, sortTodo, requestAPI, headingLevel, slugify, snakify, extraDoubleSpaces, toTitleCase, customReplace, convertLatex } from "src/format"; +import { array2markdown, table2bullet, capitalizeWord, capitalizeSentence, removeAllSpaces, zoteroNote, textWrapper, replaceLigature, ankiSelection, sortTodo, requestAPI, headingLevel, slugify, snakify, extraDoubleSpaces, toTitleCase, customReplace, convertLatex, camelCase } from "src/format"; import { CustomReplacementBuiltInCommands, LetterCaseCommands } from "src/commands"; import { getString } from "src/langs/langs"; import { selectionBehavior, FormatSelectionReturn } from "src/types"; @@ -145,6 +145,22 @@ export default class TextFormat extends Plugin { this.editorTextFormat(editor, view, "snakify"); }, }); + this.addCommand({ + id: "camel-case-lower", + name: { en: "camelCase selected text", zh: "使用小驼峰格式化选中文本", "zh-TW": "使用小駝峰格式化選取文字" }[lang], + icon: "case-sensitive", + editorCallback: (editor: Editor, view: MarkdownView) => { + this.editorTextFormat(editor, view, "camel-case", { lowerFirst: true }); + }, + }); + this.addCommand({ + id: "camel-case-upper", + name: { en: "CamelCase selected text", zh: "使用大驼峰格式化选中文本", "zh-TW": "使用大駝峰格式化選取文字" }[lang], + icon: "case-sensitive", + editorCallback: (editor: Editor, view: MarkdownView) => { + this.editorTextFormat(editor, view, "camel-case", { lowerFirst: false }); + }, + }); this.addCommand({ id: "heading-upper", @@ -689,6 +705,9 @@ export default class TextFormat extends Plugin { case "snakify": replacedText = snakify(selectedText); break; + case "camel-case": + replacedText = camelCase(selectedText, context.lowerFirst); + break; case "custom-replace": replacedText = customReplace(selectedText, context.settings); break; diff --git a/manifest-beta.json b/manifest-beta.json index 9a20436..87ef66b 100644 --- a/manifest-beta.json +++ b/manifest-beta.json @@ -1,7 +1,7 @@ { "id": "obsidian-text-format", "name": "Text Format", - "version": "3.0.1-b1", + "version": "3.0.2-b1", "minAppVersion": "0.9.7", "description": "Format text such as lowercase/uppercase/capitalize/titlecase, converting order/bullet list, removing redundant spaces/newline characters.", "author": "Benature", diff --git a/src/format.ts b/src/format.ts index 681e434..9354641 100644 --- a/src/format.ts +++ b/src/format.ts @@ -202,9 +202,9 @@ export function array2markdown(content: string): string { return beautify_markdown; } -export function toTitleCase(text: string, settings: FormatSettings): string { +export function toTitleCase(text: string, settings: FormatSettings | null = null): string { // reference: https://github.com/gouch/to-title-case - var properNouns = RegExp(`^(` + settings.ProperNoun.split(",").map((w) => w.trim()).join("|") + `)$`); + var properNouns = RegExp(`^(` + settings?.ProperNoun.split(",").map((w) => w.trim()).join("|") + `)$`); var smallWords = /^(a|an|and|as|at|but|by|en|for|if|in|nor|of|on|or|per|the|to|v.?|vs.?|via)$/i; var alphanumericPattern = /([A-Za-z0-9\u00C0-\u00FF])/; @@ -213,10 +213,10 @@ export function toTitleCase(text: string, settings: FormatSettings): string { return text.split(wordSeparators) .map(function (current: string, index: number, array: string[]): string { - if (current.search(properNouns) > -1) { /* Check for proper nouns */ + if (settings && current.search(properNouns) > -1) { /* Check for proper nouns */ return current; } else { - if (settings.LowercaseFirst) { + if (settings && settings.LowercaseFirst) { current = current.toLowerCase(); } } @@ -524,12 +524,21 @@ export function slugify(text: string, maxLength: number = 76): string { return text; } -export function snakify(text: string, maxLength: number = 76): string { +export function snakify(text: string): string { text = text.toLowerCase(); text = text.replace(/\s+/g, "_"); return text; } +export function camelCase(text: string, lowerFirst = false): string { + text = toTitleCase(text.toLowerCase()); + text = text.replace(/\s+/g, ""); + if (lowerFirst) { + text = text.charAt(0).toLowerCase() + text.slice(1); + } + return text; +} + export function extraDoubleSpaces(editor: Editor, view: MarkdownView): void { if (!view) { return;