-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic_publish.js
103 lines (82 loc) · 2.3 KB
/
basic_publish.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
/* PUBLISH.JS PROPERTIES */
let id;
function insertMetaDates() {
const frontmatter = app.site.cache.cache[app.currentFilepath].frontmatter;
if (!frontmatter) {
return;
}
/* selects the properties to be published */
const type = frontmatter["type"]?.replaceAll("-", "/");
const tags = frontmatter["tags"];
const aliases = frontmatter["aliases"];
/* maps out and formats the tags */
const tagElms = tags
.map(
(tag) => `
<a href="#${tag}" class="tag" target="_blank" rel="noopener">#${tag}</a>
`
)
.join("");
/* maps out and formats the aliases */
const aliasElms = aliases
.map(
(alias) => `
<a class="alias" target="_blank" rel="noopener">${alias}</a>
`
)
.join("");
const frontmatterEl = document.querySelector(".frontmatter");
if (!frontmatterEl) {
return;
}
/* inserts the html */
frontmatterEl.insertAdjacentHTML(
"afterend",
`
<div class="propertyitemtable">
<div id="typeproperty" class="propertyitem">type: ${type}</div>
</div>
<div id="tagsproperty" class="propertyitemtags">
${tagElms}
</div>
<div id="aliasesproperty" class="propertyitemaliases">
${aliasElms}
</div>
`
);
/* makes sure that only existing properties are shown */
if (type) {
document.getElementById('typeproperty').style.display = ""
} else {
document.getElementById('typeproperty').style.display = "none"
}
if (aliases) {
document.getElementById('aliasesproperty').style.display = ""
} else {
document.getElementById('aliasesproperty').style.display = "none"
}
if (tags) {
document.getElementById('tagsproperty').style.display = ""
} else {
document.getElementById('tagsproperty').style.display = "none"
}
clearInterval(id);
}
/* put the properties after the note title */
const onChangeDOM = (mutationsList, observer) => {
for (let mutation of mutationsList) {
if (
mutation.type === "childList" &&
mutation.addedNodes[0]?.className === "page-header"
) {
clearInterval(id);
id = setInterval(insertMetaDates, 50);
}
}
};
const targetNode = document.querySelector(
".markdown-preview-sizer.markdown-preview-section"
);
const observer = new MutationObserver(onChangeDOM);
observer.observe(targetNode, { childList: true, subtree: true });
id = setInterval(insertMetaDates, 50);