Add function that looks for all blogs
This commit is contained in:
70
astro/src/content/blogs/blogs.ts
Normal file
70
astro/src/content/blogs/blogs.ts
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
import { createDirectusConnection } from "@/lib/directus";
|
||||||
|
import { print } from 'graphql';
|
||||||
|
import getBlogs from '@/graphql/blogs/getBlogs.graphql';
|
||||||
|
import { formatDate } from "@/lib/dates";
|
||||||
|
|
||||||
|
export async function getAllBlogs(blogSettings: BlogSettings): Promise<BlogPost[]> {
|
||||||
|
const client = await createDirectusConnection();
|
||||||
|
const result = await client.query(print(getBlogs), {
|
||||||
|
date: formatDate(new Date(), "%Y-%M-%D")
|
||||||
|
});
|
||||||
|
|
||||||
|
let blogs: BlogPost[] = [];
|
||||||
|
|
||||||
|
result["Blogs"].forEach((blogRecord: any) => {
|
||||||
|
let dates: string[] = [
|
||||||
|
blogRecord["date_created"],
|
||||||
|
blogRecord["date_updated"],
|
||||||
|
blogRecord["search_engine"][0]["date_created"],
|
||||||
|
blogRecord["search_engine"][0]["date_updated"],
|
||||||
|
blogRecord["search_engine"][0]["thumbnail"]["created_on"]
|
||||||
|
];
|
||||||
|
|
||||||
|
const blog: BlogPost = {
|
||||||
|
lastModified: new Date(),
|
||||||
|
title: blogRecord["title"],
|
||||||
|
content: blogRecord["content"],
|
||||||
|
date: blogRecord["date"],
|
||||||
|
url: blogRecord["url"],
|
||||||
|
searchEngine: {
|
||||||
|
title: blogRecord["search_engine"][0]["title"],
|
||||||
|
description: blogRecord["search_engine"][0]["description"],
|
||||||
|
allowCrawlers: blogRecord["search_engine"][0]["allow_crawler"],
|
||||||
|
canonical: blogRecord["search_engine"][0]["canonical"],
|
||||||
|
priority: blogRecord["search_engine"][0]["priority"],
|
||||||
|
thumbnail: {
|
||||||
|
url: blogRecord["search_engine"][0]["thumbnail"]["filename_disk"],
|
||||||
|
height: blogRecord["search_engine"][0]["thumbnail"]["height"],
|
||||||
|
width: blogRecord["search_engine"][0]["thumbnail"]["width"]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
tags: []
|
||||||
|
};
|
||||||
|
|
||||||
|
blogRecord["tags"].forEach((tagRecord: any) => {
|
||||||
|
blog["tags"].push({
|
||||||
|
text: tagRecord["Tags_id"]["text"],
|
||||||
|
code: tagRecord["Tags_id"]["code"],
|
||||||
|
color: tagRecord["Tags_id"]["color"]
|
||||||
|
});
|
||||||
|
|
||||||
|
dates.push(tagRecord["Tags_id"]["date_created"]);
|
||||||
|
dates.push(tagRecord["Tags_id"]["date_updated"]);
|
||||||
|
});
|
||||||
|
|
||||||
|
if (dates.filter(e => e !== null).length === 0) {
|
||||||
|
blog.lastModified = new Date();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
const sortedDates: string[] = dates.sort((a: string, b: string) => {
|
||||||
|
return new Date(b).getTime() - new Date(a).getTime();
|
||||||
|
});
|
||||||
|
|
||||||
|
blog.lastModified = new Date(sortedDates[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
blogs.push(blog);
|
||||||
|
});
|
||||||
|
|
||||||
|
return blogs;
|
||||||
|
}
|
||||||
@@ -12,4 +12,4 @@ export async function getRobotsSettings(): Promise<RobotsSettings> {
|
|||||||
crawlers: robotsResult["crawlers"],
|
crawlers: robotsResult["crawlers"],
|
||||||
extraContent: robotsResult["extra_content"]
|
extraContent: robotsResult["extra_content"]
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
39
astro/src/graphql/blogs/getBlogs.graphql
Normal file
39
astro/src/graphql/blogs/getBlogs.graphql
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
query getAllBlogs($date: String!) {
|
||||||
|
Blogs(sort: ["-date", "-date_created"], filter: { status: { _eq: "published" }, date: { _gte: $date } }) {
|
||||||
|
id,
|
||||||
|
date_created,
|
||||||
|
date_updated,
|
||||||
|
status,
|
||||||
|
title,
|
||||||
|
url,
|
||||||
|
date,
|
||||||
|
content,
|
||||||
|
tags {
|
||||||
|
Tags_id {
|
||||||
|
id,
|
||||||
|
date_created,
|
||||||
|
date_updated,
|
||||||
|
text,
|
||||||
|
code,
|
||||||
|
color
|
||||||
|
}
|
||||||
|
},
|
||||||
|
search_engine {
|
||||||
|
id,
|
||||||
|
date_created,
|
||||||
|
date_updated,
|
||||||
|
title,
|
||||||
|
description,
|
||||||
|
thumbnail {
|
||||||
|
id,
|
||||||
|
created_on,
|
||||||
|
filename_disk,
|
||||||
|
width,
|
||||||
|
height
|
||||||
|
},
|
||||||
|
canonical,
|
||||||
|
allow_crawler,
|
||||||
|
priority
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
6
astro/src/lib/dates.ts
Normal file
6
astro/src/lib/dates.ts
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
export function formatDate(date: Date, format: string) {
|
||||||
|
return format
|
||||||
|
.replaceAll("%Y", date.getFullYear().toString())
|
||||||
|
.replaceAll("%M", (date.getMonth() + 1).toString().padStart(2, '0'))
|
||||||
|
.replaceAll("%D", date.getDate().toString().padStart(2, '0'));
|
||||||
|
}
|
||||||
@@ -1,8 +1,10 @@
|
|||||||
---
|
---
|
||||||
|
import { getAllBlogs } from "@/content/blogs/blogs";
|
||||||
import { getSettings } from "@/content/settings/settings"
|
import { getSettings } from "@/content/settings/settings"
|
||||||
import WebpageLayout from "@/layouts/WebpageLayout.astro";
|
import WebpageLayout from "@/layouts/WebpageLayout.astro";
|
||||||
|
|
||||||
const settings = await getSettings();
|
const settings = await getSettings();
|
||||||
|
const blogs = await getAllBlogs(settings.blog);
|
||||||
---
|
---
|
||||||
|
|
||||||
<WebpageLayout>
|
<WebpageLayout>
|
||||||
|
|||||||
12
astro/src/types/blogs/blog.d.ts
vendored
Normal file
12
astro/src/types/blogs/blog.d.ts
vendored
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
type BlogPost = {
|
||||||
|
title: string;
|
||||||
|
url: string;
|
||||||
|
date: string;
|
||||||
|
content: string;
|
||||||
|
|
||||||
|
tags: Tag[];
|
||||||
|
|
||||||
|
searchEngine: SearchEngine;
|
||||||
|
|
||||||
|
lastModified: Date;
|
||||||
|
}
|
||||||
8
astro/src/types/common/searchEngine.d.ts
vendored
Normal file
8
astro/src/types/common/searchEngine.d.ts
vendored
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
type SearchEngine = {
|
||||||
|
title: string;
|
||||||
|
description: string;
|
||||||
|
thumbnail: PhotoProps;
|
||||||
|
canonical: string | null;
|
||||||
|
allowCrawlers: boolean;
|
||||||
|
priority: number;
|
||||||
|
}
|
||||||
5
astro/src/types/common/tag.d.ts
vendored
Normal file
5
astro/src/types/common/tag.d.ts
vendored
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
type Tag = {
|
||||||
|
text: string;
|
||||||
|
code: string;
|
||||||
|
color: string;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user