s9e / TextFormatter

Text formatting library that supports BBCode, HTML and other markup via plugins. Handles emoticons, censors words, automatically embeds media and more.
MIT License
233 stars 36 forks source link

Get custom bbcode tag content #203

Closed Nearata closed 2 years ago

Nearata commented 2 years ago

at the moment im using useContent to get the content between the brackets

'[custom content="{ANYTHING;useContent}"]{ANYTHING}[/custom]',
'<div class="custom-bbcode">{ANYTHING}</div>'

but with this the inner content is rendered as plain text.

the goal is to replace the content of the bbcode dynamically with Utils::replaceAttributes().

is there an easy way to achieve this? i'm using Flarum 1.4.1.

JoshyPHP commented 2 years ago

It depends on whether you're more interested in "easy" or "correct". For the former, you can load the XML in a DOMDocument and perform your alterations using DOM operations. For the latter, if you describe your use case there is probably a better way to achieve the same result.

Nearata commented 2 years ago

i'd like to edit the bbcode output based on its attributes value

'[custom attr1="{TEXT}" attr2="{TEXT1}" content="{ANYTHING;useContent}"]{ANYTHING}[/custom]',
'<div class="custom-bbcode">{ANYTHING}</div>'
return Utils::replaceAttributes($xml, $tag, function ($attrs) {
  if ($attrs['attr1'] === 'pog') {
    $attrs['content'] = 'PogU';
  }

  return $attrs;
});

the above code works as expected, but the output (content attribute) is rendered as plain text instead of html.

JoshyPHP commented 2 years ago

But what is the purpose of that BBCode? If you want custom emoticons, you can use the Emoticons plugin. They are parsed at parsing (or posting) time though.

https://s9etextformatter.readthedocs.io/Plugins/Emoticons/Add_emoticons/

Nearata commented 2 years ago

rethinking about it, the purpose of this bbcode has nothing to do with this library.

Sorry if i wasted your time.

JoshyPHP commented 2 years ago

Not at all. Let me know if you need anything else.