import {calculateSimilarityScore} from './distance.js'; document.addEventListener("DOMContentLoaded", function() { handleRedirection(); }); function handleRedirection() { let path = window.location.pathname.substring(1).toLowerCase(); if (path === "") { window.location.href = "/"; return; } fetch('/content/links.json') .then(response => response.json()) .then(data => { const bestMatch = findBestMatch(path, data.links); if (bestMatch) { window.location.href = bestMatch.href; } else { displayNotFound(); } }) .catch(error => { console.error('Error fetching links.json:', error); displayError(); }); } function findBestMatch(path, links) { let bestMatch = null; let highestScore = -Infinity; const threshold = 0.5; for (let item of links) { const itemName = item.name.toLowerCase(); const score = calculateSimilarityScore(path, itemName); if (score > highestScore) { highestScore = score; bestMatch = item; } } if (highestScore < threshold) { window.location.href = "/"; return null; } return bestMatch; } function displayNotFound() { document.body.innerHTML = '

Not Found

The requested page could not be found.

'; } function displayError() { document.body.innerHTML = '

Error

There was an error retrieving the redirection data.

'; }