UNPKG

@uiw/react-markdown-preview-example

Version:

Preview the markdown files and run the React examples in the documentation.

316 lines (265 loc) • 16.9 kB
<div markdown="1"> <sup align="center">Using <a href="https://wangchujiang.com/#/app" target="_blank">my app</a> is also a way to <a href="https://wangchujiang.com/#/sponsor" target="_blank">support</a> me:</sup> <br> <a target="_blank" href="https://apps.apple.com/app/6758053530" title="Scap: Screenshot & Markup Edit for macOS"><img alt="Scap: Screenshot & Markup Edit" height="52" width="52" src="https://wangchujiang.com/appicon/scap.png"></a> <a target="_blank" href="https://apps.apple.com/app/6757317079" title="Screen Test for macOS"><img alt="Screen Test" height="52" width="52" src="https://wangchujiang.com/appicon/screen-test.png"></a> <a target="_blank" href="https://apps.apple.com/app/Deskmark/6755948110" title="Deskmark for macOS"><img alt="Deskmark" height="52" width="52" src="https://wangchujiang.com/appicon/deskmark.png"></a> <a target="_blank" href="https://apps.apple.com/app/Keyzer/6500434773" title="Keyzer for macOS"><img alt="Keyzer" height="52" width="52" src="https://wangchujiang.com/appicon/keyzer.png"></a> <a target="_blank" href="https://github.com/jaywcjlove/vidwall-hub" title="Vidwall Hub for macOS"><img alt="Vidwall Hub" height="52" width="52" src="https://wangchujiang.com/appicon/vidwall-hub.png"></a> <a target="_blank" href="https://apps.apple.com/app/VidCrop/6752624705" title="VidCrop for macOS"><img alt="VidCrop" height="52" width="52" src="https://wangchujiang.com/appicon/vidcrop.png"></a> <a target="_blank" href="https://apps.apple.com/app/Vidwall/6747587746" title="Vidwall for macOS"><img alt="Vidwall" height="52" width="52" src="https://wangchujiang.com/appicon/vidwall.png"></a> <a target="_blank" href="https://wangchujiang.com/mousio-hint/" title="Mousio Hint for macOS"><img alt="Mousio Hint" height="52" width="52" src="https://wangchujiang.com/appicon/mousio-hint.png"></a> <a target="_blank" href="https://apps.apple.com/app/6746747327" title="Mousio for macOS"><img alt="Mousio" height="52" width="52" src="https://wangchujiang.com/appicon/mousio.png"></a> <a target="_blank" href="https://apps.apple.com/app/6745227444" title="Musicer for macOS"><img alt="Musicer" height="52" width="52" src="https://wangchujiang.com/appicon/musicer.png"></a> <a target="_blank" href="https://apps.apple.com/app/6743841447" title="Audioer for macOS"><img alt="Audioer" height="52" width="52" src="https://wangchujiang.com/appicon/audioer.png"></a> <a target="_blank" href="https://apps.apple.com/app/6744690194" title="FileSentinel for macOS"><img alt="FileSentinel" height="52" width="52" src="https://wangchujiang.com/appicon/file-sentinel.png"></a> <a target="_blank" href="https://apps.apple.com/app/6743495172" title="FocusCursor for macOS"><img alt="FocusCursor" height="52" width="52" src="https://wangchujiang.com/appicon/focus-cursor.png"></a> <a target="_blank" href="https://apps.apple.com/app/6742680573" title="Videoer for macOS"><img alt="Videoer" height="52" width="52" src="https://wangchujiang.com/appicon/videoer.png"></a> <a target="_blank" href="https://apps.apple.com/app/6740425504" title="KeyClicker for macOS"><img alt="KeyClicker" height="52" width="52" src="https://wangchujiang.com/appicon/key-clicker.png"></a> <a target="_blank" href="https://apps.apple.com/app/6739052447" title="DayBar for macOS"><img alt="DayBar" height="52" width="52" src="https://wangchujiang.com/appicon/daybar.png"></a> <a target="_blank" href="https://apps.apple.com/app/6739444407" title="Iconed for macOS"><img alt="Iconed" height="52" width="52" src="https://wangchujiang.com/appicon/iconed.png"></a> <a target="_blank" href="https://apps.apple.com/app/6737160756" title="Menuist for macOS"><img alt="Menuist" height="52" width="52" src="https://wangchujiang.com/appicon/rightmenu-master.png"></a> <a target="_blank" href="https://apps.apple.com/app/6723903021" title="Paste Quick for macOS"><img alt="Quick RSS" height="52" width="52" src="https://wangchujiang.com/appicon/paste-quick.png"></a> <a target="_blank" href="https://apps.apple.com/app/6670696072" title="Quick RSS for macOS/iOS"><img alt="Quick RSS" height="52" width="52" src="https://wangchujiang.com/appicon/quick-rss.png"></a> <a target="_blank" href="https://apps.apple.com/app/6670167443" title="Web Serve for macOS"><img alt="Web Serve" height="52" width="52" src="https://wangchujiang.com/appicon/web-serve.png"></a> <a target="_blank" href="https://apps.apple.com/app/6503953628" title="Copybook Generator for macOS/iOS"><img alt="Copybook Generator" height="52" width="52" src="https://wangchujiang.com/appicon/copybook-generator.png"></a> <a target="_blank" href="https://apps.apple.com/app/6471227008" title="DevTutor for macOS/iOS"><img alt="DevTutor for SwiftUI" height="52" width="52" src="https://wangchujiang.com/appicon/devtutor.png"></a> <a target="_blank" href="https://apps.apple.com/app/6479819388" title="RegexMate for macOS/iOS"><img alt="RegexMate" height="52" width="52" src="https://wangchujiang.com/appicon/regex-mate.png"></a> <a target="_blank" href="https://apps.apple.com/app/6479194014" title="Time Passage for macOS/iOS"><img alt="Time Passage" height="52" width="52" src="https://wangchujiang.com/appicon/time-passage.png"></a> <a target="_blank" href="https://apps.apple.com/app/6478772538" title="IconizeFolder for macOS"><img alt="Iconize Folder" height="52" width="52" src="https://wangchujiang.com/appicon/iconize-folder.png"></a> <a target="_blank" href="https://apps.apple.com/app/6478511402" title="Textsound Saver for macOS/iOS"><img alt="Textsound Saver" height="52" width="52" src="https://wangchujiang.com/appicon/textsound-saver.png"></a> <a target="_blank" href="https://apps.apple.com/app/6476924627" title="Create Custom Symbols for macOS"><img alt="Create Custom Symbols" height="52" width="52" src="https://wangchujiang.com/appicon/create-custom-symbols.png"></a> <a target="_blank" href="https://apps.apple.com/app/6476452351" title="DevHub for macOS"><img alt="DevHub" height="52" width="52" src="https://wangchujiang.com/appicon/devhub.png"></a> <a target="_blank" href="https://apps.apple.com/app/6476400184" title="Resume Revise for macOS"><img alt="Resume Revise" height="52" width="52" src="https://wangchujiang.com/appicon/resume-revise.png"></a> <a target="_blank" href="https://apps.apple.com/app/6472593276" title="Palette Genius for macOS"><img alt="Palette Genius" height="52" width="52" src="https://wangchujiang.com/appicon/palette-genius.png"></a> <a target="_blank" href="https://apps.apple.com/app/6470879005" title="Symbol Scribe for macOS"><img alt="Symbol Scribe" height="52" width="52" src="https://wangchujiang.com/appicon/symbol-scribe.png"></a> </div> <hr> <!--rehype:ignore:start--> @uiw/react-markdown-preview-example === <!--rehype:ignore:end--> [![Buy me a coffee](https://img.shields.io/badge/Buy_Me_a_Coffee-ffdd00?logo=buy-me-a-coffee&logoColor=black)](https://jaywcjlove.github.io/#/sponsor) [![Follow On X](https://img.shields.io/badge/Follow%20on%20X-333333?logo=x&logoColor=white)](https://x.com/jaywcjlove) [![CI](https://github.com/uiwjs/react-markdown-preview-example/actions/workflows/ci.yml/badge.svg)](https://github.com/uiwjs/react-markdown-preview-example/actions/workflows/ci.yml) [![npm version](https://img.shields.io/npm/v/@uiw/react-markdown-preview-example.svg)](https://www.npmjs.com/package/@uiw/react-markdown-preview-example) [![NPM Downloads](https://img.shields.io/npm/dm/@uiw/react-markdown-preview-example.svg?style=flat&label=)](https://www.npmjs.com/package/@uiw/react-markdown-preview-example) [![react@^18](https://shields.io/badge/react-^18-green?style=flat&logo=react)](https://github.com/facebook/react/releases) Preview the markdown files and run the React examples in the documentation. ## Features šŸ“š Use Typescript to write, better code hints. šŸŒ’ Support dark/light mode šŸ‹šŸ¾ā€ā™‚ļø Support [GFM](https://github.github.com/gfm/) (autolink literals, footnotes, strikethrough, tables, tasklists). šŸ Support for defining styles via comment. šŸ™†šŸ¼ā€ā™‚ļø GitHub style: The markdown content is rendered as close to the way it's rendered on GitHub as possible. ## Quick Start ```bash $ npm install @uiw/react-markdown-preview-example --save ``` ```jsx import MarkdownPreviewExample from '@uiw/react-markdown-preview-example'; import data from './docs/README.md'; data.source // => `README.md` raw string text data.components // => The component index object, the React component converted from the markdown indexed example. (need to configure meta) data.data // => The component source code index object, the sample source code indexed from markdown. (need to configure meta) const Github = MarkdownPreviewExample.Github; const Example = MarkdownPreviewExample.Example; const NavMenu = MarkdownPreviewExample.NavMenu; <MarkdownPreviewExample source={data.source} components={data.components} data={data.data} title="MarkdownPreviewExample for React" version={`v${VERSION}`} > <Github href="https://github.com/uiwjs/react-markdown-preview-example" /> <Example> <div>test</div> </Example> </MarkdownPreviewExample> ``` ```jsx import PreviewExample from '@uiw/react-markdown-preview-example'; <PreviewExample.NavMenu title="Markdown Preview Example" menus={[ <a target="_blank" href="https://uiwjs.github.io/react-markdown-preview/" rel="noopener noreferrer">Markdown</a>, <a target="_blank" href="https://jaywcjlove.github.io/#/sponsor" rel="noopener noreferrer">Sponsor</a> ]} /> ``` just markdown _preview_ and **run** in markdown show **react** example. ```tsx import MarkdownPreview from '@uiw/react-markdown-preview-example/markdown'; import data from './docs/README.md'; data.source // => `README.md` raw string text data.components // => The component index object, the React component converted from the markdown indexed example. (need to configure meta) data.data // => The component source code index object, the sample source code indexed from markdown. (need to configure meta) <Markdown source={data.source} data={data} /> ``` ```ts import type { MarkdownPreviewProps } from '@uiw/react-markdown-preview'; import type { CodeBlockData } from 'markdown-react-code-preview-loader'; export interface MarkdownProps extends MarkdownPreviewProps { data: CodeBlockData; } export default function Markdown(props: MarkdownProps): import("react/jsx-runtime").JSX.Element; ``` There is a step to pay attention to, which needs to configure the webpack [`loader`](https://www.npmjs.com/package/markdown-react-code-preview-loader). The configuration and usage methods are consistent in Webpack: ```ts // .kktrc.ts import webpack, { Configuration } from 'webpack'; import scopePluginOptions from '@kkt/scope-plugin-options'; import { LoaderConfOptions } from 'kkt'; export default (conf: Configuration, env: 'development' | 'production', options: LoaderConfOptions) => { // .... config.module.rules.forEach((ruleItem) => { if (typeof ruleItem === 'object') { if (ruleItem.oneOf) { ruleItem.oneOf.unshift({ test: /.md$/, use: [ { loader: 'markdown-react-code-preview-loader', options: { lang:["jsx","tsx"] }, }, ], }); } } }); // .... return conf; }; ``` Or ```ts // .kktrc.ts import scopePluginOptions from '@kkt/scope-plugin-options'; import { LoaderConfOptions, WebpackConfiguration } from 'kkt'; import { mdCodeModulesLoader } from 'markdown-react-code-preview-loader'; export default (conf: WebpackConfiguration, env: 'development' | 'production', options: LoaderConfOptions) => { // .... conf = mdCodeModulesLoader(conf); // .... return conf; }; ``` `src/react-app-env.d.ts` ```ts declare var VERSION: string; declare module '*.md' { import { CodeBlockData } from 'markdown-react-code-preview-loader'; const src: CodeBlockData; export default src; } ``` ## Preview React Example ```tsx mdx:preview&boreder=0 import React from 'react'; const Demo = () => <div>Preview React Example</div> export default Demo; ``` Note āš ļø: You need to add a special `meta` identifier to the code block example, and [`loader`](https://www.npmjs.com/package/markdown-react-code-preview-loader) will index the `react` example for code conversion. ``` Meta Tag Meta ID Meta Param ā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆ ā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆ ā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆ ā•­ā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ā•²ā”ā”ā”ā”ā”ā”ā”ā”ā”ā”ā”ā”ā•±ā”ā”ā”ā”ā”ā”ā”ā•±ā”ā”ā”ā”ā”ā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā•® ┆ ```jsx mdx:preview:demo12&boreder=0 ┆ ┆ import React from "react" ┆ ┆ const Demo = () => <div>Preview React Example</div> ┆ ┆ export default Demo ┆ ┆ ``` ┆ ā•°ā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā”ˆā•Æ ``` 1. `mdx:` special identifier prefix 2. `mdx:preview` Controls whether to perform example indexing, and obtain the required example object through the corresponding line number. 3. `mdx:preview:demo12` Uniquely identified by `demo12`, accurately obtain the `example code` or `example component object` of the index. 4. `mdx:preview:&code=0&border=0` pass the parameters for the rendering layer to use. Support **meta** param: 1. `title` sample title 2. `boreder` = `1` | `0`, Set the display border 3. `checkered` = `1` | `0`, disable Checkered 4. `code` = `1` | `0`, Whether to display source code 5. `toolbar` = `1` | `0`, Whether to show the code folding button ```tsx mdx:preview import React from 'react'; const Demo = () => <div>Preview React Example: <b>mdx:preview</b></div> export default Demo; ``` ```tsx mdx:preview&checkered=0 import React from 'react'; const Demo = () => <div>Preview React Example: <b>mdx:preview&checkered=0</b></div> export default Demo; ``` ## Props ```ts import '@wcj/dark-mode'; import type { PropsWithChildren } from 'react'; import type { CodeBlockData } from 'markdown-react-code-preview-loader'; import type { GitHubCornersProps } from '@uiw/react-github-corners'; import type { MarkdownPreviewProps } from '@uiw/react-markdown-preview'; declare function Github(props: GitHubCornersProps): null; declare function Corners(props: GlobalStore['darkMode']): null; declare function Example({ children }: PropsWithChildren): null; declare function NavMenu(props: NavMenuProps): null; export interface MarkdownPreviewExampleProps extends Omit<React.HTMLAttributes<HTMLDivElement>, 'title'> { source: string; components: CodeBlockData['components']; data: CodeBlockData['data']; version?: string; title?: JSX.Element | string; markdownProps?: MarkdownPreviewProps; exampleProps?: React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>; logo?: JSX.Element; description?: JSX.Element | string; disableCorners?: boolean; disableDarkMode?: boolean; disableHeader?: boolean; disableBackToUp?: boolean; } declare const InternalMarkdownPreviewExample: import("react").ForwardRefExoticComponent<MarkdownPreviewExampleProps & import("react").RefAttributes<HTMLUListElement>>; type ExampleComponent = typeof InternalMarkdownPreviewExample & { Example: typeof Example; Github: typeof Github; Corners: typeof Corners; NavMenu: typeof NavMenu; }; declare const MarkdownPreviewExample: ExampleComponent; export default MarkdownPreviewExample; export interface NavMenuProps { title?: string; logo?: React.ReactNode; github?: string; menus?: Array<React.AnchorHTMLAttributes<HTMLAnchorElement>>; } export interface GlobalStore { corners: GitHubCornersProps; darkMode: Partial<HTMLElementTagNameMap['dark-mode']>; example?: React.ReactNode; navMenu?: NavMenuProps; } ``` ## Development Runs the project in development mode. ```bash # Step 1, run first, listen to the component compile and output the .js file # listen for compilation output type .d.ts file npm run watch # Step 2, development mode, listen to compile preview website instance npm run start ``` Builds the app for production to the build folder. ```bash npm run build ``` The build is minified and the filenames include the hashes. Your app is ready to be deployed! ## Contributors As always, thanks to our amazing contributors! <a href="https://github.com/uiwjs/react-markdown-preview-example/graphs/contributors"> <img src="https://uiwjs.github.io/react-markdown-preview-example/CONTRIBUTORS.svg" /> </a> Made with [contributors](https://github.com/jaywcjlove/github-action-contributors). ## License Licensed under the MIT License.