Skip to content

Commit

Permalink
[FR] Camel Case #89
Browse files Browse the repository at this point in the history
  • Loading branch information
Benature committed Apr 2, 2024
1 parent 78cc39e commit dd2eb27
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
21 changes: 20 additions & 1 deletion main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion manifest-beta.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
19 changes: 14 additions & 5 deletions src/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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])/;
Expand All @@ -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();
}
}
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit dd2eb27

Please sign in to comment.