WebブラウザのReact Component Preview Markdownテキスト。 GitHub Markdownスタイルを複製するための最小限のCSS。現在のドキュメントWebサイトは、このReactコンポーネントを使用して変換されます。
@v4$ npm install @uiw/react-markdown-preview --save import React from 'react' ;
import MarkdownPreview from '@uiw/react-markdown-preview' ;
const source = `
## MarkdownPreview
> todo: React component preview markdown text.
` ;
export default function Demo ( ) {
return (
< MarkdownPreview source = { source } style = { { padding : 16 } } />
)
} import React from 'react' ;
import MarkdownPreview from '@uiw/react-markdown-preview' ;
const source = `
## MarkdownPreview
## Header 2
### Header 3
` ;
export default function Demo ( ) {
return (
< MarkdownPreview
source = { source }
style = { { padding : 16 } }
rehypeRewrite = { ( node , index , parent ) => {
if ( node . tagName === "a" && parent && / ^h(1|2|3|4|5|6) / . test ( parent . tagName ) ) {
parent . children = parent . children . slice ( 1 )
}
} }
/>
) ;
} 構文: ```jsx {1,4-5}
import React from 'react' ;
import MarkdownPreview from '@uiw/react-markdown-preview' ;
const source = `
```js {2}
function () {
console.log('hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello')
}
```
```js {2}
function () {
console.log('hello ')
}
```
` ;
export default function Demo ( ) {
return (
< MarkdownPreview
source = { source }
style = { { padding : 16 } }
rehypeRewrite = { ( node , index , parent ) => {
if ( node . tagName === "a" && parent && / ^h(1|2|3|4|5|6) / . test ( parent . tagName ) ) {
parent . children = parent . children . slice ( 1 )
}
} }
/>
) ;
} 構文: ```jsx showLineNumbers {1,4-5}
import React from 'react' ;
import MarkdownPreview from '@uiw/react-markdown-preview' ;
const source = `
```js showLineNumbers
function () {
console.log('hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello')
}
```
```js showLineNumbers {2}
function () {
console.log('hello ')
}
```
` ;
export default function Demo ( ) {
return (
< MarkdownPreview
source = { source }
style = { { padding : 16 } }
rehypeRewrite = { ( node , index , parent ) => {
if ( node . tagName === "a" && parent && / ^h(1|2|3|4|5|6) / . test ( parent . tagName ) ) {
parent . children = parent . children . slice ( 1 )
}
} }
/>
) ;
} import React from 'react' ;
import MarkdownPreview from '@uiw/react-markdown-preview' ;
const source = `
```js
function () {
console.log('hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello')
}
```
```js
function () {
console.log('hello ')
}
```
` ;
export default function Demo ( ) {
return (
< MarkdownPreview source = { source } style = { { padding : 16 } } />
) ;
} 次の例は、コードをバンドルに含めることからコードを強調表示するコードを除外するのに役立ちます。 @uiw/react-markdown-preview/nohighlightコンポーネントには、パッケージ、 showLineNumbers 、およびhighlight line関数が機能しなくなるrehype-prism-plusコードが含まれていません。 (#586)
import React from 'react' ;
import MarkdownPreview from '@uiw/react-markdown-preview/nohighlight' ;
const source = `
```js
function () {
console.log('hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello')
}
```
```js
function () {
console.log('hello ')
}
```
` ;
export default function Demo ( ) {
return (
< MarkdownPreview
source = { source }
style = { { padding : 16 } }
rehypeRewrite = { ( node , index , parent ) => {
if ( node . tagName === "a" && parent && / ^h(1|2|3|4|5|6) / . test ( parent . tagName ) ) {
parent . children = parent . children . slice ( 1 )
}
} }
/>
) ;
} HTMLで除外されたGitHub ReadMeに示されているHTMLコメントを介してコンテンツディスプレイを無視します。
import React from 'react' ;
import MarkdownPreview from '@uiw/react-markdown-preview' ;
const source = `
<!--rehype:ignore:start-->
Content ignored
<!--rehype:ignore:end-->
Some content is ignored, please check the source code
` ;
export default function Demo ( ) {
return (
< MarkdownPreview
source = { source }
style = { { padding : 16 } }
rehypeRewrite = { ( node , index , parent ) => {
if ( node . tagName === "a" && parent && / ^h(1|2|3|4|5|6) / . test ( parent . tagName ) ) {
parent . children = parent . children . slice ( 1 )
}
} }
/>
) ;
} <!-- rehype:ignore:start --> Ignored content <!-- rehype:ignore:end --> Katexは、Web上でTex Mathレンダリングのための高速で使いやすいJavaScriptライブラリであり、 KaTeXを通じて数学レンダリングを実行します。
npm install katex import React from 'react' ;
import MarkdownPreview from '@uiw/react-markdown-preview' ;
import { getCodeString } from 'rehype-rewrite' ;
import katex from 'katex' ;
import 'katex/dist/katex.css' ;
const source = `This is to display the
`$$c = \pm\sqrt{a^2 + b^2}$$`
in one line
```KaTeX
c = \pm\sqrt{a^2 + b^2}
```
` ;
export default function Demo ( ) {
const [ value , setValue ] = React . useState ( source ) ;
return (
< MarkdownPreview
source = { source }
style = { { padding : 16 } }
components = { {
code : ( { children = [ ] , className , ... props } ) => {
if ( typeof children === 'string' && / ^$$(.*)$$ / . test ( children ) ) {
const html = katex . renderToString ( children . replace ( / ^$$(.*)$$ / , '$1' ) , {
throwOnError : false ,
} ) ;
return < code dangerouslySetInnerHTML = { { __html : html } } style = { { background : 'transparent' } } /> ;
}
const code = props . node && props . node . children ? getCodeString ( props . node . children ) : children ;
if (
typeof code === 'string' &&
typeof className === 'string' &&
/ ^language-katex / . test ( className . toLocaleLowerCase ( ) )
) {
const html = katex . renderToString ( code , {
throwOnError : false ,
} ) ;
return < code style = { { fontSize : '150%' } } dangerouslySetInnerHTML = { { __html : html } } /> ;
}
return < code className = { String ( className ) } > { children } </ code > ;
} ,
} }
/>
) ;
} マークダウンと同様の方法でテキストから図とフローチャートの生成に人魚を使用する
import React , { useState , useRef , useEffect , Fragment , useCallback } from "react" ;
import MarkdownPreview from '@uiw/react-markdown-preview' ;
import { getCodeString } from 'rehype-rewrite' ;
import mermaid from "mermaid" ;
const randomid = ( ) => parseInt ( String ( Math . random ( ) * 1e15 ) , 10 ) . toString ( 36 ) ;
const Code = ( { inline , children = [ ] , className , ... props } ) => {
const demoid = useRef ( `dome ${ randomid ( ) } ` ) ;
const [ container , setContainer ] = useState ( null ) ;
const isMermaid = className && / ^language-mermaid / . test ( className . toLocaleLowerCase ( ) ) ;
const code = props . node && props . node . children ? getCodeString ( props . node . children ) : children [ 0 ] || '' ;
const reRender = async ( ) => {
if ( container && isMermaid ) {
try {
const str = await mermaid . render ( demoid . current , code ) ;
container . innerHTML = str . svg ;
} catch ( error ) {
container . innerHTML = error ;
}
}
}
useEffect ( ( ) => {
reRender ( )
} , [ container , isMermaid , code , demoid ] ) ;
const refElement = useCallback ( ( node ) => {
if ( node !== null ) {
setContainer ( node ) ;
}
} , [ ] ) ;
if ( isMermaid ) {
return (
< Fragment >
< code id = { demoid . current } style = { { display : "none" } } />
< code ref = { refElement } data-name = "mermaid" />
</ Fragment >
) ;
}
return < code > { children } </ code > ;
} ;
const source = `The following are some examples of the diagrams, charts and graphs that can be made using Mermaid and the Markdown-inspired text specific to it.
```mermaid
graph TD
A[Hard] -->|Text| B(Round)
B --> C{Decision}
C -->|One| D[Result 1]
C -->|Two| E[Result 2]
```
```mermaid
sequenceDiagram
Alice->>John: Hello John, how are you?
loop Healthcheck
John->>John: Fight against hypochondria
end
Note right of John: Rational thoughts!
John-->>Alice: Great!
John->>Bob: How about you?
Bob-->>John: Jolly good!
```
` ;
// const source = `
// ```mermaid
// graph TD;
// A-->B;
// A-->C;
// B-->D;
// C-->D;
// ```
// `;
export default function Demo ( ) {
return (
< MarkdownPreview
source = { source }
style = { { padding : 16 } }
components = { {
code : Code
} }
/>
) ;
} 著者を完全に信頼していない場合は、マークダウンを消毒する必要があることに注意してください。それ以外の場合、アプリはXSSに対して脆弱です。これは、Rehype-Sanitizeをプラグインとして追加することで実現できます。
import React from 'react' ;
import rehypeSanitize from "rehype-sanitize" ;
import MarkdownPreview from '@uiw/react-markdown-preview' ;
const source = `
## MarkdownPreview
**Hello world!!!** <IFRAME SRC="javascript:javascript:alert(window.origin);"></IFRAME>
<!-- test --> 123
<!-- test --> 456 <!-- test -->
` ;
const rehypePlugins = [ rehypeSanitize ] ;
export default function Demo ( ) {
return (
< MarkdownPreview source = { source } rehypePlugins = { rehypePlugins } style = { { padding : 16 } } />
)
} import { ReactMarkdownProps } from 'react-markdown' ;
import { RehypeRewriteOptions } from 'rehype-rewrite' ;
type MarkdownPreviewProps = {
prefixCls ?: string ;
className ?: string ;
source ?: string ;
disableCopy ?: boolean ;
style ?: React . CSSProperties ;
pluginsFilter ?: ( type : 'rehype' | 'remark' , plugin : PluggableList ) => PluggableList ;
wrapperElement ?: React . DetailedHTMLProps < React . HTMLAttributes < HTMLDivElement > , HTMLDivElement > & {
'data-color-mode' ?: 'light' | 'dark' ;
} ;
onScroll ?: ( e : React . UIEvent < HTMLDivElement > ) => void ;
onMouseOver ?: ( e : React . MouseEvent < HTMLDivElement > ) => void ;
rehypeRewrite ?: RehypeRewriteOptions [ 'rewrite' ] ;
} & ReactMarkdownProps ;source ( string 、デフォルト: '' )className ( string? )divにマークダウンを包むこのReactMarkdownProps詳細。アップグレードreact-markdown V9
children ( string 、デフォルト: '' )className ( string? )divにマークダウンを包むskipHtml ( boolean 、デフォルト:falsetrue )allowElement ( (element, index, parent) => boolean?オプション)allowedElements / disallowedElementsが最初に使用されます!remarkPlugins ( Array.<Plugin> 、default: [] )rehypePlugins ( Array.<Plugin> 、デフォルト: [] )注記
react-markdownアップグレードV8 V9へ
urlTransformを追加しますtransformImageUriとtransformLinkUriが除去されました。 2つの機能を持つことは少し多くなります。特に、変更したい可能性のあるURLが増えているためです(または安全でない可能性があるため、安全にします)。そして、彼らの名前とAPIは少し奇妙でした。代わりに新しいurlTransform Propを使用して、すべてのURLを変更できます。
linkTargetを削除しますlinkTargetオプションが削除されました。ターゲットを設定しないでください。必要に応じて、 rehype-external-linksを使用してください。
includeElementIndexを削除しますincludeElementIndexオプションが削除されたため、 indexコンポーネントに渡されることはありません。 indexを渡すプラグインを書きます:
import { visit } from 'unist-util-visit'
function rehypePluginAddingIndex ( ) {
/**
* @param {import('hast').Root} tree
* @returns {undefined}
*/
return function ( tree ) {
visit ( tree , function ( node , index ) {
if ( node . type === 'element' && typeof index === 'number' ) {
node . properties . index = index
}
} )
}
}rawSourcePosを削除しますrawSourcePosオプションは削除されたため、 sourcePosコンポーネントに渡されることはありません。すべてのコンポーネントは通過したnodeであるため、 node.positionを取得できます。
sourcePosを削除しますsourcePosオプションが削除されたため、 data-sourcepos要素に渡されることはありません。 indexを渡すプラグインを書きます:
import { stringifyPosition } from 'unist-util-stringify-position'
import { visit } from 'unist-util-visit'
function rehypePluginAddingIndex ( ) {
/**
* @param {import('hast').Root} tree
* @returns {undefined}
*/
return function ( tree ) {
visit ( tree , function ( node ) {
if ( node . type === 'element' ) {
node . properties . dataSourcepos = stringifyPosition ( node . position )
}
} )
}
}コンポーネントを上書きすると、これらの小道具は渡されなくなりました。
codeにinline - プラグインを作成するか、ブロックのpreを使用しますh1 、 h2 、 h3 、 h4 、 h5 、 h6のlevel - 代わりにnode.tagNameをチェックします。liでchecked - task-list-itemクラスを確認するか、 props.children確認してください。liのindex - プラグインを作成しますliでordered - プラグインを作成するか、親を確認しますol 、 ulのdepth - プラグインを作成しますol 、 ulでordered - 代わりにnode.tagNameを確認しますtdのisHeader 、 th - 代わりにnode.tagNameを確認しますtrのisHeader - プラグインを作成するか、子供をチェックしてくださいHTMLコメント<!--rehype:xxx-->を使用して、マークダウンスタイルのカスタマイズをサポートします。
## Title
<!-- rehype:style=display: flex; height: 230px; align-items: center; justify-content: center; font-size: 38px; -->
Markdown Supports ** Style ** <!-- rehype:style=color: red; -->Here is a simple footnote [ ^ 1 ] . With some additional text after it.
[ ^ 1 ] : My reference. # Hello World
<!-- rehype:ignore:start --> Hello World <!-- rehype:ignore:end -->
Good!出力:
< h1 > Hello World </ h1 >
< p > Good! </ p > import React from 'react' ;
import MarkdownPreview from '@uiw/react-markdown-preview' ;
const source = `>
>
> [!NOTE]
> Useful information that users should know, even when skimming content.
> [!TIP]
> Helpful advice for doing things better or more easily.
> [!IMPORTANT]
> Key information users need to know to achieve their goal.
> [!WARNING]
> Urgent info that needs immediate user attention to avoid problems.
> [!CAUTION]
> Advises about risks or negative outcomes of certain actions.
` ;
export default function Demo ( ) {
return (
< MarkdownPreview source = { source } style = { { padding : 16 } } />
)
} デフォルトでは、 dark-modeシステムに応じて自動的に切り替えられます。手動で切り替える必要がある場合は、ボディのdata-color-mode="dark"パラメーターを設定するだけです。
< html data-color-mode =" dark " > document . documentElement . setAttribute ( 'data-color-mode' , 'dark' )
document . documentElement . setAttribute ( 'data-color-mode' , 'light' ) .wmde-markdown-varセレクターを追加して、カスタムカラー変数を継承します。
const Demo = ( ) => {
return (
< div >
< div className = "wmde-markdown-var" > </ div >
< MarkdownPreview source = "Hello World!" />
</ div >
)
} lightテーマを設定します。
<MarkdownPreview
source="Hello World!"
wrapperElement={{
+ "data-color-mode": "light"
}}
/>開発モードでプロジェクトを実行します。
# Step 1, run first,
# listen to the component compile and output the .js file
# listen for compilation output type .d.ts file
# listen to the component compile and output the .css file
npm run start
# Step 2, development mode, listen to compile preview website instance
npm run docBuildフォルダーへの生産用のアプリをビルドします。
npm run buildビルドが縮小され、ファイル名にはハッシュが含まれます。あなたのアプリは展開する準備ができています!
より多くの機能が豊富なMarkdownエディターが必要な場合は、 @uiwjs/React-Markdown-Editorを使用できます
いつものように、私たちの素晴らしい貢献者に感謝します!
アクションコントリビュータ師で作られています。
MITライセンスに基づいてライセンスされています。