Skip to content

Commit

Permalink
feat: comment markdown improve
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <[email protected]>
  • Loading branch information
Innei committed Jan 25, 2024
1 parent acc815e commit 65d4618
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 26 deletions.
24 changes: 2 additions & 22 deletions src/components/modules/comment/Comment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import { createPortal } from 'react-dom'
import clsx from 'clsx'
import { m } from 'framer-motion'
import { atom, useAtomValue } from 'jotai'
import Markdown from 'markdown-to-jsx'
import type { CommentModel } from '@mx-space/api-client'
import type { MarkdownToJSX } from 'markdown-to-jsx'
import type { PropsWithChildren } from 'react'

import { Avatar } from '~/components/ui/avatar'
Expand All @@ -25,6 +23,7 @@ import { softSpringPreset } from '~/constants/spring'
import { jotaiStore } from '~/lib/store'

import styles from './Comment.module.css'
import { CommentMarkdown } from './CommentMarkdown'
import { CommentPinButton, OcticonGistSecret } from './CommentPinButton'
import { CommentReplyButton } from './CommentReplyButton'

Expand Down Expand Up @@ -150,15 +149,7 @@ export const Comment: Component<{
'max-w-[calc(100%-3rem)]',
)}
>
<Markdown
options={{
disabledTypes,
disableParsingRawHTML: true,
forceBlock: true,
}}
>
{text}
</Markdown>
<CommentMarkdown>{text}</CommentMarkdown>
<CommentReplyButton commentId={comment.id} />
</div>
</div>
Expand Down Expand Up @@ -200,14 +191,3 @@ export const CommentBoxHolderPortal = (props: PropsWithChildren) => {

return createPortal(props.children, portalElement)
}

const disabledTypes = [
'footnote',
'footnoteReference',

'image',

'htmlComment',
'htmlSelfClosing',
'htmlBlock',
] as MarkdownToJSX.RuleName[]
28 changes: 28 additions & 0 deletions src/components/modules/comment/CommentMarkdown.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import type { MarkdownToJSX } from '~/components/ui/markdown'
import type { FC } from 'react'

import { Markdown } from '~/components/ui/markdown'

const disabledTypes = [
'footnote',
'footnoteReference',

'image',

'htmlComment',
'htmlSelfClosing',
'htmlBlock',
] as MarkdownToJSX.RuleName[]

export const CommentMarkdown: FC<{
children: string
}> = ({ children }) => {
return (
<Markdown
disabledTypes={disabledTypes}
disableParsingRawHTML
forceBlock
value={children}
/>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { EllipsisHorizontalTextWithTooltip } from '~/components/ui/typography'
import { clsxm } from '~/lib/helper'
import { apiClient } from '~/lib/request'

import { CommentMarkdown } from '../../comment/CommentMarkdown'
import { CommentAction } from './CommentAction'
import { CommentDataContext } from './CommentContext'
import { CommentUrlRender } from './UrlRender'
Expand Down Expand Up @@ -57,7 +58,9 @@ export const CommentContentCell: Component<{ comment: CommentModel }> = (
<RelativeTime date={created} />{TitleEl}
</div>

<p className="break-words">{text}</p>
<div className="break-words">
<CommentMarkdown>{text}</CommentMarkdown>
</div>

{parentComment && typeof parentComment !== 'string' && (
<div className="relative mt-2 break-words">
Expand All @@ -69,7 +72,9 @@ export const CommentContentCell: Component<{ comment: CommentModel }> = (
/>{' '}
<RelativeTime date={parentComment.created} /> 说:
</div>
<p className="mt-2">{parentComment.text}</p>
<div className="mt-2">
<CommentMarkdown>{parentComment.text}</CommentMarkdown>
</div>
</blockquote>
</div>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const CommentCheckBox: FC<{
}
const CommentItem = ({ comment }: { comment: CommentModel }) => {
return (
<div className="grid grid-cols-[40px_300px_auto] gap-8">
<div className="mx-auto grid w-[100rem] max-w-full grid-cols-[40px_300px_auto] gap-8">
<div className="ml-2 mt-[18px]">
<CommentCheckBox id={comment.id} />
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/components/ui/markdown/markdown.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
}

:not(pre) code {
@apply bg-zinc-200 font-mono dark:bg-neutral-800;
@apply rounded-md bg-zinc-200 px-2 font-mono dark:bg-neutral-800;
}

pre {
Expand Down

1 comment on commit 65d4618

@vercel
Copy link

@vercel vercel bot commented on 65d4618 Jan 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

shiro – ./

shiro-innei.vercel.app
shiro-git-main-innei.vercel.app
springtide.vercel.app
innei.in

Please sign in to comment.