ngx-highlightjs
Version:
Instant code highlighting, auto-detect language, super easy to use.
164 lines (156 loc) • 7.35 kB
JavaScript
import * as i0 from '@angular/core';
import { InjectionToken, Injectable, Optional, Inject, EventEmitter, Directive, Input, Output, Pipe, NgModule } from '@angular/core';
import * as i1 from '@angular/common/http';
import { HttpParams, HttpClientModule } from '@angular/common/http';
import { HighlightModule } from 'ngx-highlightjs';
import { EMPTY } from 'rxjs';
import { publishReplay, refCount, catchError } from 'rxjs/operators';
const GIST_OPTIONS = new InjectionToken('GIST_OPTIONS');
class CodeLoader {
constructor(_http, _options) {
this._http = _http;
this._options = _options;
}
/**
* Get plus code
* @param id Gist ID
*/
getCodeFromGist(id) {
let params;
if (this.isOAuthProvided()) {
params = new HttpParams().set('client_id', this._options.clientId).set('client_secret', this._options.clientSecret);
}
return this.fetchFile(`https://api.github.com/gists/${id}`, { params, responseType: 'json' });
}
/**
* Get code by URL
* @param url File raw link
*/
getCodeFromUrl(url) {
return this.fetchFile(url, { responseType: 'text' });
}
/**
* Check if OAuth option is provided
*/
isOAuthProvided() {
return !!this._options && !!this._options.clientId && !!this._options.clientSecret;
}
fetchFile(url, options) {
// Check if URL is valid
if (isUrl(url)) {
return this._http.get(url, options).pipe(
// Catch response
publishReplay(1), refCount(), catchError((err) => {
console.error('[NgxHighlight]: Unable to fetch the URL!', err.message);
return EMPTY;
}));
}
return EMPTY;
}
}
CodeLoader.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.1.0", ngImport: i0, type: CodeLoader, deps: [{ token: i1.HttpClient }, { token: GIST_OPTIONS, optional: true }], target: i0.ɵɵFactoryTarget.Injectable });
CodeLoader.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "14.1.0", ngImport: i0, type: CodeLoader, providedIn: 'root' });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.1.0", ngImport: i0, type: CodeLoader, decorators: [{
type: Injectable,
args: [{
providedIn: 'root'
}]
}], ctorParameters: function () { return [{ type: i1.HttpClient }, { type: undefined, decorators: [{
type: Optional
}, {
type: Inject,
args: [GIST_OPTIONS]
}] }]; } });
function isUrl(url) {
const regExp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
return regExp.test(url);
}
class GistDirective {
constructor(_loader) {
this._loader = _loader;
this.gistLoad = new EventEmitter();
}
set gist(value) {
if (value) {
this._loader.getCodeFromGist(value).subscribe((gist) => this.gistLoad.emit(gist));
}
}
}
GistDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.1.0", ngImport: i0, type: GistDirective, deps: [{ token: CodeLoader }], target: i0.ɵɵFactoryTarget.Directive });
GistDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "14.1.0", type: GistDirective, selector: "[gist]", inputs: { gist: "gist" }, outputs: { gistLoad: "gistLoad" }, ngImport: i0 });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.1.0", ngImport: i0, type: GistDirective, decorators: [{
type: Directive,
args: [{
selector: '[gist]'
}]
}], ctorParameters: function () { return [{ type: CodeLoader }]; }, propDecorators: { gist: [{
type: Input
}], gistLoad: [{
type: Output
}] } });
class GistFilePipe {
transform(gist, fileName) {
return (gist && gist.files && gist.files[fileName]) ? gist.files[fileName].content : null;
}
}
GistFilePipe.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.1.0", ngImport: i0, type: GistFilePipe, deps: [], target: i0.ɵɵFactoryTarget.Pipe });
GistFilePipe.ɵpipe = i0.ɵɵngDeclarePipe({ minVersion: "14.0.0", version: "14.1.0", ngImport: i0, type: GistFilePipe, name: "gistFile" });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.1.0", ngImport: i0, type: GistFilePipe, decorators: [{
type: Pipe,
args: [{
name: 'gistFile'
}]
}] });
class CodeFromUrlPipe {
constructor(_loader) {
this._loader = _loader;
}
transform(url) {
return this._loader.getCodeFromUrl(url);
}
}
CodeFromUrlPipe.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.1.0", ngImport: i0, type: CodeFromUrlPipe, deps: [{ token: CodeLoader }], target: i0.ɵɵFactoryTarget.Pipe });
CodeFromUrlPipe.ɵpipe = i0.ɵɵngDeclarePipe({ minVersion: "14.0.0", version: "14.1.0", ngImport: i0, type: CodeFromUrlPipe, name: "codeFromUrl" });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.1.0", ngImport: i0, type: CodeFromUrlPipe, decorators: [{
type: Pipe,
args: [{
name: 'codeFromUrl'
}]
}], ctorParameters: function () { return [{ type: CodeLoader }]; } });
class HighlightPlusModule {
}
HighlightPlusModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.1.0", ngImport: i0, type: HighlightPlusModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
HighlightPlusModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "14.1.0", ngImport: i0, type: HighlightPlusModule, declarations: [GistDirective,
GistFilePipe,
CodeFromUrlPipe], imports: [HighlightModule,
HttpClientModule], exports: [HighlightModule,
GistDirective,
GistFilePipe,
CodeFromUrlPipe] });
HighlightPlusModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "14.1.0", ngImport: i0, type: HighlightPlusModule, imports: [HighlightModule,
HttpClientModule, HighlightModule] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.1.0", ngImport: i0, type: HighlightPlusModule, decorators: [{
type: NgModule,
args: [{
imports: [
HighlightModule,
HttpClientModule
],
declarations: [
GistDirective,
GistFilePipe,
CodeFromUrlPipe
],
exports: [
HighlightModule,
GistDirective,
GistFilePipe,
CodeFromUrlPipe
]
}]
}] });
/**
* Generated bundle index. Do not edit.
*/
export { CodeFromUrlPipe, CodeLoader, GIST_OPTIONS, GistDirective, GistFilePipe, HighlightPlusModule };
//# sourceMappingURL=ngx-highlightjs-plus.mjs.map