UNPKG

angular-prism

Version:

Minimal PrismJS component for code highlighting in Angular 2/4

28 lines (25 loc) 912 B
import { Component, Input, Renderer2, ElementRef, AfterViewInit } from '@angular/core'; declare var Prism: any; @Component({ selector: 'prism-block', template: ``, }) export class PrismComponent implements AfterViewInit { @Input() code: string; @Input() 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; } }