-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
140 lines (132 loc) · 4.39 KB
/
Copy pathindex.ts
File metadata and controls
140 lines (132 loc) · 4.39 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
import '@logseq/libs';
interface propsObject {
icon?: string;
}
const getPageProps = async (title: string): Promise<propsObject> => {
title = title.toLowerCase();
let pageProps: propsObject = Object.create(null);
const iconQuery = `
[
:find ?props
:where
[?id :block/name "${title}"]
[?id :block/properties ?props]
]
`;
const queryResultArr = await logseq.DB.datascriptQuery(iconQuery);
if (
queryResultArr[0] &&
queryResultArr[0][0] &&
queryResultArr[0][0].icon
) {
pageProps.icon = queryResultArr[0][0].icon;
}
return pageProps;
};
const getAliasedPageTitle = async (title: string): Promise<string> => {
title = title.toLowerCase();
let aliasedPageTitle = '';
const inheritedAliasQuery = `
[
:find ?origtitle
:where
[?id :block/name "${title}"]
[?origid :block/alias ?id]
[?origid :block/name ?origtitle]
]
`;
const aliasArr = await logseq.DB.datascriptQuery(inheritedAliasQuery);
if (aliasArr.length) {
aliasedPageTitle = aliasArr[0][0];
}
return aliasedPageTitle;
};
const getAliasedPageProps = async (pageTitle: string): Promise<propsObject> => {
let pageProps: propsObject = Object.create(null);
const aliasedPageTitle = await getAliasedPageTitle(pageTitle);
if (aliasedPageTitle) {
pageProps = await getPageProps(aliasedPageTitle);
}
return pageProps;
};
const getPropsByPageName = async (pageTitle: string): Promise<propsObject> => {
let resultedPageProps: propsObject = Object.create(null);
// get from own page
let pageProps = await getPageProps(pageTitle);
if (pageProps) {
resultedPageProps = { ...pageProps };
}
if (!resultedPageProps['icon'] || !resultedPageProps['color']) {
// get from aliased page
pageProps = await getAliasedPageProps(pageTitle);
if (pageProps) {
resultedPageProps = { ...pageProps, ...resultedPageProps };
}
}
resultedPageProps = { ...resultedPageProps };
//@ts-ignore
resultedPageProps.__proto__ = null;
return resultedPageProps;
};
const isEmoji = (text: string): boolean => {
const regex_emoji =
/[\p{Extended_Pictographic}\u{1F3FB}-\u{1F3FF}\u{1F9B0}-\u{1F9B3}]/u;
return regex_emoji.test(text);
};
const setIconToLinkItem = async (
linkItem: HTMLElement,
pageProps: propsObject
) => {
const pageIcon = pageProps['icon'];
if (pageIcon && pageIcon !== 'none') {
const oldPageIcon = linkItem.querySelector('.page-icon');
oldPageIcon && oldPageIcon.remove();
linkItem.insertAdjacentHTML(
'afterbegin',
`<span class="page-icon" data-is-emoji="${isEmoji(pageIcon)}">${pageIcon} </span>`
);
}
};
const processLinkItem = async (linkItem: HTMLElement) => {
const linkText = linkItem.textContent;
if (linkText && !linkText.startsWith(' ')) {
const pageTitle =
linkItem.getAttribute('data-ref') ||
linkItem.childNodes[1]?.textContent?.trim() ||
linkItem.textContent?.trim() ||
'';
if (pageTitle) {
const pageProps = await getPropsByPageName(pageTitle);
if (pageProps) {
setIconToLinkItem(linkItem, pageProps);
}
}
}
};
const pageLinksSelector =
'.page-ref:not(.page-property-key), .tag, .references li a';
const processLinkItems = (node: Document | Element) => {
const pageLinksList = [...node.querySelectorAll(pageLinksSelector)];
for (let i = 0; i < pageLinksList.length; i++) {
const linkItem = pageLinksList[i];
processLinkItem(linkItem as HTMLElement);
}
};
const main = async () => {
const doc = parent.document;
const appContainer = doc.getElementById('app-container')!;
const extLinksObserverConfig = { childList: true, subtree: true };
const extLinksObserver = new MutationObserver((mutationsList, observer) => {
for (const element of mutationsList) {
const addedNode = element.addedNodes[0] as Element;
if (addedNode?.childNodes.length) {
processLinkItems(addedNode);
}
}
});
setTimeout(() => {
processLinkItems(doc);
extLinksObserver.observe(appContainer, extLinksObserverConfig);
}, 500);
};
logseq.ready(main).catch(console.error);