Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add edit post feature #384

Merged
merged 3 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 38 additions & 2 deletions src/components/Datetime.tsx
Original file line number Diff line number Diff line change
@@ -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;
}
Expand All @@ -15,6 +21,8 @@ export default function Datetime({
modDatetime,
size = "sm",
className = "",
editPost,
postId,
}: Props) {
return (
<div
Expand Down Expand Up @@ -42,6 +50,7 @@ export default function Datetime({
pubDatetime={pubDatetime}
modDatetime={modDatetime}
/>
<EditPost editPost={editPost} postId={postId} />
</span>
</div>
);
Expand Down Expand Up @@ -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>
</>
)
);
};
5 changes: 5 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ export const SITE: Site = {
postPerIndex: 4,
postPerPage: 3,
scheduledPostMargin: 15 * 60 * 1000, // 15 minutes
editPost: {
url: "https://github.com/satnaing/astro-paper/edit/main/src/content/blog",
text: "Suggest Changes",
appendFilePath: true,
},
};

export const LOCALE = {
Expand Down
8 changes: 8 additions & 0 deletions src/content/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ const blog = defineCollection({
.optional(),
description: z.string(),
canonicalURL: z.string().optional(),
editPost: z
.object({
disabled: z.boolean().optional(),
url: z.string().optional(),
text: z.string().optional(),
appendFilePath: z.boolean().optional(),
})
.optional(),
}),
});

Expand Down
3 changes: 3 additions & 0 deletions src/layouts/PostDetails.astro
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const {
pubDatetime,
modDatetime,
tags,
editPost,
} = post.data;

const { Content } = await post.render();
Expand Down Expand Up @@ -82,6 +83,8 @@ const nextPost =
modDatetime={modDatetime}
size="lg"
className="my-2"
editPost={editPost}
postId={post.id}
/>
<article id="article" class="prose mx-auto mt-8 max-w-3xl">
<Content />
Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ export type Site = {
postPerIndex: number;
postPerPage: number;
scheduledPostMargin: number;
editPost?: {
url?: URL["href"];
text?: string;
appendFilePath?: boolean;
};
};

export type SocialObjects = {
Expand Down