Skip to content

Rulers: Methods

Alteras1 edited this page Jun 24, 2020 · 13 revisions

Methods determine how a tag get processed into HTML. Only certain methods works with other, and some with the proper ruler type. In general, method returns either a string of a HTML element (done in CSS naming scheme; see wrap) or is a function that returns a token. Here is a list of the ones we're aware of:

  • wrap
  • replace
  • before
  • after

See Structure of a Ruler for how to make a Ruler

Functions

For the simple way of doing methods, see wrap. Anyway, function allow for tagInfo and content processing. Here is an example:

  md.block.bbcode.ruler.push("print", {
    tag: "print",
    replace: function (state, tagInfo, content) {
      let printOption = tagInfo.attrs['_default'];

      let token = state.push("div_open", "div", 1);
      if (!printOption) {
        token.attrs = [["class", "bbcode-print"]];
      } else {
        token.attrs = [["class", "bbcode-print-" + printOption]];
      }

      token = state.push("inline", "", 0);
      token.content = content;
      token.children = [];

      state.push("div_close", "div", -1);

      return true;
    }
  });

A function in method requires either state or startToken and endToken at the beginning, with the optional arguments tagInfo and content. For those looking at the API, state is just a token. Ignore the weird syntax we have, swapping between state and token, we've been through a lot.

tagInfo.attrs contains the tag arguments, as explained earlier.

Wrap

wrap is the simplest method. Here is an example:

  md.inline.bbcode.ruler.push("highlight", {
    tag: "highlight",
    wrap: "span.bbcodeHighlight"
  });

Here [highlight]text[/highlight] becomes <span class="bbcodeHighlight">text</span>.

wrap in this use case is extremely simple, but not very useful. Here is a use case where it take an argument.

sfadsf