1

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.

George Sisco
  • 621
  • 3
  • 9

1 Answers1

0

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?

theyetiman
  • 8,514
  • 2
  • 32
  • 41
  • Thanks. Yeah - the issue is, then, how do I get output such that there is no margin between the para and the ul? I'll look for a workaround as I'm sure that's outside the scope of this discussion. I edited the question to reflect that even the whole list inside a para would be fine – George Sisco Jun 02 '14 at 15:12
  • Due to some other constraints on the project I am working on, I decided to insert a blank line as you propose in the second output. I'm accepting your answer, and adding some detail to the question above. – George Sisco Jun 02 '14 at 15:33
  • @GeorgeSisco the issue of the margin is an issue of styling, not an issue of semantic markup (the HTML output from MarkdownSharp is fine). I think what you're saying you want is a list inside a paragraph, but Markdown doesn't support this as far as I know. – theyetiman Jun 02 '14 at 16:26
  • I'm not sure I agree that it's fine. IMO, if I come across a '+', indicating a list, then either I should get my list, outside of the para, or I should get a list of one item inside the para. At any rate, I don't think the initial plus sign should be ignored. However, that's simply my opinion and clearly not the way markdown works, nor the way everyone else expects it. At the very least, it's a 'gotcha' to be aware of, which I now am. I can definitely live with it, and I'm thankful for the great input from everyone about it. – George Sisco Jun 17 '14 at 12:57
  • Markdown’s insistence that lists are not semantic parts of paragraphs is one of my biggest complaints about it. The HTML output is *not* fine if the list is part of the paragraph. – randy Oct 12 '21 at 19:45