Skip to content

Commit 3a100f2

Browse files
committed
Complete theme service and update docs
1 parent b3f5990 commit 3a100f2

File tree

8 files changed

+243
-58
lines changed

8 files changed

+243
-58
lines changed

README.md

Lines changed: 70 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,23 @@
2626
[![Sponsors](https://img.shields.io/github/sponsors/proangular?label=Sponsors)](https://github.com/sponsors/ProAngular)
2727
[![License](https://img.shields.io/npm/l/express.svg?maxAge=2592000)](/LICENSE)
2828

29-
# Information
29+
## Table of Contents
30+
31+
- [Information](#information)
32+
- [Description](#description)
33+
- [Demo](#demo)
34+
- [Installation](#installation)
35+
- [Dependencies](#dependencies)
36+
- [Usage](#usage)
37+
- [Component API](#api)
38+
- [Compatibility](#compatibility)
39+
- [Issues & Contribution](#development)
40+
- [Donations](#donations)
41+
- [License](#license)
42+
43+
<a name="information"/>
44+
45+
## Information
3046

3147
What is Gist? Gist is an easy method to share snippets or excerpts of data with others. A gist can be a string of code, a bash script or some other small piece of data. These bits of information are hosted by GitHub as a repository.
3248

@@ -35,19 +51,25 @@ More info in the following links:
3551
* GitHub Docs: https://docs.github.com/en/get-started/writing-on-github/editing-and-sharing-content-with-gists/creating-gists
3652
* Create a gist: https://gist.github.com/
3753

38-
# Description
54+
<a name="description"/>
55+
56+
## Description
3957

4058
Behold, this package contains an Angular Material and HighlighJs styled element which displays your GitHub gists in a conveniant, easy to view interface. Don't have a gist? No problem, display your own code snippets by just passing in the same model! All files from the remote/local gist are displayed in separate tabs for users to easily navigate. Many optional features and themes are available.
4159

4260
GitHub gists can be created here: https://gist.github.com/
4361

4462
Enjoy!
4563

46-
# Demo
64+
<a name="demo"/>
65+
66+
## Demo
4767

4868
TODO: Add demo here.
4969

50-
# Installation
70+
<a name="installation"/>
71+
72+
## Installation
5173

5274
```bash
5375
ng add @proangular/ngx-gist@latest
@@ -73,15 +95,17 @@ Import `NgxGistModule` where needed
7395
export class FeatureModule { }
7496
```
7597

76-
# Dependencies
98+
<a name="dependencies"/>
99+
100+
## Dependencies
77101

78102
### Styling
79103

80104
You should have an Angular Material theme set up prior to using this, but it's not strictly neccessary. Disabled by default, you can alternatively use the `materialTheme` component input (see API documentation below) to auto load a pre-defined Angular Material theme from a CDN. I don't recommend depending on the CDN or this approach, but I wanted to make it possible for you. It's also possible to use this without an angular theme and style it yourself entirely!
81105

82106
More information on theming Angular Material: https://material.angular.io/guide/theming
83107

84-
### Packages
108+
### Core Packages
85109

86110
Depending on how your project is set up, you may also need the following imports either in your `FeatureModule` for example or your root module (generally these are already imported in `AppModule` or similar, they are common features of Angular). These dependencies are needed for animating the Angular Material tabs click transition and making an HTTP request to GitHub to retrieve the remote gist information.
87111

@@ -104,7 +128,11 @@ Try using `ngx-gist` before importing these, you'll know if you need them if you
104128
export class AppModule { }
105129
```
106130

107-
# Usage
131+
See other `peerDependencies` you may need in the package.json in the root of this project.
132+
133+
<a name="usage"/>
134+
135+
## Usage
108136

109137
1. Provide your gist id into the input `gistId`, or
110138
2. Provide a direct `NgxGist` mobdel in the input `gist`.
@@ -130,6 +158,16 @@ Force no cache. This will force ngx-gist to retrieve the content live from GitHu
130158
></ngx-gist>
131159
```
132160

161+
### Setting the code style theme
162+
163+
Select a "highlight.js" code theme to apply. Note: Only one theme can be loaded on a single page at a time! The first theme to load will apply to all gists on the page.
164+
```html
165+
<ngx-gist
166+
codeTheme="github"
167+
gistId="gistId_123abc"
168+
></ngx-gist>
169+
```
170+
133171
### Displaying one specific file
134172

135173
Display only one specific file when your gist has many.
@@ -167,28 +205,34 @@ Line numbers are enabled by default, but you can turn them off like so.
167205
></ngx-gist>
168206
```
169207

170-
# Component API
208+
<a name="api"/>
171209

172-
| Input Name | Input Typing | Default Value | Description |
173-
| ------------------------ | ------------------------------------------ | ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
174-
| **displayOnlyFileNames** | `string \| readonly string[] \| undefined` | `undefined` | Display in the DOM only the selected filename(s) from the gists files array. Can be either a string or string array. File names much match exactly, be sure to remove any leading or trailing whitespace in the provided strings. |
175-
| **hideGistLink** | `bool` | `false` | Optionally hide the gist link which opens the gist on GitHub. The gist links automatically display for remote gists, but can be hidden with this feature. |
176-
| **gist** | `NgxGist \| undefined` | `undefined` | Provide a static gist model here directly which will be displayed if no `gistId` is provided for remote fetching. Also this model will be displayed should a fetch fail when retrieving `gistId`, or overwritten once the pertaining `gistId` data is fetched. |
177-
| **gistId** | `string` | `undefined` | Provide the GitHub gist id to be fetched and loaded. This can be found in URL of the gists you create. For example the id `TH1515TH31DT0C0PY` in: https://gist.github.com/YourUserName/TH1515TH31DT0C0PY. Alternatively, provide a value directly in the sibling input `gist`. |
178-
| **languageName** | `string \| undefined` | `undefined` | When defined, override automatic language detection [and styling] and treat all gists as this lanuage. See supported language strings here: https://github.com/highlightjs/highlight.js/blob/main/SUPPORTED_LANGUAGES.md |
179-
| **materialTheme** | `MaterialPrebuiltTheme \| undefined` | `undefined` | Define a material core theme to apply. Ideally, you should already have your global material theme set at the root of your project so try to avoid using this if possible. Note: These are loaded from the CDN: `https://unpkg.com` |
180-
| **showLineNumbers** | `bool` | `true` | Display or hide the line numbers in your gist code snippets. |
181-
| **useCache** | `bool` | `true` | Cache the GitHub gist request in local memory for 24 hours. GitHub has a request limit, so this helps in reducing bandwidth. Loads previously fetched gist content from the users machine on refresh and page re-visits. |
210+
## Component API
182211

183-
# Compatibility
212+
| Input Name | Input Typing | Default Value | Description |
213+
| ------------------------ | ------------------------------------ | ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
214+
| **codeTheme** | `HilightJsTheme` | `'default'` | The `highlight.js` code theme to use and display. Note: Only _one_ theme can be loaded on a single page at a time! The first theme to load will apply to all gists on the page. |
215+
| **displayOnlyFileNames** | `string \| string[] \| undefined` | `undefined` | Display in the DOM only the selected filename(s) from the gists files array. Can be either a string or string array. File names much match exactly, be sure to remove any leading or trailing whitespace in the provided strings. |
216+
| **gist** | `NgxGist \| undefined` | `undefined` | Provide a static gist model here directly which will be displayed if no `gistId` is provided for remote fetching. Also this model will be displayed should a fetch fail when retrieving `gistId`, or overwritten once the pertaining `gistId` data is fetched. |
217+
| **gistId** | `string` | `undefined` | Provide the GitHub gist id to be fetched and loaded. This can be found in URL of the gists you create. For example the id `TH1515TH31DT0C0PY` in: https://gist.github.com/YourUserName/TH1515TH31DT0C0PY. Alternatively, provide a value directly in the sibling input `gist`. |
218+
| **hideGistLink** | `bool` | `false` | Optionally hide the gist link which opens the gist on GitHub. The gist links automatically display for remote gists, but can be hidden with this feature. |
219+
| **materialTheme** | `MaterialPrebuiltTheme \| undefined` | `undefined` | Define a material core theme to apply. Ideally, you should already have your global material theme set at the root of your project so try to avoid using this if possible. Note: These are loaded from the CDN: `https://unpkg.com` |
220+
| **showLineNumbers** | `bool` | `true` | Display or hide the line numbers in your gist code snippets. |
221+
| **useCache** | `bool` | `true` | Cache the GitHub gist request in local memory for 24 hours. GitHub has a request limit, so this helps in reducing bandwidth. Loads previously fetched gist content from the users machine on refresh and page re-visits. |
222+
223+
<a name="compatibility"/>
224+
225+
## Compatibility
184226

185227
| Angular version | @proangular/ngx-gist | Install |
186228
| --------------- | -------------------------- | ------------------------------------ |
187229
| v14 | v1.x.x | `ng add @proangular/ngx-gist@latest` |
188230
| v13 | v1.x.x | `ng add @proangular/ngx-gist@latest` |
189231
| v12 | v1.x.x | `ng add @proangular/ngx-gist@latest` |
190232

191-
# Development
233+
<a name="development"/>
234+
235+
## Issues & Contribution
192236

193237
Please submit all issues, and feature requests here: [https://github.com/ProAngular/ngx-gist/issues](https://github.com/ProAngular/ngx-gist/issues)
194238

@@ -208,13 +252,17 @@ Contribution:
208252

209253
Thank you for any and all contributions!
210254

211-
# Donation
255+
<a name="donations"/>
256+
257+
## Donation
212258

213259
As a husband and father of four children, your donations mean the world to me! Any donations are greatly appreciated and keep me going!
214260
* [https://github.com/sponsors/ProAngular](https://github.com/sponsors/ProAngular)
215261
* [https://www.paypal.me/CodyTolene](https://www.paypal.me/CodyTolene)
216262

217-
# License
263+
<a name="license"/>
264+
265+
## License
218266

219267
Copyright &copy; 2022 [Cody Tolene](https://www.CodyTolene.com)
220268

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
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

Lines changed: 17 additions & 2 deletions
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

Lines changed: 126 additions & 16 deletions
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?: HilightJsTheme;
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 HilightJsTheme =
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';

0 commit comments

Comments
 (0)