-
Notifications
You must be signed in to change notification settings - Fork 562
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(markdown): separate the markdown ctx and decouple it from th…
…e biz Signed-off-by: Innei <tukon479@gmail.com>
- Loading branch information
Showing
24 changed files
with
367 additions
and
268 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import katexStyle from "katex/dist/katex.min.css?raw" | ||
import { createElement, Fragment, memo, useEffect, useMemo, useRef, useState } from "react" | ||
|
||
import { MemoedDangerousHTMLStyle } from "~/components/common/MemoedDangerousHTMLStyle" | ||
import { parseHtml } from "~/lib/parse-html" | ||
import { useWrappedElementSize } from "~/providers/wrapped-element-provider" | ||
|
||
import type { MediaInfoRecord } from "../media" | ||
import { MediaContainerWidthProvider, MediaInfoRecordProvider } from "../media" | ||
import { MarkdownRenderContainerRefContext } from "./context" | ||
|
||
export type HTMLProps<A extends keyof JSX.IntrinsicElements = "div"> = { | ||
children: string | null | undefined | ||
as: A | ||
|
||
accessory?: React.ReactNode | ||
noMedia?: boolean | ||
mediaInfo?: MediaInfoRecord | ||
|
||
handleTranslate?: (html: HTMLElement | null) => void | ||
} & JSX.IntrinsicElements[A] & | ||
Partial<{ | ||
renderInlineStyle: boolean | ||
}> | ||
const HTMLImpl = <A extends keyof JSX.IntrinsicElements = "div">(props: HTMLProps<A>) => { | ||
const { | ||
children, | ||
renderInlineStyle, | ||
as = "div", | ||
accessory, | ||
noMedia, | ||
mediaInfo, | ||
handleTranslate: translate, | ||
...rest | ||
} = props | ||
const [remarkOptions, setRemarkOptions] = useState({ | ||
renderInlineStyle, | ||
noMedia, | ||
}) | ||
const [shouldForceReMountKey, setShouldForceReMountKey] = useState(0) | ||
|
||
useEffect(() => { | ||
setRemarkOptions((options) => { | ||
if (JSON.stringify(options) === JSON.stringify({ renderInlineStyle, noMedia })) { | ||
return options | ||
} | ||
|
||
setShouldForceReMountKey((key) => key + 1) | ||
return { ...options, renderInlineStyle, noMedia } | ||
}) | ||
}, [renderInlineStyle, noMedia]) | ||
|
||
const [refElement, setRefElement] = useState<HTMLElement | null>(null) | ||
|
||
const onceRef = useRef(false) | ||
useEffect(() => { | ||
if (onceRef.current || !refElement) { | ||
return | ||
} | ||
|
||
translate?.(refElement) | ||
onceRef.current = true | ||
}, [translate, refElement]) | ||
|
||
const markdownElement = useMemo( | ||
() => | ||
children && | ||
parseHtml(children, { | ||
...remarkOptions, | ||
}).toContent(), | ||
[children, remarkOptions], | ||
) | ||
|
||
const { w: containerWidth } = useWrappedElementSize() | ||
|
||
if (!markdownElement) return null | ||
return ( | ||
<MarkdownRenderContainerRefContext.Provider value={refElement}> | ||
<MediaContainerWidthProvider width={containerWidth}> | ||
<MediaInfoRecordProvider mediaInfo={mediaInfo}> | ||
<MemoedDangerousHTMLStyle>{katexStyle}</MemoedDangerousHTMLStyle> | ||
{createElement(as, { ...rest, ref: setRefElement }, markdownElement)} | ||
</MediaInfoRecordProvider> | ||
</MediaContainerWidthProvider> | ||
{!!accessory && <Fragment key={shouldForceReMountKey}>{accessory}</Fragment>} | ||
</MarkdownRenderContainerRefContext.Provider> | ||
) | ||
} | ||
|
||
export const HTML = memo(HTMLImpl) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,14 @@ | ||
import { createContext } from "react" | ||
import { createContext as reactCreateContext } from "react" | ||
import { createContext } from "use-context-selector" | ||
|
||
export const MarkdownRenderContainerRefContext = createContext<HTMLElement | null>(null) | ||
import type { MarkdownImage, MarkdownRenderActions } from "./types" | ||
|
||
export const MarkdownRenderContainerRefContext = reactCreateContext<HTMLElement | null>(null) | ||
|
||
export const MarkdownImageRecordContext = createContext<Record<string, MarkdownImage>>({}) | ||
|
||
export const MarkdownRenderActionContext = reactCreateContext<MarkdownRenderActions>({ | ||
transformUrl: (url) => url ?? "", | ||
isAudio: () => false, | ||
ensureAndRenderTimeStamp: () => false, | ||
}) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 7 additions & 6 deletions
13
apps/renderer/src/components/ui/markdown/renderers/MarkdownLink.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 3 additions & 6 deletions
9
apps/renderer/src/components/ui/markdown/renderers/MarkdownP.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export type MarkdownImage = { | ||
url: string | ||
blurhash?: string | ||
width: number | ||
height: number | ||
} | ||
|
||
export interface MarkdownRenderActions { | ||
transformUrl: (url?: string) => string | ||
isAudio: (url?: string) => boolean | ||
ensureAndRenderTimeStamp: (children: string) => React.ReactNode | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.