feat: add edit post feature in blog posts (#384)

* feat: add edit post feature

* fix: remove edit button except on post detail pages

* docs: update site config docs for editPost

Resolves #191
---------

Co-authored-by: satnaing <satnaingdev@gmail.com>
This commit is contained in:
Patrick Wilson
2024-10-11 19:14:17 +08:00
committed by GitHub
parent cd417f5b7d
commit 0bc8eeeac8
6 changed files with 76 additions and 13 deletions
+38 -2
View File
@@ -1,11 +1,17 @@
import { LOCALE } from "@config";
import { LOCALE, SITE } from "@config";
import type { CollectionEntry } from "astro:content";
interface DatetimesProps {
pubDatetime: string | Date;
modDatetime: string | Date | undefined | null;
}
interface Props extends DatetimesProps {
interface EditPostProps {
editPost?: CollectionEntry<"blog">["data"]["editPost"];
postId?: CollectionEntry<"blog">["id"];
}
interface Props extends DatetimesProps, EditPostProps {
size?: "sm" | "lg";
className?: string;
}
@@ -15,6 +21,8 @@ export default function Datetime({
modDatetime,
size = "sm",
className = "",
editPost,
postId,
}: Props) {
return (
<div
@@ -42,6 +50,7 @@ export default function Datetime({
pubDatetime={pubDatetime}
modDatetime={modDatetime}
/>
{size === "lg" && <EditPost editPost={editPost} postId={postId} />}
</span>
</div>
);
@@ -72,3 +81,30 @@ const FormattedDatetime = ({ pubDatetime, modDatetime }: DatetimesProps) => {
</>
);
};
const EditPost = ({ editPost, postId }: EditPostProps) => {
let editPostUrl = editPost?.url ?? SITE?.editPost?.url ?? "";
const showEditPost = !editPost?.disabled && editPostUrl.length > 0;
const appendFilePath =
editPost?.appendFilePath ?? SITE?.editPost?.appendFilePath ?? false;
if (appendFilePath && postId) {
editPostUrl += `/${postId}`;
}
const editPostText = editPost?.text ?? SITE?.editPost?.text ?? "Edit";
return (
showEditPost && (
<>
<span aria-hidden="true"> | </span>
<a
className="hover:opacity-75"
href={editPostUrl}
rel="noopener noreferrer"
target="_blank"
>
{editPostText}
</a>
</>
)
);
};