diff --git a/packages/console/src/components/Markdown/components/GithubRawImage/index.tsx b/packages/console/src/components/Markdown/components/GithubRawImage/index.tsx new file mode 100644 index 000000000..b4bc6a945 --- /dev/null +++ b/packages/console/src/components/Markdown/components/GithubRawImage/index.tsx @@ -0,0 +1,34 @@ +import React, { HTMLProps, useRef, useState } from 'react'; + +const githubRawUrlPrefix = 'https://raw.githubusercontent.com/logto-io/logto/master'; + +const GithubRawImage = ({ src, alt }: HTMLProps) => { + const imgRef = useRef(null); + const [width, setWidth] = useState(0); + + const onLoad = () => { + if (imgRef.current) { + const { naturalWidth, parentElement } = imgRef.current; + const parentClientWidth = parentElement?.clientWidth ?? 0; + const preferredWidth = Math.min(parentClientWidth, naturalWidth / 2); + + setWidth(preferredWidth); + } + }; + + if (!src) { + return null; + } + + return ( + {alt} + ); +}; + +export default GithubRawImage; diff --git a/packages/console/src/components/Markdown/index.tsx b/packages/console/src/components/Markdown/index.tsx index 280795962..efc7b7b79 100644 --- a/packages/console/src/components/Markdown/index.tsx +++ b/packages/console/src/components/Markdown/index.tsx @@ -4,6 +4,7 @@ import ReactMarkdown from 'react-markdown'; import remarkGfm from 'remark-gfm'; import CodeEditor from '../CodeEditor'; +import GithubRawImage from './components/GithubRawImage'; import * as styles from './index.module.scss'; type Props = { @@ -27,6 +28,9 @@ const Markdown = ({ className, children }: Props) => ( ); }, + img: ({ src, alt }) => { + return ; + }, }} > {children}