npm i ngx-highlight-js
app.module.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import 'zone.js'; import { bootstrapApplication } from '@angular/platform-browser'; import { AppComponent } from './app/app.component'; import { HighlightJsConfig, HIGHLIGHTJS_CONFIG } from 'ngx-highlight-js'; bootstrapApplication(AppComponent, { providers: [ { provide: HIGHLIGHTJS_CONFIG, useValue: { lang: 'html' } as HighlightJsConfig, }, ], }).catch((err) => console.error(err)); |
app.component.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
import { Component, OnInit } from "@angular/core"; import { CommonModule } from '@angular/common'; import { FormsModule } from '@angular/forms'; import { HighlightJsDirective } from 'ngx-highlight-js'; const r = (min: number, max: number) => Math.floor(Math.random() * (max - min + 1) + min); @Component({ selector: "my-app", templateUrl: "./app.component.html", standalone: true, imports: [CommonModule, FormsModule, HighlightJsDirective], }) export class AppComponent implements OnInit { switchStatus = true; html = `<textarea highlight-js [lang]="'html'" [ngModel]="html"></textarea>`; random = ``; code = ` <p> The bare minimum for using highlight.js on a web page is linking to the library along with one of the styles and calling <a href="http://highlightjs.readthedocs.io/en/latest/api.html#inithighlightingonload"><code>initHighlightingOnLoad</code></a >: </p> <pre><code class="language-html"><link rel="stylesheet" href="/path/to/styles/default.css"> <script src="/path/to/highlight.min.js"></script> <script>hljs.initHighlightingOnLoad();</script> </code></pre> <p> This will find and highlight code inside of <code><pre><code></code> tags; it tries to detect the language automatically. If automatic detection doesn’t work for you, you can specify the language in the <code>class</code> attribute: </p> <pre><code class="language-html"><pre><code class="html">...</code></pre> </code></pre> `; private getHtml(): string { const DATA = [ `<a href="">1</a>`, `<div>2</div>`, `<span>3</span>`, `<i>4</i>`, `<p>5</p>` ]; return DATA[r(0, DATA.length - 1)]; } updateHTML(): void { this.html = this.getHtml(); } randomHtml(): void { this.random = this.getHtml(); } ngOnInit(): void { this.randomHtml(); } } |
app.component.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
<div class="row mb-5"> <div class="col-sm-12"> <h5 class="mt-3 mb-3">Installation instructions</h5> <h6 class="mb-2">1、Install</h6> <textarea highlight-js [lang]="'bash'"> npm install --save ngx-highlight-js</textarea > </div> <div class="col-sm-12"> <h6 class="mb-2"> 2、Import the `HighlightJsModule` in to your root `AppModule`. </h6> <textarea highlight-js lang="ts"> import { HighlightJsModule } from 'ngx-highlight-js'; @NgModule({ imports: [ HighlightJsModule ], bootstrap: [AppComponent] }) export class AppModule { } </textarea > </div> <div class="col-sm-12"> <h6 class="mb-2">3、Add highlight.js</h6> <p>Load the highlight.js and theme css in page.</p> <textarea highlight-js> <script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.18.1/highlight.min.js"></script> </textarea > <textarea highlight-js> <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.18.1/styles/atom-one-dark.min.css"> </textarea > </div> <div class="col-sm-12"> <h5 class="mt-5 mb-3">Only `<textarea>` Tag</h5> <div class="row"> <div class="col-sm-6"> <textarea highlight-js [lang]="'html'" *ngIf="switchStatus"> <textarea highlight-js [options]="{}" [lang]="'typescript'"> /* tslint:disable */ import { Component } from '@angular/core'; @Component({ selector: 'demo', templateUrl: './demo.component.html', styleUrls: ['./demo.component.scss'] }) export class DemoComponent { switchStatus: boolean = true; } </textarea> </textarea > <button class="btn btn-primary btn-sm mb-3" (click)="switchStatus = !switchStatus" > {{ switchStatus ? 'Hide' : 'Show' }} </button> </div> <div class="col-sm-6"> <p> <strong>[options]</strong> equar <a href="http://highlightjs.readthedocs.io/en/latest/api.html#configure-options" target="_blank" >configure(options)</a >. (optional) </p> <p> <strong>[lang]</strong> uses language detection by default but you can specify the language. (optional) </p> </div> </div> <h5 class="mt-5 mb-3">Support `ngModel`</h5> <div class="row"> <div class="col-sm-12"> <textarea highlight-js [lang]="'html'" [ngModel]="html"></textarea> <button class="btn btn-primary btn-sm mb-3" (click)="updateHTML()"> Update </button> </div> </div> <h5 class="mt-5 mb-3">Observe Content</h5> <div class="row"> <div class="col-sm-12"> <textarea highlight-js [lang]="'html'"> {{ random }} </textarea> <button class="btn btn-primary btn-sm mb-3" (click)="randomHtml()"> Random Html </button> </div> </div> <h5 class="mt-5 mb-3">Default mode</h5> <div class="row"> <div class="col-sm-12"> <p>Should be will render each <pre><code></p> <textarea highlight-js> <div [innerHTML]="code" highlight-js mode="default"></div></textarea > <hr /> <h6>Result</h6> <div [innerHTML]="code" highlight-js mode="default"></div> </div> </div> <p class="mt-5 mb-5"> <strong>no more</strong> </p> </div> </div> |
FULL SOURCE CODE