showdownjs / showdown

A bidirectional Markdown to HTML to Markdown converter written in Javascript
http://www.showdownjs.com/
MIT License
14.32k stars 1.57k forks source link

Add support for <ins> #455

Closed ckoppelman closed 6 years ago

ckoppelman commented 7 years ago

In some flavors of markdown, just like ~~ means <del>, ++ means <ins>

I'd like an option parallel with strikethrough that allows me to convert the following:

My name is ~~Mike~~ ++John++.

into:

<p>My name is <del>Mike</del> <ins>John</ins>.</p>

I believe it's fairly simple. My lang converter to approximate this is below, but probably cloning the strikethrough parser is more robust:

showdown.extension('pp-insert', function() {
  return {
    type: 'lang',
    regex: /(?<!\\)[+]{2}((?=\S).*?\S)[+]{2}/gi,
    replace: '<ins>$1</ins>'
  };
});
tivie commented 7 years ago

That is non-standard syntax, not supported in any flavor showdown supports (see babelmark2).

As such, is better suited for an extension, namely a listener extension (you can check a working example in this fiddle)

// Extension of type listener
showdown.extension('ins', function() {
  return [{
    type: 'listener',
    listeners: {
      // the extension is called right AFTER the parser parses italicsAndBold
      // and only during span gamut
      'italicsAndBold.after': function (event, text, options, globals) {
        return text.replace(/(?:\+){2}([\s\S]+?)(?:\+){2}/g, function (wm, txt) {
          if (options.simplifiedAutoLink) {
            txt = showdown.subParser('simplifiedAutoLinks')(txt, options, globals);
          }
          return '<ins>' + txt + '</ins>'
        });
      }
    }
  }];
});

As you know, ShowdownJS is a free library and it will remain free forever. However, maintaining and improving the library costs time and money. Currently, we're looking to improve showdown with automated tests in all browsers and a proper domain and webpage. 500$ should be enough to to keep showdown testing framework running for a year or two.

If you like our work and find our library useful, please donate through Pledgie or directly through paypal!! Your contribution will be greatly appreciated.

ckoppelman commented 7 years ago

I tried it out. Works perfectly. I understand why you'd want to limit the scope of the project.

For those who find this after me, this regex works a bit better by including trailing +'s and ensuring there's a non-whitespace character after the first ++ and before the last:

/(?:\+){2}(?=\S)([\s\S]*?\S)(?:\+){2}(?!\+)/g