Search code examples
c#markdownsharp

Is markdownsharp able to make a list when there is no blank line immediately preceding the list?


Here is a little snippet from my project in csharp 2010 express, using markdownsharp:

        MarkdownSharp.Markdown md = new MarkdownSharp.Markdown();



        switch (htmlPageTemplate)
        {
            case "Text.html":
                contentNode.InnerHtml = md.Transform(moduleArray[4, i]);
                break;
        }

I begin with the following value in moduleArray[4, i]:

Key points are summarized here.
+ Point 1

+ Point 2

+ Point 3

+ Point 4

+ Point 5

I am expecting the following output:

Key points are summarized here.
<ul>
    <li><p>Point 1</p></li>
    <li><p>Point 2</p></li>
    <li><p>Point 3</p></li>
    <li><p>Point 4</p></li>
    <li><p>Point 5</p></li>
</ul>

Instead, I get:

<p>Key points are summarized here.
+ Point 1</p>
<ul>
    <li><p>Point 2</p></li>
    <li><p>Point 3</p></li>
    <li><p>Point 4</p></li>
    <li><p>Point 5</p></li>
</ul>

I would even be happy with:

<p>Key points are summarized here.</p>
<ul>
    <li><p>Point 1</p></li>
    <li><p>Point 2</p></li>
    <li><p>Point 3</p></li>
    <li><p>Point 4</p></li>
    <li><p>Point 5</p></li>
</ul>

or

<p>Key points are summarized here.
<ul>
    <li><p>Point 1</p></li>
    <li><p>Point 2</p></li>
    <li><p>Point 3</p></li>
    <li><p>Point 4</p></li>
    <li><p>Point 5</p></li>
</ul>
</p>

Does anyone know why I'm getting the output that I am? Even if the first bullet is getting pulled into the para, I feel like it should still end up as a list - but I'm getting distracted by that unimportant detail - All I really care about is why my list isn't staying together. Something is triggering paragraph mode, and I feel like it's getting stuck there somehow. I'll keep looking and update here if I find anything else.

EDIT: I'm going with the answer marked below because of the constraints of my project. Some unrelated background information, in case it is helpful for others:

This project involves starting with a semi-structured Word doc which gets saved down as text. The text is then parsed for about five values, one of which is the content that I refer to in my question.

From this parsed text, I have the basis for building what we call a 'module'. The module consists of a menu, a glossary, pages of content, and a few other little things. The pages of content are what I am working on in the question above.

Once I get to the point where I am ready to build my pages, I parse through the text content and change it into markdown. Mostly, this just involves the Word formatted plain text lists, which begin with "* " instead of "+ ".

Once I have the markdown, I run it through markdownsharp, linked above.

Going with the answer below, I end up with a margin I don't want. In order to do this, I use a regex to replace the first instance of "* " with "\n +" and then the rest just with "+ ".

I will probably just add a class to the resulting para for cases where I have to add the \n where I really don't want it. The only catch will be not adding the class in cases where I do want the margin, but that will work fine in my situation.


Solution

  • I'm pretty sure you need that blank line to make it work, regardless of the Markdown parser. If there is no blank line, it doesn't know that you want to differentiate it from the paragraph you are currently in.

    StackOverflow has the same behaviour:

    sdsdasddasdasda
    + 1
    
    + 2
    
    + 3
    

    gives

    <p>sdsdasddasdasda
    + 1</p>
    <ul>
    <li><p>2</p></li>
    <li><p>3</p></li>
    </ul>
    

    but

    sdsdasddasdasda
    
    + 1
    
    + 2
    
    + 3
    

    gives

    <p>sdsdasddasdasda</p>    
    <ul>
    <li><p>1</p></li>
    <li><p>2</p></li>
    <li><p>3</p></li>
    </ul>
    

    Is the second output not what you want?