Custom BBCode Help/Advice

AI-generated summary

In a discussion titled “Custom BBCode Help/Advice” under the Dev category, Ellibereth sought help creating a custom BBCode that converts [vote]name[/vote] into <p><span class="highlight"><b>VOTE:name</b></span></p>. After referencing documentation and existing plugin examples, they successfully implemented the solution using ruler.push with a replace function that manually constructs tokens for a <span> element with class vote, prepending “VOTE:” to the content. They also defined a new CSS class vote styled similarly to highlight but with bold formatting. riking requested that Ellibereth share the solution, which they did in post 3, noting initial confusion around token usage and class definition, and that studying complex wrap examples helped clarify the approach despite some trial and error.

UPDATE: Figured it out!


I’m trying to make make a plugin that makes a custom bbcode that maps

[vote]name[/vote]
to
<p><span class="highlight"><b>VOTE:name</b></span></p>

I’ve been reading the stuff in this topic along with going through the various bbcode plugin repos that exist, and If I’m understanding the examples correctly the only relevant nontrivial places I have to change would be the stuff after wrap here

ruler.push('vote',{
    tag: 'vote',
    wrap: 
  });

and the stuff in square brackets here:

replaceBBCode("vote", contents => [].concat(contents));

I’ve tried a few things but none of them have worked as intended. I’d appreciate if anyone could help with this/point me towards what documentation/examples I’m missing and should read to make this and future similar things easier. Sorry if this is fairly trivial - I’m new to this.

Thanks.

Would you care to share the result here? Having a overview of what wasn’t obvious to you would be nice!

    tag: 'vote',
    replace: (state, tagInfo, content) => {
      let token;
      token = state.push('span_open', 'span', 1);
      token.attrs = [['class', 'vote']];
      token = state.push('text', '', 0);
      token.content = 'VOTE:' + content;
      token = state.push('span_close', 'span', -1);
      return true;
    },
  });

Basically did this and defined a new vote class that was the same as highlight’s but also with bold.
I tried a lot of silly thing without tokens and without defining the new class at first but it became clearer after looking at some of the more complicated wrap examples, though there was still some trial and error for notation.