erusev / parsedown

Better Markdown Parser in PHP
https://parsedown.org
MIT License
14.74k stars 1.12k forks source link

Use HTML when echoing Parsedown #634

Closed ghost closed 6 years ago

ghost commented 6 years ago

I know it kind of defeats the purpose of Parsedown, but is there a way to simply also echo tags? For instance:

echo $parsedown->text('# Hello World
    <div class="container">Hello World</div>
    ');
aidantwoods commented 6 years ago

I edited your comment so it would include the HTML you wrote visibly (and also importantly the indentation).


While

echo $parsedown->text('# Hello World
    <div class="container">Hello World</div>
    ');

Will not output the HTML you've written as HTML, the following will:

echo $parsedown->text('# Hello World
<div class="container">Hello World</div>
');

Note the difference in indentation. In markdown, four spaces of indentation will produce an "indented code block" (which is why the HTML was being escaped). There's a short cheatsheet here if you wanted to see some of the other syntaxes :)

ghost commented 6 years ago

image

This is the result for the following code:

echo $parsedown->text('<div class="flol"><h1>' . $row[0] . '</h1><br><h3>By: ' . $row[1] . '</h3><br><br><img height="600" width="800" src="' . $row[2] . '"><br><br><br><p>' . $row[3] . '</p>');

aidantwoods commented 6 years ago

You'd need to add at least two newlines after any HTML before markdown would be recognised again (per spec). Alternatively, if you have static HTML which you want to render before markdown, why not include it before any parsing takes place? e.g.

echo '<div class="flol"><h1>' . $row[0] . '</h1><br><h3>By: ' . $row[1] . '</h3><br><br><img height="600" width="800" src="' . $row[2] . '"><br><br><br>'
    . $parsedown->text($row[3])
;

Assuming here that $row[3] holds the markdown text you want to output?