Skip to content

Commit 8b680c1

Browse files
authored
Merge pull request #3 from ProAngular/ct/doc-and-style-updates
Readme updates
2 parents da12a83 + 92b14cc commit 8b680c1

9 files changed

+312
-76
lines changed

README.md

+139-40
Large diffs are not rendered by default.

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@proangular/ngx-gist",
3-
"version": "1.0.4",
3+
"version": "1.0.5",
44
"description": "An Angular Material and HighlighJs styled display box for GitHub gist and local code snippets.",
55
"author": "Pro Angular <webmaster@proangular.com>",
66
"homepage": "https://www.proangular.com",

src/app/app.component.ts

+17-2
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,32 @@ import { Component } from '@angular/core';
3333
[useCache]="false"
3434
></ngx-gist>
3535
36+
<h4>SETTING THE CODE STYLE THEME</h4>
37+
<p>
38+
Select a "highlight.js" code theme to apply. Note: Only one theme can be
39+
loaded on a single page at a time! The first theme to load will apply to
40+
all gists on the page.
41+
</p>
42+
<ngx-gist
43+
codeTheme="github"
44+
gistId="d55ea012b585a16a9970878d90106d74"
45+
></ngx-gist>
46+
3647
<h4>DISPLAYING ONE SPECIFIC FILE</h4>
3748
<p>Display only one specific file when your gist has many.</p>
3849
<ngx-gist
39-
displayOnlyFileNames="super.js"
50+
displayOnlyFileNames="javascript.js"
4051
gistId="d55ea012b585a16a9970878d90106d74"
4152
></ngx-gist>
4253
4354
<h4>DISPLAYING MULTIPLE, SPECIFIC FILES</h4>
4455
<p>You can also display any number of specific files by name.</p>
4556
<ngx-gist
46-
[displayOnlyFileNames]="['csstest.css', 'main.ts']"
57+
[displayOnlyFileNames]="[
58+
'markup.html',
59+
'typescript.ts',
60+
'stylesheet.scss'
61+
]"
4762
gistId="d55ea012b585a16a9970878d90106d74"
4863
></ngx-gist>
4964

src/app/public/ngx-gist-theme.service.ts

