简体   繁体   中英

Converting Markdown/ASCI (Multiline) To HTML (nodejs)

I currently have this in my editor..

* Line one
* Line two
some more info

Which sits in the database like this

* Line one\\r\\n* Line two\\r\\nsome more info

I am using Markdown package to turn the markdown to HTML to display on my site, this works fine.

However it is ignoring the line breaks, thus giving me this output...

<ul><li>Line one</li><li>Line two\\r\\nsome more info</li></ul>

When the output I want is...

<ul><li>Line one</li><li>Line two</li></ul>some more info

I guess i need to make a 'multiline string' from my single line before i run it through the markdown?

Any thoughts on the best approach?

Currently using this code

var markdown = require( "markdown" ).markdown;
var unMarkdownDescriptions = function(description){
    //Check if currently contains HTML.
    if(typeof description !== "undefined"){
        if(description.indexOf("<")  !=-1){
            return description;
        }else{

            return html_content = markdown.toHTML(description);
        }    
    }else{
        return '';
    }
}

Current code check if already stored as HTML in DB and ignores them (we are migrated from HTML to MD, the HTML generated on the ERP is dodgy at the best of times!)

However it is ignoring the line breaks

That's how it should behave, according to the Markdown spec :

A paragraph is simply one or more consecutive lines of text, separated by one or more blank lines. (A blank line is any line that looks like a blank line — a line containing nothing but spaces or tabs is considered blank.) Normal paragraphs should not be indented with spaces or tabs.

The implication of the "one or more consecutive lines of text" rule is that Markdown supports "hard-wrapped" text paragraphs. This differs significantly from most other text-to-HTML formatters (including Movable Type's “Convert Line Breaks” option) which translate every line break character in a paragraph into a <br /> tag.

To get the output you want, simply include a blank line after your list:

* Line one
* Line two

some more info

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