-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.ts
155 lines (127 loc) · 4.22 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
import { Plugin, MarkdownRenderer, TFile, MarkdownPostProcessorContext, MarkdownView, parseYaml, requestUrl} from 'obsidian';
import { EmbedCodeFileSettings, EmbedCodeFileSettingTab, DEFAULT_SETTINGS} from "./settings";
import { analyseSrcLines, extractSrcLines} from "./utils";
export default class EmbedCodeFile extends Plugin {
settings: EmbedCodeFileSettings;
async onload() {
await this.loadSettings();
this.addSettingTab(new EmbedCodeFileSettingTab(this.app, this));
this.registerMarkdownPostProcessor((element, context) => {
this.addTitle(element, context);
});
// live preview renderers
const supportedLanguages = this.settings.includedLanguages.split(",")
supportedLanguages.forEach(l => {
console.log(`registering renderer for ${l}`)
this.registerRenderer(l)
});
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
async registerRenderer(lang: string) {
this.registerMarkdownCodeBlockProcessor(`embed-${lang}`, async (meta, el, ctx) => {
let fullSrc = ""
let src = ""
let metaYaml: any
try {
metaYaml = parseYaml(meta)
} catch(e) {
await MarkdownRenderer.renderMarkdown("`ERROR: invalid embedding (invalid YAML)`", el, '', this)
return
}
let srcPath = metaYaml.PATH
if (!srcPath) {
await MarkdownRenderer.renderMarkdown("`ERROR: invalid source path`", el, '', this)
return
}
if (srcPath.startsWith("https://") || srcPath.startsWith("http://")) {
try {
let httpResp = await requestUrl({url: srcPath, method: "GET"})
fullSrc = httpResp.text
} catch(e) {
const errMsg = `\`ERROR: could't fetch '${srcPath}'\``
await MarkdownRenderer.renderMarkdown(errMsg, el, '', this)
return
}
} else if (srcPath.startsWith("vault://")) {
srcPath = srcPath.replace(/^(vault:\/\/)/,'');
const tFile = app.vault.getAbstractFileByPath(srcPath)
if (tFile instanceof TFile) {
fullSrc = await app.vault.read(tFile)
} else {
const errMsg = `\`ERROR: could't read file '${srcPath}'\``
await MarkdownRenderer.renderMarkdown(errMsg, el, '', this)
return
}
} else {
const errMsg = "`ERROR: invalid source path, use 'vault://...' or 'http[s]://...'`"
await MarkdownRenderer.renderMarkdown(errMsg, el, '', this)
return
}
let srcLinesNum: number[] = []
const srcLinesNumString = metaYaml.LINES
if (srcLinesNumString) {
srcLinesNum = analyseSrcLines(srcLinesNumString)
}
if (srcLinesNum.length == 0) {
src = fullSrc
} else {
src = extractSrcLines(fullSrc, srcLinesNum)
}
let title = metaYaml.TITLE
if (!title) {
title = srcPath
}
await MarkdownRenderer.renderMarkdown('```' + lang + '\n' + src + '\n```', el, '', this)
this.addTitleLivePreview(el, title);
});
}
addTitleLivePreview(el: HTMLElement, title: string) {
const codeElm = el.querySelector('pre > code')
if (!codeElm) { return }
const pre = codeElm.parentElement as HTMLPreElement;
this.insertTitlePreElement(pre, title)
}
addTitle(el: HTMLElement, context: MarkdownPostProcessorContext) {
// add some commecnt
let codeElm = el.querySelector('pre > code')
if (!codeElm) {
return
}
const pre = codeElm.parentElement as HTMLPreElement;
const codeSection = context.getSectionInfo(pre)
if (!codeSection) {
return
}
const view = app.workspace.getActiveViewOfType(MarkdownView)
if (!view) {
return
}
const num = codeSection.lineStart
const codeBlockFirstLine = view.editor.getLine(num)
let matchTitle = codeBlockFirstLine.match(/TITLE:\s*"([^"]*)"/i)
if (matchTitle == null) {
return
}
const title = matchTitle[1]
if (title == "") {
return
}
this.insertTitlePreElement(pre, title)
}
insertTitlePreElement(pre: HTMLPreElement, title: string) {
pre
.querySelectorAll(".obsidian-embed-code-file")
.forEach((x) => x.remove());
let titleElement = document.createElement("pre");
titleElement.appendText(title);
titleElement.className = "obsidian-embed-code-file";
titleElement.style.color = this.settings.titleFontColor;
titleElement.style.backgroundColor = this.settings.titleBackgroundColor;
pre.prepend(titleElement);
}
}