Skip to content

Commit

Permalink
refactor(client): move content check to markdown component
Browse files Browse the repository at this point in the history
  • Loading branch information
lareii committed Sep 21, 2024
1 parent e1d180a commit 5548fd6
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 86 deletions.
55 changes: 42 additions & 13 deletions client/components/app/Markdown/index.jsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,40 @@
import { usePathname } from 'next/navigation';
import { useState } from 'react';
import Link from 'next/link';
import Markdown from 'react-markdown';
import remarkGfm from 'remark-gfm';
import { SquareArrowOutUpRight } from 'lucide-react';

export default function MarkdownContent({ content }) {
const isPostPage = usePathname().includes('/app/posts/');

const [isExpanded, setIsExpanded] = useState(false);

const contentLines = content.split('\n');
const contentLength = content.length;
const lineCount = contentLines.length;

const isLong = lineCount > 5 || contentLength > 500;

const truncatedContent =
lineCount > 5
? contentLines.slice(0, 5).join('\n') + '...'
: contentLength > 500
? content.substring(0, 500) + '...'
: content;

const contentToShow = isExpanded || isPostPage ? content : truncatedContent;

const customRenderers = {
img: ({ src, alt }) => (
<Link href={src} target='_blank'>
{alt || src}
<SquareArrowOutUpRight className='w-3 h-3 ml-1 inline-block' />
</Link>
),
a: ({ node, children }) => (
<Link
href={node.properties.href}
target='_blank'
>
{children || node.properties.href}
a: ({ href, children }) => (
<Link href={href} target='_blank'>
{children || href}
<SquareArrowOutUpRight className='w-3 h-3 ml-1 inline-block' />
</Link>
),
Expand All @@ -29,12 +47,23 @@ export default function MarkdownContent({ content }) {
};

return (
<Markdown
components={customRenderers}
remarkPlugins={[remarkGfm]}
className='md'
>
{content}
</Markdown>
<div>
<Markdown
components={customRenderers}
remarkPlugins={[remarkGfm]}
className='md'
>
{contentToShow}
</Markdown>

{!isPostPage && isLong && (
<div
onClick={() => setIsExpanded(!isExpanded)}
className='text-xs cursor-pointer w-fit'
>
{isExpanded ? 'gizle' : 'devamını oku'}
</div>
)}
</div>
);
}
29 changes: 1 addition & 28 deletions client/components/app/Post/index.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use client';

import { usePathname } from 'next/navigation';
import { useState } from 'react';
import Dropdown from '@/components/app/Post/Dropdown';
import LikeButton from '@/components/app/Post/LikeButton';
Expand All @@ -11,24 +10,6 @@ import MarkdownContent from '@/components/app/Markdown';

export default function Post({ post: initialPost, onDelete, onNewComment }) {
const [post, setPost] = useState(initialPost);
const isPostPage = usePathname().includes('/app/posts/');
const [isExpanded, setIsExpanded] = useState(false);

const contentLines = post.content.split('\n');
const contentLength = post.content.length;
const lineCount = contentLines.length;

const isLong = lineCount > 5 || contentLength > 500;

const truncatedContent =
lineCount > 5
? contentLines.slice(0, 5).join('\n') + '...'
: contentLength > 500
? post.content.substring(0, 500) + '...'
: post.content;

const contentToShow =
isExpanded || isPostPage ? post.content : truncatedContent;

return (
<div className='p-5 bg-zinc-900 rounded-lg'>
Expand All @@ -37,15 +18,7 @@ export default function Post({ post: initialPost, onDelete, onNewComment }) {
<Dropdown post={post} setPost={setPost} onDelete={onDelete} />
</div>
<div className='relative'>
<MarkdownContent content={contentToShow} />
{!isPostPage && isLong && (
<div
onClick={() => setIsExpanded(!isExpanded)}
className='text-xs cursor-pointer w-fit'
>
{isExpanded ? 'gizle' : 'devamını oku'}
</div>
)}
<MarkdownContent content={post.content} />
</div>
<div className='mt-4 flex items-center justify-between'>
<div className='flex gap-2'>
Expand Down
88 changes: 44 additions & 44 deletions client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"framer-motion": "^11.3.28",
"jose": "^5.6.3",
"lucide-react": "^0.428.0",
"next": "14.2.5",
"next": "^14.2.13",
"react": "^18",
"react-dom": "^18",
"react-hook-form": "^7.52.2",
Expand Down

0 comments on commit 5548fd6

Please sign in to comment.