the-forest/server/graphStuff.js

50 lines
1.3 KiB
JavaScript
Raw Normal View History

const { JSDOM } = require("jsdom");
const graphology = require("graphology");
const { circular } = require('graphology-layout');
const graphQueryString = `
select p.number, p.html, p.time, a.contents
from pages p
left join attributes a on a.number = p.number
where time >= (
select max(s.time)
from pages s
where s.number = p.number
)
order by p.time desc
`;
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);
}
for (const {number, html, contents} of allTheStuff) {
const { document } = (new JSDOM(html)).window;
const links = document.querySelectorAll('a');
links.forEach((link) => {
const referent = link.href.replace("/","");
graph.mergeEdge(number, referent, {color: "white"});
});
if (typeof contents?.verbs?.forEach === 'function') {
contents?.verbs?.forEach((verbNumber) => {
graph.mergeEdge(number, verbNumber, {color: "red"})
});
}
if (typeof contents?.contents?.forEach === 'function') {
contents?.contents?.forEach((nounNumber) => {
graph.mergeEdge(number, nounNumber, {color: "green"});
});
}
}
circular.assign(graph);
return graph;
}
module.exports = { graphFromList, graphQueryString };