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 blog extesions #871

Merged
merged 2 commits into from
Sep 18, 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
22 changes: 19 additions & 3 deletions blog/loaders/extensions/BlogpostList/ratings.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
import { ExtensionOf } from "../../../../website/loaders/extension.ts";
import { AppContext } from "../../../mod.ts";
import { BlogPost } from "../../../types.ts";
import { BlogPost, Ignore } from "../../../types.ts";
import { getRatings } from "../../../utils/records.ts";

interface Props {
/**
* @description Ignore ratings in the aggregateRating calc
*/
ignoreRatings?: Ignore;
/**
* @description Return only aggregate rating object
*/
onlyAggregate?: boolean;
}

/**
* @title ExtensionOf BlogPost list: Ratings
* @description It can harm performance. Use wisely
*/
export default function ratingsExt(
_props: unknown,
{ ignoreRatings, onlyAggregate }: Props,
_req: Request,
ctx: AppContext,
): ExtensionOf<BlogPost[] | null> {
Expand All @@ -19,7 +30,12 @@ export default function ratingsExt(

const postsWithRatings = await Promise.all(
posts.map(async (post) => {
const ratings = await getRatings({ post, ctx });
const ratings = await getRatings({
post,
ctx,
ignoreRatings,
onlyAggregate,
});
return { ...post, ...ratings };
}),
);
Expand Down
17 changes: 14 additions & 3 deletions blog/loaders/extensions/BlogpostList/reviews.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
import { ExtensionOf } from "../../../../website/loaders/extension.ts";
import { AppContext } from "../../../mod.ts";
import { BlogPost } from "../../../types.ts";
import { BlogPost, Ignore } from "../../../types.ts";
import { getReviews } from "../../../utils/records.ts";

interface Props {
/**
* @description Ignore specific reviews
*/
ignoreReviews?: Ignore;
/**
* @description Order By
*/
orderBy?: "date_asc" | "date_desc";
}

/**
* @title ExtensionOf BlogPost list: Reviews
* @description It can harm performance. Use wisely
*/
export default function reviewsExt(
_props: unknown,
{ ignoreReviews, orderBy }: Props,
_req: Request,
ctx: AppContext,
): ExtensionOf<BlogPost[] | null> {
Expand All @@ -19,7 +30,7 @@ export default function reviewsExt(

const postsWithReviews = await Promise.all(
posts.map(async (post) => {
const reviews = await getReviews({ post, ctx });
const reviews = await getReviews({ post, ctx, ignoreReviews, orderBy });
return { ...post, ...reviews };
}),
);
Expand Down
24 changes: 20 additions & 4 deletions blog/loaders/extensions/BlogpostListing/ratings.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
import { ExtensionOf } from "../../../../website/loaders/extension.ts";
import { AppContext } from "../../../mod.ts";
import { BlogPostListingPage } from "../../../types.ts";
import { BlogPostListingPage, Ignore } from "../../../types.ts";
import { getRatings } from "../../../utils/records.ts";

interface Props {
/**
* @description Ignore ratings in the aggregateRating calc
*/
ignoreRatings?: Ignore;
/**
* @description Return only aggregate rating object
*/
onlyAggregate?: boolean;
}

/**
* @title ExtensionOf BlogPostPage: Ratings
* @title ExtensionOf BlogPostListing: Ratings
* @description It can harm performance. Use wisely
*/
export default function ratingsExt(
_props: unknown,
{ ignoreRatings, onlyAggregate }: Props,
_req: Request,
ctx: AppContext,
): ExtensionOf<BlogPostListingPage | null> {
Expand All @@ -19,7 +30,12 @@ export default function ratingsExt(

const posts = await Promise.all(
blogpostListingPage.posts.map(async (post) => {
const ratings = await getRatings({ post, ctx });
const ratings = await getRatings({
post,
ctx,
onlyAggregate,
ignoreRatings,
});
return { ...post, ...ratings };
}),
);
Expand Down
19 changes: 15 additions & 4 deletions blog/loaders/extensions/BlogpostListing/reviews.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
import { ExtensionOf } from "../../../../website/loaders/extension.ts";
import { AppContext } from "../../../mod.ts";
import { BlogPostListingPage } from "../../../types.ts";
import { BlogPostListingPage, Ignore } from "../../../types.ts";
import { getReviews } from "../../../utils/records.ts";

interface Props {
/**
* @description Ignore specific reviews
*/
ignoreReviews?: Ignore;
/**
* @description Order By
*/
orderBy?: "date_asc" | "date_desc";
}

/**
* @title ExtensionOf BlogPostPage: Reviews
* @title ExtensionOf BlogPostListing: Reviews
* @description It can harm performance. Use wisely
*/
export default function reviewsExt(
_props: unknown,
{ ignoreReviews, orderBy }: Props,
_req: Request,
ctx: AppContext,
): ExtensionOf<BlogPostListingPage | null> {
Expand All @@ -19,7 +30,7 @@ export default function reviewsExt(

const posts = await Promise.all(
blogpostListingPage.posts.map(async (post) => {
const reviews = await getReviews({ post, ctx });
const reviews = await getReviews({ post, ctx, ignoreReviews, orderBy });
return { ...post, ...reviews };
}),
);
Expand Down
17 changes: 14 additions & 3 deletions blog/loaders/extensions/BlogpostPage/ratings.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
import { ExtensionOf } from "../../../../website/loaders/extension.ts";
import { AppContext } from "../../../mod.ts";
import { BlogPostPage } from "../../../types.ts";
import { BlogPostPage, Ignore } from "../../../types.ts";
import { getRatings } from "../../../utils/records.ts";

interface Props {
/**
* @description Ignore ratings in the aggregateRating calc
*/
ignoreRatings?: Ignore;
}

/**
* @title ExtensionOf BlogPostPage: Ratings
* @description It can harm performance. Use wisely
*/
export default function ratingsExt(
_props: unknown,
{ ignoreRatings }: Props,
_req: Request,
ctx: AppContext,
): ExtensionOf<BlogPostPage | null> {
return async (blogpostPage: BlogPostPage | null) => {
if (!blogpostPage) {
return null;
}
const post = await getRatings({ post: blogpostPage.post, ctx });
const post = await getRatings({
post: blogpostPage.post,
ctx,
ignoreRatings,
});
return { ...blogpostPage, post };
};
}
22 changes: 19 additions & 3 deletions blog/loaders/extensions/BlogpostPage/reviews.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,38 @@
import { ExtensionOf } from "../../../../website/loaders/extension.ts";
import { AppContext } from "../../../mod.ts";
import { BlogPostPage } from "../../../types.ts";
import { BlogPostPage, Ignore } from "../../../types.ts";
import { getReviews } from "../../../utils/records.ts";

interface Props {
/**
* @description Ignore specific reviews
*/
ignoreReviews?: Ignore;
/**
* @description Order By
*/
orderBy?: "date_asc" | "date_desc";
}

/**
* @title ExtensionOf BlogPostPage: Reviews
* @description It can harm performance. Use wisely
*/
export default function reviewsExt(
_props: unknown,
{ ignoreReviews, orderBy }: Props,
_req: Request,
ctx: AppContext,
): ExtensionOf<BlogPostPage | null> {
return async (blogpostPage: BlogPostPage | null) => {
if (!blogpostPage) {
return null;
}
const post = await getReviews({ post: blogpostPage.post, ctx });
const post = await getReviews({
post: blogpostPage.post,
ctx,
ignoreReviews,
orderBy,
});
return { ...blogpostPage, post };
};
}
8 changes: 6 additions & 2 deletions blog/sections/Template.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default function Template({ post }: Props) {
excerpt = "Excerpt",
date,
image,
alt
alt,
} = post;

return (
Expand All @@ -33,7 +33,11 @@ export default function Template({ post }: Props) {
: ""}
</p>
{image && (
<img class="w-full rounded-2xl bg-cover" src={image} alt={alt ?? title} />
<img
class="w-full rounded-2xl bg-cover"
src={image}
alt={alt ?? title}
/>
)}
<div dangerouslySetInnerHTML={{ __html: content }} />
</div>
Expand Down
11 changes: 11 additions & 0 deletions blog/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,14 @@ export interface AggregateRating {
/** The lowest value allowed in this rating system. */
worstRating?: number;
}

export interface Ignore {
/**
* @title Active
*/
active?: boolean;
/**
* @title When additionalType is marked with:
*/
markedAs?: string[];
}
Loading
Loading