Markdown Js



21st August, 2020 | 4min read

Markdown is a markup language like HTML. It is quite popular among developers to write blogs, readme files, documentation and blogs. Some of the popular websites that support rich text like Reddit, GitHub, Notion etc allow you to write markdown. I use markdown to convert my blog from a markdown file to HTML web pages. Markdown is simple yet very powerful. In this blog, I will be writing about how to build a simple markdown parser to convert md to HTML with JavaScript using Regular Expressions.

âŦ‡ī¸ low-level compiler for parsing markdown without caching or blocking for long periods of time; ⚖ī¸ light-weight while implementing all markdown features from the supported flavors & specifications; 🌐 works in a browser, on a server, or from a command line interface (CLI) Demo. Checkout the demo page to see marked in action ⛹ī¸. Remark is a Markdown processor powered by plugins part of the unified collective. The project parses and compiles Markdown, and lets programs process Markdown without ever compiling to HTML (it can though). Powered by plugins to do all kinds of things: check Markdown code style, transform safely to React, add a table of contents, or compile to. A low-level markdown compiler for parsing markdown without caching or blocking for long periods of time. Any HTML tags rendered by the compiler and/or Markdown component can be overridden to include additional props or even a different HTML representation entirely. GFM task list support. Fenced code blocks with highlight.js support. All this clocks in at around 5 kB gzipped, which is a fraction of the size of most other React markdown components.

How does a markdown text look like

If you open a markdown file, you'll see the following syntax.

Learn more from this markdown cheatsheet.

Regular Expressions

A regular expression is a character sequence that helps us to capture patterns in a text. We can use it to validate user input, find and replace texts and yup, you guessed it, build our markdown parser. 😉

Different languages have different ways to represent RegEx. Here is how it is done in JavaScript.

I will explain the patterns we use in our parser as we reach that section. However, if you want to read more about regular expressions, visit https://javascript.info.

Markdown parser

The markdown parser I intend to build is a function that takes markdown text as input and returns HTML.

Here, we want to find a certain pattern in markdownText and perform replace operations.

String replace function

Our markdown parser is simple. Sierra wireless motherboards driver download for windows 10. It captures a pattern from markdown string passed to the function as markdownText argument and replaces it with certain HTML pattern.

Here is how the string replace function works.

Note: Here the i flag represents case insensitive and g flag represents global, which means it matches patterns everywhere on the string, not just the first match.

Capturing groups in Regular Expression

Markdown

Regular Expressions allows us to capture patterns of text and reference them with something like an index. We can use the index in the replace operation. To represent a group, we can simply wrap it in a parenthesis ().

Here, we have stored the starting hello in a group. The group can then be referenced with $1 on our replace operation.

Back to the parser

Now, we want to parse the markdown text and replace it with HTML.

Markdown Js

Here are the RegExes we will use in our parser and their explanation.

  1. Heading
    For heading, we want a string that starts with a hash(es) and captures everything after those characters.

    Here the first carat ^ 315 garage sale. represents line starting with and m flag represents multiple lines and by doing .* we are capturing everything (letters, numbers, special characters) that exists there.

  2. Blockquote
    For blockquote, we want a line that starts with > and captures everything after that character.

    Note: > represents escaping > character. That means, don't treat > as a part of special regEx character but as a part of that text itself.

  3. Bold Text
    For bold text, we want to capture a text between 2 asterisks.

  4. Seeshow usb devices driver download for windows. Italics Text
    For italic text, we want to capture a text between one asterisk.

  5. For image, link and line break

Markdown Editor Js

Fitting it all together

Markdown Js

By this point, you probably have all the background necessary to understand the concepts. Let's fit all the things that we have learnt up to now and build the parser.

Time to test the parser.

Should print:

Markdown Parser Js

Our markdown parser is now completed. It doesn't cover everything that markdown supports. Try implementing them and share the solution with me via twitter.