VersionTag Plugin no longer works; implementing MarkdownIt-API Calls seems not to work

AI-generated summary

nstoecki42 reports that their VersionTag plugin no longer works after attempting to adapt it to the new MarkdownIt API. The plugin is designed to convert text like _VERSION:X.y into a styled HTML div containing an image and version number. Their current code uses both the legacy pre-processor approach and an attempted MarkdownIt plugin registration, but the latter fails. They’re seeking guidance on correctly implementing the MarkdownIt API to restore functionality. The plugin’s goal is to render version tags visually in posts, using a predefined image and CSS styling. The issue appears to stem from incorrect usage of MarkdownIt’s inline BBCode rule system, particularly in how tokens are being manipulated within the wrap function. nstoecki42 requests direction on how to properly register and render the version tag using the modern API.

Hi folks!

after my plugin stopped working I tried to adapt it to the new API but I didn’t succeed. Here is my code:

'discourse-versiontag/assets/javascripts/lib/discourse-markdown/versiontag.js.es6'
import { registerOption } from 'pretty-text/pretty-text';

registerOption((siteSettings, opts) => {
  opts.features.versiontag = !!siteSettings.versiontag_plugin_enabled;
});

function replaceVersionTag(text) {
  text = text || "";
  while (text !== (text = text.replace(/^\_VERSION\:(\d|\.|x)+/mg, function (match, p1) {
    var ver = match.split(':')[1];
    return "<div id=\"my_version\" title=\"supported Version\"><img src=\"/uploads/default/original/2X/c/cb3ca9fec2e0d595125432e2d5df26dd47f6dcce.png\" alt=\"Version "+ver+"\" />"+ver+"</div>";
  })));
  return text;
}

export function setup(helper) {
  helper.whiteList([ 'div[id=my_version]', 'div[title=*]' ]);

  if (helper.markdownIt) {
    helper.registerPlugin(md => {
      const ruler = md.inline.bbcode.ruler;

      ruler.push('versiontag', {
        tag: 'versiontag',
        wrap: function(startToken, endToken, tagInfo){
          startToken.type = 'div_open';
          startToken.tag = 'div';
          startToken.attrs = [['id', 'my_version'], ['title', 'supported Version']];
          startToken.content = '<img src="/uploads/default/original/2X/c/cb3ca9fec2e0d595125432e2d5df26dd47f6dcce.png" alt="Version ' + tagInfo.attrs._default + '" />' + tagInfo.attrs._default;
          startToken.nesting = 1;

          endToken.type = 'div_close';
          endToken.tag = 'div';
          endToken.nesting = -1;
          endToken.content = '';
        }
      });

    });
  } else {
    helper.addPreProcessor(text => replaceVersionTag(text));
  }
}

Could you point me to the right direction how to get it working again?

What this Plugin does

In an article I just can write:

_VERSION:X.y

and this is printed as CSS-styled Tag with a graphic and the Version.

1 лайк