How to parse Markdown in PHP?

2020-02-17 02:40发布

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?

4条回答
唯我独甜
2楼-- · 2020-02-17 02:57

Ciconia - A New Markdown Parser for PHP is a good one I found.

You just need 3 things to do :

1.Install Ciconia and parse file according to the document.
2. Add corresponding css theme to make it nice, like github markdown style or here.
3. Add syntax highlighting javascript, like google Javascript code prettifier.

Then everything will look pretty good.

If you want a complete example, here is my working demo for github style markdown:

<?php
header("Content-Type: text/html;charset=utf-8");
require 'vendor/autoload.php';
use Ciconia\Ciconia;
use Ciconia\Extension\Gfm;

$ciconia = new Ciconia();
$ciconia->addExtension(new Gfm\FencedCodeBlockExtension());
$ciconia->addExtension(new Gfm\TaskListExtension());
$ciconia->addExtension(new Gfm\InlineStyleExtension());
$ciconia->addExtension(new Gfm\WhiteSpaceExtension());
$ciconia->addExtension(new Gfm\TableExtension());
$ciconia->addExtension(new Gfm\UrlAutoLinkExtension());
$contents = file_get_contents('Readme.md');
$html = $ciconia->render($contents);
?>
<!DOCTYPE html>
<html>
    <head>
        <title>Excel to Lua table - Readme</title>
        <script src="https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js"></script>
        <link rel="stylesheet" href="./github-markdown.css">
        <style>
            .markdown-body {
                box-sizing: border-box;
                min-width: 200px;
                max-width: 980px;
                margin: 0 auto;
                padding: 45px;
            }
        </style>
    </head>
    <body>
        <article class="markdown-body">
        <?php
            # Put HTML content in the document
            echo $html;
        ?>
        </article>
    </body>
</html>
查看更多
乱世女痞
3楼-- · 2020-02-17 02:59

Also, there is an object-oriented implementation of Markdown which is faster: markdown-oo-php

查看更多
够拽才男人
4楼-- · 2020-02-17 03:04

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

查看更多
Deceive 欺骗
5楼-- · 2020-02-17 03:10

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.

查看更多
登录 后发表回答