Skip to content

Commit

Permalink
heading upper/lower
Browse files Browse the repository at this point in the history
  • Loading branch information
Benature committed Jan 7, 2024
1 parent 4d6f0ef commit 025e98f
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 4 deletions.
29 changes: 27 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ Or you can consider to bind custom hotkeys to those commands according to [#29](

---

- [Text Format](#text-format)
- [Features](#features)
- [Basic](#basic)
- [Markdown Grammar](#markdown-grammar)
- [List](#list)
- [Links](#links)
- [PDF copy / OCR](#pdf-copy--ocr)
- [Academic/Study](#academicstudy)
- [Zotero format](#zotero-format)
- [Convert citation index to the file name of paper note](#convert-citation-index-to-the-file-name-of-paper-note)
- [Others](#others)
- [Wrapper](#wrapper)
- [Support](#support)
- [Some Examples](#some-examples)


⚙️: There is setting of this command.

### Basic
Expand All @@ -38,15 +54,24 @@ Or you can consider to bind custom hotkeys to those commands according to [#29](
| **Slugify**selected text | convert any input text into a URL-friendly slug |
| **Snakify** selected text | Lowercase all letters in selection *and* replace spaces with underscores |

### List
### Markdown Grammar

| Command | Description |
| ------------- | -------------------------------------------------------------------- |
| Heading Upper | e.g.: `# Heading` -> `## Heading` (default shortcut: `Ctrl+Shift+]`) |
| Heading Lower | e.g.: `## Heading` -> `# Heading` (default shortcut: `Ctrl+Shift+[`) |


#### List
| Command | Description |
| ---------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------- |
| Format **bullet** list ⚙️ | Change `` into bullet list, i.e. `- `; split every bullet point into single line; and remove blank lines. |
| Format **ordered** list | Change `*)`(star could be any letter) into ordered list (e.g. `1. `, `2. `); split every ordered point into single line; and remove blank lines. (#4) |
| Convert table to bullet list | The first volume is 1st list, other volumes are sub-list |
| Convert table to bullet list with header | Sub-list begins with `${header}: ` |
| **Sort to-do** list | [#37](https://github.com/Benature/obsidian-text-format/issues/37) |
### Links

#### Links

| Command | Description |
| ------------------------------------------- | -------------------------------------------------- |
Expand Down
36 changes: 35 additions & 1 deletion main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
ankiSelection,
sortTodo,
requestAPI,
headingLevel,
slugify,
snakify
} from "src/format";
Expand All @@ -41,6 +42,28 @@ export default class TextFormat extends Plugin {
await this.loadSettings();
this.addSettingTab(new TextFormatSettingTab(this.app, this));

this.addCommand({
id: "text-format-heading-upper",
name: "Heading upper",
callback: () => this.textFormat("heading", true),
hotkeys: [
{
modifiers: ["Ctrl", "Shift"],
key: "]",
},
],
});
this.addCommand({
id: "text-format-heading-lower",
name: "Heading lower",
callback: () => this.textFormat("heading", false),
hotkeys: [
{
modifiers: ["Ctrl", "Shift"],
key: "[",
},
],
});
this.settings.WrapperList.forEach((wrapper, index) => {
this.addCommand({
id: `text-format-wrapper-${index}`,
Expand Down Expand Up @@ -296,6 +319,7 @@ export default class TextFormat extends Plugin {

let from = editor.getCursor("from"),
to = editor.getCursor("to");
let cursorOffset = 0;

// adjust selection
switch (cmd) {
Expand All @@ -310,7 +334,7 @@ export default class TextFormat extends Plugin {
case "split-blank":
case "bullet":
case "convert-ordered":
// force to select how paragraph(s)
// force to select whole paragraph(s)
from.ch = 0;
to.line += 1;
to.ch = 0;
Expand Down Expand Up @@ -340,6 +364,11 @@ export default class TextFormat extends Plugin {

// modify selection text
switch (cmd) {
case "heading":
const headingRes = headingLevel(selectedText, args);
replacedText = headingRes.text;
cursorOffset = headingRes.offset
break;
case "anki":
replacedText = ankiSelection(selectedText);
break;
Expand Down Expand Up @@ -556,6 +585,11 @@ export default class TextFormat extends Plugin {
editor.offsetToPos(tos)
);
break;
case "heading":
// put cursor back to the original position
editor.setSelection(editor.offsetToPos(editor.posToOffset(origin_cursor_from) + cursorOffset),
editor.offsetToPos(editor.posToOffset(origin_cursor_to) + cursorOffset));
break;
case "todo-sort":
if (!somethingSelected) {
editor.setSelection(origin_cursor_from, origin_cursor_to);
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "obsidian-text-format",
"name": "Text Format",
"version": "2.3.0",
"version": "2.4.0",
"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
20 changes: 20 additions & 0 deletions src/format.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { MarkdownView, EditorPosition, App, requestUrl, TFile, Notice, } from "obsidian";
import { off } from "process";

export function stringFormat(str: string, values: Record<string, string>) {
return str.replace(/\{(\w+)\}/g, (match, key) => values[key] === undefined ? match : values[key]);
Expand Down Expand Up @@ -28,6 +29,25 @@ export function capitalizeSentence(s: string): string {
});
}

export function headingLevel(s: string, upper: boolean = true): { text: string, offset: number } {
let offset = 0;
if (upper) {
let prefix = /^#+\s/.test(s) ? `#` : `# `
s = prefix + s;
offset = prefix.length;
} else {
if (/^#\s/.test(s)) {
s = s.slice(2);
offset = -2;
} else if (/^#+\s/.test(s)) {
s = s.slice(1);
offset = -1;
}
}
return { text: s, offset: offset };
}


export function ankiSelection(str: string): string {
let sections = str.split(/\r?\n/);
var seclen = sections.length;
Expand Down

0 comments on commit 025e98f

Please sign in to comment.