From b8c01cd59d358a07812512b6f8a7fd24564da404 Mon Sep 17 00:00:00 2001 From: J-onasJones Date: Fri, 28 Jun 2024 01:58:07 +0200 Subject: [PATCH 1/4] added some verbose logging --- src/routes/Post.svelte | 1 + 1 file changed, 1 insertion(+) diff --git a/src/routes/Post.svelte b/src/routes/Post.svelte index b68ea6d..77b8f0d 100644 --- a/src/routes/Post.svelte +++ b/src/routes/Post.svelte @@ -67,6 +67,7 @@ if (await content.ok) { let markdowncontent = await content.text(); + console.log("MarkdownContent: " + markdowncontent); markdowncontent = await marked.parse(removePostVars(markdowncontent)); const markdowncontentElement = document.getElementById('markdowncontent'); if (markdowncontentElement) { From 74e686daf407efe79730c9d6576cd8a28b2da525 Mon Sep 17 00:00:00 2001 From: J-onasJones Date: Fri, 28 Jun 2024 02:00:48 +0200 Subject: [PATCH 2/4] added more verbose logging --- src/routes/Post.svelte | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/routes/Post.svelte b/src/routes/Post.svelte index 77b8f0d..c2e85ec 100644 --- a/src/routes/Post.svelte +++ b/src/routes/Post.svelte @@ -68,6 +68,8 @@ if (await content.ok) { let markdowncontent = await content.text(); console.log("MarkdownContent: " + markdowncontent); + console.log("Posts: ", posts) + console.log("Post: ", post) markdowncontent = await marked.parse(removePostVars(markdowncontent)); const markdowncontentElement = document.getElementById('markdowncontent'); if (markdowncontentElement) { From 8b5f7bb7e82c1eebe37dd20075686d13cc1747ee Mon Sep 17 00:00:00 2001 From: J-onasJones Date: Fri, 28 Jun 2024 02:37:25 +0200 Subject: [PATCH 3/4] rewrote date parsing system --- src/routes/Post.svelte | 66 +++++++++++++++++++++++++++++++++--------- 1 file changed, 52 insertions(+), 14 deletions(-) diff --git a/src/routes/Post.svelte b/src/routes/Post.svelte index c2e85ec..d70ca7a 100644 --- a/src/routes/Post.svelte +++ b/src/routes/Post.svelte @@ -4,6 +4,7 @@ import Loading from '../components/Loading.svelte'; import NotFound from "./Error.svelte"; import navigate from 'svelte-spa-router'; + import { parse } from "svelte/compiler"; export let params: {year: string, month: string, day: string, title: string} = { year: "", @@ -70,15 +71,18 @@ console.log("MarkdownContent: " + markdowncontent); console.log("Posts: ", posts) console.log("Post: ", post) - markdowncontent = await marked.parse(removePostVars(markdowncontent)); + const parsedContent = removeAndParsePostVars(markdowncontent); + console.log("ParsedContent: ", parsedContent) + markdowncontent = await marked.parse(parsedContent[0]); const markdowncontentElement = document.getElementById('markdowncontent'); if (markdowncontentElement) { markdowncontentElement.innerHTML = await markdowncontent; } loading = false; - postTitle = post.title; - postAuthor = post.author; - postDate = post.date; + postTitle = parsedContent[1]; + postAuthor = parsedContent[2]; + postDate = parsedContent[3]; + } else { loading = false error404 = true @@ -91,9 +95,26 @@ } - function removePostVars(content: string) { + function extractMarkdownValue(content: string, key: string) { + const match = content.match(new RegExp(`^\\[${key}\\]: (.*)$`, 'm')); + return match ? match[1] : ''; + } + + function removeAndParsePostVars(content: string) { + const title = extractMarkdownValue(content, 'title'); + const author = extractMarkdownValue(content, 'author'); + const date = extractMarkdownValue(content, 'date'); + const description = extractMarkdownValue(content, 'description'); + + + console.log("The parsed stuff:") + console.log("Title: " + title) + console.log("Author: " + author) + console.log("Date: " + date) + console.log("Description: " + description) + // with regex if the line begins with a markdown variable declaration, remove it - return content.replace(/^\[.*?\]: .*$(?:\r?\n)?/gm, ''); + return [content.replace(/^\[.*?\]: .*$(?:\r?\n)?/gm, ''), title, author, date, description]; } }); @@ -109,19 +130,36 @@ function findPostByDate(posts: any[], params: { year: any; month: any; day: any; title: any; }) { return posts.find(post => { - // Create a Date object from the post's date string - const postDate = new Date(post.date); // Extract the year, month, and day from the Date object - const postYear = postDate.getFullYear(); - const postMonth = postDate.getMonth() + 1; // Months are zero-based - const postDay = postDate.getDate(); + const postYear = post.date.split('-')[0]; + const postMonth = post.date.split('-')[1]; + const postDay = post.date.split('-')[2]; + + if (params.month?.toString().length === 1) { + params.month = "0" + params.month + } + + if (params.day?.toString().length === 1) { + params.day = "0" + params.day + } + + console.log("PostYear: " + postYear) + console.log("PostMonth: " + postMonth) + console.log("PostDay: " + postDay) + + console.log("ParamsYear: " + params.year) + console.log("ParamsMonth: " + params.month) + console.log("ParamsDay: " + params.day) + + console.log("PostID: " + post.id) + console.log("ParamsTitle: " + params.title) // Compare the extracted year, month, and day with params, and the title return ( - postYear === parseInt(params.year) && - postMonth === parseInt(params.month) && - postDay === parseInt(params.day) && + postYear === params.year && + postMonth === params.month && + postDay === params.day && post.id === params.title ); }); From 027917aa3759e69855d97c96702185bced06b975 Mon Sep 17 00:00:00 2001 From: J-onasJones Date: Fri, 28 Jun 2024 02:45:18 +0200 Subject: [PATCH 4/4] cleaned up loads of code --- src/routes/Post.svelte | 71 ------------------------------------------ 1 file changed, 71 deletions(-) diff --git a/src/routes/Post.svelte b/src/routes/Post.svelte index d70ca7a..57ffc20 100644 --- a/src/routes/Post.svelte +++ b/src/routes/Post.svelte @@ -13,15 +13,6 @@ title: "" }; - let posts: { id: any; }[] = []; - let post: {id: string, date: string, title: string, author: string, description: string} = { - id: '', - date: '', - title: '', - author: '', - description: '' - }; - let postTitle = ""; let postAuthor = ""; let postDate = ""; @@ -42,20 +33,6 @@ onMount(async () => { redirectUrl() try { - const response = await fetch('https://cdn.jonasjones.dev/blog/index.json'); - - posts = await response.json(); - - post = await findPostByDate(posts, params); - - if (await !post) { - loading = false; - error404 = true; - const markdowncontentElement = document.getElementById('markdowncontent'); - if (markdowncontentElement) { - markdowncontentElement.style.display = "none"; - } - } if (params.month?.toString().length === 1) { params.month = "0" + params.month @@ -68,11 +45,7 @@ if (await content.ok) { let markdowncontent = await content.text(); - console.log("MarkdownContent: " + markdowncontent); - console.log("Posts: ", posts) - console.log("Post: ", post) const parsedContent = removeAndParsePostVars(markdowncontent); - console.log("ParsedContent: ", parsedContent) markdowncontent = await marked.parse(parsedContent[0]); const markdowncontentElement = document.getElementById('markdowncontent'); if (markdowncontentElement) { @@ -106,13 +79,6 @@ const date = extractMarkdownValue(content, 'date'); const description = extractMarkdownValue(content, 'description'); - - console.log("The parsed stuff:") - console.log("Title: " + title) - console.log("Author: " + author) - console.log("Date: " + date) - console.log("Description: " + description) - // with regex if the line begins with a markdown variable declaration, remove it return [content.replace(/^\[.*?\]: .*$(?:\r?\n)?/gm, ''), title, author, date, description]; } @@ -128,43 +94,6 @@ } } - function findPostByDate(posts: any[], params: { year: any; month: any; day: any; title: any; }) { - return posts.find(post => { - - // Extract the year, month, and day from the Date object - const postYear = post.date.split('-')[0]; - const postMonth = post.date.split('-')[1]; - const postDay = post.date.split('-')[2]; - - if (params.month?.toString().length === 1) { - params.month = "0" + params.month - } - - if (params.day?.toString().length === 1) { - params.day = "0" + params.day - } - - console.log("PostYear: " + postYear) - console.log("PostMonth: " + postMonth) - console.log("PostDay: " + postDay) - - console.log("ParamsYear: " + params.year) - console.log("ParamsMonth: " + params.month) - console.log("ParamsDay: " + params.day) - - console.log("PostID: " + post.id) - console.log("ParamsTitle: " + params.title) - - // Compare the extracted year, month, and day with params, and the title - return ( - postYear === params.year && - postMonth === params.month && - postDay === params.day && - post.id === params.title - ); - }); -} - function copyLink() { navigator.clipboard.writeText(location.href).then(() => { clickText = "Copied!";