-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
144 lines (126 loc) · 5.67 KB
/
script.js
File metadata and controls
144 lines (126 loc) · 5.67 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
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
mdFileContentMap = {}
isDarkmode = window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches;
colorTable = {
"existing_node": {
backgroundColor: (isDarkmode ? "#aaaaaa" : "#555555"),
borderColor: "#999999",
fontColor: (isDarkmode ? "#aaaaaa" : "#555555")
},
"nonexisting_node": {
backgroundColor: (isDarkmode ? "#555555" : "#aaaaaa"),
borderColor: "#999999",
fontColor: (isDarkmode ? "#555555" : "#aaaaaa")
},
"edge": {
color: "#999999"
}
};
async function loadGraph() {
let mdFileList = await fetchMdFileListFromDisk();
let graphData = { nodes: [], edges: [] };
for (let file of mdFileList) {
let filename = file.name;
graphData.nodes.push({ id: filename,
label: filename.substring(filename.lastIndexOf("/") + 1).replace(".md", ""),
shape: "dot",
color: {
background: colorTable["existing_node"].backgroundColor,
border: colorTable["existing_node"].borderColor
},
size: 5,
font: {
color: colorTable["existing_node"].fontColor,
bold: true
}
});
mdFileContentMap[filename] = null;
for (let link of file.links_to_exist) {
graphData.edges.push({
from: filename,
to: link,
color: {
color: colorTable["edge"].color,
opacity: 0.7
},
chosen: false
});
}
for (let link of file.links_to_nonexist) {
graphData.nodes.push({
id: link,
label: link,
shape: "dot",
color: {
background: colorTable["nonexisting_node"].backgroundColor,
border: colorTable["nonexisting_node"].borderColor
},
chosen: false,
size: 5,
font: {
color: colorTable["nonexisting_node"].fontColor,
bold: false
}
});
graphData.edges.push({
from: filename,
to: link,
color: {
color: colorTable["edge"].color,
opacity: 0.7
},
chosen: false
});
}
}
renderGraph(graphData);
}
async function fetchMdFileListFromDisk() {
try {
const response = await fetch("./notes/index.json");
return await response.json();
} catch (error) {
console.error("Error fetching JSON:", error);
return [];
}
}
async function fetchMarkdownContent(file) {
try {
const response = await fetch(file);
return await response.text();
} catch (error) {
console.error("Error fetching markdown file:", file);
return "";
}
}
function renderGraph(graphData) {
let container = document.getElementById("graph");
let options = { interaction: { hideEdgesOnDrag: true, hideEdgesOnZoom: true, hover: true },
layout: { improvedLayout: false },
nodes: { font: { face: "D2 Coding" } },
physics: { enabled: true } };
let network = new vis.Network(container, graphData, options);
network.on("click", async function (params) {
if (params.nodes.length > 0) {
let file = params.nodes[0];
if (file in mdFileContentMap) {
let content = mdFileContentMap[file];
if (content === null) {
content = await fetchMarkdownContent(file);
content = removeLinks(content);
mdFileContentMap[file] = content;
}
document.getElementById("viewer").innerHTML = marked.parse(content);
}
}
});
}
function removeLinks (content) {
let wiki_link_pattern1 = /\[\[[^|^\]]+[|]([^|^\]]+)\]\]/g; // [[LinkFile|ShownText]]
let wiki_link_pattern2 = /\[\[([^\]]+)\]\]/g; // [[LinkFileAlsoShownText]]
let md_link_pattern = /\[(.*?)\]\(.*?\)/g; // [ShownText](Link)
content = content.replaceAll (wiki_link_pattern1, "$1");
content = content.replaceAll (wiki_link_pattern2, "$1");
content = content.replaceAll (md_link_pattern, "$1");
return content;
}
window.onload = loadGraph;