Skip to content

Commit

Permalink
Merge pull request #122 from ant-design/fix/MarkdownMemo
Browse files Browse the repository at this point in the history
🐛 fix: fix markdown & highlight not support memo
  • Loading branch information
rdmclin2 authored Dec 18, 2023
2 parents b618cda + fd9ff8c commit 4b62051
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 78 deletions.
6 changes: 3 additions & 3 deletions src/Highlight/components/HighLightJS/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 优先支持主流语言,没有import在代码中使用的不会打包
*/
import classNames from 'classnames';
import { useEffect, useState } from 'react';
import { memo, useEffect, useState } from 'react';
import { HighlightProps } from '../../defalut';
import { useHighlight } from '../../hooks/useHighlight';
import { THEME_LIGHT } from '../../theme';
Expand All @@ -17,7 +17,7 @@ export type HighLighJSProps = Pick<
'language' | 'children' | 'theme' | 'prefixCls' | 'lineNumber'
>;

const HighLighJS: React.FC<HighLighJSProps> = (props) => {
const HighLighJS: React.FC<HighLighJSProps> = memo((props) => {
const { children, lineNumber = false, theme = THEME_LIGHT, language, prefixCls } = props;
const [codeBlock, setCodeBlock] = useState(null);
const { styles } = useStyles(theme);
Expand Down Expand Up @@ -60,6 +60,6 @@ const HighLighJS: React.FC<HighLighJSProps> = (props) => {
</table>
</pre>
);
};
});

export default HighLighJS;
5 changes: 3 additions & 2 deletions src/Highlight/components/HighLighter/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { THEME_LIGHT } from '@/Highlight/theme';
import { STUDIO_UI_PREFIX } from '@/theme';
import { Loading3QuartersOutlined as Loading } from '@ant-design/icons';
import classNames from 'classnames';
import { memo } from 'react';
import { Center } from 'react-layout-kit';
import { HighlightProps } from '../../defalut';
import { useShiki } from '../../hooks/useShiki';
Expand All @@ -19,7 +20,7 @@ export type ShikiProps = Pick<
'language' | 'children' | 'theme' | 'prefixCls' | 'lineNumber'
>;

const HighLighter: React.FC<ShikiProps> = (props) => {
const HighLighter: React.FC<ShikiProps> = memo((props) => {
const {
children,
lineNumber = false,
Expand Down Expand Up @@ -52,6 +53,6 @@ const HighLighter: React.FC<ShikiProps> = (props) => {
) : null}
</>
);
};
});

export default HighLighter;
6 changes: 3 additions & 3 deletions src/Highlight/defalut.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import classNames from 'classnames';
import { createRef } from 'react';
import { createRef, memo } from 'react';
import { getPrefixCls } from '../theme';
import CopyButton from './components/CopyButton';
import HighLighter from './components/HighLighter';
Expand Down Expand Up @@ -72,7 +72,7 @@ export interface HighlightProps {
showLanguage?: boolean;
}

