Skip to content

How to write custom Extension with nested elements #850

Open

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

Activity

  1. xoofx commented on Mar 13, 2025

    @xoofx
    Owner

    There are not so many inline parsers that support nested elements (with their own parsing/rendering rules). You would have to look at examples in the code inheriting from IPostInlineProcessor (e.g. EmphasisInlineParser, or SmartyPantsInlineParser...) but none are easy to grasp. The idea is that you have to create pseudo-inline "marker" elements that delemit opening/closing during InlineParser.Match that insert them into the syntax tree, and then you process them once the paragraph is done (via IPostInlineProcessor.Process) and you recover opening/closing inlines, and replace them with proper inline containers. Unfortunately, it's not easy. You would have to dig into existing inline parsers that support this and figure this out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions