diff --git a/astro/src/content/blogs/blogs.ts b/astro/src/content/blogs/blogs.ts new file mode 100644 index 0000000..8bbdd8d --- /dev/null +++ b/astro/src/content/blogs/blogs.ts @@ -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 { + 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; +} diff --git a/astro/src/content/settings/robots.ts b/astro/src/content/settings/robots.ts index a195d36..0c8f470 100644 --- a/astro/src/content/settings/robots.ts +++ b/astro/src/content/settings/robots.ts @@ -12,4 +12,4 @@ export async function getRobotsSettings(): Promise { crawlers: robotsResult["crawlers"], extraContent: robotsResult["extra_content"] }; -} +} \ No newline at end of file diff --git a/astro/src/graphql/blogs/getBlogs.graphql b/astro/src/graphql/blogs/getBlogs.graphql new file mode 100644 index 0000000..23863ac --- /dev/null +++ b/astro/src/graphql/blogs/getBlogs.graphql @@ -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 + } + } +} diff --git a/astro/src/lib/dates.ts b/astro/src/lib/dates.ts new file mode 100644 index 0000000..e6d1f0e --- /dev/null +++ b/astro/src/lib/dates.ts @@ -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')); +} diff --git a/astro/src/pages/index.astro b/astro/src/pages/index.astro index a79d138..a1c7645 100644 --- a/astro/src/pages/index.astro +++ b/astro/src/pages/index.astro @@ -1,8 +1,10 @@ --- +import { getAllBlogs } from "@/content/blogs/blogs"; import { getSettings } from "@/content/settings/settings" import WebpageLayout from "@/layouts/WebpageLayout.astro"; const settings = await getSettings(); +const blogs = await getAllBlogs(settings.blog); --- diff --git a/astro/src/types/blogs/blog.d.ts b/astro/src/types/blogs/blog.d.ts new file mode 100644 index 0000000..b0ebb2b --- /dev/null +++ b/astro/src/types/blogs/blog.d.ts @@ -0,0 +1,12 @@ +type BlogPost = { + title: string; + url: string; + date: string; + content: string; + + tags: Tag[]; + + searchEngine: SearchEngine; + + lastModified: Date; +} diff --git a/astro/src/types/common/searchEngine.d.ts b/astro/src/types/common/searchEngine.d.ts new file mode 100644 index 0000000..7213ea2 --- /dev/null +++ b/astro/src/types/common/searchEngine.d.ts @@ -0,0 +1,8 @@ +type SearchEngine = { + title: string; + description: string; + thumbnail: PhotoProps; + canonical: string | null; + allowCrawlers: boolean; + priority: number; +} diff --git a/astro/src/types/common/tag.d.ts b/astro/src/types/common/tag.d.ts new file mode 100644 index 0000000..022559b --- /dev/null +++ b/astro/src/types/common/tag.d.ts @@ -0,0 +1,5 @@ +type Tag = { + text: string; + code: string; + color: string; +}