Skip to content

Commit

Permalink
feat(release): v0.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
sambacha committed Mar 20, 2024
1 parent 4d279a2 commit 852d3d9
Show file tree
Hide file tree
Showing 9 changed files with 9,894 additions and 0 deletions.
83 changes: 83 additions & 0 deletions bundle/script_404.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// SPDX-License-Identifier: UPL-1.0

// Function to calculate the Levenshtein distance between two strings
function levenshteinDistance(a: string, b: string) {
const matrix: number[][] = [];

for (let i = 0; i <= b.length; i++) {
matrix[i] = [i];
}

for (let j = 0; j <= a.length; j++) {
matrix[0][j] = j;
}

for (let i = 1; i <= b.length; i++) {
for (let j = 1; j <= a.length; j++) {
if (b.charAt(i - 1) === a.charAt(j - 1)) {
matrix[i][j] = matrix[i - 1][j - 1];
} else {
matrix[i][j] = Math.min(
matrix[i - 1][j - 1] + 1,
matrix[i][j - 1] + 1,
matrix[i - 1][j] + 1
);
}
}
}

return matrix[b.length][a.length];
}


// Function to find the path with the shortest edit distance from the sitemap
function findClosestPath(requestedPath: string, sitemapUrls: (string|null)[]) {
let closestPath = '';
let minDistance = Infinity;

for (const url of sitemapUrls) {
if (url !== null) {
const distance = levenshteinDistance(requestedPath, url);
if (distance < minDistance) {
minDistance = distance;
closestPath = url;
}
}
}

return closestPath;
}


// Function to fetch the sitemap and find the closest path
async function suggestClosestPath() {
try {
const response = await fetch('/sitemap.xml');
const sitemapXml = await response.text();

const parser = new DOMParser();
const xmlDoc = parser.parseFromString(sitemapXml, 'text/xml');
const urlElements = xmlDoc.getElementsByTagName('url');

const sitemapUrls =
Array.from(urlElements)
.map(
(urlElement) =>
urlElement.getElementsByTagName('loc')[0].textContent);

const requestedPath = window.location.pathname;
const closestPath = findClosestPath(requestedPath, sitemapUrls);

const suggestionElement = document.getElementById('suggestion');
if (suggestionElement) {
suggestionElement.innerHTML =
`Did you mean: <a href="${closestPath}">${closestPath}</a>`;
}
} catch (error) {
console.error('Error fetching sitemap:', error);
}
}


// Call the function when the page loads
window.onload = suggestClosestPath;
Loading

0 comments on commit 852d3d9

Please sign in to comment.