Cert-Paths/.obsidian/plugins/excalibrain/main.js

4 lines
382 KiB
JavaScript
Raw Normal View History

2024-10-21 15:17:23 -04:00
"use strict";var e,t,i,n=require("obsidian");!function(e){e[e.DEFINED=1]="DEFINED",e[e.INFERRED=2]="INFERRED"}(e||(e={})),function(e){e[e.PARENT=0]="PARENT",e[e.CHILD=1]="CHILD",e[e.LEFT=2]="LEFT",e[e.RIGHT=3]="RIGHT"}(t||(t={})),function(e){e[e.TO=1]="TO",e[e.FROM=2]="FROM",e[e.BOTH=3]="BOTH"}(i||(i={}));const s=/(?:\[([^[\]]+)\]\()((?:(?:ftp|https?|sftp|shttp|tftp):(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>"']|\([^\s()<>]*\))+(?:\([^\s()<>]*\)|[^\s`*!()\[\]{};:'".,<>?«»“”‘’]))\)|\b()((?:(?:ftp|https?|sftp|shttp|tftp):(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>"']|\([^\s()<>]*\))+(?:\([^\s()<>]*\)|[^\s`*!()\[\]{};:'".,<>?«»“”‘’]))\b/gi;class r{constructor(e){this.plugin=e,this.fileToUrlMap=new Map,this.fileUrlInverseMap=new Map,this.hosts=[],this.initalized=!1,this.app=e.app}async init(){const e=Date.now(),t=this.app.vault.getMarkdownFiles();for(const e of t)await this.parseFileURLs(e);this.registerFileEvents(),this.initalized=!0,console.log(`ExcaliBrain indexed ${this.fileUrlInverseMap.size} URLs from ${this.hosts.length} unique hosts in ${this.fileToUrlMap.size} of ${t.length} markdown files in ${((Date.now()-e)/1e3).toFixed(1)} seconds`)}getOrigin(e,t){try{return new URL(e).origin}catch(i){return console.log(`ExcaliBrain URLParser: Invalid URL ${e} in file ${t.path}`),":Unknown Origin:"}}async parseFileURLs(e){var t,i,r,a;if(!(e&&e instanceof n.TFile))return;let u="";try{u=await this.app.vault.cachedRead(e)}catch(t){return void console.log(`ExcaliBrain URLParser: Failed to read file ${e.path}`,t)}const o=new Map;let l;for(;null!==(l=s.exec(u));){const n=null!==(i=null!==(t=l[1])&&void 0!==t?t:l[3])&&void 0!==i?i:"";let s=null!==(a=null!==(r=l[2])&&void 0!==r?r:l[4])&&void 0!==a?a:"";if(s.match(/^(?:ftp|https?|sftp|shttp|tftp):/)||(s="https://"+s),o.has(s)){if(""!==n){const e=o.get(s);""===e.alias&&o.set(s,Object.assign(Object.assign({},e),{alias:n}))}}else{const t=this.getOrigin(s,e);o.set(s,{url:s,alias:n,origin:t}),this.updateInverseMap(s,e,t)}}const D=Array.from(o.values());D.length>0&&this.fileToUrlMap.set(e,D)}updateInverseMap(e,t,i){this.fileUrlInverseMap.has(e)||this.fileUrlInverseMap.set(e,{files:[],origin:i});const n=this.fileUrlInverseMap.get(e);n&&(n.files.includes(t)||n.files.push(t),this.hosts.includes(n.origin)||this.hosts.push(n.origin))}registerFileEvents(){const e=e=>{t(e),this.parseFileURLs(e)},t=e=>{const t=this.fileToUrlMap.get(e);this.fileToUrlMap.delete(e),t&&t.forEach((t=>{const i=this.fileUrlInverseMap.get(t.url);if(i){if(i.files=i.files.filter((t=>t!==e)),0===i.files.length)return this.fileUrlInverseMap.delete(t.url),void(this.hosts=this.hosts.filter((e=>e!==i.origin)));this.fileUrlInverseMap.set(t.url,i)}}))};this.plugin.registerEvent(this.app.vault.on("create",e)),this.plugin.registerEvent(this.app.vault.on("modify",e)),this.plugin.registerEvent(this.app.vault.on("delete",t))}}const a=(e,t,i)=>{const n=e.metadataCache.getFirstLinkpathDest(t,i);return n?n.path:t},u=(e,t)=>{var i,n,r,u,o;const l=new Set,D=e.matchAll(/[^[]*\[\[(?<wikiLink>[^#\]\|]*)[^\]]*]]|\[[^\]]*]\((?<mdLink>[^)]*)\)/g);let d,h;for(;!(d=D.next()).done;){if(null===(n=null===(i=null==d?void 0:d.value)||void 0===i?void 0:i.groups)||void 0===n?void 0:n.wikiLink){const e=a(app,d.value.groups.wikiLink,t.path);e&&l.add(e)}if(null===(u=null===(r=null==d?void 0:d.value)||void 0===r?void 0:r.groups)||void 0===u?void 0:u.mdLink){const e=a(app,decodeURIComponent(d.value.groups.mdLink),t.path);e&&l.add(e)}}for(;null!==(h=s.exec(e));)l.add(null!==(o=h[2])&&void 0!==o?o:h[4]);return Array.from(l)},o=(e,t,i)=>{var s;const r=new Set;if(t.values){if(t.hasOwnProperty("conversionAccuracy"))return[];const l=Array.from(t.values());let D;try{l.filter((e=>(null==e?void 0:e.type)&&("file"===e.type||"header"===e.type||"block"==e.type))).forEach((t=>{const n=a(e.app,t.path,i.path);n&&r.add(n)})),l.filter((e=>Boolean(null==e?void 0:e.values))).forEach((t=>{D=t;const n=Array.from(t.values());o(e,n,i).forEach((e=>r.add(e)))}))}catch(e){console.log(e),console.log(l),con
//! currently there is an issue with case sensitivity. DataView retains case sensitivity of links for the front matter, but not the others
h||console.log({error:"objectLinks is undefined which is unexpected",errorLocation:"readDVField",field:t,file:i});const c=((e,t,i)=>{const s=new Set;return t.forEach((t=>{var i;(null===(i=null==t?void 0:t.hasOwnProperty)||void 0===i?void 0:i.call(t,"ts"))&&s.add(n.moment(t.ts).format(e.dailyNoteSettings.format))})),Array.from(s)})(e,l);return Array.from(r).concat(d).concat(null!=h?h:[]).concat(c)}if(t.path){const n=a(e.app,t.path,i.path);return n?[n]:[]}return"string"==typeof t?u(t,i):[]},l=(e,t,i)=>{const n=[],s=new Set;return i.forEach((i=>{const r=t[i];r&&!s.has(i)&&(s.add(i),o(e,r,t.file).forEach((e=>n.push({link:e,field:i}))))})),n},D=(e,t)=>{var i;const n=d(e,t);if(!n)return[null,null];if(e[t.primaryTagFieldLowerCase]){const s=null===(i=e[t.primaryTagFieldLowerCase].match(/#([^\s\])$"'\\]*)(?:$|\s)/g))||void 0===i?void 0:i.map((e=>e.trim())).filter((e=>t.tagStyleList.some((t=>e.startsWith(t))))),r=s&&s.length>0?s[0]:n[0];return[r,n.filter((e=>e!=r))]}return[n[0],n.slice(1)]},d=(e,t)=>{var i,n,s;return e?(null!==(s=null===(n=null===(i=e.file)||void 0===i?void 0:i.tags)||void 0===n?void 0:n.values)&&void 0!==s?s:[]).filter((e=>t.tagStyleList.some((t=>e.startsWith(t))))):null},h=(e,t)=>{const[i,n]=e;if(!i)return{};const s=t.tagNodeStyles[t.tagStyleList.filter((e=>i.startsWith(e)))[0]];if(s&&t.displayAllStylePrefixes){const e=Object.keys(t.tagNodeStyles).filter((e=>n.includes(e))),i=new Set;s.prefix&&i.add(s.prefix),null==e||e.map((e=>t.tagNodeStyles[e].prefix)).filter((e=>Boolean(e))).forEach((e=>i.add(e)));const r=Array.from(i).join("");return Object.assign(Object.assign({},s),{prefix:r})}return s},c=e=>{console.error(Object.assign({plugin:"ExcaliBrain"},e))};console.log.bind(window.console),console.log.bind(window.console);const g=e=>`data:image/svg+xml;base64,${btoa(unescape(encodeURIComponent(e.replaceAll("&nbsp;"," "))))}`,E=(e,t)=>{if(!e.DEVICE.isDesktop)return;let i=!0;if(!t){const n=e.targetView;if(!n)return;i=app.workspace.activeLeaf===n.leaf,t=n.ownerWindow}i&&(t.electronWindow.isAlwaysOnTop()||(t.electronWindow.setAlwaysOnTop(!0),setTimeout((()=>{t.electronWindow.setAlwaysOnTop(!1)}),500)))};function p(e){const t=e.lastIndexOf("/"),i=-1==t?e:e.substring(t+1);return{folderpath:n.normalizePath(e.substring(0,t)),filename:i,basename:i.replace(/\.[^/.]+$/,"")}}function f(e,t){return!!e&&["pdf","mp4","mp3","webm","md"].includes(e.extension)&&!t.isExcalidrawFile(e)}class F{constructor(e){this.pages=new Map,this.forEach=this.pages.forEach.bind(this.pages),this.app=e.app,this.plugin=e}add(e,t){this.pages.set(e,t)}has(e){return this.pages.has(e)}get(e){return this.pages.get(e)}getPages(){return Array.from(this.pages.values())}get size(){return this.pages.size}delete(e){const t=this.pages.get(e);t&&(t.neighbours.forEach(((t,i)=>{const n=this.pages.get(i);n&&(n.unlinkNeighbour(e),n.file||0!==n.neighbours.size||this.pages.delete(i))})),this.pages.delete(e))}addInferredParentChild(t,n){this.plugin.settings.inferAllLinksAsFriends?(n.addLeftFriend(t,e.INFERRED,i.TO),t.addLeftFriend(n,e.INFERRED,i.FROM)):this.plugin.settings.inverseInfer?(n.addChild(t,e.INFERRED,i.TO),t.addParent(n,e.INFERRED,i.FROM)):(n.addParent(t,e.INFERRED,i.TO),t.addChild(n,e.INFERRED,i.FROM))}addResolvedLinks(e){const t=this.app.metadataCache.resolvedLinks;Object.keys(t).forEach((i=>{if(e&&e.path!==i)return;const n=this.pages.get(i);Object.keys(t[i]).forEach((e=>{let t=this.pages.get(e);t||(t=this.pages.get(this.plugin.lowercasePathMap.get(e.toLowerCase()))),this.addInferredParentChild(n,t)}))}))}addPageURLs(){this.plugin.urlParser.fileToUrlMap.forEach(((t,n)=>{const s=this.get(n.path);s&&t.forEach((t=>{let n=this.get(t.url);n||(n=new A(this,t.url,null,this.plugin,!1,!1,t.alias,t.url),this.add(t.url,n)),""===t.alias||""!==n.name&&n.name!==t.url||(n.name=t.alias),this.addInferredParentChild(s,n);const r=this.get(t.origin);r&&(n.addParent(r,e.INFERRED,i.FROM),r.addChild(n,e.INFERRED,i.TO))}))}))}addUnresolvedLinks(e){if(e&&(e.isFolder||e.isTag))return;const t=this.app.metadataCache.unresolvedLinks;Object.keys(t).forEach((i=>{if(e&&e.path!==i)return;let
/* nosourcemap */