the-forest/server/graphStuff.js

25 lines
668 B
JavaScript
Raw Normal View History

const { JSDOM } = require("jsdom");
const graphology = require("graphology");
const { circular } = require('graphology-layout');
function graphFromList(allTheStuff) {
const graph = new graphology.Graph();
2024-10-05 12:01:19 -04:00
for (const {number, html} of allTheStuff) {
if (!graph.hasNode(number)) graph.addNode(number);
}
2024-10-05 12:01:19 -04:00
for (const {number, html} of allTheStuff) {
const { document } = (new JSDOM(html)).window;
const links = document.querySelectorAll('a');
links.forEach((link) => {
const referent = link.href.replace("/","");
2024-10-05 12:01:19 -04:00
graph.mergeEdge(number, referent);
});
}
circular.assign(graph);
return graph;
}
module.exports = { graphFromList: graphFromList };