Skip to content

Instantly share code, notes, and snippets.

@SkepticMystic
Last active October 29, 2022 07:47
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SkepticMystic/adcfc5724cbf4e52078a97c95668c43b to your computer and use it in GitHub Desktop.
Save SkepticMystic/adcfc5724cbf4e52078a97c95668c43b to your computer and use it in GitHub Desktop.
Obsidian Adjacency Matrix Maker
<%*
const files = await app.vault.getMarkdownFiles();
const fileDataArr = await files.map(file => {
if (app.metadataCache.getFileCache(file).links) {
const noHeaderLinks = app.metadataCache.getFileCache(file).links
.map(item => item.link.replace(/#.+/g, ''));
return [file.basename, noHeaderLinks];
} else {
return [file.basename, []];
}
});
const size = fileDataArr.length;
const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");
// imgData to Binary helper function
// Source: https://gist.github.com/borismus/1032746
const BASE64_MARKER = ';base64,';
function convertDataURIToBinary(dataURI) {
let base64Index = dataURI.indexOf(BASE64_MARKER) + BASE64_MARKER.length;
let base64 = dataURI.substring(base64Index);
let raw = window.atob(base64);
let rawLength = raw.length;
let array = new Uint8Array(new ArrayBuffer(rawLength));
for (i = 0; i < rawLength; i++) {
array[i] = raw.charCodeAt(i);
}
return array;
}
function sumRows(array) {
let result = [];
array.forEach((row, i) => {
row.reduce((a, b) => result[i] = a + b)
})
return result;
}
function normalise(array) {
const max = Math.max(...array);
return array.map(x => x / max);
}
function createAdjMatrix(linksArray) {
const adj = [];
for (let i = 0; i < linksArray.length; i++) {
adj.push([]);
for (let j = 0; j < linksArray.length; j++) {
// If note i links to note j, adj[i][j] = 1
adj[i][j] = linksArray[i][1].includes(linksArray[j][0]) ? 1 : 0;
}
}
return adj
}
function addImage(scale = 1) {
// Canvas setup
canvas.width = size * scale;
canvas.height = size * scale;
const adj = createAdjMatrix(fileDataArr);
const normalisedRowSums = normalise(sumRows(adj));
for (let i = 0; i < size; i++) {
const alpha = (normalisedRowSums[i] / 1.5) + 0.33333333;
for (let j = 0; j < size; j++) {
// Position of the top-left corner of the next pixel
const x = j * scale;
const y = i * scale;
let cellColour;
if (adj[i][j] === 0) {
cellColour = '#1D2021';
} else {
cellColour = `rgba(254, 104, 37, ${alpha})`;
}
ctx.beginPath();
ctx.fillStyle = cellColour;
ctx.fillRect(x, y, scale, scale);
}
}
let image = new Image();
image.src = canvas.toDataURL();
const arrBuff = convertDataURIToBinary(image.src);
app.vault.createBinary(`/adj ${moment().format("YYYYMMDDHHmmSS")}.png`, arrBuff);
}
const scaleString = await tp.system.prompt("Choose a scale for the image (it must be a multiple of 2, and 1 is also fine)", "1");
const scaleInt = parseInt(scaleString);
if ((typeof (scaleInt) === 'number' && scaleInt % 2 === 0) || (typeof (scaleInt) === 'number' && scaleInt === 1)) {
addImage(scaleInt)
} else {
return
}
%>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment