Compare commits
2 Commits
2411c6fdc8
...
3317926f61
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3317926f61 | ||
|
|
4d56dce1af |
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"],
|
||||
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 WebpageLayout from "@/layouts/WebpageLayout.astro";
|
||||
|
||||
const settings = await getSettings();
|
||||
const blogs = await getAllBlogs(settings.blog);
|
||||
---
|
||||
|
||||
<WebpageLayout>
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { getRobotsSettings } from "@/content/settings/robots";
|
||||
import { getSettings } from "@/content/settings/settings";
|
||||
import type { APIRoute } from "astro";
|
||||
import minifyXML from "minify-xml";
|
||||
|
||||
export const GET = (async () => {
|
||||
const settings = await getSettings();
|
||||
@@ -57,7 +56,7 @@ export const GET = (async () => {
|
||||
crawlerContent = crawlerContent + "\n\n\n"
|
||||
});
|
||||
|
||||
return new Response(crawlerContent, {
|
||||
return new Response(crawlerContent.trim(), {
|
||||
status: 200,
|
||||
statusText: "OK",
|
||||
headers: {
|
||||
|
||||
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