@@ -6,6 +6,12 @@ import { Language } from 'highlight.js';
66import { BehaviorSubject , filter , firstValueFrom , ReplaySubject } from 'rxjs' ;
77import { UntilDestroy , untilDestroyed } from '@ngneat/until-destroy' ;
88import { DOCUMENT } from '@angular/common' ;
9+ import { NgxGistLineNumbersService } from './ngx-gist-line-numbers.service' ;
10+ import { DomSanitizer , SafeHtml } from '@angular/platform-browser' ;
11+ import {
12+ MaterialPrebuiltTheme ,
13+ NgxGistThemeService ,
14+ } from './ngx-gist-theme.service' ;
915
1016@UntilDestroy ( )
1117@Component ( {
@@ -21,7 +27,13 @@ import { DOCUMENT } from '@angular/common';
2127 [label]="file.filename"
2228 >
2329 <pre>
24- <code [innerHTML]="file.highlightedContent"></code>
30+ <code
31+ *ngIf="applyLineNumbers(file.highlightedContent) as content"
32+ [innerHTML]="content"
33+ ></code>
34+ <ng-template #error>
35+ <code>Error loading code...</code>
36+ </ng-template>
2537 </pre>
2638 </mat-tab>
2739 </mat-tab-group>
@@ -43,14 +55,12 @@ import { DOCUMENT } from '@angular/common';
4355} )
4456export class NgxGistComponent implements OnInit {
4557 public constructor (
46- @Inject ( DOCUMENT )
47- private readonly document : Document ,
58+ @Inject ( DOCUMENT ) private readonly document : Document ,
59+ private readonly domSanitizer : DomSanitizer ,
4860 private readonly ngxGistService : NgxGistService ,
61+ private readonly ngxGistLineNumbersService : NgxGistLineNumbersService ,
62+ private readonly ngxGistThemeService : NgxGistThemeService ,
4963 ) { }
50-
51- public codeSnippet : string | null = null ;
52- private htmlLinkElement : HTMLLinkElement | null = null ;
53-
5464 /**
5565 * Display in the DOM only the selected filename(s) from the gists files array.
5666 *
@@ -122,12 +132,13 @@ export class NgxGistComponent implements OnInit {
122132 * Tip: See theming Angular Material: https://material.angular.io/guide/theming
123133 * if you need help applying a global material theme.
124134 */
125- @Input ( ) public materialTheme :
126- | 'deeppurple-amber'
127- | 'indigo-pink'
128- | 'pink-bluegrey'
129- | 'purple-green'
130- | undefined = undefined ;
135+ @Input ( ) public materialTheme : MaterialPrebuiltTheme | undefined = undefined ;
136+ /**
137+ * Display or hide the line numbers in your gist code snippets.
138+ *
139+ * Default: `true`
140+ */
141+ @Input ( ) public showLineNumbers = true ;
131142 /**
132143 * Cache the GitHub gist request in local memory for 24 hours. GitHub has a
133144 * request limit, so this helps in reducing bandwidth. Loads previously
@@ -140,6 +151,10 @@ export class NgxGistComponent implements OnInit {
140151 public async ngOnInit ( ) : Promise < void > {
141152 this . setTheme ( ) ;
142153
154+ if ( this . showLineNumbers ) {
155+ await this . ngxGistLineNumbersService . load ( ) ;
156+ }
157+
143158 this . gistIdChanges
144159 . pipe ( filter ( isNonEmptyValue ) , untilDestroyed ( this ) )
145160 . subscribe ( async ( gistId ) => {
@@ -174,12 +189,19 @@ export class NgxGistComponent implements OnInit {
174189 if ( ! this . materialTheme ) {
175190 return ;
176191 }
192+ this . ngxGistThemeService . setTheme ( this . materialTheme ) ;
193+ }
177194
178- this . htmlLinkElement = this . document . createElement ( 'link' ) ;
179- this . htmlLinkElement . href = `https://unpkg.com/@angular/material@14.1.0/prebuilt-themes/${ this . materialTheme } .css` ;
180- this . htmlLinkElement . media = 'screen,print' ;
181- this . htmlLinkElement . rel = 'stylesheet' ;
182- this . htmlLinkElement . type = 'text/css' ;
183- this . document . head . appendChild ( this . htmlLinkElement ) ;
195+ public applyLineNumbers ( highlightedConent : string ) : SafeHtml | null {
196+ if (
197+ this . showLineNumbers &&
198+ this . document . defaultView ?. hljs &&
199+ typeof this . document . defaultView . hljs . lineNumbersValue === 'function'
200+ ) {
201+ return this . domSanitizer . bypassSecurityTrustHtml (
202+ this . document . defaultView . hljs . lineNumbersValue ( highlightedConent ) ,
203+ ) ;
204+ }
205+ return highlightedConent ;
184206 }
185207}
0 commit comments