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 to write custom Extension with nested elements #850

Open
Anton-Buzik opened this issue Feb 12, 2025 · 0 comments
Open

How to write custom Extension with nested elements #850

Anton-Buzik opened this issue Feb 12, 2025 · 0 comments

Comments

@Anton-Buzik
Copy link

Hi, i have written an extension and this works fine until i have nested elements.
Currently i am struggling to understand how it should be implemented properly.
I see that my issue is how i take the content out and render it, but i dont understand how it should be done properly.
Many thanks for suggestions and help.

here is an working example: https://dotnetfiddle.net/TiTFJj

public class JiraColorParser : InlineParser
{
    private static readonly Regex ColorRegex = new Regex(@"\{color:(#[0-9a-fA-F]{6})\}(.*?)\{color\}", RegexOptions.Compiled);

    public JiraColorParser()
    {
        OpeningCharacters = new[] { '{' };
    }

    public override bool Match(InlineProcessor processor, ref StringSlice slice)
    {
        var match = ColorRegex.Match(slice.Text.Substring(slice.Start));
        if (match.Success)
        {
            var color = match.Groups[1].Value;
            var content = match.Groups[2].Value;

            var colorInline = new JiraColorInline
            {
                Color = color,
                Content = content
            };

            processor.Inline = colorInline;
            slice.Start += match.Length;
            return true;
        }

        return false;
    }
}

public class JiraColorInline : LeafInline
{
    public string Color { get; set; }
    public string Content { get; set; }
}


  public class JiraColorRenderer : HtmlObjectRenderer<JiraColorInline>
  {
      protected override void Write(HtmlRenderer renderer, JiraColorInline obj)
      {
          renderer.Write($"<span style=\"color:{obj.Color}\">");
          renderer.WriteEscape(obj.Content);
          renderer.Write("</span>");
      }
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant