Skip to content

Instantly share code, notes, and snippets.

@DevEarley
Last active February 18, 2019 00:03
Show Gist options
  • Save DevEarley/b2353fca58ede6420b6a129ca84b2386 to your computer and use it in GitHub Desktop.
Save DevEarley/b2353fca58ede6420b6a129ca84b2386 to your computer and use it in GitHub Desktop.
Using Marked and Highlight.js to view Markdown files in Angular
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);
}
}
}
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