const HighlightBase: React.FC<HighlightProps> = (props) => {
const HighlightBase: React.FC<HighlightProps> = memo((props) => {
const {
children,
style,
Expand Down Expand Up @@ -118,6 +118,6 @@ const HighlightBase: React.FC<HighlightProps> = (props) => {
</div>
</>
);
};
});

export { HighlightBase };
5 changes: 3 additions & 2 deletions src/Highlight/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { memo } from 'react';
import { ConfigProvider } from '../ConfigProvider';
import { HighlightBase, HighlightProps } from './defalut';
import { useStyles } from './style';
import FullFeatureWrapper from './wrapper';

const Highlight = (props: HighlightProps) => {
const Highlight = memo<HighlightProps>((props: HighlightProps) => {
const { prefixCls, type, theme, containerWrapper } = props;
const { theme: token } = useStyles({
prefixCls,
Expand All @@ -28,7 +29,7 @@ const Highlight = (props: HighlightProps) => {
{containerWrapper ? <FullFeatureWrapper {...props} /> : <HighlightBase {...props} />}
</ConfigProvider>
);
};
});

export * from './defalut';
export * from './wrapper';
Expand Down
8 changes: 3 additions & 5 deletions src/Markdown/CodeBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const countLines = (str: string): number => {
return matches ? matches.length : 1;
};

const Code = memo(({ highlight, snippet, ...properties }: any) => {
export const Code = memo((properties: any) => {
const { styles, cx } = useStyles();

if (!properties.children[0]) return;
Expand All @@ -39,6 +39,7 @@ const Code = memo(({ highlight, snippet, ...properties }: any) => {

const content = Array.isArray(children) ? (children[0] as string) : children;
const lang = className?.replace('language-', '') || 'txt';

if (countLines(content) === 1 && content.length <= 60) {
return (
<Snippet
Expand All @@ -47,7 +48,6 @@ const Code = memo(({ highlight, snippet, ...properties }: any) => {
language={lang}
symbol={''}
type={'block'}
{...snippet}
>
{content}
</Snippet>
Expand All @@ -59,11 +59,9 @@ const Code = memo(({ highlight, snippet, ...properties }: any) => {
className={cx(styles.container, styles.highlight)}
language={lang}
type="block"
{...highlight}
containerWrapper={true}
>
{content}
</Highlight>
);
});

export { Code };
10 changes: 1 addition & 9 deletions src/Markdown/demos/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,5 @@ import { Markdown } from '@ant-design/pro-editor';
import { content } from './data';

export default () => {
return (
<Markdown
highlight={{
containerWrapper: true,
}}
>
{content}
</Markdown>
);
return <Markdown>{content}</Markdown>;
};
14 changes: 6 additions & 8 deletions src/Markdown/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@ You can also customize it by passing in props like components to React-Markdown,

## APIs

| Property | Type | Description |
| ------------- | --------------- | --------------------------------------------------------------- |
| children | string | The Markdown content to render. |
| className | string | The class name of the Markdown component. |
| onDoubleClick | () => void | Double-click event handler. |
| style | CSSProperties | The style of the Markdown component. |
| highlight | Highlight Props | Configuration for Highlight, will be passed through by default. |
| snippet | Snippet Props | Configuration for Snippet, will be passed through by default. |
| Property | Type | Description |
| ------------- | ------------- | ----------------------------------------- |
| children | string | The Markdown content to render. |
| className | string | The class name of the Markdown component. |
| onDoubleClick | () => void | Double-click event handler. |
| style | CSSProperties | The style of the Markdown component. |
61 changes: 23 additions & 38 deletions src/Markdown/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import rehypeKatex from 'rehype-katex';
import remarkGfm from 'remark-gfm';
import remarkMath from 'remark-math';

import { HighlightProps, SnippetProps } from '@ant-design/pro-editor';
import { PluggableList } from 'react-markdown/lib/react-markdown';
import { Code } from './CodeBlock';
import { useStyles } from './style';
Expand All @@ -16,47 +15,33 @@ export interface MarkdownProps {
* @description ClassName
*/
className?: string;
fullFeaturedCodeBlock?: boolean;
onDoubleClick?: () => void;
style?: CSSProperties;
// Highlight 的配置,会默认透传
highlight?: HighlightProps;
// Snippet 的配置,会默认透传
snippet?: SnippetProps;
}

const Markdown = memo<MarkdownProps>(
({ children, className, style, onDoubleClick, highlight = {}, snippet = {}, ...rest }) => {
const { styles } = useStyles();
const components: any = {
a: Typography.Link,
details: Collapse,
hr: () => <Divider style={{ marginBottom: '1em', marginTop: 0 }} />,
img: Image,
pre: (props) => {
const { children, ...rest } = props;
return (
<Code highlight={highlight} snippet={snippet} {...rest}>
{children}
</Code>
);
},
};
const Markdown = memo<MarkdownProps>(({ children, className, style, onDoubleClick, ...rest }) => {
const { styles } = useStyles();
const components: any = {
a: Typography.Link,
details: Collapse,
hr: () => <Divider style={{ marginBottom: '1em', marginTop: 0 }} />,
img: Image,
pre: Code,
};

return (
<Typography className={className} onDoubleClick={onDoubleClick} style={style}>
<ReactMarkdown
className={styles.markdown}
components={components}
rehypePlugins={[rehypeKatex] as PluggableList}
remarkPlugins={[remarkGfm, remarkMath]}
{...rest}
>
{children}
</ReactMarkdown>
</Typography>
);
},
);
return (
<Typography className={className} onDoubleClick={onDoubleClick} style={style}>
<ReactMarkdown
className={styles.markdown}
components={components}
rehypePlugins={[rehypeKatex] as PluggableList}
remarkPlugins={[remarkGfm, remarkMath]}
{...rest}
>
{children}
</ReactMarkdown>
</Typography>
);
});

export default Markdown;
14 changes: 6 additions & 8 deletions src/Markdown/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@ ProEditor 内置了一个默认的 Markdown 渲染器,使用 React-Markdown,

## APIs

| 属性名 | 类型 | 描述 |
| ------------- | --------------- | ------------------------------ |
| children | string | 要渲染的 Markdown 内容。 |
| className | string | Markdown 组件的类名。 |
| onDoubleClick | () => void | 双击事件处理函数。 |
| style | CSSProperties | Markdown 组件的样式。 |
| highlight | Highlight Props | Highlight 的配置,会默认透传。 |
| snippet | snippet Props | Snippet 的配置,会默认透传 |
| 属性名 | 类型 | 描述 |
| ------------- | ------------- | ------------------------ |
| children | string | 要渲染的 Markdown 内容。 |
| className | string | Markdown 组件的类名。 |
| onDoubleClick | () => void | 双击事件处理函数。 |
| style | CSSProperties | Markdown 组件的样式。 |

0 comments on commit 4b62051

Please sign in to comment.