Skip to content

Commit

Permalink
♻️ feat: reflact some components props & api
Browse files Browse the repository at this point in the history
  • Loading branch information
ONLY-yours committed Nov 22, 2023
1 parent 71ffd2b commit 1b14b65
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/Highlight/defalut.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export interface HighlightProps {
* @renderType select
* @default "typescript"
*/
language: string;
language?: string;
/**
* @title 主题
* @description 主题颜色, dark 黑色主题,light 白色主题
Expand Down
2 changes: 1 addition & 1 deletion src/Highlight/wrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export interface HighlighterWrapperProps extends DivProps {
/**
* @description The language of the code content
*/
language: string;
language?: string;
}
const options: SelectProps['options'] = Object.keys(languageMap).map((key) => ({
label: key,
Expand Down
13 changes: 4 additions & 9 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(({ fullFeatured, ...properties }: any) => {
const Code = memo(({ highlight, snippet, ...properties }: any) => {
const { styles, cx } = useStyles();

if (!properties.children[0]) return;
Expand All @@ -47,6 +47,7 @@ const Code = memo(({ fullFeatured, ...properties }: any) => {
language={lang}
symbol={''}
type={'block'}
{...snippet}
>
{content}
</Snippet>
Expand All @@ -56,19 +57,13 @@ const Code = memo(({ fullFeatured, ...properties }: any) => {
return (
<Highlight
className={cx(styles.container, styles.highlight)}
containerWrapper={fullFeatured}
language={lang}
type="block"
{...highlight}
>
{content}
</Highlight>
);
});

export const CodeLite = memo((properties: any) => {
return <Code {...properties} />;
});

export const CodeFullFeatured = memo((properties: any) => {
return <Code fullFeatured {...properties} />;
});
export { Code };
10 changes: 9 additions & 1 deletion src/Markdown/demos/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,13 @@ import { Markdown } from '@ant-design/pro-editor';
import { content } from './data';

export default () => {
return <Markdown>{content}</Markdown>;
return (
<Markdown
highlight={{
containerWrapper: true,
}}
>
{content}
</Markdown>
);
};
24 changes: 15 additions & 9 deletions src/Markdown/index.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
---
nav: 组件
group: Content
title: Markdown
group: 基础组件
title: Markdown 文档展示
atomId: Markdown
description: Markdown是一个用于渲染Markdown文本的React组件。它支持各种Markdown语法,如标题、列表、链接、图片、代码块等。它通常用于文档、博客和其他文本密集型应用中。
---

## Default

ProEditor 内置了一个默认的 Markdown 渲染器,使用 React-Markdown,使用我们自己的 Highlight 和 Snippet 进行代码块的渲染

你也可以通过自己传入 components 等 React-Markdown 的 Props 来进行自定义,多余的会透传过去。

<code src="./demos/index.tsx" nopadding></code>

## APIs

| 属性名 | 类型 | 描述 |
| --------------------- | ------------- | -------------------------- |
| children | string | 要渲染的 Markdown 内容。 |
| className | string | Markdown 组件的类名。 |
| fullFeaturedCodeBlock | boolean | 是否启用完整功能的代码块。 |
| onDoubleClick | () => void | 双击事件处理函数。 |
| style | CSSProperties | Markdown 组件的样式。 |
| 属性名 | 类型 | 描述 |
| ------------- | --------------- | ------------------------------ |
| children | string | 要渲染的 Markdown 内容。 |
| className | string | Markdown 组件的类名。 |
| onDoubleClick | () => void | 双击事件处理函数。 |
| style | CSSProperties | Markdown 组件的样式。 |
| highlight | Highlight Props | Highlight 的配置,会默认透传。 |
| snippet | snippet Props | Snippet 的配置,会默认透传 |
27 changes: 18 additions & 9 deletions src/Markdown/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,41 @@ import rehypeKatex from 'rehype-katex';
import remarkGfm from 'remark-gfm';
import remarkMath from 'remark-math';

import { CodeFullFeatured, CodeLite } from './CodeBlock';
import { HighlightProps, SnippetProps } from '@ant-design/pro-editor';
import { Code } from './CodeBlock';
import { useStyles } from './style';

export interface MarkdownProps {
/**
* @description The markdown content to be rendered
*/
children: string;
/**
* @description The class name for the Markdown component
* @description ClassName
*/
className?: string;
fullFeaturedCodeBlock?: boolean;
onDoubleClick?: () => void;
style?: CSSProperties;
// Highlight 的配置,会默认透传
highlight?: HighlightProps;
// Snippet 的配置,会默认透传
snippet?: SnippetProps;
}

const Markdown = memo<MarkdownProps>(
({ children, className, style, fullFeaturedCodeBlock = true, onDoubleClick, ...props }) => {
({ 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: fullFeaturedCodeBlock ? CodeFullFeatured : CodeLite,
pre: (props) => {
const { children, ...rest } = props;
return (
<Code highlight={highlight} snippet={snippet} {...rest}>
{children}
</Code>
);
},
};

return (
Expand All @@ -42,7 +51,7 @@ const Markdown = memo<MarkdownProps>(
className={styles.markdown}
components={components}
remarkPlugins={[remarkGfm]}
{...props}
{...rest}
>
{children}
</ReactMarkdown>
Expand All @@ -53,7 +62,7 @@ const Markdown = memo<MarkdownProps>(
components={components}
rehypePlugins={[rehypeKatex]}
remarkPlugins={[remarkGfm, remarkMath]}
{...props}
{...rest}
>
{children}
</ReactMarkdown>
Expand Down

0 comments on commit 1b14b65

Please sign in to comment.