Skip to content

Commit 1008cf8

Browse files
committed
Add some structured data to the post page
1 parent 4ea0ef2 commit 1008cf8

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

client/src/views/PostView.vue

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ import PostNotAvailable from "@client/components/PostNotAvailable.vue";
8888
import { faClock } from "@fortawesome/free-regular-svg-icons";
8989
import { faArrowLeft, faEdit, faTrash } from "@fortawesome/free-solid-svg-icons";
9090
import type { ConfirmDialogData, Post, UserRolePermissionsType } from "@fumix/fu-blog-common";
91-
import { useSeoMeta } from "@unhead/vue";
91+
import { DateUtil } from "@fumix/fu-blog-common";
92+
import { useHead, useSeoMeta } from "@unhead/vue";
9293
import { onMounted, type PropType, ref } from "vue";
9394
import { useI18n } from "vue-i18n";
9495
import { useRoute, useRouter } from "vue-router";
@@ -122,7 +123,32 @@ onMounted(async () => {
122123
});
123124
const res = await fetch(`/api/posts/${id}`);
124125
const response = await res.json();
125-
post.value = response.data;
126+
const responseData: Post | null = response.data;
127+
if (responseData) {
128+
useHead({
129+
title: responseData.title + "" + t("posts.blogTitle"),
130+
script: [
131+
{
132+
type: "application/json+ld",
133+
textContent: JSON.stringify({
134+
"@context": "https://schema.org",
135+
"@type": "BlogPosting",
136+
name: responseData.title,
137+
datePublished: responseData.createdAt ? DateUtil.formatDateOnlyUtcIso(new Date(responseData.createdAt)) : undefined,
138+
dateModified: responseData.updatedAt ? DateUtil.formatDateOnlyUtcIso(new Date(responseData.updatedAt)) : undefined,
139+
description: responseData.description,
140+
author: responseData.createdBy
141+
? {
142+
"@type": "Person",
143+
name: responseData.createdBy.fullName,
144+
}
145+
: undefined,
146+
}),
147+
},
148+
],
149+
});
150+
}
151+
post.value = responseData;
126152
loading.value = false;
127153
} catch (e) {
128154
console.log("ERROR: ", e);

common/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export * from "./types/public-post.js";
2727
export * from "./types/search-operator.js";
2828
export * from "./util/base64.js";
2929
export * from "./util/cookie-header-helpers.js";
30+
export * from "./util/date-util.js";
3031
export * from "./util/filesize.js";
3132
export * from "./util/markdown.js";
3233
export * from "./util/mimeType.js";

common/src/util/date-util.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export const DateUtil = (() => {
2+
function formatDateOnlyUtcIso(date: Date): string {
3+
console.log("Date", date, typeof date);
4+
console.log(date.getUTCFullYear());
5+
return `${date.getUTCFullYear()}-${(date.getMonth() + 1).toString().padStart(2, "0")}-${date.getDate().toString().padStart(2, "0")}`;
6+
}
7+
8+
return {
9+
formatDateOnlyUtcIso,
10+
};
11+
})();

0 commit comments

Comments
 (0)