xoofx / markdig

A fast, powerful, CommonMark compliant, extensible Markdown processor for .NET
BSD 2-Clause "Simplified" License
4.21k stars 444 forks source link

How can i print the original Inline code instead of the converted text? #766

Open RicBruIAI opened 5 months ago

RicBruIAI commented 5 months ago

What I am trying to do is to parse only headers and then get the original text without parsing the string. Something like:

# This is a header
Hello **everyone** this is a list:
- Item 1;
- Item 2

And I've done something like this:

      string chapterContent = "";
      foreach (var element in document.Descendants()) {
                    if (element is HeadingBlock header) {
                        var chapterTitle = header.Inline.FirstChild.ToString().Trim();
                    }
                    else if(element is ParagraphBlock paragraph) {
                        chapterContent += paragraph.Inline.FirstChild.ToString()
                    }
      }

But The result is: Hello everyone this is a list: Item 1; Item 2

Instead of:

Hello **everyone** this is a list:
- Item 1;
- Item 2

Because I want to maintain the original code, then I suppose the "ToString()" is not what I am looking for.

MihaZupan commented 5 months ago

Every MarkdownObject has a Span property that points to the section of the input string it is based on. You must specify UsePreciseSourceLocation for those positions to be accurate.

private static readonly MarkdownPipeline s_pipeline = new MarkdownPipelineBuilder()
    .UsePreciseSourceLocation()
    .Build();

Then you can look at the original input like so

else if (element is ParagraphBlock paragraph)
{
    string source = markdown.Substring(paragraph.Span.Start, paragraph.Span.Length);
    // Hello **everyone** this is a list:
}