flexmark-java icon indicating copy to clipboard operation
flexmark-java copied to clipboard

How should rendering macros via the flexmark-ext-xwiki-macros extension work?

Open dswitzer opened this issue 9 months ago • 4 comments

I've been reading through the docs and codebase and I'm stilling having trouble understanding how the flexmark-ext-xwiki-macros extension can be used to render custom output.

What I'm looking to do is create some special macros for rendering pre-made HTML snippets, but I do not want to allow raw HTML in the input.

For example:

{{alert}}
Hello **world**
{{/alert}}

I'd want that to generate something like:

<div class="alert alert-primary" role="alert">
Hello <strong>world</strong>
</div>

When I enable the plugin and set the MacroExtension.ENABLE_RENDERING to true, my input renders as:

{{alert}}
Hello <strong>world</strong>
{{/alert}}

When I set MacroExtension.ENABLE_RENDERING to false, the content is left out of the rendered output.

What I'd like to be able to do is a few things:

  1. Decide on a case-by-case bases whether output should be rendered. There may be some cases where I do not want the child content rendered from Commonmark.
  2. Replace the {{alert}} and {{/alert}} placeholders with the correct text.

In looking at the MacroNodeRenderer I see why it's rendering that way, but I'm not seeing how to overwrite the behavior.

I could call setOpeningMarker() and setClosingMarker() to change the markers and then setName() to change the contents to the HTML I want, but that doesn't seem right.

And how I would I choose to skip outputting and just capture the data if I needed to?

I'm obviously missing something basic.

Can anyone help me?

Thanks! -Dan

dswitzer avatar Mar 13 '25 20:03 dswitzer

Just following up on this issue.

After spending more time looking through the Flexmark code, I'm guessing the flexmark-ext-xwiki-macros extension isn't designed to do anything other than either strip the instructions out or to leave them as-is.

I'm thinking I'll need to fork the extension to do what I was hoping to do.

dswitzer avatar Apr 01 '25 20:04 dswitzer

What did you end up doing?

tedneward avatar Jul 17 '25 21:07 tedneward

@tedneward,

I ended up having to write a completely custom extension to handle my use case. I ultimately decided to use an HTML-style tag and my extension allows you to safe list HTML tags you want to render.

Unfortunately, I cannot share this code at this time.

dswitzer avatar Jul 17 '25 21:07 dswitzer

Ah, bummer. OK. I was hoping your code could help save me a few steps writing one of my own. Oh, well. Thanks for responding.

tedneward avatar Jul 17 '25 21:07 tedneward