forked from juracy/obsidian-asciidoc-blocks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
70 lines (61 loc) · 2.06 KB
/
main.ts
File metadata and controls
70 lines (61 loc) · 2.06 KB
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
import asciidoctor from "@asciidoctor/core";
import {
sanitizeHTMLToDom,
MarkdownPostProcessor,
MarkdownPostProcessorContext,
Plugin,
} from "obsidian";
export default class AsciiDocBlocks extends Plugin {
postprocessors: Map<string, MarkdownPostProcessor> = new Map();
// TODO: Research about @asciidoctor/core types
converter: any;
async onload() {
console.log("Obsidian AsciiDoc Blocks loaded");
this.converter = asciidoctor();
const processor = this.registerMarkdownCodeBlockProcessor(
"asciidoc-table",
(src, el, ctx) => this.postprocessor("asciidoc-table", src, el, ctx)
);
this.postprocessors.set("asciidoc-table", processor);
}
async postprocessor(
type: string,
src: string,
el: HTMLElement,
ctx?: MarkdownPostProcessorContext
) {
console.log(`Obsidian AsciiDoc Blocks: ${type}`);
try {
const html = createEl("div");
const output = this.converter.convert(src);
html.appendChild(sanitizeHTMLToDom(output.toString()));
/**
* Replace the <pre> tag with asciidoc output.
*/
const parent = el.parentElement;
if (parent) {
parent.addClass(
"asciidoc-blocks-parent",
`asciidoc-blocks-${type.replace("asciidoc-", "")}-parent`
);
}
el.replaceWith(html);
return html;
} catch (e) {
console.error(e);
const pre = createEl("pre");
pre.createEl("code", {
attr: {
style: `color: var(--text-error) !important`,
},
}).createSpan({
text:
"There was an error rendering the asciidoc-block:" +
"\n\n" +
src,
});
el.replaceWith(pre);
}
}
onunload() {}
}