-
-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathmain.ts
More file actions
112 lines (89 loc) · 2.85 KB
/
main.ts
File metadata and controls
112 lines (89 loc) · 2.85 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
import {Notice, Plugin, stringifyYaml, TFile} from 'obsidian';
import {DEFAULT_SETTINGS, MetaBindPluginSettings, MetaBindSettingTab} from './settings/Settings';
import {InputField} from './InputField';
import {getFileName, isPath, removeFileEnding} from './Utils';
export default class MetaBindPlugin extends Plugin {
settings: MetaBindPluginSettings;
async onload() {
await this.loadSettings();
// This creates an icon in the left ribbon.
const ribbonIconEl = this.addRibbonIcon('dice', 'Sample Plugin', (evt: MouseEvent) => {
});
this.registerMarkdownPostProcessor((element, context) => {
const codeBlocks = element.querySelectorAll('code');
for (let index = 0; index < codeBlocks.length; index++) {
const codeBlock = codeBlocks.item(index);
const text = codeBlock.innerText;
const isInputField = text.startsWith('INPUT[') && text.endsWith(']');
// console.log(context.sourcePath);
if (isInputField) {
context.addChild(new InputField(codeBlock, text, this, context.sourcePath));
}
}
});
this.registerEvent(this.app.vault.on('modify', () => {
// console.log('file modified')
}));
this.addSettingTab(new MetaBindSettingTab(this.app, this));
}
onunload() {
}
async updateMetaData(key: string, value: any, file: TFile) {
// console.log('update', key, value);
if (!file) {
console.log('no file');
return;
}
let fileContent: string = await this.app.vault.read(file);
const regExp = new RegExp('^(---)\\n[\\s\\S]*\\n---');
fileContent = fileContent.replace(regExp, '');
let metadata: any = this.getMetaDataForFile(file);
if (!metadata) {
return;
}
metadata[key] = value;
fileContent = `---\n${stringifyYaml(metadata)}---` + fileContent;
await this.app.vault.modify(file, fileContent);
}
getFilesByName(name: string): TFile[] {
// console.log(getFileName(removeFileEnding(name)))
const allFiles = this.app.vault.getFiles();
const files: TFile[] = [];
for (const file of allFiles) {
// console.log(removeFileEnding(file.path));
if (isPath(name)) {
if (removeFileEnding(file.path) === removeFileEnding(name)) {
files.push(file);
}
} else {
if (getFileName(removeFileEnding(file.name)) === getFileName(removeFileEnding(name))) {
files.push(file);
}
}
}
return files;
}
getMetaDataForFile(file: TFile): any {
let metadata: any;
try {
metadata = this.app.metadataCache.getFileCache(file).frontmatter;
} catch (e) {
new Notice('Waring: ' + e.toString());
console.warn(e.toString());
return;
}
if (metadata) {
metadata = JSON.parse(JSON.stringify(metadata)); // deep copy
delete metadata.position;
} else {
metadata = {};
}
return metadata;
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}