Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer peopleโ€™s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer peopleโ€™s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 2425

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 26, 20242024-11-26T05:17:08+00:00 2024-11-26T05:17:08+00:00

Rendering markdown made easy with react-markdown in ReactJS and NextJS web APPs

  • 61k

Hello Developers ๐Ÿ‘‹

If you are using ReactJS or NextJS(React Framework) to make your blog then rendering MARKDOWN is so much easy with React-Markdown

React-Markdown comes with build-in TypeScript support ๐Ÿคฉ.
Let's stop talking and start coding.

Basic setup

  • I am using NextJS with TypeScript for this blog.
  • I have a folder/directory with the name post containing all the posts and inside the post folder/directory I have two posts.
  • You can find the code on my GitHub (Link will be shared at the end).
// pages/index.tsx import { FC, Fragment } from "react"; import Link from "next/link";  interface Props {}  const Home: FC<Props> = () => (     <Fragment>         <h1>Hello Next.js ๐Ÿ‘‹</h1>         <div className="posts-container">             <Link href="/posts/first-post">First post</Link>             <Link href="/posts/second-post">Second post</Link>         </div>     </Fragment> );  export default Home; 
Enter fullscreen mode Exit fullscreen mode

  • I am keeping things simple to focus more on React-Markdown

These two posts are taken from Maximilian course on NextJS

  • Then we have a file to show individual posts and render our MARKDOWN
// /pages/posts/[slug].tsx  import { FC, Fragment } from "react"; import { GetStaticProps, GetStaticPropsContext, GetStaticPaths } from "next"; import { PostType } from "../../interfaces"; import { getPostData, getPostsFiles } from "../../lib/post-utils"; import PostContent from "../../components/PostContent";  interface Props {     post: PostType; }  const BlogPost: FC<Props> = ({ post }: Props) => {     return (         <Fragment>             // /components/PostContent.tsx             <PostContent post={post} />         </Fragment>     ); };  export const getStaticProps: GetStaticProps = async (context: GetStaticPropsContext) => {     const { slug } = context.params;     const postData = getPostData(slug);     return {         props: {             post: postData,         },         // regenerate after every 600s(10mins)         revalidate: 600,     }; };  export const getStaticPaths: GetStaticPaths = async () => {     const postFilenames = getPostsFiles();     const slugs = postFilenames.map((fileName) =>         fileName.replace(/.md$/, "")     );     return {         paths: slugs.map((slug) => ({ params: { slug: slug } })),         fallback: false,     }; };  export default BlogPost;  
Enter fullscreen mode Exit fullscreen mode

Here I set up the basic /posts/[slug] page, I will not go into details, let's Work on the PostContent component

Let's work with React-Markdown.

  • Install React-Markdwon
     npm i react-markdown 
Enter fullscreen mode Exit fullscreen mode

Now the easiest way to render MARKDOWN

 import React from "react"; import { PostType } from "../interfaces"; import ReactMarkdown from "react-markdown";  interface Props {     post: PostType; }  const PostContent = ({ post }: Props) => {     return (         <article className="content">             <ReactMarkdown>{post.content}</ReactMarkdown>         </article>     ); };  export default PostContent;  
Enter fullscreen mode Exit fullscreen mode

Wooowww! with just one line we render MARKDOWN
But if we go to the first blog it doesn't show an image because we get only the name of the file, we have to generate the path for the image and we can also customize how an image should display.

Let's optimize & customize the image.

  • ReactMarkdown has components prop which help us to map tag name to React Component
  • components take an object which contains the mapping of HTML tags.

Here, we are checking if a P tag has an image element if yes, then we are rendering our own image with our custom styles and if it is not an image then we are just returning the content of it.

 ... const PostContent = ({ post }: Props) => {     return (         <article className="content">             <ReactMarkdown                 components={{                     p: ({ node, children }) => {                         if (node.children[0].tagName === "img") {                             const image: any = node.children[0];                             return (                                 <div className="image">                                     <Image                                         src={`/images/${image.properties.src}`}                                         alt={image.properties.alt}                                         width="600"                                         height="300"                                     />                                 </div>                             );                         }                         // Return default child if it's not an image                         return <p>{children}</p>;                     },                 }}             >                 {post.content}             </ReactMarkdown>         </article>     ); };  
Enter fullscreen mode Exit fullscreen mode

Let's add syntax highlighting for CODE blocks

  • Firstly install syntax-highlighter
npm i react-syntax-highlighter @types/react-syntax-highlighter 
Enter fullscreen mode Exit fullscreen mode

  • react-syntax-highlighter makes it very easy to style our CODE blocks.
  • Here I am using 'materialDark' but you have many options and many themes to apply for your site.
 import { Prism as SyntaxHighlighter } from "react-syntax-highlighter"; import { materialDark } from "react-syntax-highlighter/dist/cjs/styles/prism";  const PostContent = ({ post }: Props) => {     return (         <article className="content">             <ReactMarkdown                 components={{                     p: ({ node, children }) => {                         ...                     },                     code({ className, children }) {                         // Removing "language-" because React-Markdown already added "language-"                         const language = className.replace("language-", "");                         return (                             <SyntaxHighlighter                                 style={materialDark}                                 language={language}                                 children={children[0]}                             />                         );                     },                 }}             >                 {post.content}             </ReactMarkdown>         </article>     ); };  
Enter fullscreen mode Exit fullscreen mode

Closing here ๐Ÿ‘‹๐Ÿ‘‹๐Ÿ‘‹

This is Shareef.
My Portfolio
Twitter ShareefBhai99
GitHub repo of this blog
Linkedin
My other Blogs

Cover photo by Dustin Curtis

markdownnextjsreactmarkdownwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.