-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathmain.ts
166 lines (131 loc) · 4.39 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
162
163
164
165
166
import { App, Modal, Notice, Plugin, PluginSettingTab, Setting, EventRef, MarkdownView, TAbstractFile } from 'obsidian';
const illegalSymbols = ['*', '\\', '/', '<', '>', ':', '|', '?'];
interface LinePointer {
LineNumber: number;
Text: string;
}
interface FilenameHeadingSyncPluginSettings {
numLinesToCheck: number;
}
const DEFAULT_SETTINGS: FilenameHeadingSyncPluginSettings = {
numLinesToCheck: 1,
};
export default class FilenameHeadingSyncPlugin extends Plugin {
settings: FilenameHeadingSyncPluginSettings;
async onload() {
await this.loadSettings();
this.registerEvent(this.app.vault.on('rename', (file) => this.handleSyncFilenameToHeading(file)));
this.registerEvent(this.app.vault.on('modify', (file) => this.handleSyncHeadingToFile(file)));
this.registerEvent(this.app.workspace.on('file-open', (file) => this.handleSyncFilenameToHeading(file)));
this.addSettingTab(new FilenameHeadingSyncSettingTab(this.app, this));
}
handleSyncHeadingToFile(file: TAbstractFile) {
const view = this.app.workspace.getActiveViewOfType(MarkdownView);
if (view === null) {
return;
}
if (view.file !== file) {
return;
}
const editor = view.sourceMode.cmEditor;
const doc = editor.getDoc();
const heading = this.findHeading(doc);
// no heading found, nothing to do here
if (heading == null) {
return;
}
const sanitizedHeading = this.sanitizeHeading(heading.Text);
if (this.sanitizeHeading(view.file.basename) !== sanitizedHeading) {
const newPath = view.file.path.replace(view.file.basename.trim(), sanitizedHeading);
this.app.vault.rename(view.file, newPath);
}
}
handleSyncFilenameToHeading(file) {
const view = this.app.workspace.getActiveViewOfType(MarkdownView);
if (view === null) {
return;
}
if (view.file !== file) {
return;
}
const editor = view.sourceMode.cmEditor;
const doc = editor.getDoc();
const cursor = doc.getCursor();
const foundHeading = this.findHeading(doc);
const sanitizedHeading = this.sanitizeHeading(file.basename);
if (foundHeading !== null) {
if (this.sanitizeHeading(foundHeading.Text) !== sanitizedHeading) {
this.replaceLine(doc, foundHeading, `# ${sanitizedHeading}`);
doc.setCursor(cursor);
}
return;
}
this.insertLine(doc, `# ${sanitizedHeading}`);
doc.setCursor(cursor);
}
findHeading(doc: CodeMirror.Doc): LinePointer | null {
for (let i = 0; i <= this.settings.numLinesToCheck; i++) {
const line = doc.getLine(i);
if (line === undefined) {
continue;
}
if (line.startsWith('# ')) {
return {
LineNumber: i,
Text: line.substring(2),
};
}
}
return null;
}
sanitizeHeading(text: string) {
illegalSymbols.forEach((symbol) => {
text = text.replace(symbol, '');
});
return text.trim();
}
insertLine(doc: CodeMirror.Doc, text: string) {
doc.replaceRange(`${text}\n`, { line: 0, ch: 0 }, { line: 0, ch: 0 });
}
replaceLine(doc: CodeMirror.Doc, line: LinePointer, text: string) {
doc.replaceRange(`${text}\n`, { line: line.LineNumber, ch: 0 }, { line: line.LineNumber + 1, ch: 0 });
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}
class FilenameHeadingSyncSettingTab extends PluginSettingTab {
plugin: FilenameHeadingSyncPlugin;
constructor(app: App, plugin: FilenameHeadingSyncPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
let { containerEl } = this;
containerEl.empty();
containerEl.createEl('h2', { text: 'Filename Heading Sync' });
containerEl.createEl('p', {
text: 'This plugin will overwrite the first heading found in a file with the filename.',
});
containerEl.createEl('p', {
text:
'If no header is found within the first few lines (set below), will insert a new one at the first line.',
});
new Setting(containerEl)
.setName('Number of lines to check')
.setDesc('How many lines from top to check for a header')
.addSlider((slider) =>
slider
.setDynamicTooltip()
.setValue(this.plugin.settings.numLinesToCheck)
.setLimits(1, 5, 1)
.onChange(async (value) => {
this.plugin.settings.numLinesToCheck = value;
await this.plugin.saveSettings();
}),
);
}
}