Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse Markdown in PHP?

First, I know, there already is a Markdown parser for PHP. I also took a look to this question but it doesn't answer to my question.

Obviously, even if the title mention PHP, if it's language agnostic, because I'd like to know what are the step I've to go through to do that.

I've read about PEG, but I've to admit, I didn't really understand the example provided with the PHP parser.

I've also read about CFG.

I've found Zend_Markup_Parser_Textile which seems to construct a so called "Token Tree" (what's about it?) but it currently unusable. (Btw, Textile is not Markdown)

So, concretely, how would you go to this?

Obviously I though about using Regex but, I'm afraid.

Because Markdown supports several syntaxes for the same element (Setext and atx).

Could you give some starting point?

like image 734
Jérémy Avatar asked Feb 25 '11 10:02

Jérémy


People also ask

What is md file in PHP?

Markdown is a text-to-HTML conversion tool for web writers. It has gained its deserved popularity after its introduction on 2004. Markdown allows you to write text while providing a format in an easy and clean way.

What is PHP markdown?

PHP Markdown is a port of the original Markdown parser written in Perl (by John Gruber), with improvements added later on. PHP Markdown follows an architecture similar to Parsedown, as it offers a single PHP class that contains everything.

What is Parsedown?

Parsedown is capable of escaping user-input within the HTML that it generates. Additionally Parsedown will apply sanitisation to additional scripting vectors (such as scripting link destinations) that are introduced by the markdown syntax itself.


Video Answer


2 Answers

You should have a look at Parsedown.

It parses Markdown text the way people do. First, it divides texts into lines. Then it looks at how these lines start and relate to each other. Finally, it looks for special characters to identify inline elements.

like image 119
Emanuil Rusev Avatar answered Sep 18 '22 19:09

Emanuil Rusev


There is PHP Markdown Extra that seems to be popular, you could start by looking at its source.

like image 23
Matti Pastell Avatar answered Sep 21 '22 19:09

Matti Pastell