angular-prism
Version:
Minimal PrismJS component for code highlighting in Angular 2/4
28 lines (25 loc) • 912 B
text/typescript
import { Component, Input, Renderer2, ElementRef, AfterViewInit } from '@angular/core';
declare var Prism: any;
export class PrismComponent implements AfterViewInit {
code: string;
language: string;
private preNode: Node;
private codeNode: Node;
private nativeElement: Node;
ngAfterViewInit () {
this.preNode = this._renderer.createElement('pre');
this.codeNode = this._renderer.createElement('code');
this._renderer.addClass(this.codeNode, 'language-' + this.language);
this._renderer.appendChild(this.nativeElement, this.preNode);
this._renderer.appendChild(this.preNode, this.codeNode);
this.codeNode.textContent = this.code;
Prism.highlightElement(this.codeNode);
}
constructor(private _renderer: Renderer2, private _el: ElementRef) {
this.nativeElement = _el.nativeElement;
}
}