generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 65
/
helpers.ts
147 lines (124 loc) · 3.19 KB
/
helpers.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import {
FileManager,
MarkdownView,
Notice,
Vault,
Modal,
App,
Setting,
} from "obsidian";
// check for unclosed code block in MD (three backticks), string should contain three backticks in a row
export const unfinishedCodeBlock = (txt: string) => {
const matcher = txt.match(/```/g);
if (!matcher) {
return false;
}
if (matcher.length % 2 !== 0)
console.log("[ChatGPT MD] unclosed code block detected");
return matcher.length % 2 !== 0;
};
export const writeInferredTitleToEditor = async (
vault: Vault,
view: MarkdownView,
fileManager: FileManager,
chatFolder: string,
title: string
) => {
try {
// set title of file
const file = view.file;
// replace trailing / if it exists
const folder = chatFolder.replace(/\/$/, "");
// if new file name exists in directory, append a number to the end
let newFileName = `${folder}/${title}.md`;
let i = 1;
while (await vault.adapter.exists(newFileName)) {
newFileName = `${folder}/${title} (${i}).md`;
i++;
}
fileManager.renameFile(file, newFileName);
} catch (err) {
new Notice("[ChatGPT MD] Error writing inferred title to editor");
console.log("[ChatGPT MD] Error writing inferred title to editor", err);
throw err;
}
};
export const createFolderModal = async (
app: App,
vault: Vault,
folderName: string,
folderPath: string
) => {
const folderCreationModal = new FolderCreationModal(
app,
folderName,
folderPath
);
folderCreationModal.open();
const result = await folderCreationModal.waitForModalValue();
if (result) {
console.log("[ChatGPT MD] Creating folder");
await vault.createFolder(folderPath);
} else {
console.log("[ChatGPT MD] Not creating folder");
}
return result;
};
class FolderCreationModal extends Modal {
result: boolean;
folderName: string;
folderPath: string;
modalPromise: Promise<boolean>;
resolveModalPromise: (value: boolean) => void;
constructor(
app: App,
folderName: string,
folderPath: string
) {
super(app);
this.folderName = folderName;
this.folderPath = folderPath;
this.result = false;
this.modalPromise = new Promise((resolve) => {
this.resolveModalPromise = resolve;
});
}
onOpen() {
const { contentEl } = this;
contentEl.createEl("h2", {
text: `[ChatGPT MD] No ${this.folderName} folder found.`,
});
contentEl.createEl("p", {
text: `If you choose "Yes, Create", the plugin will automatically create a folder at: ${this.folderPath}. You can change this path in the plugin settings.`,
});
new Setting(contentEl).addButton((btn) =>
btn
.setButtonText("Yes, Create Folder")
.setTooltip("Create folder")
.setCta()
.onClick(() => {
this.result = true; // This can be any value the user provides.
this.resolveModalPromise(this.result);
this.close();
})
);
new Setting(contentEl).addButton((btn) =>
btn
.setButtonText("No, I'll create it myself")
.setTooltip("Cancel")
.setCta()
.onClick(() => {
this.result = false; // This can be any value the user provides.
this.resolveModalPromise(this.result);
this.close();
})
);
}
waitForModalValue() {
return this.modalPromise;
}
onClose() {
const { contentEl } = this;
contentEl.empty();
}
}