You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Thanks for offering help. My mental model is off somewhere, but I need to figure out where. The functionality of this parser isn't as important as understanding the process around StringSlice, which I find confusing when looking at other examples.
Given the following markdown, I expect the following result.
this is a link to [github:khalidabuhakmeh]
another [github:maartenba]
with the result.
<p>this is a link to <ahref="https://github.com/khalidabuhakmeh/>khalidabuhakmeh</a>another <a href="https://github.com/maartenba/>maartenba</a></p>
Instead, I get this. You'll notice the text this is a link to and another is missing from the final output. I'm not sure what I'm doing wrong. If I had to guess, I'm skipping the text, but other processors would handle that.
My current implementation is below. No rush. I'm trying to write a blog post about this subject because I find it fascinating, and this is a great library. Cheers :)
using System.Text.RegularExpressions;using Markdig;using Markdig.Helpers;using Markdig.Parsers;using Markdig.Renderers;using Markdig.Syntax.Inlines;varpipeline=new MarkdownPipelineBuilder().Use<GitHubUserProfileExtension>().Build();// Note: The text of "this is a link to" gets swallowed up.varhtml= Markdown
.ToHtml("""
this is a link to [github:khalidabuhakmeh]
another [github:maartenba]
""", pipeline);
Console.WriteLine(html);publicclassGitHubUserProfileExtension:IMarkdownExtension{publicvoidSetup(MarkdownPipelineBuilderpipeline){if(!pipeline.InlineParsers.Contains<GitHubUserProfileParser>()){
pipeline.InlineParsers.Insert(0,new GitHubUserProfileParser());}}publicvoidSetup(MarkdownPipelinepipeline,IMarkdownRendererrenderer){}}publicclassGitHubUserProfileParser:InlineParser{publicoverrideboolMatch(InlineProcessorprocessor,refStringSliceslice){varprecedingCharacter= slice.PeekCharExtra(-1);if(!precedingCharacter.IsWhiteSpaceOrZero()){returnfalse;}while(!slice.Match("[github:")){// keep skipping
slice.NextChar();}varregex=new Regex(@"\[github:(?<username>\w+)]");varmatch= regex.Match(slice.ToString());if(!match.Success){returnfalse;}varusername= match.Groups["username"].Value;varliteral=$"<a href=\"https://github.com/{username}/>{username}</a>";
processor.Inline =new HtmlInline(literal){Span={Start= processor.GetSourcePosition(slice.Start,outvar line,outvar column)},Line=line,Column=column,IsClosed=true};
processor.Inline.Span.End = processor.Inline.Span.Start + match.Length -1;
slice.Start += match.Length;returntrue;}}
The text was updated successfully, but these errors were encountered:
🤦 That fixed it. I'm a dummy. I appreciate your help.
Now, the parser only starts when it sees an opening character [, which is the expected behavior. Previously, it sent the entire text block, which is too much.
👋 Hi @xoofx,
Thanks for offering help. My mental model is off somewhere, but I need to figure out where. The functionality of this parser isn't as important as understanding the process around
StringSlice
, which I find confusing when looking at other examples.Given the following markdown, I expect the following result.
with the result.
Instead, I get this. You'll notice the text
this is a link to
andanother
is missing from the final output. I'm not sure what I'm doing wrong. If I had to guess, I'm skipping the text, but other processors would handle that.My current implementation is below. No rush. I'm trying to write a blog post about this subject because I find it fascinating, and this is a great library. Cheers :)
The text was updated successfully, but these errors were encountered: