+const QueryStr='""',sortOption=1,weightBacklinks=.1,weightWordCount=.5,minFontSize=12,maxFontSize=32,arrColors=[],QueryFiles=dv.pages(QueryStr);for(let i=0;i<40;i++){let itemColor=`#${((Math.floor(100*Math.random()+100)<<16)+(Math.floor(100*Math.random()+100)<<8)+Math.floor(100*Math.random()+100)).toString(16)}`;arrColors.push(itemColor)}async function QueryBacklinks(e){let t=e.split("/").pop().split(".").slice(0,-1).join(".");return dv.query(`LIST FROM [[${t}]] AND ${QueryStr}`)}async function QueryWordcount(e){let t=require("fs"),r=require("path"),o=t.readFileSync(r.join(app.vault.adapter.basePath,e),"utf-8"),n=/---[\s\S]*?---|```[\s\S]*?```|\$[\s\S]*?\$|\$\$[\s\S]*?\$\$/g,a=o.replace(n,""),l=a.match(/\S+/g);return l?l.length:0}function Generate_FontSize(e,t){let r=2.5*Math.sqrt(e*weightBacklinks+t*weightWordCount);return Math.round(r/100*(maxFontSize-minFontSize)+minFontSize)}function Generate_Color(e){if(null==e)return"#FFFFFF";let t=e.split(/\W+/g),r=t.reduce((e,t)=>e+t.charCodeAt(0),0)%Object.keys(arrColors).length;return arrColors[Object.keys(arrColors)[r]]}function Sort_DESC(e){return e.sort((e,t)=>e.id.localeCompare(t.id)),e}function Sort_ASC(e){return e.sort((e,t)=>t.id.localeCompare(e.id)),e}function Sort_Shuffle(e){for(let t=e.length-1;t>0;t--){let r=Math.floor(Math.random()*(t+1));[e[t],e[r]]=[e[r],e[t]]}return e}const CreatePageCloud=async()=>{let files=new Map;Promise.all(QueryFiles.map(async e=>{let t=e.file,r=QueryBacklinks(t.path),o=QueryWordcount(t.path),n={backlinks:0,wordCount:0},a=await r;n.backlinks=a.value.values.length;let l=await o;n.wordCount=l,files.set(t,n)})).then(()=>{let data=[];files.forEach((e,t)=>{if(null==t)return;var r=t.frontmatter.name||t.frontmatter.title||t.frontmatter.alias,o=t.name;r&&(o=r);let n=Generate_FontSize(e.backlinks,e.wordCount),a=Generate_Color(t.name);data.push({name:o,id:t.name,size:t.size,path:t.path,fontSize:n,color:a})});let sortOptions={1:"Sort_DESC",2:"Sort_ASC",3:"Sort_Shuffle"},funcSort=sortOptions[sortOption];return void 0===funcSort&&(funcSort=sortOptions[1]),eval(funcSort)(data).map(e=>`<span class="page-cloud-v1-item"><a class="page-cloud-v1-link" href="obsidian://open?file=${encodeURIComponent(e.id)}" style="font-size:${e.fontSize}px; color: ${e.color};">${e.name}</a></span>`).join("")}).then(e=>dv.paragraph(e)).catch(e=>{console.error("Error: "+e)})};CreatePageCloud();
0 commit comments