public MDSection AddSubsection(string title, int level)
        {
            var ss = new MDSection(title, level);

            ss.Parent = this;
            Subsections.Add(ss);
            return(ss);
        }
 public MDSection Parse()
 {
     Root = new MDSection("", 0);
     while (!file.EndOfStream)
     {
         var line = file.ReadLine();
         //check for section
         var match = section.Match(line);
         if (match.Success)
         {
             var level = match.Groups[1].Value.Length;
             var title = ConvertEmphasis(match.Groups[2].Value);
             if (sec == null)
             {
                 sec = Root.AddSubsection(title, level);                                 //first section
             }
             else if (level > sec.Level)
             {
                 sec.Flush(); sec = sec.AddSubsection(title, level);
             }                                                                           //child
             else
             {
                 sec.Flush(); sec = sec.ParentForLevel(level).AddSubsection(title, level);
             }                                                                                                      //sibling or cousin
         }
         else
         {
             sec.AddLine(line);
         }
     }
     if (sec != null)
     {
         sec.Flush();
     }
     //if the document has a single top section, chroot to it
     if (Root.NoText && Root.Subsections.Count == 1)
     {
         Root        = Root.Subsections[0];
         Root.Parent = null;
     }
     return(Root);
 }