Skip to content

Commit 6021761

Browse files
committed
Wrap up additional features
1 parent da92026 commit 6021761

File tree

5 files changed

+131
-38
lines changed

5 files changed

+131
-38
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<p align="center">
22
<a href="https://www.ProAngular.com" target="_blank">
3-
<img src="src/assets/images/pro-angular-logo.png" />
3+
<img src="https://github.com/ProAngular/ngx-gist/raw/main/src/assets/images/pro-angular-logo.png" />
44
</a>
55
<h1 align="center">
66
<a href="https://www.ProAngular.com" target="_blank">

src/app/app.component.ts

Lines changed: 68 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { NgxGist } from './public/ngx-gist.model';
12
import { Component } from '@angular/core';
23

34
@Component({
@@ -9,26 +10,82 @@ import { Component } from '@angular/core';
910
<h3 align="center">
1011
Examples of displaying local and GitHub gists and code snippets.
1112
</h3>
12-
<!-- EXAMPLE: FETCH _NEW_ GIST FROM GITHUB (NOT-SAVED) -->
13+
14+
<hr />
15+
16+
<h4>FETCHED GIST (AUTO CACHED FOR 24 HOURS)</h4>
1317
<ngx-gist gistId="d55ea012b585a16a9970878d90106d74"></ngx-gist>
14-
<!-- EXAMPLE: FETCH _CACHED_ GIST FROM MEMORY (ON SUBSEQUENT REQUESTS) -->
18+
19+
<h4>FETCHED GIST (FORCED NO CACHE)</h4>
1520
<ngx-gist
1621
gistId="d55ea012b585a16a9970878d90106d74"
17-
[useCache]="true"
22+
[useCache]="false"
1823
></ngx-gist>
19-
<!-- EXAMPLE: DISPLAYING A SPECIFIC FILE -->
24+
25+
<h4>DISPLAYING ONE SPECIFIC FILE</h4>
26+
<p>Display only one specific file when your gist has many.</p>
2027
<ngx-gist
21-
displayOnlyFileName="super.js"
28+
displayOnlyFileNames="super.js"
2229
gistId="d55ea012b585a16a9970878d90106d74"
23-
[useCache]="true"
2430
></ngx-gist>
25-
<!-- TODO: SUPPORT LOCAL GIST -->
26-
<!--
31+
32+
<h4>DISPLAYING MULTIPLE, SPECIFIC FILES</h4>
33+
<p>You can also display any number of specific files by name.</p>
34+
<ngx-gist
35+
[displayOnlyFileNames]="['csstest.css', 'main.ts']"
36+
gistId="d55ea012b585a16a9970878d90106d74"
37+
></ngx-gist>
38+
39+
<h4>DISPLAYING A BASIC CODE SNIPPET (WITHOUT A REMOTE GIST)</h4>
40+
<p>
41+
These are not fetched from GitHub and are brought in elsewhere from your
42+
application (seperate HTTP request, or statically for example). With
43+
this method you can display code snippets without having to create a
44+
remote gist. Also, please notice here that no "Open Gist on GitHub" link
45+
is displayed here.
46+
</p>
2747
<ngx-gist [gist]="localGistObject"></ngx-gist>
28-
-->
2948
</ngx-body>
3049
<ngx-footer #footer></ngx-footer>
3150
`,
32-
styles: [],
51+
styles: [
52+
`
53+
h2 {
54+
margin-top: 2rem;
55+
}
56+
h3 {
57+
margin-bottom: 3rem;
58+
}
59+
`,
60+
],
3361
})
34-
export class AppComponent {}
62+
export class AppComponent {
63+
public readonly localGistObject = NgxGist.create({
64+
// Required
65+
files: [
66+
{
67+
content: getTimeSnippet,
68+
filename: 'get-time.ts',
69+
},
70+
{
71+
content: printHelloWorldSnippet,
72+
filename: 'print-hello-world.js',
73+
},
74+
],
75+
// Optional
76+
created_at: undefined,
77+
languageOverride: undefined,
78+
});
79+
}
80+
81+
const getTimeSnippet = `
82+
function getTime(): number {
83+
return new Date().getTime();
84+
}
85+
`.trimStart();
86+
87+
const printHelloWorldSnippet = `
88+
function printHelloWorld() {
89+
console.log('Hello world!');
90+
}
91+
`.trimStart();

src/app/public/ngx-gist-file-filter.pipe.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ export class GistFileFilterPipe implements PipeTransform {
1313
return [];
1414
}
1515

16+
console.log(displayOnlyFileNames);
17+
1618
if (!displayOnlyFileNames || displayOnlyFileNames === '') {
1719
return files;
1820
}

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

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { DOCUMENT } from '@angular/common';
1616
<mat-tab
1717
*ngFor="
1818
let file of gist.highlightedFiles
19-
| gistFileFilter: displayOnlyFileName
19+
| gistFileFilter: displayOnlyFileNames
2020
"
2121
[label]="file.filename"
2222
>
@@ -25,16 +25,18 @@ import { DOCUMENT } from '@angular/common';
2525
</pre>
2626
</mat-tab>
2727
</mat-tab-group>
28-
<mat-card-footer>
28+
29+
<mat-card-footer *ngIf="gistIdChanges | async as gid">
2930
<a
30-
*ngIf="gistIdChanges | async as gid"
31+
*ngIf="!hideGistLink"
3132
target="_blank"
3233
[href]="'https://gist.github.com/' + gid"
3334
>
3435
<mat-icon>link</mat-icon> Open Gist on GitHub
3536
</a>
3637
</mat-card-footer>
37-
<ng-template #loading> Loading Code Snippet... </ng-template>
38+
39+
<ng-template #loading>Loading Code Snippet...</ng-template>
3840
</mat-card>
3941
`,
4042
styleUrls: ['./ngx-gist.component.scss'],
@@ -50,13 +52,23 @@ export class NgxGistComponent implements OnInit {
5052
private htmlLinkElement: HTMLLinkElement | null = null;
5153

5254
/**
53-
* Display in the DOM only the selected filename from the gists files array.
54-
*
55-
* TODO: Make this possible for string array input.
55+
* Display in the DOM only the selected filename(s) from the gists files array.
5656
*
5757
* Default: `undefined`
58+
*
59+
* Example: `'my-styles.scss'` or `'super-feature.ts'`
60+
*
61+
* Tip: Can be either a string or string array. File names much match exactly,
62+
* be sure to remove any leading or trailing whitespace in the provided strings.
63+
*/
64+
@Input() public displayOnlyFileNames?: string | readonly string[];
65+
/**
66+
* Optionally hide the gist link which opens the gist on GitHub. The gist links
67+
* automatically dispaly for remote gists, but can be hidden with this feature.
68+
*
69+
* Default: `false`
5870
*/
59-
@Input() public displayOnlyFileName?: string;
71+
@Input() public hideGistLink = false;
6072
/**
6173
* Provide a static gist model here directly which will be displayed if
6274
* no `gistId` is provided for remote fetching. Also this model will be
@@ -77,12 +89,13 @@ export class NgxGistComponent implements OnInit {
7789
* URL of the gists you create. For example the id `TH1515th31DT0C0PY` in:
7890
* https://gist.github.com/FakeUserName/TH1515th31DT0C0PY
7991
*
80-
* Alternatively, provide a value directly in the sibling input `gist`.
92+
* Default: `undefined`
93+
*
94+
* Tip: Alternatively, provide a value directly in the sibling input `gist`.
8195
*/
8296
@Input() public set gistId(value: string) {
8397
this.gistIdSubject.next(value);
8498
}
85-
// We want reactive behavior for `gistId` so we can update gists asynchronously
8699
private readonly gistIdSubject = new ReplaySubject<
87100
NgxGistComponent['gistId']
88101
>(1);
@@ -91,22 +104,23 @@ export class NgxGistComponent implements OnInit {
91104
* When defined, override automatic language detection [and styling] and
92105
* treat all gists as this lanuage.
93106
*
94-
* See supported languages here:
95-
* https://github.com/highlightjs/highlight.js/blob/main/SUPPORTED_LANGUAGES.md
96-
*
97107
* Default: `undefined`
108+
*
109+
* Tip: See supported language strings here:
110+
* https://github.com/highlightjs/highlight.js/blob/main/SUPPORTED_LANGUAGES.md
98111
*/
99112
@Input() public languageName?: Language['name'];
100113
/**
101114
* Define a material core theme to apply. Ideally, you should already have
102115
* your global material theme set at the root of your project so try to
103-
* avoid using this if possible. Note: These are also loaded from a CDN.
116+
* avoid using this if possible.
104117
*
105-
* See theming Angular Material: https://material.angular.io/guide/theming
106-
*
107-
* CDN used: `https://unpkg.com`
118+
* Note: These are loaded from the CDN: `https://unpkg.com`
108119
*
109120
* Default: `undefined`
121+
*
122+
* Tip: See theming Angular Material: https://material.angular.io/guide/theming
123+
* if you need help applying a global material theme.
110124
*/
111125
@Input() public materialTheme:
112126
| 'deeppurple-amber'
@@ -117,7 +131,7 @@ export class NgxGistComponent implements OnInit {
117131
/**
118132
* Cache the GitHub gist request in local memory for 24 hours. GitHub has a
119133
* request limit, so this helps in reducing bandwidth. Loads previously
120-
* fetched gist content from the users machine.
134+
* fetched gist content from the users machine on refresh and page re-visits.
121135
*
122136
* Default: `true`
123137
*/

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

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,7 @@ export class NgxGist implements Gist {
7070
/* eslint-enable @typescript-eslint/naming-convention */
7171

7272
/** Additional properties */
73-
public readonly highlightedFiles: Array<
74-
io.TypeOf<typeof gistFilesContent> & HighlightedContent
75-
>;
73+
public readonly highlightedFiles: HighlightedFiles;
7674
public readonly languageOverride?: string;
7775

7876
/**
@@ -85,15 +83,32 @@ export class NgxGist implements Gist {
8583
* @returns A 'partial' model in which unnecessary fields are dummny data.
8684
*/
8785
public static create(
88-
args: Pick<Gist, 'files' | 'created_at' | 'description'> &
89-
Pick<NgxGist, 'languageOverride'>,
86+
args: { files: Files } & Partial<Pick<Gist, 'created_at' | 'description'>> & // Required // Optional
87+
Pick<NgxGist, 'languageOverride'>, // Optional
9088
): NgxGist {
89+
const files: NgxGist['files'] = args.files.reduce((prev, curr) => {
90+
const file: GistFilesContent = {
91+
// Passed in values, use these.
92+
content: curr.content,
93+
filename: curr.filename,
94+
// Leave these empty, not needed for a local, static model.
95+
language: '',
96+
raw_url: '',
97+
size: 0,
98+
truncated: false,
99+
type: '',
100+
};
101+
return {
102+
...prev,
103+
[curr.filename]: file,
104+
};
105+
}, {});
91106
return new NgxGist({
92107
// Properties with settable values. These are the mimimum values needed
93108
// for displaying non "remote".
94-
created_at: new Date(args.created_at),
95-
description: args.description,
96-
files: args.files,
109+
created_at: args.created_at ? new Date(args.created_at) : new Date(),
110+
description: args.description ?? '',
111+
files,
97112
languageOverride: args.languageOverride,
98113
// Empty properties that aren't needed for displaying non "remote" gists
99114
comments: 0,
@@ -140,6 +155,10 @@ export class NgxGist implements Gist {
140155
}
141156
}
142157

158+
type Files = Array<Pick<GistFilesContent, 'content' | 'filename'>>;
159+
160+
type HighlightedFiles = Array<GistFilesContent & HighlightedContent>;
161+
143162
interface HighlightedContent {
144163
highlightedContent: string;
145164
}
@@ -208,6 +227,7 @@ const gistFilesContent = io.type({
208227
truncated: io.boolean,
209228
type: io.string,
210229
});
230+
type GistFilesContent = io.TypeOf<typeof gistFilesContent>;
211231

212232
const gistFilesCodec = io.record(io.string, gistFilesContent);
213233

0 commit comments

Comments
 (0)