Skip to content

Instantly share code, notes, and snippets.

@min50250
Created September 18, 2023 02:50
Show Gist options
  • Save min50250/196377218680a7a0b4cc2d0af5b3f841 to your computer and use it in GitHub Desktop.
Save min50250/196377218680a7a0b4cc2d0af5b3f841 to your computer and use it in GitHub Desktop.
step7 - CustomJS
class LucyKeywordCloud {
getKeywordCloudData(path){
let dv = app.plugins.plugins.dataview.api
let currentFile = dv.page(path)
let html = ""
console.log(currentFile)
let keywords = dv.page(path).file.outlinks.values.filter(link => link.display.startsWith(')')).map(link => link.display)
keywords = keywords.filter(p => !p.includes(currentFile.file.name))
let keywordsArr = this.countDuplicates(keywords)
let table = this.valueToLevel(keywordsArr)
for(let j = 0 ; j < table.length ; j++){
html = html + "<span class=\"" + table[j][2] + " cloud\" >" + table[j][0] + "</span>"
}
return html
}
countDuplicates(array) {
const counts = {};
const result = [];
// 중복된 요소의 개수를 세기
array.forEach((item) => {
counts[item] = (counts[item] || 0) + 1;
});
// 중복이 제거된 2차원 배열 생성
for (const key in counts) {
result.push(["[[" + key + "|#" + key.slice(2,) + "]]", counts[key]]);
}
// 중복 개수에 따라 내림차순으로 정렬
result.sort((a, b) => b[1] - a[1]);
return result;
}
valueToLevel(arr) {
arr = this.shuffleArray(arr)
const values = arr.map((item) => item[1]);
const minValue = Math.min(...values);
const maxValue = Math.max(...values);
const totalLevels = 7;
const levelSize = (maxValue - minValue) / totalLevels;
return arr.map((item) => {
const name = item[0];
const value = item[1];
const level = Math.ceil((value - minValue) / levelSize);
const adjustedLevel = Math.min(totalLevels, Math.max(1, level));
return [name, value, `cloud${adjustedLevel}`];
});
}
shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment