mirror of
https://github.com/JonasunderscoreJones/blog.jonasjones.dev.git
synced 2025-10-23 00:09:18 +02:00
rewrote date parsing system
This commit is contained in:
parent
74e686daf4
commit
8b5f7bb7e8
1 changed files with 52 additions and 14 deletions
|
@ -4,6 +4,7 @@
|
||||||
import Loading from '../components/Loading.svelte';
|
import Loading from '../components/Loading.svelte';
|
||||||
import NotFound from "./Error.svelte";
|
import NotFound from "./Error.svelte";
|
||||||
import navigate from 'svelte-spa-router';
|
import navigate from 'svelte-spa-router';
|
||||||
|
import { parse } from "svelte/compiler";
|
||||||
|
|
||||||
export let params: {year: string, month: string, day: string, title: string} = {
|
export let params: {year: string, month: string, day: string, title: string} = {
|
||||||
year: "",
|
year: "",
|
||||||
|
@ -70,15 +71,18 @@
|
||||||
console.log("MarkdownContent: " + markdowncontent);
|
console.log("MarkdownContent: " + markdowncontent);
|
||||||
console.log("Posts: ", posts)
|
console.log("Posts: ", posts)
|
||||||
console.log("Post: ", post)
|
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');
|
const markdowncontentElement = document.getElementById('markdowncontent');
|
||||||
if (markdowncontentElement) {
|
if (markdowncontentElement) {
|
||||||
markdowncontentElement.innerHTML = await markdowncontent;
|
markdowncontentElement.innerHTML = await markdowncontent;
|
||||||
}
|
}
|
||||||
loading = false;
|
loading = false;
|
||||||
postTitle = post.title;
|
postTitle = parsedContent[1];
|
||||||
postAuthor = post.author;
|
postAuthor = parsedContent[2];
|
||||||
postDate = post.date;
|
postDate = parsedContent[3];
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
loading = false
|
loading = false
|
||||||
error404 = true
|
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
|
// 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; }) {
|
function findPostByDate(posts: any[], params: { year: any; month: any; day: any; title: any; }) {
|
||||||
return posts.find(post => {
|
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
|
// Extract the year, month, and day from the Date object
|
||||||
const postYear = postDate.getFullYear();
|
const postYear = post.date.split('-')[0];
|
||||||
const postMonth = postDate.getMonth() + 1; // Months are zero-based
|
const postMonth = post.date.split('-')[1];
|
||||||
const postDay = postDate.getDate();
|
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
|
// Compare the extracted year, month, and day with params, and the title
|
||||||
return (
|
return (
|
||||||
postYear === parseInt(params.year) &&
|
postYear === params.year &&
|
||||||
postMonth === parseInt(params.month) &&
|
postMonth === params.month &&
|
||||||
postDay === parseInt(params.day) &&
|
postDay === params.day &&
|
||||||
post.id === params.title
|
post.id === params.title
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue