Giter Site home page Giter Site logo

rsc-markdown's Introduction

rsc-markdown

npm i rsc-markdown

Markdown for react server components


rsc-markdown, a powerful markdown rendering library for React Server Components. This library uses remark and rehype to provide a powerful server component for rendering markdown content. Compatible with react, nextjs, and remix.

Features

  • ๐Ÿš€ Server-Side Rendering: Built as a React Server Component, ensuring lightning-fast performance by rendering markdown content directly on the server.
  • ๐Ÿ”’ Safe Rendering: Does not use dangerouslySetInnerHTML, ensuring a secure rendering process.
  • ๐Ÿ“ Remark and Rehype Powered: Utilizes the powerful and flexible parsing and transformation capabilities of remark and rehype.
  • ๐ŸŽจ Custom Components: Allows for the substitution of standard markdown elements with custom React components.
  • ๐Ÿ”ง Configurable Options: Supports options like trimming white space, GitHub Flavored Markdown (GFM), and syntax highlighting for code blocks.
  • ๐ŸŒˆ Syntax Highlighting: Integrated with rehype-prism-plus for beautiful syntax highlighting in code blocks.

Installation

To use rsc-markdown in your project, you can install it via npm or yarn:

npm install rsc-markdown
yarn add rsc-markdown

Usage

rsc-markdown is designed for Next.js, Remix, and React 19.

Server Component:

'use server';

import Markdown from 'rsc-markdown';

const markdownContent = ` # Welcome to rsc-markdown This is a **powerful** library for rendering markdown in React Server Components. `;

function MyComponent() {
  return <Markdown markdown={markdownContent} />;
}

Client Component:

'use client';

import Markdown from 'rsc-markdown';

const markdownContent = ` # Welcome to rsc-markdown This is a **powerful** library for rendering markdown in React Server Components. `;

function MyComponent() {
  return (
    <Suspense fallback={<div>Loading...</div>}>
      <Markdown markdown={markdownContent} />
    </Suspense>
  );
}

With Api:

'use server';

import Markdown from 'rsc-markdown';

const prisma = new PrismaClient();

export default async function MyComponent() {
  const markdown = await fetch('/api/markdown').then((res) => res.text());

  return <Markdown markdown={markdown} />;
}

Props

  • markdown: The markdown content to be rendered. (Required)
  • options: Configuration options (Optional):
    • trimWhiteSpace: Trims leading white space from each line in the markdown content.
    • gfm: Enables GitHub Flavored Markdown.
    • syntaxHighlighting: Enables syntax highlighting for code blocks.
    • components: An optional object to override default HTML elements with custom React components.

Custom Components

You can replace standard markdown elements with your custom components. For example, to use a custom component for rendering links:

import Markdown from 'rsc-markdown';
const components = {
  a: ({ href, children }) => (
    <a href={href} style={{ color: 'red' }}>
      {children}
    </a>
  ),
};
const markdownContent = `[Custom Link](https://example.com)`;
function MyComponent() {
  return <Markdown markdown={markdownContent} components={components} />;
}

Note

Remember, rsc-markdown is a server component and cannot use any hooks or functions that start with the word use or have event handlers. It's designed to render static markdown content efficiently on the server side, enhancing the performance and SEO of your React applications.

Contributing

We welcome contributions to rsc-markdown! Whether it's adding new features, improving documentation, or reporting bugs, please feel free to open an issue or submit a pull request. Thank you for considering rsc-markdown for your markdown rendering needs in React Server Components.

rsc-markdown's People

Contributors

insightfulfuture avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.