mirror of
https://github.com/JonasunderscoreJones/blog.jonasjones.dev.git
synced 2025-10-23 00:09:18 +02:00
Merge pull request #1 from JonasunderscoreJones/us-fix-apparently
Fixed issue with fetching posts in different time zones
This commit is contained in:
commit
62fc36d2c4
1 changed files with 19 additions and 49 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: "",
|
||||||
|
@ -12,15 +13,6 @@
|
||||||
title: ""
|
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 postTitle = "";
|
||||||
let postAuthor = "";
|
let postAuthor = "";
|
||||||
let postDate = "";
|
let postDate = "";
|
||||||
|
@ -41,20 +33,6 @@
|
||||||
onMount(async () => {
|
onMount(async () => {
|
||||||
redirectUrl()
|
redirectUrl()
|
||||||
try {
|
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) {
|
if (params.month?.toString().length === 1) {
|
||||||
params.month = "0" + params.month
|
params.month = "0" + params.month
|
||||||
|
@ -67,15 +45,17 @@
|
||||||
|
|
||||||
if (await content.ok) {
|
if (await content.ok) {
|
||||||
let markdowncontent = await content.text();
|
let markdowncontent = await content.text();
|
||||||
markdowncontent = await marked.parse(removePostVars(markdowncontent));
|
const parsedContent = removeAndParsePostVars(markdowncontent);
|
||||||
|
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
|
||||||
|
@ -88,9 +68,19 @@
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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');
|
||||||
|
|
||||||
// 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];
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -104,26 +94,6 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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();
|
|
||||||
|
|
||||||
// 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) &&
|
|
||||||
post.id === params.title
|
|
||||||
);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function copyLink() {
|
function copyLink() {
|
||||||
navigator.clipboard.writeText(location.href).then(() => {
|
navigator.clipboard.writeText(location.href).then(() => {
|
||||||
clickText = "Copied!";
|
clickText = "Copied!";
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue