generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
161 lines (144 loc) · 5.28 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import { App, Editor, Notice, Plugin, PluginSettingTab, Setting } from 'obsidian';
interface AnchorDisplayTextSettings {
includeNoteName : string;
whichHeadings: string;
includeNotice: boolean;
noticeText: string;
sep : string;
}
const DEFAULT_SETTINGS: AnchorDisplayTextSettings = {
includeNoteName: 'headersOnly',
whichHeadings: 'allHeaders',
includeNotice: false,
noticeText: 'Link changed!',
sep: ' '
}
export default class AnchorDisplayText extends Plugin {
settings: AnchorDisplayTextSettings;
async onload() {
await this.loadSettings();
this.addSettingTab(new AnchorDisplayTextSettingTab(this.app, this));
// look for header link creation
this.registerEvent(
this.app.workspace.on('editor-change', (editor: Editor) => {
// get what is being typed
const cursor = editor.getCursor();
const currentLine = editor.getLine(cursor.line);
// match links to other anchor links WITHOUT an already defined display text
const headerLinkPattern = /\[\[([^\]]+#[^|]+)\]\]/;
const match = currentLine.slice(0, cursor.ch).match(headerLinkPattern);
if (match) {
// handle multiple subheadings
const headings = match[1].split('#')
let displayText = ''
if (this.settings.whichHeadings === 'lastHeader') {
displayText = headings[headings.length - 1];
} else {
displayText = headings[1];
if (this.settings.whichHeadings === 'allHeaders') {
for (let i = 2; i < headings.length; i++) {
displayText += this.settings.sep + headings[i];
}
}
}
const startIndex = (match.index ?? 0) + match[0].length - 2;
if (this.settings.includeNoteName === 'headersOnly') {
editor.replaceRange(`|${displayText}`, {line: cursor.line, ch: startIndex}, undefined, 'headerDisplayText');
} else if (this.settings.includeNoteName === 'noteNameFirst') {
editor.replaceRange(`|${headings[0]}${this.settings.sep}${displayText}`, {line: cursor.line, ch: startIndex}, undefined, 'headerDisplayText');
} else if (this.settings.includeNoteName === 'noteNameLast') {
editor.replaceRange(`|${displayText}${this.settings.sep}${headings[0]}`, {line: cursor.line, ch: startIndex}, undefined, 'headerDisplayText');
}
if (this.settings.includeNotice) {
new Notice (this.settings.noticeText)
}
}
})
);
}
onunload() {
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}
class AnchorDisplayTextSettingTab extends PluginSettingTab {
plugin: AnchorDisplayText;
private notificationTextSetting?: Setting;
constructor(app: App, plugin: AnchorDisplayText) {
super(app, plugin);
this.plugin = plugin;
}
displayNotificationTextSetting() {
if (this.notificationTextSetting) {
this.notificationTextSetting.settingEl.style.display = this.plugin.settings.includeNotice ? 'flex' : 'none';
}
}
display(): void {
const {containerEl} = this;
containerEl.empty();
new Setting(containerEl).setName('Display text format').setHeading();
new Setting(containerEl)
.setName('Include note name')
.setDesc('Include the title of the note in the display text.')
.addDropdown(dropdown => {
dropdown.addOption('headersOnly', 'Don\'t include note name');
dropdown.addOption('noteNameFirst', 'Note name and then heading(s)');
dropdown.addOption('noteNameLast', 'Heading(s) and then note name');
dropdown.setValue(this.plugin.settings.includeNoteName);
dropdown.onChange(value => {
this.plugin.settings.includeNoteName = value;
this.plugin.saveSettings();
});
});
new Setting(containerEl)
.setName('Include subheadings')
.setDesc('Change which headings and subheadings are in the display text.')
.addDropdown(dropdown => {
dropdown.addOption('allHeaders', 'All linked headings');
dropdown.addOption('lastHeader', 'Last heading only');
dropdown.addOption('firstHeader', 'First heading only');
dropdown.setValue(this.plugin.settings.whichHeadings);
dropdown.onChange(value => {
this.plugin.settings.whichHeadings = value;
this.plugin.saveSettings();
});
});
new Setting(containerEl)
.setName('Seperator')
.setDesc('Choose what to insert between headings instead of #.')
.addText(text => {
text.setValue(this.plugin.settings.sep);
text.onChange(value => {
this.plugin.settings.sep = value;
this.plugin.saveSettings();
});
});
new Setting(containerEl).setName('Notifications').setHeading();
new Setting(containerEl)
.setName('Enable notifications')
.setDesc('Have a notice pop up whenever a link is automatically changed.')
.addToggle(toggle => {
toggle.setValue(this.plugin.settings.includeNotice);
toggle.onChange(value => {
this.plugin.settings.includeNotice = value;
this.displayNotificationTextSetting();
this.plugin.saveSettings();
});
});
this.notificationTextSetting = new Setting(containerEl)
.setName('Notification text')
.setDesc('Set the text to appear in the notification.')
.addText(text => {
text.setValue(this.plugin.settings.noticeText);
text.onChange(value => {
this.plugin.settings.noticeText = value;
this.plugin.saveSettings();
});
});
this.displayNotificationTextSetting();
}
}