使用 dailynote 笔记法的用户,会倾向于用 moc 来管理笔记,而不用文档树管理笔记,管理文档链接而不管理文档,这时候「文档层级导航插件」的显示父子文档功能就没有什么导航作用了,需要靠反链的文档来导航,显示相关文档。
由于使用 dailynote 积累笔记,反链有很多 dailynote,但这些 dailynote 和这篇文档的主题无关系只有补充信息。
我希望文档层级导航插件的反链展示可以只展示与本文档真正有关联的笔记(即真正的相关笔记),排除 dailynote 的干扰,因此希望是可以有一个选项可以过滤掉这些 dailynote 的
我给插件提了一个 issue:反链能支持排除 dailynote 笔记嘛 · Issue #64 · OpaqueGlass/syplugin-hierarchyNavigate (github.com)
暂时没得到回复,于是自己问 GPT 写了一个 js 代码片段,先用上了
具有如下功能
- 通过正则过滤 dailynote 笔记,此外我还有周总结、月总结的习惯,也希望反链不出现这些笔记。可以根据需要修改 filterPatterns 变量的正则表达式
- 对反链的文档进行排序
- 我习惯 moc 笔记添加前缀 @,所以额外处理,将 moc 笔记放在最前面
(async ()=>{function filterAndReorderLinks() { const filterPatterns = [ /^\d{8} \w{3}$/, // Example: 20240728 Sun /^\d{6}$/, /^\d{8} ~ \d{8}$/, /^\d{8} \- \d{8}$/ // Add more patterns here if needed ]; const containers = document.querySelectorAll("div.og-hierachy-navigate-backlink-doc-container"); if (containers.length === 0) { return; } containers.forEach(container => { const refLinks = container.querySelectorAll("span.refLinks.docLinksWrapper"); if (refLinks.length === 0) { return; } // Hide elements that match any pattern in filterPatterns refLinks.forEach(link => { const docName = link.getAttribute("title"); if (filterPatterns.some(pattern => pattern.test(docName))) { link.style.display = "none"; } else { link.style.display = ""; // Reset display property if it doesn't match } }); // Separate elements into two arrays: one for @-prefixed and one for otherItems const mocItems = []; const otherItems = []; refLinks.forEach(link => { const docName = link.getAttribute("title"); if (docName.startsWith("@")) { mocItems.push(link); } else { otherItems.push(link); } }); // Sort the 'otherItems' array based on the 'title' attribute in ascending order otherItems.sort((a, b) => { const titleA = a.getAttribute("title").toUpperCase(); // Ignore upper and lowercase const titleB = b.getAttribute("title").toUpperCase(); // Ignore upper and lowercase return titleA.localeCompare(titleB); }); // Clear only the refLinks elements and append them in the desired order refLinks.forEach(link => link.remove()); mocItems.concat(otherItems).forEach(link => { container.appendChild(link); }); }); } // 监控dom变化 function observeDomChange(targetNode, callback) { const config = { childList: true, subtree: true }; const observer = new MutationObserver((mutationsList) => { for (const mutation of mutationsList) { if (mutation.type === 'childList') { callback(mutation); } } }); observer.observe(targetNode, config); // 返回observer,用于停止观察 // observer.disconnect(); return observer; } // 等待元素渲染完成后执行的函数 function whenElementExist(selector) { return new Promise(resolve => { const checkForElement = () => { let element = null; if (typeof selector === 'function') { element = selector(); } else { element = document.querySelector(selector); } if (element) { resolve(element); } else { // 如果元素不存在,等浏览器再次重绘,递归调用checkForElement,直到元素出现 requestAnimationFrame(checkForElement); } }; checkForElement(); }); } // 等待笔记区出现 whenElementExist(".layout__center").then((element)=>{ const observer = observeDomChange(element, (mutation) => { // 监听弹窗代码列表 if(mutation.target.classList.contains("protyle-top") || mutation.target.classList.contains("og-hn-heading-docs-container")) { setTimeout(filterAndReorderLinks, 100); } }); }); })();
使用
运行前
自定义 js 运行后
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于