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

Open
opened 2026-01-29 14:42:09 +00:00 by claunia · 1 comment
Owner

Originally created by @RicBruIAI on GitHub (Jan 30, 2024).

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.

Originally created by @RicBruIAI on GitHub (Jan 30, 2024). 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.
claunia added the question label 2026-01-29 14:42:09 +00:00
Author
Owner

@MihaZupan commented on GitHub (Jan 30, 2024):

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:
}

@MihaZupan commented on GitHub (Jan 30, 2024): 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. ```c# private static readonly MarkdownPipeline s_pipeline = new MarkdownPipelineBuilder() .UsePreciseSourceLocation() .Build(); ``` Then you can look at the original input like so ```c# else if (element is ParagraphBlock paragraph) { string source = markdown.Substring(paragraph.Span.Start, paragraph.Span.Length); // Hello **everyone** this is a list: } ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/markdig#651