-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
116 lines (114 loc) · 3.97 KB
/
main.js
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
/*
THIS IS A GENERATED/BUNDLED FILE BY ESBUILD
if you want to view the source, please visit the github repository of this plugin
*/
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// main.ts
var main_exports = {};
__export(main_exports, {
default: () => GraphNestedTagsPlugin
});
module.exports = __toCommonJS(main_exports);
var import_obsidian = require("obsidian");
class WikiLinkParser {
constructor(mdContent) {
this.mdContent = mdContent; // Markdown 文件的内容
this.nodes = {}; // 用于存储节点
}
parse() {
const lines = this.mdContent.split('\n');
let currentTag = null;
for (const line of lines) {
const tagMatch = line.match(/^## (.+)$/);
if (tagMatch) {
currentTag = tagMatch[1].trim(); // 捕获标题内容
if (!(currentTag in this.nodes)) {
this.nodes[currentTag] = { type: "tag", links: [] }; // 创建标签节点
}
}
const linkMatches = line.matchAll(/\[\[(.+?)\]\]/g);
for (const match of linkMatches) {
const link = match[1].trim(); // 捕获链接内容
if (currentTag) {
this.nodes[currentTag].links.push(link); // 将 link 添加到当前标签节点的 links 中
// 为每个 Wikilink 创建一个节点
if (!(link in this.nodes)) {
this.nodes[link] = { type: "wikilink", links: [] }; // 创建 Wikilink 节点
}
// 创建链接关系:把当前标签节点链接到 Wikilink 节点
this.nodes[link].links.push(currentTag); // 反向链接
}
}
}
}
getNodes() {
return this.nodes; // 返回解析后的节点对象
}
}
var GraphNestedTagsPlugin = class extends import_obsidian.Plugin {
// 使用输入文本解析 Markdown 数据
async inject_setData(localLeaf) {
const r = localLeaf.view.renderer;
if (!r._setData) {
r._setData = r.setData;
}
// Assume we fetch the Markdown content from the active file
const activeFile = this.app.workspace.getActiveFile();
if (activeFile && activeFile.extension === "md") {
const mdContent = await this.app.vault.read(activeFile);
const parser = new WikiLinkParser(mdContent);
parser.parse();
const nodes = parser.getNodes();
// 将节点数据传递给本地视图
r.setData = (data) => {
data.nodes = nodes; // 将解析出来的节点替代 data.nodes
return (r._setData) ? r._setData.call(r, data) : undefined;
};
}
return localLeaf;
}
async onload() {
// 监听本地视图的打开事件
this.registerEvent(
this.app.workspace.on("file-open", async () => {
for (const leaf of this.app.workspace.getLeavesOfType("markdown")) {
if (leaf.view.renderer._setData === void 0) {
await this.inject_setData(leaf);
}
}
})
);
// 触发初次加载
this.app.workspace.trigger("file-open");
}
onunload() {
for (const leaf of this.app.workspace.getLeavesOfType("markdown")) {
if (leaf.view.renderer._setData) {
leaf.view.renderer.setData = leaf.view.renderer._setData;
delete leaf.view.renderer._setData;
leaf.view.unload();
leaf.view.load();
}
}
}
async loadSettings() {
}
async saveSettings() {
}
};