Files
notes/.obsidian/plugins/update-relative-links/main.js
T
2026-03-01 01:43:46 +08:00

8 lines
3.1 KiB
JavaScript

/*
THIS IS A GENERATED/BUNDLED FILE BY ESBUILD
if you want to view the source, please visit the github repository of this plugin
*/
var $=Object.defineProperty;var b=Object.getOwnPropertyDescriptor;var I=Object.getOwnPropertyNames;var B=Object.prototype.hasOwnProperty;var O=(e,n)=>{for(var o in n)$(e,o,{get:n[o],enumerable:!0})},L=(e,n,o,i)=>{if(n&&typeof n=="object"||typeof n=="function")for(let u of I(n))!B.call(e,u)&&u!==o&&$(e,u,{get:()=>n[u],enumerable:!(i=b(n,u))||i.enumerable});return e};var j=e=>L($({},"__esModule",{value:!0}),e);var A={};O(A,{default:()=>x});module.exports=j(A);var c=require("obsidian");function T(e){return P(y(e).slice(0,-1))}function F(e,n){if(!e)return n;let o=y(e),i=y(n),u=o.findIndex((h,w)=>h!=i[w]),f=[];for(let h=u;h<o.length-1;h++)f.push("..");for(let h=u;h<i.length;h++)f.push(i[h]);return P(f)}function y(e){return e.split("/")}function P(e){return e.join("/")}var E=class extends c.Modal{constructor(n,o,i){super(n),this.content=o,this.onConfirm=i}onOpen(){let{contentEl:n}=this;n.createEl("h1",{text:"Update Releate Links Plugin"}),n.createEl("p",{text:this.content}),new c.Setting(n).addButton(o=>o.setButtonText("Yes").setCta().onClick(()=>{this.close(),this.onConfirm()})).addButton(o=>o.setButtonText("No").onClick(()=>{this.close()}))}onClose(){this.contentEl.empty()}},x=class extends c.Plugin{async onload(){let{app:n}=this,{metadataCache:o,vault:i}=n,u="This command will modify all links in the entire vault (not just the current file) to relative paths, and this action cannot be undone. It is recommended that you back up the vault in advance. Please confirm whether you want to execute the command.";this.addCommand({id:"update-all-relative-links",name:"Update all relative links",callback(){new E(n,u,()=>{let t=i.getMarkdownFiles().map(r=>f(r,!1));Promise.all(t).then(r=>{let s=r.filter(m=>m>0),a=s.reduce((m,C)=>m+C,0),l=s.length;new c.Notice(`Update ${a} links in ${l} file${l>1?"s":""}.`)}).catch(r=>{new c.Notice("Update links error, see console."),console.error(r)})}).open()}}),this.registerEvent(i.on("rename",(t,r)=>{var s;!r||!t.path.toLocaleLowerCase().endsWith(".md")||((s=t.parent)==null?void 0:s.path)===T(r)||t instanceof c.TFile&&setTimeout(()=>f(t,!0),100)}));async function f(t,r){var m,C;let s=o.getFileCache(t),l=[...(m=s==null?void 0:s.links)!=null?m:[],...(C=s==null?void 0:s.embeds)!=null?C:[]].map(({link:d,original:v})=>{var S;let g=d.replace(/#.*$/,"");if(!g)return null;let k=o.getFirstLinkpathDest(g,t.path);if(!k)return null;let p=((S=t.parent)==null?void 0:S.path)==="/"?k.path:F(t.path,k.path);if(g===p)return null;let U=h(v,g,p);return[v,U]}).filter(d=>d);if(!(l!=null&&l.length))return 0;try{let d=await i.read(t),v=l.reduce((k,p)=>(p==null?void 0:p.length)===2?k.replace(p[0],p[1]):k,d);await i.modify(t,v);let g=`Update ${l.length} links in ${t.path}.`;return console.log(g),r&&new c.Notice(g),l.length}catch(d){throw console.error(d),r&&new c.Notice("Update links error, see console."),d}}function h(t,r,s){let a=w(t,r,s,l=>l.replace(/ /g,"%20"));return t===a&&(a=w(t,r,s,encodeURI)),t===a&&(a=t.replace(/^(!?\[.*?\]).*$/,`$1(${encodeURI(s)})`)),a}function w(t,r,s,a){return t.replace(a(r),a(s))}}};
/* nosourcemap */