Skip to content

Commit 8893e09

Browse files
committed
handle markdown in usecase details
1 parent 5f49850 commit 8893e09

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

app/[locale]/(user)/components/ListingComponent.tsx

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,44 @@ import { cn, formatDate } from '@/lib/utils';
2424
import Filter from '../datasets/components/FIlter/Filter';
2525
import Styles from '../datasets/dataset.module.scss';
2626

27+
// Helper function to strip markdown and HTML tags for card preview
28+
const stripMarkdown = (markdown: string): string => {
29+
if (!markdown) return '';
30+
return markdown
31+
// Remove code blocks first (before other replacements)
32+
.replace(/```[\s\S]*?```/g, '')
33+
// Remove inline code
34+
.replace(/`([^`]+)`/g, '$1')
35+
// Remove images
36+
.replace(/!\[([^\]]*)\]\([^\)]+\)/g, '$1')
37+
// Remove links
38+
.replace(/\[([^\]]+)\]\([^\)]+\)/g, '$1')
39+
// Remove headers
40+
.replace(/^#{1,6}\s+/gm, '')
41+
// Remove bold
42+
.replace(/\*\*([^*]+)\*\*/g, '$1')
43+
.replace(/__([^_]+)__/g, '$1')
44+
// Remove italic
45+
.replace(/\*([^*]+)\*/g, '$1')
46+
.replace(/_([^_]+)_/g, '$1')
47+
// Remove strikethrough
48+
.replace(/~~([^~]+)~~/g, '$1')
49+
// Remove blockquotes
50+
.replace(/^\s*>\s+/gm, '')
51+
// Remove horizontal rules
52+
.replace(/^(-{3,}|_{3,}|\*{3,})$/gm, '')
53+
// Remove list markers
54+
.replace(/^\s*[-*+]\s+/gm, '')
55+
.replace(/^\s*\d+\.\s+/gm, '')
56+
// Remove HTML tags
57+
.replace(/<[^>]*>/g, '')
58+
// Remove extra whitespace and newlines
59+
.replace(/\n\s*\n/g, '\n')
60+
.replace(/\n/g, ' ')
61+
.replace(/\s+/g, ' ')
62+
.trim();
63+
};
64+
2765
// Interfaces
2866
interface Bucket {
2967
key: string;
@@ -554,7 +592,7 @@ const ListingComponent: React.FC<ListingProps> = ({
554592

555593
const commonProps = {
556594
title: item.title,
557-
description: item.description,
595+
description: stripMarkdown(item.description || ''),
558596
metadataContent: MetadataContent,
559597
tag: item.tags,
560598
formats: item.formats,

app/[locale]/(user)/usecases/components/Details.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import React, { useState } from 'react';
44
import Image from 'next/image';
55
import { Button, Icon, Spinner, Tag, Text, Tray } from 'opub-ui';
6+
import ReactMarkdown from 'react-markdown';
7+
import rehypeRaw from 'rehype-raw';
8+
import remarkGfm from 'remark-gfm';
69

710
import { Icons } from '@/components/icons';
811
import Metadata from './Metadata';
@@ -89,10 +92,13 @@ const PrimaryDetails = ({ data, isLoading }: { data: any; isLoading: any }) => {
8992
</div>
9093
<div className="mt-6 lg:mt-10">
9194
<Text variant="headingXl">Summary</Text>
92-
<div className="mt-4">
93-
<Text variant="bodyLg" fontWeight="regular" className="leading-5">
95+
<div className="prose-h1:text-3xl prose-h2:text-2xl prose-h3:text-xl prose-p:leading-relaxed prose-a:text-blue-600 hover:prose-a:text-blue-700 prose-code:bg-gray-200 prose-code:rounded prose-pre:bg-gray-100 prose-pre:border prose-pre:border-gray-300 prose-blockquote:border-l-blue-500 prose-th:bg-gray-100 prose-img:rounded-lg prose prose-lg mt-4 max-w-none prose-headings:text-gray-900 prose-p:text-gray-800 prose-a:underline prose-blockquote:text-gray-700 prose-strong:text-gray-900 prose-em:text-gray-800 prose-code:px-1 prose-code:py-0.5 prose-code:text-gray-900 prose-code:before:content-none prose-code:after:content-none prose-pre:text-gray-900 prose-ol:text-gray-800 prose-ul:text-gray-800 prose-li:text-gray-800 prose-li:marker:text-gray-600 prose-table:text-gray-800 prose-thead:border-gray-300 prose-tr:border-gray-300 prose-th:border-gray-300 prose-th:text-gray-900 prose-td:border-gray-300 prose-td:text-gray-800 prose-hr:border-gray-300">
96+
<ReactMarkdown
97+
remarkPlugins={[remarkGfm]}
98+
rehypePlugins={[rehypeRaw]}
99+
>
94100
{data.useCase.summary}
95-
</Text>
101+
</ReactMarkdown>
96102
</div>
97103
</div>
98104
</div>

0 commit comments

Comments
 (0)