-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
135 lines (108 loc) · 3.41 KB
/
main.ts
File metadata and controls
135 lines (108 loc) · 3.41 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import { App, Plugin, PluginSettingTab, Setting, TFile, Notice, MarkdownView } from 'obsidian';
interface ZettelNoteLinksSettings {
newlineCount: number;
}
const DEFAULT_SETTINGS: ZettelNoteLinksSettings = {
newlineCount: 5
}
export default class ZettelNoteLinksPlugin extends Plugin {
settings: ZettelNoteLinksSettings;
async onload() {
await this.loadSettings();
// Register the command with hotkey
this.addCommand({
id: 'create-linked-note',
name: 'Create new note with link to current note',
hotkeys: [
{
modifiers: ['Ctrl'],
key: 'n'
}
],
callback: () => {
this.createLinkedNote();
}
});
// Add settings tab
this.addSettingTab(new ZettelNoteLinksSettingTab(this.app, this));
}
async createLinkedNote() {
const activeFile = this.app.workspace.getActiveFile();
if (!activeFile) {
new Notice('No active note to link from');
return;
}
// Get the name of the previous note (without extension)
const previousNoteName = activeFile.basename;
// Get the default location for new files using Obsidian's file manager
const newFileFolderPath = (this.app.fileManager as any).getNewFileParent(activeFile.path).path;
// Create a simple "Untitled" name - user will rename it
let newNoteName = 'Untitled';
let newFilePath = `${newFileFolderPath}/${newNoteName}.md`;
// If file exists, add a number
let counter = 1;
while (await this.app.vault.adapter.exists(newFilePath)) {
newNoteName = `Untitled ${counter}`;
newFilePath = `${newFileFolderPath}/${newNoteName}.md`;
counter++;
}
// Create the content with newlines and link
const newlines = '\n'.repeat(this.settings.newlineCount);
const link = `[[${previousNoteName}]]`;
const content = `${newlines}${link}\n`;
try {
// Create the new file
const newFile = await this.app.vault.create(newFilePath, content);
// Open the new file
const leaf = this.app.workspace.getLeaf();
await leaf.openFile(newFile);
// Set cursor to the beginning of the file
const view = this.app.workspace.getActiveViewOfType(MarkdownView);
if (view) {
const editor = view.editor;
editor.setCursor(0, 0);
}
// Trigger file rename immediately so user can set the title
setTimeout(() => {
(this.app as any).fileManager.promptForFileRename(newFile);
}, 50);
} catch (error) {
new Notice(`Failed to create note: ${error.message}`);
console.error('Error creating linked note:', error);
}
}
onunload() {
// Cleanup if needed
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}
class ZettelNoteLinksSettingTab extends PluginSettingTab {
plugin: ZettelNoteLinksPlugin;
constructor(app: App, plugin: ZettelNoteLinksPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const {containerEl} = this;
containerEl.empty();
containerEl.createEl('h2', {text: 'ZettelNoteLinks Settings'});
new 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();
}
}));
}
}