I'm trying to make a simple Markdown inline editor with Angular2. I tryed several approaches but none seems to work. I installed marked with npm and it is now visible in the projects node_modules directory. I can import it and it is recognized by netbeans. Now when ever I use it nothing works and if I open the firefox debuger then i find localhost:3000/marked not found.
I put the markdown converter in a service. Which looks the following:
import { Injectable } from 'angular2/core';
import * as marked from 'marked';
interface IMarkdownConfig {
sanitize?: boolean,
gfm?: boolean,
breaks?: boolean,
smartypants?: boolean
}
@Injectable()
export class MarkdownService {
//private md: MarkedStatic;
constructor() {
//this.md = marked.setOptions({});
}
setConfig(config: IMarkdownConfig) {
// this.md = marked.setOptions(config);
}
convert(markdown: string): string {
if(!markdown) {
return '';
}
return markdown;
//return this.md.parse(markdown);
}
}
used like this everything works fine, except that markdown is not translated. If I uncomment all lines with md it stops working. The component in which I'm using it looks like that:
import {Component, Input, OnInit } from 'angular2/core';
import {RouteParams} from 'angular2/router';
import {MarkdownService} from '../services/markdown-converter'
@Component({
selector: 'my-story',
templateUrl: 'app/components/story.html',
bindings: [MarkdownService]
})
export class StoryComponent implements OnInit {
public html: string;
private md: MarkdownService;
constructor(
private _routeParams: RouteParams, private _converter: MarkdownService) {
this.html ='';
this.md = _converter;
}
ngOnInit() {
}
public updateValue(val:string) {
if(!val) { return ''; }
this.html = val;
}
}
I'm not displaying all the involved files but If there is a file I should provide just ask in the comments. If it is something I didn't get right about Typescript and or Angular2 injection or whatever, any resource of information explaining those concepts is welcome. I have read a lot on the www but it seems that all documents about Angular2 are quite fast outdated.
I would import the marked library this way:
and use it as you do:
See this plunkr: https://plnkr.co/edit/zUstS3T7IJxjQUVCXiAM?p=preview.
This question could also help you:
I finally managed to solve my problem.
I mentioned marked in my index.html file twice once to include the script I installed with npm and once I maped the name marked to
https://cdnjs.cloudflare.com/ajax/libs/marked/0.3.5/marked.js
if I change this tonode_modules/marked/marked.min.js
it doesn't work anymore and I get strange GET 404 errors for my project files.Then I had some entrys for marked in my
typings.json
andtsconfig.json
which where suggested on some sites. I removed those. Finally I left the"marked": "^0.3.5"
entry in mypackage.json
file. This is how my index.html looks like:and this is my package.json:
I found a lot of similar questions and tryed all their answers as well as the one given by Thierry Templier here but for some reason none of them where working for me, or I was not capable of reproducing them. I feel like there should be a typescript install of marked some where but where ever I add it it doesn't work.
This project provides some directives for using Markdown with Angular 2. And it is also using the Marked library.
https://github.com/dimpu/angular2-markdown