Code Refactor

This commit is contained in:
Jonas_Jones 2023-10-02 19:29:33 +02:00
parent 390f5faa6b
commit 10e669a725
4 changed files with 72 additions and 67 deletions

View file

@ -9,7 +9,14 @@
<nav>
<ul>
<li>
<a href="https://jonasjones.dev/"><img style="padding-top: 7px;" src="/homepage.png" height="30px" alt="Homepage"></a>
<a href="https://jonasjones.dev/"
><img
style="padding-top: 7px;"
src="/homepage.png"
height="30px"
alt="Homepage"
/></a
>
</li>
</ul>
</nav>
@ -62,15 +69,14 @@
clear: both;
}
@media screen and (max-width: 550px) {
header h1 {
font-size: 20px;
}
@media screen and (max-width: 550px) {
header h1 {
font-size: 20px;
header {
margin: 0;
border-radius: 0;
}
}
header {
margin: 0;
border-radius: 0;
}
}
</style>

View file

@ -1,73 +1,73 @@
<script>
// @ts-nocheck
// @ts-nocheck
let data = import.meta.glob("/src/routes/**/+page.md");
let paths = data;
let data = import.meta.glob("/src/routes/**/+page.md");
let paths = data;
/**
* @param {Record<string, () => Promise<unknown>>} paths
*/
function buildHierarchy(paths) {
const nestedList = {};
let fixedpaths = [];
/**
* @type {string[]}
*/
let fixedpaths2 = [];
fixedpaths = Object.keys(paths);
fixedpaths.forEach((path) => {
const fixedpath = path
.replace("/+page.md", "")
.replace("/src/routes", "");
fixedpaths2.push(fixedpath);
});
fixedpaths2.forEach((folder) => {
const parts = folder.split("/").filter(Boolean);
let currentNode = nestedList;
/**
* @param {Record<string, () => Promise<unknown>>} paths
*/
function buildHierarchy(paths) {
const nestedList = {};
let fixedpaths = [];
/**
* @type {string[]}
*/
let fixedpaths2 = [];
fixedpaths = Object.keys(paths);
fixedpaths.forEach((path) => {
const fixedpath = path
.replace("/+page.md", "")
.replace("/src/routes", "");
fixedpaths2.push(fixedpath);
});
fixedpaths2.forEach((folder) => {
const parts = folder.split("/").filter(Boolean);
let currentNode = nestedList;
parts.forEach((part) => {
if (!currentNode[part]) {
currentNode[part] = {};
}
currentNode = currentNode[part];
});
});
parts.forEach((part) => {
if (!currentNode[part]) {
currentNode[part] = {};
}
currentNode = currentNode[part];
});
});
return nestedList;
}
return nestedList;
}
const nestedFolders = buildHierarchy(paths);
const nestedFolders = buildHierarchy(paths);
function createHtmlList(obj, parentPath = "", depth = 0) {
let html = "";
function createHtmlList(obj, parentPath = "", depth = 0) {
let html = "";
for (const key in obj) {
const currentPath = parentPath ? `${parentPath}/${key}` : key;
for (const key in obj) {
const currentPath = parentPath ? `${parentPath}/${key}` : key;
if (
typeof obj[key] === "object" &&
Object.keys(obj[key]).length > 0
) {
// If the value is an object and not empty, create a nested div
html += `
if (
typeof obj[key] === "object" &&
Object.keys(obj[key]).length > 0
) {
// If the value is an object and not empty, create a nested div
html += `
<li><h${depth + 3}><a href="/${currentPath}">${key}</a></h${
depth + 3
}></li>
depth + 3
}></li>
<ul>
${createHtmlList(obj[key], currentPath, depth + 1)}
</ul>
`;
} else {
// If the value is not an object or empty, create a clickable list item
html += `<li><a href="/${currentPath}">${key}</a></li>`;
}
}
} else {
// If the value is not an object or empty, create a clickable list item
html += `<li><a href="/${currentPath}">${key}</a></li>`;
}
}
return html;
}
return html;
}
//const renderedList = `<ul>${renderNestedList(nestedFolders)}</ul>`;
let renderedList = createHtmlList(nestedFolders);
//const renderedList = `<ul>${renderNestedList(nestedFolders)}</ul>`;
let renderedList = createHtmlList(nestedFolders);
</script>
{@html renderedList}
{@html renderedList}

View file

@ -156,4 +156,4 @@ details a {
margin-bottom: 1rem;
border-radius: 0;
}
}
}

View file

@ -2,7 +2,6 @@
import Header from "$lib/components/Header.svelte";
import Footer from "$lib/components/Footer.svelte";
import Navbar from "$lib/components/Navbar.svelte";
</script>
<Header />