the-forest/server/graphStuff.js

50 lines
1.3 KiB
JavaScript

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();
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 };