create a html page with js network graph visualizer of that parse the html pages in a…
- User
create a html page with js network graph visualizer of that parse the html pages in a directory and show backlinks related each other in a graph
- Assistant
create a html page with js network graph visualizer of that parse the html pages in a directory and show backlinks related each other in a graph
Creating an HTML page with a JavaScript-based network graph visualizer to analyze HTML pages in a directory and visualize their backlinks as a graph is a multifaceted task. To achieve this, we'll need to use a combination of server-side scripting to parse HTML files and JavaScript for visualization. I'll guide you through creating a simple setup using Node.js for the server-side operations and Vis.js for the graph visualization on the client side.
Ensure you have the following installed:
Create a new directory for your project and navigate into it:
mkdir network-graph-visualizer
cd network-graph-visualizer
Initialize a new Node.js project:
npm init -y
Install required NPM packages:
npm install express cheerio vis
Create a new file named server.js:
const express = require('express');
const fs = require('fs');
const cheerio = require('cheerio');
const path = require('path');
const app = express();
const PORT = 3000;
app.use(express.static('public'));
app.get('/data', (req, res) => {
const directoryPath = path.join(__dirname, 'html_pages');
fs.readdir(directoryPath, function(err, files) {
if (err) {
return console.log('Unable to scan directory: ' + err);
}
const linksData = files.map(file => analyzeHtmlFile(directoryPath, file));
Promise.all(linksData).then(data => {
res.json({ nodes: data.flat(), edges: createEdges(data.flat()) });
});
});
});
function analyzeHtmlFile(dir, file) {
return new Promise((resolve) => {
const filePath = path.join(dir, file);
fs.readFile(filePath, 'utf8', (err, html) => {
if (err) {
console.error(err);
return resolve([]);
}
const $ = cheerio.load(html);
const links = [];
$('a').each((i, link) => {
links.push({
from: file,
to: $(link).attr('href'),
});
});
resolve(links);
});
});
}
function createEdges(links) {
return links.filter(link => link.to.includes('.html')).map(link => ({
from: link.from,
to: link.to,
}));
}
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
Place your HTML files inside a new directory named html_pages in your project root.
Create a directory named public and inside it, create an index.html and app.js:
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF8">
<title>Network Graph of HTML Pages</title>
<script type="text/javascript" src="https://unpkg.com/vis-network/standalone/umd/vis-network.min.js"></script>
<style type="text/css">
#mynetwork {
width: 100%;
height: 100vh;
border: 1px solid lightgray;
}
</style>
</head>
<body>
<div id="mynetwork"></div>
<script src="app.js"></script>
</body>
</html>
app.js
fetch('/data')
.then(response => response.json())
.then((data) => {
const nodes = [];
const edges = [];
data.nodes.forEach(link => {
if (!nodes.find(n => n.id === link.from)) {
nodes.push({ id: {
label: `${link.from.substr(0, link.from.length - 5).toUpperCase()}`,
shape: 'box'
});
}
});
data.edges.forEach(link => {
edges.push({ from: `${link.from}`, to: `${link.to}` });
});
var container = document.getElementById('mynetwork');
var graphData = {
nodes: new vis.DataSet(nodes),
edges: new vis.DataSet(edges),
};
var options = {};
var network = new vis.Network(container, graphData, options);
})
.catch(error => console.error('Error fetching data:', error));
node server.js.
2.keys));http://localhost:3000 to view the network graph of your HTML files.This setup will visually represent backlinks between HTML pages in the specified directory on your local machine. Make sure you adjust the directory path and links setup as per your specific requirements.
Not yet included in a published release.