Skip to content

Commit 5b391ec

Browse files
committed
Page Cloud : add minified version
1 parent 3231a0e commit 5b391ec

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

Snippets/Page Cloud 1/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,22 @@ The following are preview images of what the snippet will do and appear like:
5454

5555
<br />
5656

57+
Two versions of the script are provided below.
58+
1. [Normal Version](#normal-version)
59+
2. [Minified Version](#minified-version)
60+
61+
<br />
62+
63+
The [Minified](#minified-version) version is much easier to paste.
64+
<br />
65+
The [Normal](#normal-version) version can help you read the code.
66+
67+
Pick one or the other to paste in your Obsidian note.
68+
69+
<br />
70+
71+
### Normal Version
72+
5773
````shell
5874
```dataviewjs
5975
/*
@@ -283,6 +299,25 @@ CreatePageCloud( )
283299
284300
<br />
285301
302+
### Minified Version
303+
304+
````shell
305+
```dataviewjs
306+
const QueryStr = `""`;
307+
const sortOption = 1;
308+
const weightBacklinks = 0.1;
309+
const weightWordCount = 0.5;
310+
const minFontSize = 12;
311+
const maxFontSize = 32;
312+
const arrColors = [];
313+
314+
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();
315+
```
316+
````
317+
318+
<br />
319+
<br />
320+
286321
Next, you need to add some custom CSS.
287322
Open Obsidian Settings, click **Appearance**, and then scroll all the way down. (See image below).
288323

0 commit comments

Comments
 (0)