generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
106 lines (87 loc) · 2.67 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
import { App, Plugin, PluginSettingTab, Setting } from "obsidian";
interface ObsidianBiblePluginSettings {
scriptureFolder: string;
}
const DEFAULT_SETTINGS: ObsidianBiblePluginSettings = {
scriptureFolder: "Scripture",
};
export default class ObsidianBiblePlugin extends Plugin {
settings: ObsidianBiblePluginSettings;
async onload() {
await this.loadSettings();
// This adds a settings tab so the user can configure various aspects of the plugin
this.addSettingTab(new SettingTab(this.app, this));
/**
* Convert Scripture link text into readable format
* e.g. Scripture/KJV/Philippians/Phil 4#5 becomes Phil 4:5
*/
this.registerMarkdownPostProcessor((postProcessor) => {
postProcessor.querySelectorAll("a").forEach((element) => {
this.formatVerseLink(element);
});
});
// Register a format backlinks function run every 1 second
this.registerInterval(
window.setInterval(this.formatBacklinks.bind(this), 1000)
);
}
async loadSettings() {
this.settings = Object.assign(
{},
DEFAULT_SETTINGS,
await this.loadData()
);
}
async saveSettings() {
await this.saveData(this.settings);
}
formatVerseLink(element: HTMLElement, isEmbed?: boolean) {
element.addClass("be__formatted");
if (!element.innerText.contains(`${this.settings.scriptureFolder}/`)) {
return;
}
const regex = new RegExp(`${this.settings.scriptureFolder}.*\/`, "g");
element.innerText = element.innerText
.replace(regex, "")
.replace(" > ", ":");
if (isEmbed) {
element.innerText = element.innerText.replace("#", ":");
element.innerText = element.innerText.replace(/\[|\]/g, "");
}
}
formatBacklinks(): void {
this.app.workspace.containerEl
.querySelectorAll<HTMLElement>(
".search-result-file-match span:not(.be__formatted)"
)
.forEach((element) => {
this.formatVerseLink(element, true);
});
}
}
class SettingTab extends PluginSettingTab {
plugin: ObsidianBiblePlugin;
constructor(app: App, plugin: ObsidianBiblePlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const { containerEl } = this;
containerEl.empty();
containerEl.createEl("h2", {
text: "Settings for my Obsidian Bible Enhanced plugin.",
});
new Setting(containerEl)
.setName("Scripture Folder")
.setDesc("Path to folder containing scripture")
.addText((text) =>
text
.setPlaceholder("Enter scripture folder")
.setValue(this.plugin.settings.scriptureFolder)
.onChange(async (value) => {
this.plugin.settings.scriptureFolder = value;
await this.plugin.saveSettings();
})
);
}
}