0

I'm using Markdown-it in my Angular 2 project. I want to create a plugin to extend Markdown-it. I have the basic Markdown-it working and can add a javascript plugin like this

PluginSetup(md) {
    console.log('Enter PluginSetup');
    md.inline.ruler.after('emphasis', 'cards', function x(state, silent) {
        return false;
    });
}

I see the console message and everything works fine. Now I want the callback to invoke some Typescript. I've tried

export class PlugIns {
   CardSymbols(state, silent) {
      console.log('Enter CardSymbols');
      return false;
   }

   PluginSetup(md) {
       console.log('Enter PluginSetp');
       md.inline.ruler.after('emphasis', 'cards', function x(state, silent) {
           console.log('Enter x');
           return this.CardsSymbols(state, silent);
       });
   }
}

I see the 'Enter x' console message and then

EXCEPTION: Error in ./ArticleDisplayComponent class ArticleDisplayComponent - inline template:0:5 caused by: Object doesn't support property or method 'CardSymbols'

How do I call Typescript from a javascript callback?

RESOLVED

None of the suggestions or referenced articles changed the error message but I do have a resolution.

md.inline.ruler.after('emphasis', 'cards', Plugins.prototype.CardSymbols);

Thanks for all the suggestions

PaulBBO
  • 103
  • 2
  • It's not a JS callback - your whole code is in typescript. – zerkms Jun 11 '17 at 22:58
  • 3
    Possible duplicate of [How to access the correct \`this\` context inside a callback?](https://stackoverflow.com/questions/20279484/how-to-access-the-correct-this-context-inside-a-callback) – Mattias Buelens Jun 11 '17 at 23:05
  • 1
    This is not because of JavaScript code calling TypeScript code (In fact, TypeScript is compiled into JavaScript, so the browser only sees JavaScript.) Rather, it's because of a very common pitfall with `this` in JavaScript. The quick fix would be to use an arrow function: replace `function x(a,b) {...}` with `(a,b) => {...}` For more information, see [this duplicated question](https://stackoverflow.com/a/20279485/1321716). – Mattias Buelens Jun 11 '17 at 23:09

0 Answers0