Skip to content

Commit

Permalink
fix: block link fallback render
Browse files Browse the repository at this point in the history
  • Loading branch information
Innei committed Sep 29, 2023
1 parent 74c9ce6 commit ae062ad
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 37 deletions.
28 changes: 0 additions & 28 deletions example/color.json

This file was deleted.

12 changes: 12 additions & 0 deletions src/components/ui/markdown/customize.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,18 @@ https://github.com/toeverything/AFFiNE/blob/himself65/0710/remove-effect/apps/we

https://github.com/toeverything/AFFiNE/blob/himself65/0710/remove-effect/apps/web/src/atoms/index.ts

```
https://trpc.io/docs/client/react/useInfiniteQuery
```

https://trpc.io/docs/client/react/useInfiniteQuery

```
[TRPC](https://trpc.io/docs/client/react/useInfiniteQuery)
```

[TRPC](https://trpc.io/docs/client/react/useInfiniteQuery)

## LinkCard

```
Expand Down
24 changes: 17 additions & 7 deletions src/components/ui/markdown/renderers/LinkRenderer.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { useMemo } from 'react'
import dynamic from 'next/dynamic'
import type { PropsWithChildren } from 'react'

import { GitHubBrandIcon } from '~/components/icons/platform/GitHubBrandIcon'
import {
Expand All @@ -24,7 +25,10 @@ import { MLink } from '../../link/MLink'
const Tweet = dynamic(() => import('~/components/widgets/shared/Tweet'), {
ssr: false,
})
export const LinkRenderer = ({ href }: { href: string }) => {
export const LinkRenderer = ({
href,
children,
}: PropsWithChildren<{ href: string }>) => {
const url = useMemo(() => {
try {
return new URL(href)
Expand All @@ -33,14 +37,17 @@ export const LinkRenderer = ({ href }: { href: string }) => {
}
}, [href])

if (!url) {
return (
const fallbackElement = useMemo(
() => (
<p>
<MLink href={href}>
<span>{href}</span>
</MLink>
<MLink href={href}>{children ?? <span>{href}</span>}</MLink>
</p>
)
),
[children, href],
)

if (!url) {
return fallbackElement
}
switch (true) {
case isTweetUrl(url): {
Expand Down Expand Up @@ -136,6 +143,9 @@ export const LinkRenderer = ({ href }: { href: string }) => {
case isSelfArticleUrl(url): {
return <LinkCard source="self" id={url.pathname.slice(1)} />
}

default:
return fallbackElement
}
}

Expand Down
9 changes: 7 additions & 2 deletions src/components/ui/markdown/renderers/paragraph.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react'
import clsx from 'clsx'
import type { DetailedHTMLProps, FC, HTMLAttributes } from 'react'
import type { DetailedHTMLProps, FC, HTMLAttributes, ReactNode } from 'react'

import { LinkRenderer } from './LinkRenderer'

Expand All @@ -19,7 +19,12 @@ export const MParagraph: FC<

// isLink
if (isLink(child)) {
return <LinkRenderer href={(child as any)?.props?.href} />
const children = (child as any)?.props?.children as ReactNode[]
return (
<LinkRenderer href={(child as any)?.props?.href}>
{children}
</LinkRenderer>
)
}
}
// console.log(children)
Expand Down

1 comment on commit ae062ad

@vercel
Copy link

@vercel vercel bot commented on ae062ad Sep 29, 2023

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 – ./

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

Please sign in to comment.