Last active
February 18, 2019 00:03
-
-
Save DevEarley/b2353fca58ede6420b6a129ca84b2386 to your computer and use it in GitHub Desktop.
Using Marked and Highlight.js to view Markdown files in Angular
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, OnInit } from '@angular/core'; | |
import { ActivatedRoute } from '@angular/router'; | |
import { Marked } from 'marked-ts'; | |
import { highlight } from 'highlight.js'; | |
@Component({ | |
selector: 'app-doc', | |
template: ` | |
<div id="content-container" class="doc" [innerHTML]="markdownHTML" ></div>' ` | |
}) | |
export class MarkdownComponent implements OnInit { | |
markdownHTML: any = {}; | |
raw: any; | |
filename: string; | |
path: string; | |
title: string; | |
constructor(private route: ActivatedRoute, | |
private tabService: TabService) { | |
} | |
ngOnInit() { | |
this.reload(); | |
} | |
reload() { | |
this.filename = this.route.snapshot.params['name']; | |
this.path = this.route.snapshot.params['path']; | |
this.raw = this.route.snapshot.data['file']; | |
try { | |
Marked.setOptions({ | |
highlight: (code, lang) => | |
highlight(lang, code).value | |
}); | |
this.markdownHTML = Marked.parse(this.raw.content); | |
} | |
catch (e) { | |
Marked.setOptions({ | |
highlight: (code, lang) => | |
highlight("js", code).value | |
}); | |
this.markdownHTML = Marked.parse(this.raw.content); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, OnInit } from '@angular/core'; | |
import { ActivatedRoute } from '@angular/router'; | |
import { Marked } from 'marked-ts'; | |
import { highlight } from 'highlight.js'; | |
@Component({ | |
selector: 'app-doc', | |
template: ` | |
<div id="content-container" class="doc" [innerHTML]="markdownHTML" ></div>' ` | |
}) | |
export class MarkdownComponent implements OnInit { | |
markdownHTML: any = {}; | |
raw: any; | |
filename: string; | |
path: string; | |
title: string; | |
constructor(private route: ActivatedRoute, | |
private tabService: TabService) { | |
} | |
ngOnInit() { | |
this.reload(); | |
} | |
reload() { | |
this.filename = this.route.snapshot.params['name']; | |
this.path = this.route.snapshot.params['path']; | |
this.raw = this.route.snapshot.data['file']; | |
try { | |
Marked.setOptions({ | |
highlight: (code, lang) => | |
highlight(lang, code).value | |
}); | |
this.markdownHTML = Marked.parse(this.raw.content); | |
} | |
catch (e) { | |
Marked.setOptions({ | |
highlight: (code, lang) => | |
highlight("js", code).value | |
}); | |
this.markdownHTML = Marked.parse(this.raw.content); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment