Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
RicBruD4S opened this issue Jan 30, 2024 · 1 comment
Open
Labels

Comments

@RicBruD4S
Copy link

RicBruD4S commented 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.

@MihaZupan
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants