Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create custom markdown parser with JavaScript

This is not a duplicate of this question.

I want to make my own rules to a markdown parser like the one here in StackOverflow.

That means, converting *italic* into <span style="font-style:italic">italic</span>.

I know there are a lot of Parsers out there, but i don't understand them. The question previously mentioned doesn't really give me a lot to go on, it just links to more parsers and doesn't explain how they work.

So I would like to know the basics, or the logic, of creating a whole markdown parser, and if you think explaining it to me it's not a pleasent task then don't. Thanks for understanding :)

like image 740
undefined Avatar asked Dec 30 '14 22:12

undefined


1 Answers

A common way of doing this, is by using a RegExp expression, followed by the replace method.

This is one way you could do this:

"*This is italic*"
      .replace(/\*(.*?)\*/gi, '<span style="font-style: italic">$1</span>');

What is happening here is that you are searching for any sequence of characters surrounded by the two asteriks and capturing those characters so then you can put them between the HTML tags.

like image 179
Afonso Matos Avatar answered Sep 30 '22 12:09

Afonso Matos