-
Notifications
You must be signed in to change notification settings - Fork 563
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
91 changed files
with
1,391 additions
and
1,167 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
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
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,8 @@ | ||
import { atom } from "jotai" | ||
|
||
import { createAtomHooks } from "~/lib/jotai" | ||
import type { ServerConfigs } from "~/models" | ||
|
||
export const [, , useServerConfigs, , getServerConfigs, setServerConfigs] = createAtomHooks( | ||
atom<Nullable<ServerConfigs>>(null), | ||
) |
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
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
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
Oops, something went wrong.