I'm building a CMS and I'm trying to re-render Prism.js when the body of the article changes to display the syntax highlignting in the preview.
The template:
<textarea
[(ngModel)]="article.body"
(ngModelChange)="onBodyChange($event)"
formControlName="body" type="text" name="body">
</textarea>
The Component ( The ngAfterViewChecked() works but the onBodyChange() doesn't ):
onBodyChange() {
this.highlightService.highlightAll();
this.highlighted = true;
}
ngAfterViewChecked() {
if (this.article && !this.highlighted) {
this.highlightService.highlightAll();
this.highlighted = true;
}
}
Some people suggest to re-render Prism.js using the following method but I'm a bit confused on how to implement it:
Prism.highlightElement(precode);
This is the service:
import { Injectable, Inject } from '@angular/core';
import { PLATFORM_ID } from '@angular/core';
import { isPlatformBrowser } from '@angular/common';
import 'clipboard';
import 'prismjs';
import 'prismjs/plugins/toolbar/prism-toolbar';
import 'prismjs/plugins/copy-to-clipboard/prism-copy-to-clipboard';
import 'prismjs/components/prism-css';
import 'prismjs/components/prism-javascript';
import 'prismjs/components/prism-markup';
import 'prismjs/components/prism-typescript';
import 'prismjs/components/prism-scss';
declare var Prism: any;
@Injectable()
export class HighlightService {
constructor(@Inject(PLATFORM_ID) private platformId: Object) { }
highlightAll() {
if (isPlatformBrowser(this.platformId)) {
Prism.highlightAll();
}
}
}