@angular-package/prism
Version:
Simple Angular 5+ Prism highlighter module.
47 lines • 1.63 kB
JavaScript
import { ElementRef, Injectable, SecurityContext } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';
import Prism from 'prismjs';
import * as _ from 'lodash-es';
import { PrismInterface, OptionsInterface } from './prism.interface';
import { CallbackType } from './prism.type';
export class PrismService {
constructor(sanitizer) {
this.sanitizer = sanitizer;
}
highlight(el, options) {
if (el instanceof ElementRef) {
if (options.code) {
el.nativeElement.innerHTML = this.sanitizer.sanitize(SecurityContext.HTML, this.escapeHtml(options.code));
}
if (options.interpolation) {
el.nativeElement.innerHTML = this.interpolate(el.nativeElement.innerHTML, options.interpolation);
}
Prism.highlightElement(el.nativeElement, options.async, options.callback);
}
}
hooks() {
return Prism.hooks;
}
escapeHtml(unsafe) {
return unsafe
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
interpolate(string, interpolation) {
if (interpolation && typeof interpolation === 'object') {
_.templateSettings.interpolate = /{{([\s\S]+?)}}/g;
return _.template(string)(interpolation);
}
return string;
}
}
PrismService.decorators = [
{ type: Injectable },
];
PrismService.ctorParameters = () => [
{ type: DomSanitizer, },
];
//# sourceMappingURL=prism.service.js.map