-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.ts
153 lines (126 loc) · 3.98 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
import {
App, HeadingCache, MetadataCache, Notice,
Plugin,
PluginSettingTab,
Setting,
TFile,
} from 'obsidian';
interface MyPluginSettings {
concatHeader: string;
pathIncluded: string;
}
const DEFAULT_SETTINGS: MyPluginSettings = {
concatHeader: '## Reading Log',
pathIncluded: ''
}
async function asyncForEach(array:TFile[], callback: (markFile: TFile) => void) {
for (let index = 0; index < array.length; index++) {
await callback(array[index]);
}
}
export default class MyPlugin extends Plugin {
settings: MyPluginSettings;
async performConcatenation() {
let markdownFiles = this.app.vault.getMarkdownFiles();
if (this.settings.pathIncluded) {
markdownFiles = markdownFiles.filter(e => e.path.startsWith(this.settings.pathIncluded))
}
if (!markdownFiles.length) {
new Notice("Can't find files which fit the filter")
return
}
let content = Array();
let timenow = Date.now();
// Await lets you block further execution until this thing is done. It must be paired with async.
const level = this.settings.concatHeader.split(' ').filter(e => /^#/.test(e))[0]?.split('#').length - 1 || 0
const title = this.settings.concatHeader.split(' ').slice(1).join(' ')
await asyncForEach(markdownFiles, async (markFile: TFile) => {
const metadata = this.app.metadataCache.getFileCache(markFile)
type HeadingWithOffset = [HeadingCache, { sectionEndLine: number }]
const matchedHeadings: HeadingWithOffset[] = metadata.headings
?.filter(h => h.level >= level)
.map((h, i, arr) => {
return [
h,
{ sectionEndLine: arr[i + 1]?.position.start.line || undefined }
] as HeadingWithOffset
})
.filter(h => h[0].level === level && h[0].heading === title)
|| []
if (matchedHeadings.length) {
const fileContent = await this.app.vault.read(markFile)
content.push(
matchedHeadings.map(h => {
return fileContent
.split('\n')
.slice(h[0].position.start.line + 1, h[1].sectionEndLine)
.join('\n')
}).join('\n')
)
}
});
const finalizedContents = content.join('\n')
await this.app.vault.create("Concatenated_Note-" + timenow + ".md", finalizedContents);
new Notice('File: "' + 'Concatenated_Note-' + timenow + '.md' + '" created.');
}
async onload() {
console.log('loading plugin');
await this.loadSettings();
this.addCommand({
id: 'concatenate-headings',
name: 'Concatenate Headings',
checkCallback: (checking: boolean) => {
let leaf = this.app.workspace.activeLeaf;
if (leaf) {
if (!checking) {
this.performConcatenation();
}
return true;
}
return false;
},
callback: () => this.performConcatenation(),
});
this.addSettingTab(new SampleSettingTab(this.app, this));
}
onunload() {
console.log('unloading plugin');
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}
class SampleSettingTab extends PluginSettingTab {
plugin: MyPlugin;
constructor(app: App, plugin: MyPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
let {containerEl} = this;
containerEl.empty();
containerEl.createEl('h2', {text: 'Settings for my awesome plugin.'});
new Setting(containerEl)
.setName('Heading Title')
.setDesc('Type the heading (including the hashtags) you would like to concatenate')
.addText(text => text
.setValue(this.plugin.settings.concatHeader)
.onChange(async (value) => {
this.plugin.settings.concatHeader = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('Folder in which search for')
.setDesc('Specify the folder where plugin will search headers')
.addText(text => text
.setPlaceholder('path/folder/deep')
.setValue(this.plugin.settings.pathIncluded)
.onChange(async (value) => {
this.plugin.settings.pathIncluded = value;
await this.plugin.saveSettings();
}));
}
}