From 4479d87130a0eda6017a9e1f0125d19b0f662b7f Mon Sep 17 00:00:00 2001 From: DaiQiangReal Date: Tue, 20 Aug 2024 18:19:41 +0800 Subject: [PATCH] feat: markdownRender support plugin --- content/plus/markdownrender/index-en-US.md | 6 ++++++ content/plus/markdownrender/index.md | 19 +++++++++++++------ .../markdownRender/foundation.ts | 12 +++++++++--- packages/semi-ui/markdownRender/index.tsx | 3 ++- 4 files changed, 30 insertions(+), 10 deletions(-) diff --git a/content/plus/markdownrender/index-en-US.md b/content/plus/markdownrender/index-en-US.md index a2ded01232..b0fc9a5f55 100644 --- a/content/plus/markdownrender/index-en-US.md +++ b/content/plus/markdownrender/index-en-US.md @@ -158,6 +158,10 @@ Just write JSX directly in Markdown ``` +# Add plugins + +Support all RemarkPlugin and RehypePlugins plugins of MDXJS through `remarkPlugins` `rehypePlugins`, please refer to [MDXJS](https://mdxjs.com/docs/extending-mdx/) for details + ### API @@ -167,6 +171,8 @@ Just write JSX directly in Markdown | components | Used to override Markdown elements and add custom components | Record | - | | format | The incoming raw type, whether it is pure Markdown | 'md'\|'mdx' | 'mdx' | | raw | plain text in Markdown or MDX | string | - | +| remarkPlugins | custom Remark Plugin | Remark Plugin Array | - | +| rehypePlugins | custom Rehype Plugin | Rehype Plugin Array | - | | style | style | CSSProperties | - | ## Design Token diff --git a/content/plus/markdownrender/index.md b/content/plus/markdownrender/index.md index ecf46b3695..ce2386d4a2 100644 --- a/content/plus/markdownrender/index.md +++ b/content/plus/markdownrender/index.md @@ -175,16 +175,23 @@ function Demo() { ``` +# 添加插件 + +通过 `remarkPlugins` `rehypePlugins` 支持 MDXJS 的所有 RemarkPlugin 和 RehypePlugins 插件,详情请参考 [MDXJS](https://mdxjs.com/docs/extending-mdx/) + + ### API -| 属性 | 说明 | 类型 | 默认值 | -|------------|----------------------------|---------------------------------------|-------| -| className | 类名 | string | - | +| 属性 | 说明 | 类型 | 默认值 | +|------------|----------------------------|--------------------------------------|-------| +| className | 类名 | string | - | | components | 用于覆盖 Markdown 元素,也可添加自定义组件 | Record | - | -| format | 传入的 raw 类型,是否是纯 Markdown | 'md'\|'mdx' | 'mdx' | -| raw | Markdown 或 MDX 的纯文本 | string | - | -| style | 样式 | CSSProperties | - | +| format | 传入的 raw 类型,是否是纯 Markdown | 'md'\|'mdx' | 'mdx' | +| raw | Markdown 或 MDX 的纯文本 | string | - | +| remarkPlugins | 自定义 Remark Plugin | Remark Plugin Array | - | +| rehypePlugins | 自定义 Rehype Plugin | Rehype Plugin Array | - | +| style | 样式 | CSSProperties | - | ## 设计变量 diff --git a/packages/semi-foundation/markdownRender/foundation.ts b/packages/semi-foundation/markdownRender/foundation.ts index 5bec633941..acf159a012 100644 --- a/packages/semi-foundation/markdownRender/foundation.ts +++ b/packages/semi-foundation/markdownRender/foundation.ts @@ -2,6 +2,7 @@ import BaseFoundation, { DefaultAdapter } from '../base/foundation'; import { CompileOptions, evaluate, compile, EvaluateOptions, evaluateSync, RunOptions } from '@mdx-js/mdx'; import { MDXProps } from 'mdx/types'; import remarkGfm from 'remark-gfm'; +import { type PluggableList } from "@mdx-js/mdx/lib/core"; export interface MarkdownRenderAdapter

, S = Record> extends DefaultAdapter { getRuntime: () => any @@ -13,7 +14,9 @@ export interface MarkdownRenderAdapter

, S = Record { private getOptions = ()=>{ return { evaluateOptions: { - remarkPlugins: [remarkGfm], + remarkPlugins: [remarkGfm, ...(this.getProp("remarkPlugins") ?? [])], + rehypePlugins: this.getProp("rehypePlugins") ?? [], format: this.getProp("format") }, compileOptions: { - format: this.getProp("format") + format: this.getProp("format"), + remarkPlugins: [remarkGfm, ...(this.getProp("remarkPlugins") ?? [])], + rehypePlugins: this.getProp("rehypePlugins") ?? [], }, runOptions: { } diff --git a/packages/semi-ui/markdownRender/index.tsx b/packages/semi-ui/markdownRender/index.tsx index 1a939a942c..80dfc19bdf 100644 --- a/packages/semi-ui/markdownRender/index.tsx +++ b/packages/semi-ui/markdownRender/index.tsx @@ -38,7 +38,8 @@ class MarkdownRender extends BaseComponent