Commit 6a37537a authored by Lê Bảo Hồng Đức's avatar Lê Bảo Hồng Đức

fix

parent 0c9063d2
......@@ -16,8 +16,8 @@ import {
buildVisibleNewsFilters,
fetchDynamicPostList,
findDisplayCategoryForPost,
getDynamicPostExcerpt,
resolveDynamicPostImage,
stripHtml,
} from "./data";
import type { DynamicCategoryRouteItem } from "./types";
......@@ -128,13 +128,7 @@ export default function ArticlePage({ category, allCategories }: ArticlePageProp
<div className="space-y-9">
{paginatedPosts.length ? (
paginatedPosts.map((item, index) => {
const fallbackDescription = item.content_structure?.post_content
?.map((section) => section.content)
.join(" ");
const description =
stripHtml(item.summary) ||
stripHtml(item.content) ||
stripHtml(fallbackDescription);
const description = getDynamicPostExcerpt(item);
const primaryCategory = findDisplayCategoryForPost(
item,
category,
......@@ -152,7 +146,7 @@ export default function ArticlePage({ category, allCategories }: ArticlePageProp
>
<Link
href={buildDynamicPostHref(item.external_link, item.id, category.id)}
className="group grid gap-5 sm:grid-cols-[250px_minmax(0,1fr)]"
className="group grid items-center gap-5 sm:grid-cols-[250px_minmax(0,1fr)]"
>
<div className="relative overflow-hidden rounded-md bg-[#edf1f5] aspect-[25/15] sm:aspect-[5/3]">
<ImageNext
......@@ -164,7 +158,7 @@ export default function ArticlePage({ category, allCategories }: ArticlePageProp
/>
</div>
<div className="min-w-0 pt-1">
<div className="min-w-0">
<div className="flex flex-wrap items-center gap-3 text-xs">
<span
className={`rounded-full px-2.5 py-1 font-semibold ${getTagClassName(tagIndex)}`}
......
......@@ -483,13 +483,53 @@ export function resolveDynamicPostImage(thumbnail?: DynamicPostThumbnail) {
export function stripHtml(value?: string | null) {
if (!value) return "";
return value
.replace(/\[caption[^\]]*]/gi, " ")
.replace(/\[\/caption]/gi, " ")
.replace(/\[[^[\]]+]/g, " ")
.replace(/<img[^>]*>/gi, " ")
.replace(/<[^>]+>/g, " ")
.replace(/\s+/g, " ")
.trim();
}
export function getDynamicPostExcerpt(post: DynamicPostItem | null) {
if (!post) return "";
const structuredContentText = (post.content_structure?.post_content ?? [])
.map((section) => stripHtml(section.content))
.filter(Boolean)
.join(" ");
const candidates = [
stripHtml(post.summary),
stripHtml(post.content),
structuredContentText,
].filter(Boolean);
const parts: string[] = [];
for (const candidate of candidates) {
const normalizedCandidate = candidate.trim();
if (!normalizedCandidate) continue;
const isDuplicated = parts.some((part) => {
return (
part === normalizedCandidate ||
part.includes(normalizedCandidate) ||
normalizedCandidate.includes(part)
);
});
if (!isDuplicated) {
parts.push(normalizedCandidate);
}
}
return parts.join(" ").replace(/\s+/g, " ").trim();
}
export function getDynamicPostBodyHtml(post: DynamicPostItem | null) {
if (!post) return "";
......
......@@ -13,8 +13,8 @@ import {
buildDynamicPostHref,
buildVisibleNewsFilters,
fetchDynamicPostList,
getDynamicPostExcerpt,
resolveDynamicPostImage,
stripHtml,
} from "@/app/(main)/[...slug]/templates/data";
import type { DynamicPostItem } from "@/app/(main)/[...slug]/templates/types";
......@@ -43,11 +43,7 @@ const getTagClassName = (index: number) => {
};
function SearchResultItem({ item, index }: { item: DynamicPostItem; index: number }) {
const fallbackDescription = item.content_structure?.post_content
?.map((section) => section.content)
.join(" ");
const description =
stripHtml(item.summary) || stripHtml(item.content) || stripHtml(fallbackDescription);
const description = getDynamicPostExcerpt(item);
const date = formatPostDate(item.release_at || item.published_at || item.created_at);
const categoryName = item.categories[0]?.name || "Tin tức";
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment