Knagis/CommonMark.NET

Header1 not processed if it is the first line

Closed this issue · 2 comments

I'm having a problem with the following mark-down:

# Main header
This is a very simple file

## Section 1
This is section 1

## Section 2
This is section 2

In the format above, the first header '# Main header' is not correctly processed and the resulting HTML looks like this:

<p># Main header
This is a very simple file</p>
<h2>Section 1</h2>
etc

Interesting, if I have 1st level headers further down my MD file, they are correctly parsed to HTML H1's. Also if I make the first line an empty line, I get the output below which is closer to what I want but ideally I'd rather not have the opening P

<p></p>
<h1>Main header</h1>
<p>This is a very simple file</p>
etc

Is this something to do with the CommonMark spec or am I doing something wrong or is it actually a bug?

My code looks like this. This is an MVC Action. The file itself is passed in a simple form.

C Sharp

        [HttpPost]
        public ActionResult Viewer(FormCollection collection)
        {
            //read file in incoming form to a string
            var files = GetFilesFromForm(Request.Files);
            var fileMd = Encoding.UTF8.GetString(files.FirstOrDefault());

            //get full html string from md
            var fileHTML = string.Empty;
            using (var writer = new StringWriter())
            {
                CommonMarkConverter.ProcessStage3(CommonMarkConverter.Parse(fileMd), writer);
                fileHTML += writer.ToString();
            }

            //view model
            var viewModel = new ViewerViewModel()
            {
                ConvertedHtml = fileHTML
            };

            return View(viewModel);
        }

HTML

        @using (Html.BeginForm("Viewer", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
        {
            <div class="row">
                <div class="small-12 columns">
                    <input type="file" name="file" id="file" />
                </div>
            </div>
            <div class="row">
                <div class="small-12 columns">
                    <input type="submit" class="button" value="Submit">
                </div>
            </div>
        }

Did you try reproducing this in the console application? Works without a hitch for me.

dump the chart codes of the output - it might be possible that your code does not handle utf-8 preamble (byte-order-mark) correctly and thus the # symbol is not actually the first in line.

Also you should be passing a stream to the parser, instead of reading everything into a string.