-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
115 lines (113 loc) · 3.9 KB
/
main.js
File metadata and controls
115 lines (113 loc) · 3.9 KB
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
/*
THIS IS A GENERATED/BUNDLED FILE BY ESBUILD
if you want to view the source, please visit the github repository of this plugin
*/
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// main.ts
var main_exports = {};
__export(main_exports, {
default: () => ZettelNoteLinksPlugin
});
module.exports = __toCommonJS(main_exports);
var import_obsidian = require("obsidian");
var DEFAULT_SETTINGS = {
newlineCount: 5
};
var ZettelNoteLinksPlugin = class extends import_obsidian.Plugin {
async onload() {
await this.loadSettings();
this.addCommand({
id: "create-linked-note",
name: "Create new note with link to current note",
hotkeys: [
{
modifiers: ["Ctrl"],
key: "n"
}
],
callback: () => {
this.createLinkedNote();
}
});
this.addSettingTab(new ZettelNoteLinksSettingTab(this.app, this));
}
async createLinkedNote() {
const activeFile = this.app.workspace.getActiveFile();
if (!activeFile) {
new import_obsidian.Notice("No active note to link from");
return;
}
const previousNoteName = activeFile.basename;
const newFileFolderPath = this.app.fileManager.getNewFileParent(activeFile.path).path;
let newNoteName = "Untitled";
let newFilePath = `${newFileFolderPath}/${newNoteName}.md`;
let counter = 1;
while (await this.app.vault.adapter.exists(newFilePath)) {
newNoteName = `Untitled ${counter}`;
newFilePath = `${newFileFolderPath}/${newNoteName}.md`;
counter++;
}
const newlines = "\n".repeat(this.settings.newlineCount);
const link = `[[${previousNoteName}]]`;
const content = `${newlines}${link}
`;
try {
const newFile = await this.app.vault.create(newFilePath, content);
const leaf = this.app.workspace.getLeaf();
await leaf.openFile(newFile);
const view = this.app.workspace.getActiveViewOfType(import_obsidian.MarkdownView);
if (view) {
const editor = view.editor;
editor.setCursor(0, 0);
}
setTimeout(() => {
this.app.fileManager.promptForFileRename(newFile);
}, 50);
} catch (error) {
new import_obsidian.Notice(`Failed to create note: ${error.message}`);
console.error("Error creating linked note:", error);
}
}
onunload() {
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
};
var ZettelNoteLinksSettingTab = class extends import_obsidian.PluginSettingTab {
constructor(app, plugin) {
super(app, plugin);
this.plugin = plugin;
}
display() {
const { containerEl } = this;
containerEl.empty();
containerEl.createEl("h2", { text: "ZettelNoteLinks Settings" });
new import_obsidian.Setting(containerEl).setName("Number of newlines").setDesc("Number of newlines to add before the link in new notes").addText((text) => text.setPlaceholder("5").setValue(String(this.plugin.settings.newlineCount)).onChange(async (value) => {
const num = parseInt(value);
if (!isNaN(num) && num >= 0) {
this.plugin.settings.newlineCount = num;
await this.plugin.saveSettings();
}
}));
}
};