简体   繁体   中英

Can't convert markdown into HTML using StackExchanges's PageDown

I have a method of storing straight markup code (generate by PageDown's editor) into a database. On another page, I grab the markup from the database and run it through the markdown converter, however I can't seem to get any output. This is my script:

<script type="text/javascript" src="include/Markdown.Converter.js"></script>
<script type="text/javascript" src="include/Markdown.Sanitizer.js"></script>
<script type="text/javascript" src="include/Markdown.Editor.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        var converter = Markdown.Converter();
        $("#description-content").text(converter.makeHTML("<?php echo $description ?>"));
    });
</script>

PHP is echoing the pure markdown text from the database. When I visit the page, there isn't anything that shows up in the div. What am I doing wrong?

EDIT: I'm not seeing any echo'ed text and if I simple add plaintext inside the .text() , I can see it.

Markdown.Converter is a constructor; you have to use

var converter = new Markdown.Converter();

A late answer but it might help new visitors

If you are fetchig result from database and echo through PHP i would prefer https://michelf.ca/projects/php-markdown/classic/

Once you download the file, One can use it to convert the markdown text to html.

<?php 

$query_post = mysqli_query($con,"SELECT * FROM `posts` ...//your query
$row_post = mysqli_fetch_assoc($query_post);

$content = $row_post['Body'];
include("path-to/PHP-Markdown-Extra-1.2.8/markdown.php");
$text = <<<EOD

$content

EOD;
echo Markdown($text);
?>

This will display result like my answer here, or any other answer or question here in stackoverflow

cheers

Maybe you can try using Showdown instead of PageDown and see if you get any different results, just as a test: https://github.com/coreyti/showdown/blob/master/src/showdown.js Showdown is another Markdown js implementation.

The code you have there doesn't look to have anything wrong with it, the only edge case I can think of would be unescaped quotation marks in $description .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM