React Markdown Preview

repository·master·Indexed 43 days ago

https://github.com/uiwjs/react-markdown-preview

A React component for previewing markdown text in web browsers, designed to replicate GitHub's markdown style. Version 5.2.1 supports GFM, dark mode, and automatic code highlighting via rehype-prism-plus. It provides the MarkdownPreview component with customizable rehypePlugins, a pluginsFilter for intercepting plugin lists, and built-in support for rehype-rewrite to manipulate generated HTML.

Tokens
1.7K
Snippets
8
Records
8
Agent score
56%

What's inside React Markdown Preview

  1. Use the MarkdownPreview component

    master

    The MarkdownPreview component is the primary entrypoint for rendering markdown with preview capabilities. It is a forward-ref component that accepts MarkdownPreviewProps and allows you to customize the rendering pipeline via rehypePlugins.

    By default, it includes several plugins to handle raw HTML, metadata retrieval, and syntax highlighting via rehype-prism-plus. You can extend this behavior by providing your own plugins through the rehypePlugins prop.

    import MarkdownPreview from '@uiw/react-markdown-preview';
    
    function MyComponent() {
      return (
        <MarkdownPreview 
          codeEditable={true} 
          rehypePlugins={[]} // Add custom rehype plugins here
        />
      );
    }
  2. Configure MarkdownPreviewProps

    master

    The MarkdownPreviewProps interface defines the configuration options for the MarkdownPreview component. It extends the standard Options from react-markdown (excluding children) and adds UI-specific controls for styling, behavior, and plugin manipulation.

    interface MarkdownPreviewProps extends Omit<Options, 'children'> {
      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'];
    }
  3. Set color mode with wrapperElement

    master

    To control the color mode (light or dark) of the preview container, use the wrapperElement prop. This prop accepts a React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement> object and supports a 'data-color-mode' attribute.

    <MarkdownPreview
      source="# Hello"
      wrapperElement={{
        'data-color-mode': 'dark'
      }}
    />
  4. Customize plugins with pluginsFilter

    master

    The pluginsFilter prop allows you to intercept and modify the remark or rehype plugin lists before they are processed. This is useful for dynamically adding, removing, or altering plugins based on the component's lifecycle or configuration.

    // Example usage of pluginsFilter
    pluginsFilter: (type, plugins) => {
      if (type === 'rehype') {
        // return modified rehype plugins list
      }
      return plugins;
    }
  5. Use rehypeRewriteHandle to customize HTML transformation

    master

    The rehypeRewriteHandle function is a factory that returns a rehype plugin function. It is used to inject custom logic into the HTML transformation lifecycle, specifically for handling heading links and code block copy buttons.

    Behavior

    • Heading Links: If a heading (h1-h6) contains a child element with ariaHidden='true', it transforms that child into an anchor link using octiconLink and adds the anchor class.
    • Copy Button: If disableCopy is false, it finds pre elements, extracts the code string, and appends a copy button element (copyElement) to the node's children.
    • Custom Rewrite: If a rewrite function is provided via the options, it is executed after the default transformations.

    Parameters

    • disableCopy (boolean): If true, prevents the automatic injection of copy buttons into <pre> blocks.
    • rewrite (RehypeRewriteOptions['rewrite']): An optional function to perform additional custom transformations on the HAST (Hypertext Abstract Syntax Tree) nodes.
    export const rehypeRewriteHandle =
      (disableCopy: boolean, rewrite?: RehypeRewriteOptions['rewrite']) =>
      (node: Root | RootContent, index: number | null, parent: Root | Element | null) => {
        // ... implementation
      };
  6. Configure rehypeRewrite and disableCopy in MarkdownPreview

    master

    The MarkdownPreview component includes built-in support for rehype-rewrite to manipulate the generated HTML. You can control this behavior using the following props:

    • disableCopy: A boolean that, when set to true, influences how rehype-rewrite handles content (internally passed to rehypeRewriteHandle).
    • rehypeRewrite: An object passed to rehype-rewrite to define custom rewriting logic.

    These options are applied automatically within the internal plugin pipeline before any user-provided rehypePlugins.

    <MarkdownPreview 
      disableCopy={true} 
      rehypeRewrite={{ 
        // Custom rewrite logic configuration
      }} 
    />
  7. Configure default rehype plugins

    master

    The defaultRehypePlugins array contains the standard set of rehype plugins used by the library to process the HTML tree generated from Markdown. These include:

    • rehype-slug: Automatically adds id attributes to headings.
    • rehype-autolink-headings: Adds anchor links to headings.
    • rehype-ignore: Allows ignoring specific parts of the content.
    export const defaultRehypePlugins: PluggableList = [slug, headings, rehypeIgnore];