diff --git a/frontend/application/app/blog/[slug]/page.tsx b/frontend/application/app/blog/[slug]/page.tsx
index 33fa4ff0..03ca3731 100644
--- a/frontend/application/app/blog/[slug]/page.tsx
+++ b/frontend/application/app/blog/[slug]/page.tsx
@@ -4,6 +4,7 @@ import type { Metadata } from 'next';
import BlogPostClient from "../../../components/BlogPostClient";
import { getSeoConfig, formatPageTitle } from "../../../lib/seo";
import { safeJsonLd } from "../../../lib/schema";
+import { sanitizeBlogHtml } from "../../../lib/sanitizeBlogHtml";
function safeIso(val: any, fallback?: string): string | undefined {
if (!val) return fallback;
@@ -182,6 +183,7 @@ export default async function BlogPostPage({ params }: { params: Promise<{ slug:
const publishedIso = safeIso(blog.publishedAt) || safeIso(blog.createdAt) || new Date().toISOString();
const modifiedIso = safeIso(blog.updatedAt) || publishedIso;
+ const sanitizedContent = sanitizeBlogHtml(blog.content);
const schemaType = blog.schemaType || 'BlogPosting';
const authorSchema = blog.author && (blog.author.firstName || blog.author.lastName)
@@ -279,6 +281,7 @@ export default async function BlogPostPage({ params }: { params: Promise<{ slug:
blog={blog}
publishedDate={publishedDate}
authorName={authorName}
+ sanitizedContent={sanitizedContent}
/>
>
);
diff --git a/frontend/application/components/BlogPage.tsx b/frontend/application/components/BlogPage.tsx
index e787f465..18c3cfe6 100644
--- a/frontend/application/components/BlogPage.tsx
+++ b/frontend/application/components/BlogPage.tsx
@@ -297,11 +297,11 @@ export default function BlogPage({
{/* Featured Post Banner */}
{featuredPost && (
-
router.push(`/blog/${encodeURIComponent(featuredPost.slug || featuredPost.id)}`)}
>
-
+
)}
@@ -375,14 +375,14 @@ export default function BlogPage({
{slicedRegularPosts.map((post, idx) => (
-
router.push(`/blog/${encodeURIComponent(post.slug || post.id)}`)}
>
@@ -440,7 +440,7 @@ export default function BlogPage({
-
+
))}
diff --git a/frontend/application/components/BlogPostClient.tsx b/frontend/application/components/BlogPostClient.tsx
index 5d6e5696..03e68985 100644
--- a/frontend/application/components/BlogPostClient.tsx
+++ b/frontend/application/components/BlogPostClient.tsx
@@ -1,6 +1,5 @@
"use client";
import React, { useState, useEffect } from "react";
-import DOMPurify from 'dompurify';
import {
ChevronRight,
Calendar,
@@ -24,9 +23,10 @@ interface BlogPostClientProps {
blog: any;
publishedDate: string;
authorName: string;
+ sanitizedContent: string;
}
-export default function BlogPostClient({ blog, publishedDate, authorName }: BlogPostClientProps) {
+export default function BlogPostClient({ blog, publishedDate, authorName, sanitizedContent }: BlogPostClientProps) {
const [copied, setCopied] = useState(false);
const [viewCount, setViewCount] = useState(blog.viewCount || 0);
const [commentName, setCommentName] = useState("");
@@ -176,14 +176,7 @@ export default function BlogPostClient({ blog, publishedDate, authorName }: Blog
{/* Main Rich Content HTML Renderer */}
{/* Tags Chip Footer */}
diff --git a/frontend/application/components/__tests__/BlogPage.test.tsx b/frontend/application/components/__tests__/BlogPage.test.tsx
index 5333bcc0..8c1dd055 100644
--- a/frontend/application/components/__tests__/BlogPage.test.tsx
+++ b/frontend/application/components/__tests__/BlogPage.test.tsx
@@ -20,7 +20,7 @@ const blogs=[{id:'1',slug:'joint',title:'Joint article',excerpt:'Mobility guide'
const categories=[{id:'joints',name:'Joints',slug:'joints'},{id:'skin',name:'Skin',slug:'skin'}];
beforeEach(()=>vi.clearAllMocks());
describe('BlogPage',()=>{
- it('prioritizes featured post and navigates',()=>{render();expect(screen.getAllByRole('heading',{level:2})[0]).toHaveTextContent('Skin article');fireEvent.click(screen.getByText('Skin article'));expect(h.push).toHaveBeenCalledWith('/blog/skin');});
+ it('renders featured and regular posts as semantic links',()=>{render();expect(screen.getAllByRole('heading',{level:2})[0]).toHaveTextContent('Skin article');expect(screen.getByRole('link',{name:/Skin article/})).toHaveAttribute('href','/blog/skin');expect(screen.getByRole('link',{name:/Joint article/})).toHaveAttribute('href','/blog/joint');});
it('combines category, excerpt search and tags then resets empty intersection',()=>{render();fireEvent.click(screen.getByRole('button',{name:'Joints'}));expect(h.replace).toHaveBeenCalledWith('/blog?categoryId=joints',{scroll:false});expect(screen.queryByText('Skin article')).not.toBeInTheDocument();fireEvent.change(screen.getByPlaceholderText('جستجو در موضوعات، بیماریها، ویتامینها...'),{target:{value:'MOBILITY'}});expect(screen.getByText('Joint article')).toBeInTheDocument();fireEvent.click(screen.getByRole('button',{name:'#coat'}));expect(screen.getByText('هیچ مقالهای با معیارهای جستجوی شما یافت نشد.')).toBeInTheDocument();fireEvent.click(screen.getByRole('button',{name:'پاکسازی همه فیلترها'}));expect(screen.getByText('Skin article')).toBeInTheDocument();expect(screen.getByText('Joint article')).toBeInTheDocument();});
it('syncs category with route props',()=>{const view=render();expect(screen.queryByText('Skin article')).not.toBeInTheDocument();view.rerender();expect(screen.queryByText('Joint article')).not.toBeInTheDocument();});
it('clears search and toggles tags off',()=>{render();fireEvent.click(screen.getByRole('button',{name:'#health'}));expect(screen.queryByText('Skin article')).not.toBeInTheDocument();fireEvent.click(screen.getByRole('button',{name:'#health'}));expect(screen.getByText('Skin article')).toBeInTheDocument();fireEvent.change(screen.getByPlaceholderText('جستجو در موضوعات، بیماریها، ویتامینها...'),{target:{value:'missing'}});fireEvent.click(screen.getByRole('button',{name:'پاک کردن'}));expect(screen.getByText('Skin article')).toBeInTheDocument();});
diff --git a/frontend/application/components/__tests__/BlogPostClient.test.tsx b/frontend/application/components/__tests__/BlogPostClient.test.tsx
index eeb3e52c..f4b6f412 100644
--- a/frontend/application/components/__tests__/BlogPostClient.test.tsx
+++ b/frontend/application/components/__tests__/BlogPostClient.test.tsx
@@ -80,6 +80,7 @@ describe("BlogPostClient Component", () => {
render(
@@ -113,6 +114,7 @@ describe("BlogPostClient Component", () => {
render(
@@ -133,6 +135,7 @@ describe("BlogPostClient Component", () => {
render(
@@ -170,6 +173,7 @@ describe("BlogPostClient Component", () => {
render(
@@ -215,6 +219,7 @@ describe("BlogPostClient Component", () => {
render(
@@ -241,6 +246,7 @@ describe("BlogPostClient Component", () => {
render(
@@ -254,4 +260,25 @@ describe("BlogPostClient Component", () => {
expect(screen.getByText("لینک کپی شد!")).toBeInTheDocument();
});
});
+
+ it("renders the server-sanitized article HTML instead of recomputing content in the browser", () => {
+ const rawContent = "متن خام متفاوت
";
+ const sanitizedContent = "اگر بچهگربهای یتیم پیدا کردهاید
راهنمای تغذیه
";
+
+ const { container } = render(
+
+ );
+
+ const article = container.querySelector('.article-html-content');
+ expect(article?.innerHTML).toBe(sanitizedContent);
+ expect(article).toHaveTextContent('اگر بچهگربهای یتیم پیدا کردهاید');
+ expect(article?.innerHTML).not.toContain('metaTitle');
+ expect(article?.innerHTML).not.toContain('