Ionic2 Pipe with 3rd party libray


#1

I am looking for a way to use a 3d party library (https://github.com/borgar/textile-js) in an ionic2 Pipe. I am trying to make a textile pipe so i can access news feed and parse with {{ post.body | textile }}

I have the pipe set up, I just do not know how to include the textile library into the pipe… any help would be appreciated.


#2

So not sure about textile, but I created a simple pipe for markdown

import {Injectable, Pipe} from 'angular2/core';
import * as marked from 'marked'

@Injectable()

@Pipe({ name: 'markdown' })
export class Markdown {
  transform(value, args) {
    value = value + '';
    return marked(value)
  }
}

In a nutshell, npm install the package, and then import it.

npm install textile-js

and then you could use it like

import {Injectable, Pipe} from 'angular2/core';
import * as textile from 'textile-js'
@Pipe({
  name: 'textile'
})
@Injectable()
export class Textile {
  transform(value, args) {
    value = value + ''; // make sure it's a string
    return textile(value);
  }
}

#3

Thank you, that worked perfectly.

I was having issues with the line import * as textile from 'textile-js';

Much appreciated!

Anyone looking to do the same, the code was as follows…

import {Injectable, Pipe } from 'angular2/core';
import * as textile from 'textile-js';

@Pipe({
  name: 'textile'
})
@Injectable()
export class Textile {
  transform(value, args) {
    value = value + '';
    return textile.parse(value);
  }

}

#4

Did you have to add it to packages.json file? I’m having issues trying to import xm2json package.


#5

I believe I just used npm install [package] --save and then referenced the package in the import. You should be able to add "xml2json":"0.9.0" to your package.json and then run npm install.

Hope it helps…


#6

Thanks, I tried x2js just like you said, only installing via npm and worked. No need to add it to packages.json. It seems to me that no all packages are prepared to be imported by ionic 2.