generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
160 lines (129 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
154
155
156
157
158
159
160
import { App, Editor, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Setting } from 'obsidian';
import { execSync } from "child_process";
import {
default as AnsiUp
} from 'ansi_up';
interface AllFilterSettings {
filters: FilterArray;
}
interface FilterSettings {
codeblocklabel: string;
binpath: string;
colorize: boolean;
outputfont: string;
firstlineextraargs: string; // TODO use first line as extra args if available
}
interface FilterArray extends Array<FilterSettings> {}
const SLOG = "slog"
const DEFAULT_SETTINGS: AllFilterSettings = {
"filters": [{
codeblocklabel: SLOG,
binpath: 'hl',
colorize: true,
outputfont: 'IBM Plex Mono',
},
{
codeblocklabel: "haha",
binpath: 'hl',
colorize: false,
outputfont: 'Comic Sans',
}]
}
export default class FilterBlock extends Plugin {
settings: AllFilterSettings;
async onload() {
await this.loadSettings();
for (let fidx = 0; fidx < this.settings.filters.length; fidx++) {
let filter = this.settings.filters[fidx]
this.registerMarkdownCodeBlockProcessor(filter.codeblocklabel, (source, el, ctx) => {
const dest = document.createElement('div')
try{
const opts = {input: source, timeout: 60000}
const output = execSync(filter.binpath + " -", opts)
const ansi_up = new AnsiUp();
const rows = output.toString().split("\n")
for (let i = 0; i < rows.length; i++) {
let htmlized = rows[i]
if (filter.colorize){
htmlized = ansi_up.ansi_to_html(htmlized)
}
let line = dest.createEl("div")
line.innerHTML = htmlized
line.style.fontFamily = "monospace"
line.style.font = filter.outputfont
}
}catch (e){
dest.createDiv(e.message)
}
el.replaceWith(dest)
});
}
this.addSettingTab(new FilterSettingTab(this.app, this));
this.registerInterval(window.setInterval(() => console.log('setInterval'), 5 * 60 * 1000));
}
onunload() {
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}
class FilterSettingTab extends PluginSettingTab {
plugin: FilterBlock;
constructor(app: App, plugin: FilterBlock) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const {containerEl} = this;
containerEl.empty();
containerEl.createEl('h1', {text: 'Settings for filtering code blocks through local cmdline tools.'});
for (let i = 0; i < this.plugin.settings.filters.length; i++){
let filterSetting = this.plugin.settings.filters[i]
containerEl.createEl('h2', {text: 'Filter'});
new Setting(containerEl)
.setName('code block label')
.setDesc('label you add to code block to run this filter')
.addText(text => text
.setPlaceholder('label')
.setValue(filterSetting.codeblocklabel)
.onChange(async (value) => {
filterSetting.codeblocklabel = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('filter binary')
.setDesc('path and args to binary to filter the code block')
.addText(text => text
.setPlaceholder('path to binary and any cmdline args')
.setValue(filterSetting.binpath)
.onChange(async (value) => {
filterSetting.binpath = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('colorize')
.setDesc('should we try to convert ansi color escapes')
.addToggle(toggle => toggle
.setValue(filterSetting.colorize)
.onChange(async (value) => {
filterSetting.colorize = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('output font')
.setDesc('font name to use for output')
.addText(text => text
.setPlaceholder('font name')
.setValue(filterSetting.outputfont)
.onChange(async (value) => {
filterSetting.outputfont = value;
await this.plugin.saveSettings();
}));
// todo add a button to remove the filter
}
// todo: add a button to add a filter
}
}