generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.ts
86 lines (80 loc) · 3.08 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
import { MarkdownPostProcessor, Plugin} from 'obsidian';
enum ComponentChoice {
Default = "Default",
}
export default class BlurPlugin extends Plugin {
async onload() {
this.registerMarkdownCodeBlockProcessor("blur", this.blurBlockHandler.bind(this, null));
this.registerMarkdownCodeBlockProcessor("blur-brick", this.blurBlockHandler.bind(this, null));
this.registerMarkdownCodeBlockProcessor("blur-bone", this.blurBlockHandler.bind(this, null));
this.registerMarkdownPostProcessor(
buildPostProcessor()
);
console.log("%c Blur plugin loaded", 'color:lime;');
}
onunload() {
console.log("%c Blur plugin unloaded", 'color:lime;');
}
async blurBlockHandler(type: ComponentChoice, source: string, el: HTMLElement, ctx: any): Promise<any> {
if (el.className==='block-language-blur-brick') {
const block = el.createEl("div", {cls: "blur-brick-block"})
let inputElement: HTMLElement
inputElement = block.createEl("div", {text: '', cls: "blur-brick-innerblock"})
source.split(/\W+/).forEach((w:string) => {
let word = w.trim();
if (word !== '') {
//redact w/ char '█' █ █
inputElement.appendChild(createEl('code', {text: word.replace(/[^\s]/g, '█'), cls: "blur-brick" }));
}
})
}
else if (el.className==='block-language-blur-bone') {
const block = el.createEl("div", {cls: "blur-bone-block"})
let inputElement: HTMLElement
inputElement = block.createEl("div", {text: '', cls: "blur-bone-innerblock"})
source.split(/\W+/).forEach((w:string) => {
let word = w.trim();
if (word !== '') {
inputElement.appendChild(createEl('code', {text: word, cls: "blur-bone"}));
}
})
}
else if (el.className==='block-language-blur') {
const block = el.createEl("div", {cls: "blur-block"})
let inputElement: HTMLElement
inputElement = block.createEl("div", {text: '', cls: "blur-innerblock"})
source.split(/\W+/).forEach((w:string) => {
let word = w.trim();
if (word !== '') {
inputElement.appendChild(createEl('code', {text: word, cls: "blur-inline"}));
}
})
}
}
}
export function buildPostProcessor(): MarkdownPostProcessor {
return (el) => {
el.findAll("code").forEach((code) => {
let text = code.innerText.trim();
if (text.startsWith('~[') && text.endsWith(']')) {
let part = text.substring(1);
let content = part.substring(part.length-1,1);
code.addClass('blur-brick');
//redact w/ char '█' █ █
code.innerText=content.replace(/[^\s]/g, '█');
}
else if (text.startsWith("~(") && text.endsWith(')')) {
let part = text.substring(1);
let content = part.substring(part.length-1,1);
code.addClass('blur-bone');
code.innerText=content;
}
else if (text.startsWith('~{') && text.endsWith('}')) {
let part = text.substring(1);
let content = part.substring(part.length-1,1);
code.addClass('blur-inline');
code.innerText=content;
}
})
}
}