Skip to content

How to write custom Extension with nested elements #850

@Anton-Buzik

Description

@Anton-Buzik

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>");
      }
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions