-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathcodeBlockProcessor.ts
More file actions
109 lines (100 loc) · 2.86 KB
/
Copy pathcodeBlockProcessor.ts
File metadata and controls
109 lines (100 loc) · 2.86 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
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
import { App, MarkdownPostProcessorContext, parseYaml } from "obsidian";
import { Renders } from "src/render/renders";
import { MISS_CONFIG } from "./bizErrors";
import { GraphProcessError } from "./graphProcessError";
import { CompositeDataSourceQuery } from "src/query/compositeDataSourceQuery";
import { YamlGraphConfig } from "./types";
import { YamlConfigReconciler } from "./yamlConfigReconciler";
import { getAPI } from "obsidian-dataview";
export class CodeBlockProcessor {
dataSourceQuery: CompositeDataSourceQuery = new CompositeDataSourceQuery();
async renderFromCodeBlock(
code: string,
el: HTMLElement,
ctx: MarkdownPostProcessorContext,
app: App
) {
try {
const graphConfig: YamlGraphConfig = this.loadYamlConfig(el, code);
await this.renderFromYaml(graphConfig, el, app);
} catch (e) {
if (e instanceof GraphProcessError) {
Renders.renderErrorTips(el, e.summary, e.recommends);
} else {
console.error(e);
const notice = "unexpected error: " + e.message;
Renders.renderErrorTips(el, notice);
}
}
}
async renderFromYaml(graphConfig: YamlGraphConfig, el: HTMLElement, app: App) {
const renderCallback = async () => {
try {
// validate
YamlGraphConfig.validate(graphConfig);
const data = await this.dataSourceQuery.query(
graphConfig.dataSource,
app
);
const aggregatedData = [];
if (graphConfig.data) {
aggregatedData.push(...graphConfig.data);
}
aggregatedData.push(...data);
graphConfig.data = aggregatedData;
// render
Renders.render(
el,
YamlGraphConfig.toContributionGraphConfig(graphConfig)
);
} catch (e) {
if (e instanceof GraphProcessError) {
Renders.renderErrorTips(el, e.summary, e.recommends);
} else {
console.error(e);
const notice = "unexpected error: " + e.message;
Renders.renderErrorTips(el, notice);
}
}
}
const dv = getAPI(app);
if (!dv) {
throw new GraphProcessError({
summary: "Initialize Dataview failed",
recommends: ["Please install Dataview plugin"],
});
}
if (dv.index.initialized) {
await renderCallback();
} else {
// @ts-ignore
app.metadataCache.on("dataview:index-ready", async () => {
await renderCallback();
});
}
}
loadYamlConfig(el: HTMLElement, code: string): YamlGraphConfig {
if (code == null || code.trim() == "") {
throw new GraphProcessError(MISS_CONFIG());
}
try {
// @ts-ignore
const yamlConfig: YamlGraphConfig = parseYaml(code);
return YamlConfigReconciler.reconcile(yamlConfig);
} catch (e) {
if (e.mark?.line) {
throw new GraphProcessError({
summary:
"yaml parse error at line " +
(e.mark.line + 1) +
", please check the format",
});
} else {
throw new GraphProcessError({
summary:
"content parse error, please check the format(such as blank, indent)",
});
}
}
}
}