+126-16
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,63 @@ export class NgxGistThemeService {
66
public constructor(@Inject(DOCUMENT) private readonly document: Document) {}
77

88
private importElMaterialTheme: HTMLLinkElement | null = null;
9+
private importElHljsTheme: HTMLLinkElement | null = null;
910

10-
public setTheme(materialPrebuiltTheme: MaterialPrebuiltTheme): void {
11-
const themeId = 'material-theme-import';
12-
const currentEl = this.document.getElementById(themeId);
13-
14-
if (currentEl) {
15-
this.document.removeChild(currentEl);
11+
public setTheme({
12+
materialTheme,
13+
hilightJsTheme,
14+
}: {
15+
materialTheme?: MaterialPrebuiltTheme;
16+
hilightJsTheme?: HighlightJsTheme;
17+
} = {}): void {
18+
if (!materialTheme && !hilightJsTheme) {
19+
throw new Error('You must provide a theme.');
1620
}
1721

18-
if (this.importElMaterialTheme) {
19-
this.document.removeChild(this.importElMaterialTheme);
20-
}
22+
const materialThemeId = 'material-theme-import';
23+
const hljsThemeId = 'hljs-theme-import';
2124

22-
this.importElMaterialTheme = this.document.createElement('link');
23-
this.importElMaterialTheme.href = `https://unpkg.com/@angular/material@14.1.0/prebuilt-themes/${materialPrebuiltTheme}.css`;
24-
this.importElMaterialTheme.media = 'screen,print';
25-
this.importElMaterialTheme.rel = 'stylesheet';
26-
this.importElMaterialTheme.type = 'text/css';
27-
this.importElMaterialTheme.id = themeId;
25+
const materialThemeLinkEl = this.document.getElementById(materialThemeId);
26+
if (materialThemeLinkEl && !hilightJsTheme) {
27+
// Material theme aleady exists, return.
28+
return;
29+
}
2830

29-
this.document.head.appendChild(this.importElMaterialTheme);
31+
const hljsThemeLinkEl = this.document.getElementById(hljsThemeId);
32+
if (hljsThemeLinkEl && hilightJsTheme === 'default' && !materialTheme) {
33+
// Default theme already in use, return.
34+
console.log('returned');
35+
return;
36+
} else if (
37+
hljsThemeLinkEl &&
38+
hilightJsTheme !== 'default' &&
39+
!materialTheme
40+
) {
41+
// Override previously used theme, but remove it first.
42+
console.log('remove');
43+
this.document.head.removeChild(hljsThemeLinkEl);
44+
}
45+
console.log('info: ', materialTheme, hilightJsTheme);
46+
if (materialTheme) {
47+
// !!! Update version when needed.
48+
const version = '14.1.0';
49+
const url = `@angular/material@${version}/prebuilt-themes/${materialTheme}.css`;
50+
this.importElMaterialTheme = this.document.createElement('link');
51+
this.importElMaterialTheme.href = `https://unpkg.com/${url}`;
52+
this.importElMaterialTheme.rel = 'stylesheet';
53+
this.importElMaterialTheme.id = materialThemeId;
54+
this.document.head.appendChild(this.importElMaterialTheme);
55+
} else if (hilightJsTheme) {
56+
console.log('apply', hilightJsTheme);
57+
// !!! Update version when needed.
58+
const version = '11.6.0';
59+
const url = `highlight.js@${version}/styles/${hilightJsTheme}.css`;
60+
this.importElHljsTheme = this.document.createElement('link');
61+
this.importElHljsTheme.href = `https://unpkg.com/${url}`;
62+
this.importElHljsTheme.rel = 'stylesheet';
63+
this.importElHljsTheme.id = hljsThemeId;
64+
this.document.head.appendChild(this.importElHljsTheme);
65+
}
3066
}
3167
}
3268

@@ -35,3 +71,77 @@ export type MaterialPrebuiltTheme =
3571
| 'indigo-pink'
3672
| 'pink-bluegrey'
3773
| 'purple-green';
74+
75+
export type HighlightJsTheme =
76+
| 'a11y-dark'
77+
| 'a11y-light'
78+
| 'agate'
79+
| 'androidstudio'
80+
| 'an-old-hope'
81+
| 'arduino-light'
82+
| 'arta'
83+
| 'ascetic'
84+
| 'atom-one-dark'
85+
| 'atom-one-dark-reasonable'
86+
| 'atom-one-light'
87+
| 'brown-paper'
88+
| 'codepen-embed'
89+
| 'color-brewer'
90+
| 'dark'
91+
| 'default'
92+
| 'devibeans'
93+
| 'docco'
94+
| 'far'
95+
| 'felipec'
96+
| 'foundation'
97+
| 'github'
98+
| 'github-dark'
99+
| 'github-dark-dimmed'
100+
| 'gml'
101+
| 'googlecode'
102+
| 'gradient-dark'
103+
| 'gradient-light'
104+
| 'grayscale'
105+
| 'hybrid'
106+
| 'idea'
107+
| 'intellij-light'
108+
| 'ir-black'
109+
| 'isbl-editor-dark'
110+
| 'isbl-editor-light'
111+
| 'kimbie-dark'
112+
| 'kimbie-light'
113+
| 'lightfair'
114+
| 'lioshi'
115+
| 'magula'
116+
| 'mono-blue'
117+
| 'monokai'
118+
| 'monokai-sublime'
119+
| 'night-owl'
120+
| 'nnfx-dark'
121+
| 'nnfx-light'
122+
| 'nord'
123+
| 'obsidian'
124+
| 'panda-syntax-dark'
125+
| 'panda-syntax-light'
126+
| 'paraiso-dark'
127+
| 'paraiso-light'
128+
| 'pojoaque'
129+
| 'purebasic'
130+
| 'qtcreator-dark'
131+
| 'qtcreator-light'
132+
| 'rainbow'
133+
| 'routeros'
134+
| 'school-book'
135+
| 'shades-of-purple'
136+
| 'srcery'
137+
| 'stackoverflow-dark'
138+
| 'stackoverflow-light'
139+
| 'sunburst'
140+
| 'tokyo-night-dark'
141+
| 'tokyo-night-light'
142+
| 'tomorrow-night-blue'
143+
| 'tomorrow-night-bright'
144+
| 'vs'
145+
| 'vs2015'
146+
| 'xcode'
147+
| 'xt256';

src/app/public/ngx-gist.component.ts

+22-16
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ import { NgxGistService } from './ngx-gist.service';
22
import { isNonEmptyValue } from './ngx-gist.utilities';
33
import { NgxGist } from './ngx-gist.model';
44
import { Component, Inject, Input, OnInit } from '@angular/core';
5-
import { Language } from 'highlight.js';
65
import { BehaviorSubject, filter, firstValueFrom, ReplaySubject } from 'rxjs';
76
import { UntilDestroy, untilDestroyed } from '@ngneat/until-destroy';
87
import { DOCUMENT } from '@angular/common';
98
import { NgxGistLineNumbersService } from './ngx-gist-line-numbers.service';
109
import { DomSanitizer, SafeHtml } from '@angular/platform-browser';
1110
import {
11+
HighlightJsTheme,
1212
MaterialPrebuiltTheme,
1313
NgxGistThemeService,
1414
} from './ngx-gist-theme.service';
@@ -27,7 +27,7 @@ import {
2727
[label]="file.filename"
2828
>
2929
<pre>
30-
<code
30+
<code
3131
*ngIf="applyLineNumbers(file.highlightedContent) as content"
3232
[innerHTML]="content"
3333
></code>
@@ -57,14 +57,10 @@ export class NgxGistComponent implements OnInit {
5757
public constructor(
5858
@Inject(DOCUMENT) private readonly document: Document,
5959
private readonly domSanitizer: DomSanitizer,
60-
private readonly ngxGistService: NgxGistService,
6160
private readonly ngxGistLineNumbersService: NgxGistLineNumbersService,
61+
private readonly ngxGistService: NgxGistService,
6262
private readonly ngxGistThemeService: NgxGistThemeService,
6363
) {}
64-
65-
// TODO: Apply HighlightJs code theme.
66-
// @Input() public codeTheme?: unknown;
67-
6864
/**
6965
* Display in the DOM only the selected filename(s) from the gists files array.
7066
*
@@ -115,15 +111,14 @@ export class NgxGistComponent implements OnInit {
115111
>(1);
116112
public readonly gistIdChanges = this.gistIdSubject.asObservable();
117113
/**
118-
* When defined, override automatic language detection [and styling] and
119-
* treat all gists as this lanuage.
114+
* The `highlight.js` code theme to use and display.
120115
*
121-
* Default: `undefined`
116+
* Default: `'default'`
122117
*
123-
* Tip: See supported language strings here:
124-
* https://github.com/highlightjs/highlight.js/blob/main/SUPPORTED_LANGUAGES.md
118+
* Note: Only _one_ theme can be loaded on a single page at a time! The first
119+
* theme to load will apply to all gists on the page.
125120
*/
126-
@Input() public languageName?: Language['name'];
121+
@Input() public codeTheme: HighlightJsTheme = 'default';
127122
/**
128123
* Define a material core theme to apply. Ideally, you should already have
129124
* your global material theme set at the root of your project so try to
@@ -153,12 +148,16 @@ export class NgxGistComponent implements OnInit {
153148
@Input() public useCache = true;
154149

155150
public async ngOnInit(): Promise<void> {
156-
this.setTheme();
151+
// Load themes
152+
this.setMaterialTheme();
153+
this.setHljsTheme();
157154

155+
// Load line numbers
158156
if (this.showLineNumbers) {
159157
await this.ngxGistLineNumbersService.load();
160158
}
161159

160+
// Load gist(s) async
162161
this.gistIdChanges
163162
.pipe(filter(isNonEmptyValue), untilDestroyed(this))
164163
.subscribe(async (gistId) => {
@@ -189,11 +188,18 @@ export class NgxGistComponent implements OnInit {
189188
}
190189
}
191190

192-
private setTheme(): void {
191+
private setHljsTheme(): void {
192+
if (!this.codeTheme) {
193+
return;
194+
}
195+
this.ngxGistThemeService.setTheme({ hilightJsTheme: this.codeTheme });
196+
}
197+
198+
private setMaterialTheme(): void {
193199
if (!this.materialTheme) {
194200
return;
195201
}
196-
this.ngxGistThemeService.setTheme(this.materialTheme);
202+
this.ngxGistThemeService.setTheme({ materialTheme: this.materialTheme });
197203
}
198204

199205
public applyLineNumbers(highlightedConent: string): SafeHtml | null {

src/app/public/public.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
/** Public API Exports for Node Package */
22

3+
export {
4+
HighlightJsTheme,
5+
MaterialPrebuiltTheme,
6+
} from './ngx-gist-theme.service';
37
export * from './ngx-gist.component';
48
export * from './ngx-gist.model';
59
export * from './ngx-gist.module';

src/assets/images/demo-gist.gif

199 KB
Loading

src/styles.scss

+2
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@ body {
1717
margin: 0;
1818
background-color: #faf9f6;
1919
}
20+
21+
@import "~highlight.js/styles/github.css";

0 commit comments

Comments
 (0)