forked from kristianfreeman/obsidian-alias-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
132 lines (103 loc) · 3.56 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
import { App, Notice, Plugin, PluginSettingTab, Setting, parseFrontMatterAliases, Vault, TFile } from 'obsidian';
import mdify from 'mdify'
interface OAMSettings {
indexRegex: string;
}
const DEFAULT_SETTINGS: OAMSettings = {
indexRegex: String.raw`\d{1,}\.\d{1,}`
}
export default class OAM extends Plugin {
settings: OAMSettings;
async onload() {
console.log('loading plugin');
await this.loadSettings();
this.addRibbonIcon('dice', 'Sample Plugin', () => {
new Notice('This is a notice!');
});
this.addCommand({
id: 'update-aliases',
name: 'Update Aliases',
callback: async () => {
this.updateAllFiles()
},
});
this.addSettingTab(new OAMSettingTab(this.app, this));
this.registerCodeMirror((cm: CodeMirror.Editor) => {
// console.log('codemirror', cm);
});
this.registerDomEvent(document, 'click', (evt: MouseEvent) => {
// console.log(this.app)
});
const regex = new RegExp(this.settings.indexRegex)
this.app.vault.on("create", file => {
if (file instanceof TFile) {
this.updateFileAliases(this.app, file, regex)
} else {
console.log("File is TAbstractFile, not TFile, can't handle event")
}
})
await this.updateAllFiles()
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
async updateAllFiles() {
const vault = this.app.vault
const files: [TFile] = (vault as any).fileMap
const regex = new RegExp(this.settings.indexRegex)
await Promise.all(Object.values(files).map((file: TFile) => this.updateFileAliases(this.app, file, regex)))
}
async updateFileAliases(app: App, file: TFile, regex: RegExp) {
const fileContent = await app.vault.read(file)
const nameWithoutIndex = file
.basename
.replace(regex, "")
.trim()
if (file.basename == nameWithoutIndex) return
const parsedFile = mdify.parse(fileContent)
let aliases: string[] = []
if (parsedFile.metadata && parsedFile.metadata.aliases) {
aliases = aliases.concat(parsedFile.metadata.aliases)
if (!parsedFile.metadata.aliases.includes(nameWithoutIndex)) {
aliases.push(nameWithoutIndex)
}
} else {
// Add frontmatter and aliases since there isn't any
aliases = [nameWithoutIndex]
}
// This sucks, but the Markdown parser that I'm using seems
// to append new lines after the metadata, and doesn't clean
// up any extras. Essentially, we're reclearing the space between
// metadata and note content repeatedly, knowing that mdify will
// re-add a new line on each operation
const newLineRegex = new RegExp(/^\n+/g)
const sanitizedContent = parsedFile.markdown.replace(newLineRegex, '\n')
const newContent = mdify.stringify({ aliases }, sanitizedContent)
await app.vault.modify(file, newContent)
}
}
class OAMSettingTab extends PluginSettingTab {
plugin: OAM;
constructor(app: App, plugin: OAM) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
let { containerEl } = this;
containerEl.empty();
containerEl.createEl('h2', { text: 'Settings' });
new Setting(containerEl)
.setName('Index Regex')
.setDesc("Index for parsing your index system. By default, this is the Johnny Decimal system, such as '12.34'.")
.addText(text => text
.setPlaceholder(DEFAULT_SETTINGS.indexRegex)
.setValue('')
.onChange(async (value) => {
this.plugin.settings.indexRegex = value;
await this.plugin.saveSettings();
}));
}
}