-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
58 lines (52 loc) · 1.75 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
import { Plugin, TFile } from 'obsidian';
export default class ZettelkastenOutliner extends Plugin {
async onload() {
this.addCommand({
id: 'create-outline',
name: 'Create outline',
checkCallback: (checking: boolean) => {
const currentFile = this.app.workspace.getActiveFile();
if (!!currentFile) {
if (checking) {
return true;
} else {
const outlineName = `Zettelkasten Outline ${new Date().getTime()}.md`;
this.app.vault.create(outlineName, "").then((outputFile: TFile) => {
this.parseZettel(outputFile, currentFile, 0);
this.app.workspace.openLinkText(outputFile.name, "", true);
});
}
} else {
return false;
}
},
});
}
getChildrenFiles(file: TFile): TFile[] {
let children = [] as TFile[];
const linkToFile = `[[${file.name.replace(/\.md$/, "")}]]`;
const fullLinkToFile = `[[${file.path.replace(/\.md$/, "")}]]`;
this.app.vault.getMarkdownFiles().forEach((markdownFile) => {
if (
this.app.metadataCache.getFileCache(markdownFile)?.frontmatter?.parent === linkToFile
|| this.app.metadataCache.getFileCache(markdownFile)?.frontmatter?.parent === fullLinkToFile
) {
children.push(markdownFile);
}
});
return children;
}
parseZettel(outputFile: TFile, zettel: TFile, indentationLevel: number) {
this.app.vault.append(outputFile, this.generateListItem(zettel, indentationLevel));
this.getChildrenFiles(zettel).forEach((child) => {
this.parseZettel(outputFile, child, indentationLevel + 1);
});
}
generateListItem(file: TFile, indentationLevel: number): string {
let identation = "";
for (let i = 0; i < indentationLevel; i++) {
identation = identation.concat(" ");
}
return `${identation}- ![[${file.path.replace(/\.md$/, "")}]]\n`;
}
}