generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
88 lines (74 loc) · 1.94 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import {
App,
Editor,
MarkdownView,
Modal,
Notice,
Plugin,
PluginSettingTab,
Setting,
} from "obsidian";
import { markdownToAsciiDoc } from "./markdownToAsciiDoc";
// Remember to rename these classes and interfaces!
interface MyPluginSettings {
mySetting: string;
}
const DEFAULT_SETTINGS: MyPluginSettings = {
mySetting: "default",
};
export default class MyPlugin extends Plugin {
settings: MyPluginSettings;
async onload() {
await this.loadSettings();
/** ⭐
* Editor commands that can perform some operation on the current editor instance
*/
this.addCommand({
id: "to-asciidoc-table",
name: "To AsciiDoc Table",
editorCallback: this.convertContent,
});
}
onunload() {}
async loadSettings() {
this.settings = Object.assign(
{},
DEFAULT_SETTINGS,
await this.loadData()
);
}
async saveSettings() {
await this.saveData(this.settings);
}
/**
* Converts a markdown table in the editor to an AsciiDoc table.
* It identifies the table based on the cursor position, extracts it,
* converts it using the markdownToAsciiDoc function, and replaces
* the original markdown table with the converted AsciiDoc table.
*
* @param editor - The editor instance where the command is executed.
* @param view - The current markdown view.
*/
private convertContent(editor: Editor, view: MarkdownView) {
const cursor = editor.getCursor();
const lines = editor.getValue().split("\n");
// Find the start and end of the table
let start = cursor.line;
while (start > 0 && lines[start - 1].includes("|")) {
start--;
}
let end = cursor.line;
while (end < lines.length && lines[end].includes("|")) {
end++;
}
// Extract the table
const tableLines = lines.slice(start, end).join("\n");
const asciidocTable = markdownToAsciiDoc(tableLines);
// Replace the markdown table with the AsciiDoc table
editor.replaceRange(
asciidocTable,
{ line: start, ch: 0 },
{ line: end, ch: 0 }
);
}
}