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 all commits
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}
/>
{size === "lg" && <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
28 changes: 17 additions & 11 deletions src/content/blog/how-to-configure-astropaper-theme.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,28 @@ export const SITE = {
lightAndDarkMode: true,
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,
},
};
```

Here are SITE configuration options

| Options | Description |
| --------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `website` | Your deployed website url |
| `author` | Your name |
| `desc` | Your site description. Useful for SEO and social media sharing. |
| `title` | Your site name |
| `ogImage` | Your default OG image for the site. Useful for social media sharing. OG images can be an external image url or they can be placed under `/public` directory. |
| `lightAndDarkMode` | Enable or disable `light & dark mode` for the website. If disabled, primary color scheme will be used. This option is enabled by default. |
| `postPerIndex` | The number of posts to be displayed at the home page under `Recent` section. |
| `postPerPage` | You can specify how many posts will be displayed in each posts page. (eg: if you set SITE.postPerPage to 3, each page will only show 3 posts per page) |
| `scheduledPostMargin` | In Production mode, posts with a future `pubDatetime` will not be visible. However, if a post's `pubDatetime` is within the next 15 minutes, it will be visible. You can set `scheduledPostMargin` if you don't like the default 15 minutes margin. |
| Options | Description |
| --------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `website` | Your deployed website url |
| `author` | Your name |
| `desc` | Your site description. Useful for SEO and social media sharing. |
| `title` | Your site name |
| `ogImage` | Your default OG image for the site. Useful for social media sharing. OG images can be an external image url or they can be placed under `/public` directory. |
| `lightAndDarkMode` | Enable or disable `light & dark mode` for the website. If disabled, primary color scheme will be used. This option is enabled by default. |
| `postPerIndex` | The number of posts to be displayed at the home page under `Recent` section. |
| `postPerPage` | You can specify how many posts will be displayed in each posts page. (eg: if you set SITE.postPerPage to 3, each page will only show 3 posts per page) |
| `scheduledPostMargin` | In Production mode, posts with a future `pubDatetime` will not be visible. However, if a post's `pubDatetime` is within the next 15 minutes, it will be visible. You can set `scheduledPostMargin` if you don't like the default 15 minutes margin. |
| `editPost` | This option allows users to suggest changes to a blog post by providing an edit link under blog post titles. This feature can be disabled by removing it from the `SITE` config. You can also set `appendFilePath` to `true` to automatically append the file path of the post to the url, directing users to the specific post they wish to edit. |

## Configuring 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