Skip to content

Instantly share code, notes, and snippets.

@sethyuan
Last active August 6, 2022 12:54
Show Gist options
  • Save sethyuan/4ea9ed4305d0145ad565b2128ae6cef4 to your computer and use it in GitHub Desktop.
Save sethyuan/4ea9ed4305d0145ad565b2128ae6cef4 to your computer and use it in GitHub Desktop.
Logseq namespace collapsing
function collapseNamespaceRefs() {
function onEnter(e) {
const el = e.target;
el.textContent = el.dataset.origText;
}
function onLeave(e) {
const el = e.target;
const text = el.dataset.origText;
el.textContent = `..${text.substring(text.lastIndexOf("/") + 1)}`;
}
const observer = new MutationObserver((mutationList) => {
for (const mutation of mutationList) {
for (const node of mutation.addedNodes) {
if (!node.querySelectorAll) continue;
const namespaceRefs = node.querySelectorAll(
'a.page-ref[data-ref*="/"],a.tag[data-ref*="/"]'
);
for (const namespaceRef of namespaceRefs) {
const text = namespaceRef.textContent;
const testText = namespaceRef.classList.contains("tag")
? text.substring(1).toLowerCase()
: text.toLowerCase();
if (testText !== namespaceRef.dataset.ref) continue;
// Perform collapsing.
const content = `..${text.substring(text.lastIndexOf("/") + 1)}`;
namespaceRef.dataset.origText = text;
namespaceRef.textContent = content;
namespaceRef.addEventListener("mouseenter", onEnter);
namespaceRef.addEventListener("mouseleave", onLeave);
}
}
}
});
observer.observe(document.getElementById("app-container"), {
subtree: true,
childList: true,
});
}
collapseNamespaceRefs();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment