Skip to content

Commit

Permalink
new command: title case
Browse files Browse the repository at this point in the history
  • Loading branch information
Benature committed Jun 11, 2021
1 parent aa84ad9 commit 04ffd97
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 34 deletions.
19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,30 @@ Sometimes I encounter some issues like
1. I copy some text from pdf or some other source, but the copied content is out of format. For example, there are more than one space between words or one paragraph brokes into several lines.
2. I want to lowercase the letters when they are all uppercase, etc.

Therefore, I wrote this plugin to format selected text lowercase/uppercase/capitalize or remove redundant spaces/newline characters.
Therefore, I wrote this plugin to format selected text lowercase/uppercase/capitalize/titlecase or remove redundant spaces/newline characters.

## Features

Press <kbd>cmd/ctrl+P</kbd> to enter the command. 👇

Or you can consider to bind custom hotkeys to those commands.

| Command | Description |
| :----------------------------------------- | ------------------------------------------------- |
| Lowercase selected text | Lowercase all letters in selection |
| Uppercase selected text | Uppercase all letters in selection |
| Capitalize selected text | Capitalize all words in selection |
| Remove redundant spaces in selection | Ensure only one space between words |
| Remove all newline characters in selection | Change multi-line selection into single-line text |
| Command | Description |
| :----------------------------------------- | ------------------------------------------------------------------- |
| Lowercase selected text | Lowercase all letters in selection |
| Uppercase selected text | Uppercase all letters in selection |
| Capitalize selected text | Capitalize all words in selection |
| Title case selected text | Capitalize words but leave certain words in lower case in selection |
| Remove redundant spaces in selection | Ensure only one space between words |
| Remove all newline characters in selection | Change multi-line selection into single-line text |

## Example


- `lowercase`: "Hello, I am using Obsidian." -> "hello, i am using obsidian."
- `uppercase`: "Hello, I am using Obsidian." -> "HELLO, I AM USING OBSIDIAN."
- `capitalize`: "Hello, I am using Obsidian." -> "Hello, I Am Using Obsidian."
- `titlecase`: "Obsidian is a good app." -> "Obsidian Is a Good App."
- `remove blanks`: "There are so many redundant blanks" -> "There are so many redundant blanks"

![demo](demo.gif)
25 changes: 19 additions & 6 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { MarkdownView, Plugin } from "obsidian";

import "node_modules/@gouch/to-title-case/to-title-case";

export default class Underline extends Plugin {
async onload() {
this.addCommand({
Expand All @@ -17,6 +19,11 @@ export default class Underline extends Plugin {
name: "Capitalize selected text",
callback: () => this.textFormat("capitalize"),
});
this.addCommand({
id: "text-format-titlecase",
name: "Title case selected text",
callback: () => this.textFormat("titlecase"),
});
this.addCommand({
id: "text-format-remove-spaces",
name: "Remove redundant spaces in selection",
Expand Down Expand Up @@ -49,6 +56,9 @@ export default class Underline extends Plugin {
case "capitalize":
replacedText = toTitleCase(selectedText);
break;
case "titlecase":
replacedText = selectedText.toTitleCase();
break;
case "spaces":
replacedText = selectedText.replace(/ +/g, " ");
// replacedText = replacedText.replace(/\n /g, "\n"); // when a single space left at the head of the line
Expand All @@ -67,10 +77,13 @@ export default class Underline extends Plugin {
}
}

function toTitleCase(phrase: string): string {
return phrase
.toLowerCase()
.split(" ")
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
.join(" ");
function toTitleCase(s: string): string {
return s.replace(/\w\S*/g, function (t) {
return t.charAt(0).toUpperCase() + t.substr(1).toLowerCase();
});
// return s
// .toLowerCase()
// .split(" ")
// .map((word) => word.charAt(0).toUpperCase() + word.slice(1))
// .join(" ");
}
4 changes: 2 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"id": "obsidian-text-format",
"name": "Text Format",
"version": "0.1.0",
"version": "0.2.0",
"minAppVersion": "0.9.7",
"description": "Format selected text lowercase/uppercase/capitalize or remove redundant spaces/newline characters.",
"description": "Format selected text lowercase/uppercase/capitalize/titlecase or remove redundant spaces/newline characters.",
"author": "Benature",
"authorUrl": "https://github.com/Benature",
"isDesktopOnly": false
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,8 @@
"rollup": "^2.32.1",
"tslib": "^2.2.0",
"typescript": "^4.2.4"
},
"dependencies": {
"@gouch/to-title-case": "^2.2.1"
}
}
31 changes: 13 additions & 18 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@
import typescript from '@rollup/plugin-typescript';
import {nodeResolve} from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import typescript from "@rollup/plugin-typescript";
import { nodeResolve } from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";

const isProd = (process.env.BUILD === 'production');
const isProd = process.env.BUILD === "production";

const banner =
`/*
const banner = `/*
THIS IS A GENERATED/BUNDLED FILE BY ROLLUP
if you want to view the source visit the plugins github repository
*/
`;

export default {
input: 'main.ts',
input: "main.ts",
output: {
dir: '.',
sourcemap: 'inline',
dir: ".",
sourcemap: "inline",
sourcemapExcludeSources: isProd,
format: 'cjs',
exports: 'default',
format: "cjs",
exports: "default",
banner,
},
external: ['obsidian'],
plugins: [
typescript(),
nodeResolve({browser: true}),
commonjs(),
]
};
external: ["obsidian"],
plugins: [typescript(), nodeResolve({ browser: true }), commonjs()],
};

0 comments on commit 04ffd97

Please sign in to comment.