generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.ts
115 lines (89 loc) · 3.12 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
import {
App,
Editor,
MarkdownView,
Modal,
Notice,
Plugin,
PluginSettingTab,
Setting,
WorkspaceItem,
WorkspaceLeaf
} from 'obsidian';
import {StringHelper} from './StringHelper';
import {ast as bibTexParserAst, parse as bibTexParserParse} from "@retorquere/bibtex-parser";
import {Entry} from "@retorquere/bibtex-parser/grammar";
const pluginName = "Pretty BibTeX";
export default class PrettyBibTexPlugin extends Plugin {
settings: ISettings;
async onload() {
await this.loadSettings();
this.addSettingTab(new SettingTab(this.app, this));
this.registerMarkdownCodeBlockProcessor("bibtex", (source: string, el, ctx) => {
const codeBlock = el.createEl("div").createEl("pre").createEl("code");
try {
const result = bibTexParserParse(source);
if (result.entries.length == 0)
codeBlock.createEl("span", {text: "No valid BibTex entries found!", cls: "bibtex key"});
const ast = bibTexParserAst(source);
result.entries.forEach((entry, index) => {
codeBlock.createEl("span", {text: `${entry.key}\n`, cls: "bibtex header"});
if (this.settings.showType)
this.addKeyValueToCodeBlock(codeBlock, "Type", entry.type);
const desiredOrderOfFields = (ast[index] as Entry).fields.map(f => f.name);
const sortByDesiredOrderOfFields = (a: string, b: string): number => {
return desiredOrderOfFields.indexOf(a) - desiredOrderOfFields.indexOf(b);
};
Object.keys(entry.fields).sort(sortByDesiredOrderOfFields).forEach(key => {
this.addKeyValueToCodeBlock(codeBlock, key, entry.fields[key].join(" and "));
});
if (result.entries.length >= 1 && index < result.entries.length - 1)
this.addSpacerCodeBlock(codeBlock);
});
} catch (exception) {
codeBlock.createEl("span", {text: "Invalid BibTeX format!", cls: "bibtex key"});
}
});
}
addKeyValueToCodeBlock(codeBlock: HTMLElement, key: string, value: string): void {
codeBlock.createEl("span", {text: StringHelper.sanitizeKeyString(key), cls: "bibtex key"});
codeBlock.createEl("span", {text: ":", cls: "bibtex normal"});
codeBlock.createEl("span", {text: ` ${value}\n`, cls: "bibtex value"});
}
addSpacerCodeBlock(codeBlock: HTMLElement): void {
codeBlock.createEl("span", {text: "\n", cls: "bibtex normal"});
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}
interface ISettings {
showType: boolean;
}
const DEFAULT_SETTINGS: ISettings = {
showType: true
}
class SettingTab extends PluginSettingTab {
plugin: PrettyBibTexPlugin;
constructor(app: App, plugin: PrettyBibTexPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const {containerEl} = this;
containerEl.empty();
containerEl.createEl('h2', {text: `Settings for ${pluginName}`});
new Setting(containerEl)
.setName('Show Type')
.setDesc('Shows the type e.g. "article"')
.addToggle(toggle => toggle
.setValue(this.plugin.settings.showType)
.onChange(async (value) => {
this.plugin.settings.showType = value;
await this.plugin.saveSettings();
}));
}
}