paint-brush
Using React to Take Advantage of Markdown and Adding Functionalities to Code Blocksby@ljaviertovar
6,434 reads
6,434 reads

Using React to Take Advantage of Markdown and Adding Functionalities to Code Blocks

by L Javier TovarFebruary 2nd, 2023
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Markdown is a markup language that makes it easy to format text using a series of characters in a unique way. This tutorial will learn how to take advantage of Markdown with React. We will develop the basis of a blog in which we want to share programming tutorials. In Markdown, we can add color highlighting for different programming languages.
featured image - Using React to Take Advantage of Markdown and Adding Functionalities to Code Blocks
L Javier Tovar HackerNoon profile picture

Create a tutorial blog with code syntax highlighting, and dark mode and copy to clipboard


Nowadays, there are many websites with which we can create a blog and start writing, but if we want to write technical articles like tutorials, we will need to show code to exemplify the tutorial.

In this case, the same websites offer the option to write code and display it in a different format than the text, for example, here on Medium. And how does the site give another format to this block of text?


Well, in most cases, they use Markdown. Markdown is a markup language that makes it easy to format text using a series of characters in a unique way.

This tutorial will learn how to take advantage of Markdown with React.


We will develop the basis of a blog in which we want to share programming tutorials.

We will also add a couple more functionalities to the code blocks that allow us to change the style or theme and copy the code.

What is Markdown?

It’s a markup language that in principle, was designed to produce texts whose destination was going to be the web more quickly and efficiently than if we were using HTML directly.

In short, it’s a software tool that converts the language into valid HTML.


Let’s say we want to write a level 1 heading. In HTML this is done with the h1 tag. Therefore, we would write:


<h1>Header</h1>


However, using Markdown we would not need to write the tag, but just put a pad at the beginning:


# Header


Here you can find everything about Markdown.


Writing code blocks in Markdown

A commonly used element in Markdown especially for developer blogs is the delimited code block. If we want to display a code block, in our article, we create a delimited code block with a “fence” of backticks before and after the code as follows:


var value = 6;


What Markdown does is convert that fenced block of code into the following:


<pre><code>var value = 6;</code></pre>


Syntax Highlighting a fenced code block

In Markdown, we can highlight delimited code blocks. This function allows us to add color highlighting for different programming languages.


To add syntax highlighting, we just need to specify the language together with the backticks.


```javascript
var value = 6;

\
Now that we know what Markdown is, let’s see how we can use it inside React. We will create a blog full of tutorials.

\
## Setting Up Our Blog

We create our application with the following command:

\
```bash
npx create-react-app myApp


We install the dependencies that we will need in the project:


npm i markdown-to-jsx react-syntax-highlighter react-



After that, we create the following structure for the project:


src/
├── assets/
│   └── icons/
│   └── img/
├── components/
│   └── Code.jsx
│   └── Post.jsx
├── styles/
│   └── App.css
│   └── Code.css
│   └── Post.css
├── tutorials/
│   └── tutorial1.md
├── App.js
└── index.js


To display markdown files with React, we will use a library that converts markdown to JSX called markdown-to-jsx.


With this library, we will be able to work with React components inside markdown files. To do this, let’s follow these steps.


1: Inside the tutorials folder, we create a new .md file, in this case, tutorial1.md, and add the Markdown content we want to display.


To exemplify or simulate a tutorial that we would like to show in our blog, I have taken a part in the React documentation. In addition, I have replaced the native Markdown code blocks with our <Code/>component.


I mean, instead of writing code inside the inverted quotes that Markdown uses, we write code inside the Code tags. We have called this component this way because in the overrides{} properties of the <Markdown/> component we have assigned that name to it. It can be any name we want.

We will understand this better below.


2: In the components folder we create our <Post/> component and import the markdown-to-jsx package.


3: Setup the state. We configure the state that we will use to store the Markdown content. Initially, we set the postContent variable to a blank string and place it between the <Markdown> tags.

We also have a file name to act as a way to dynamically choose the Markdown file we want.


4: Fetch and display the Markdown file. Once our component is loaded, inside useEffect() we use the import as a function.


The import used in this way acts as a promise and returns the absolute path to our Markdown file.

We then fetch the Markdown file we want using the Fetch API. Once we retrieve the file, we must parse the response as a text file and then store the parsed response in our postContent variable.


5: The <Markdown> component we imported from markdown-to-jsx has a property called options, which in turn has an overrides property that allows us to embed React components into Markdown files by receiving their properties.


In this case, we want to replace the Markdown code blocks with a component that we will create ourselves.

Highlighting blocks of code with a theme and copy to the clipboard

We have already seen that Markdown offers code highlighting when we add the type of language we are displaying, but what if we want to give a theme to that block of code as a theme in VS?


For that, we will use react-syntax-highlighter, this library offers code highlighting for a variety of languages, and using Prism, which is another lightweight syntax highlighter, we can access a list of themes or styles for our code.


1: We create another component that we will call <Code/> which will be the one that will show the code blocks. We import Syntax Highlighter, PrismLight, and the style we want, in this case, we select two styles, one for dark mode and another for light mode.


2: Syntax Highlighter asks us to register the languages we are going to work with, we register jsx.


3: Syntax Highlighter receives two properties, the language to highlight and the style, we will receive these properties from our <Post/>component together with the code block that we will pass as children.


4: Copy To Clipboard will help us to copy the content of the code in the clipboard, this component receives a property called text that is what we want to copy. We will pass the children’s property that we passed to Syntax Highlighter.


5: How to Copy To Clipboard commonly is an icon we will do it in the same way, an icon which serves to copy, and once copied the text we will show the icon of Pay for 1000 ms. There are several ways to tell the user that the text has been copied, such as a toast message, an alert, etc.


6: The last functionality that we want to add to our <Code/> component is to be able to choose between a dark or light theme. We will do this simply with the icons that are usually used in these cases, a sun icon and a moon icon. This button will do is to change the state that we have at the moment by its opposite to show the corresponding style.

Last steps

We already have our main components ready, <Post/>and <Code/>, now all we need to do is to integrate them where we want for our blog. For this simple exercise, I have only taken the main component <App/>as if it were a page of our blog. You can create the structure that suits you for your project and your own styles.


This is what our blog looks like:


See the demo here.


dark mode


light mode

Conclusion

In this tutorial, we have seen how easy it is to work with Markdown files inside React and use what we could give when using Markdown to write a blog.


But we could take one more advantage of Markdown because being a language in which we don’t necessarily need to write code but just text these files could be written by people who are not programmers.


I hope this tutorial has been useful and that you can use what you have learned in your next projects.


Also Published Here