UNPKG

ngx-highlightjs

Version:

Instant code highlighting, auto-detect language, super easy to use.

1 lines 12.1 kB
{"version":3,"file":"ngx-highlightjs-plus.mjs","sources":["../../../projects/ngx-highlightjs/plus/src/gist.model.ts","../../../projects/ngx-highlightjs/plus/src/code-loader.ts","../../../projects/ngx-highlightjs/plus/src/gist.ts","../../../projects/ngx-highlightjs/plus/src/code-file-location.ts","../../../projects/ngx-highlightjs/plus/src/code-from-url.ts","../../../projects/ngx-highlightjs/plus/src/highlight-plus.module.ts","../../../projects/ngx-highlightjs/plus/src/ngx-highlightjs-plus.ts"],"sourcesContent":["import { InjectionToken, Provider } from '@angular/core';\r\n\r\n\r\nexport function isUrl(url: string): boolean {\r\n const regExp: RegExp = /(ftp|http|https):\\/\\/(\\w+:{0,1}\\w*@)?(\\S+)(:[0-9]+)?(\\/|\\/([\\w#!:.?+=&%@!]))?/;\r\n return regExp.test(url);\r\n}\r\n\r\nexport interface GistOptions {\r\n clientId: string;\r\n clientSecret: string;\r\n}\r\n\r\nexport const GIST_OPTIONS: InjectionToken<GistOptions> = new InjectionToken<GistOptions>('GIST_OPTIONS');\r\n\r\nexport function provideGistOptions(options: GistOptions): Provider[] {\r\n return [\r\n {\r\n provide: GIST_OPTIONS,\r\n useValue: options\r\n }\r\n ]\r\n}\r\n\r\n\r\ninterface Owner {\r\n login: string;\r\n id: number;\r\n node_id: string;\r\n avatar_url: string;\r\n gravatar_id: string;\r\n url: string;\r\n html_url: string;\r\n followers_url: string;\r\n following_url: string;\r\n gists_url: string;\r\n starred_url: string;\r\n subscriptions_url: string;\r\n organizations_url: string;\r\n repos_url: string;\r\n events_url: string;\r\n received_events_url: string;\r\n type: string;\r\n site_admin: boolean;\r\n}\r\n\r\ninterface User {\r\n login: string;\r\n id: number;\r\n node_id: string;\r\n avatar_url: string;\r\n gravatar_id: string;\r\n url: string;\r\n html_url: string;\r\n followers_url: string;\r\n following_url: string;\r\n gists_url: string;\r\n starred_url: string;\r\n subscriptions_url: string;\r\n organizations_url: string;\r\n repos_url: string;\r\n events_url: string;\r\n received_events_url: string;\r\n type: string;\r\n site_admin: boolean;\r\n}\r\n\r\ninterface ChangeStatus {\r\n total: number;\r\n additions: number;\r\n deletions: number;\r\n}\r\n\r\ninterface History {\r\n user: User;\r\n version: string;\r\n committed_at: Date;\r\n change_status: ChangeStatus;\r\n url: string;\r\n}\r\n\r\ninterface Files {\r\n [fileName: string]: {\r\n filename: string;\r\n type: string;\r\n language: string;\r\n raw_url: string;\r\n size: number;\r\n truncated: boolean;\r\n content: string;\r\n };\r\n}\r\n\r\nexport interface Gist {\r\n url: string;\r\n forks_url: string;\r\n commits_url: string;\r\n id: string;\r\n node_id: string;\r\n git_pull_url: string;\r\n git_push_url: string;\r\n html_url: string;\r\n files: Files;\r\n public: boolean;\r\n created_at: Date;\r\n updated_at: Date;\r\n description: string;\r\n comments: number;\r\n user?: any;\r\n comments_url: string;\r\n owner: Owner;\r\n forks: any[];\r\n history: History[];\r\n truncated: boolean;\r\n}\r\n\r\n","import { inject, Injectable } from '@angular/core';\r\nimport { HttpClient, HttpParams } from '@angular/common/http';\r\nimport { Observable, EMPTY, catchError, shareReplay } from 'rxjs';\r\nimport { Gist, GIST_OPTIONS, GistOptions, isUrl } from './gist.model';\r\n\r\n@Injectable({\r\n providedIn: 'root'\r\n})\r\nexport class CodeLoader {\r\n\r\n private _http: HttpClient = inject(HttpClient);\r\n\r\n private _options: GistOptions = inject(GIST_OPTIONS, { optional: true });\r\n\r\n /**\r\n * Get plus code\r\n * @param id Gist ID\r\n */\r\n getCodeFromGist(id: string): Observable<Gist> {\r\n let params!: HttpParams;\r\n if (this._options?.clientId && this._options?.clientSecret) {\r\n params = new HttpParams().set('client_id', this._options.clientId).set('client_secret', this._options.clientSecret);\r\n }\r\n return this.fetchFile(`https://api.github.com/gists/${ id }`, { params, responseType: 'json' });\r\n }\r\n\r\n /**\r\n * Get code by URL\r\n * @param url File raw link\r\n */\r\n getCodeFromUrl(url: string): Observable<string> {\r\n return this.fetchFile(url, { responseType: 'text' });\r\n }\r\n\r\n private fetchFile(url: string, options: any): Observable<any> {\r\n // Check if URL is valid\r\n if (isUrl(url)) {\r\n return this._http.get(url, options).pipe(\r\n // Catch response\r\n shareReplay(1),\r\n catchError((err: Error) => {\r\n console.error('[NgxHighlight]: Unable to fetch the URL!', err.message);\r\n return EMPTY;\r\n })\r\n );\r\n }\r\n return EMPTY;\r\n }\r\n\r\n}\r\n","import { Directive, Pipe, Input, Output, PipeTransform, EventEmitter, inject } from '@angular/core';\r\nimport { CodeLoader } from './code-loader';\r\nimport { Gist } from './gist.model';\r\n\r\n@Directive({\r\n selector: '[gist]'\r\n})\r\nexport class GistDirective {\r\n\r\n private _loader: CodeLoader = inject(CodeLoader);\r\n\r\n @Input()\r\n set gist(value: string) {\r\n if (value) {\r\n this._loader.getCodeFromGist(value).subscribe((gist: Gist) => this.gistLoad.emit(gist));\r\n }\r\n }\r\n\r\n @Output() gistLoad: EventEmitter<Gist> = new EventEmitter<Gist>();\r\n}\r\n\r\n@Pipe({\r\n name: 'gistFile'\r\n})\r\nexport class GistFilePipe implements PipeTransform {\r\n transform(gist: Gist, fileName: string): string | null {\r\n return (gist && gist.files && gist.files[fileName]) ? gist.files[fileName].content : null;\r\n }\r\n}\r\n","import { inject, InjectionToken } from '@angular/core';\r\nimport { DOCUMENT } from '@angular/common';\r\n\r\n/**\r\n * Injection token used to provide the current location to `codeFromUrl` pipe.\r\n * Used to handle server-side rendering and to stub out during unit tests.\r\n */\r\nexport const HIGHLIGHT_FILE_LOCATION: InjectionToken<any> = new InjectionToken<CodeFileLocation>('HIGHLIGHT_FILE_LOCATION', {\r\n providedIn: 'root',\r\n factory: CODE_FILE_LOCATION_FACTORY,\r\n});\r\n\r\nexport interface CodeFileLocation {\r\n getPathname: () => string;\r\n}\r\n\r\nexport function CODE_FILE_LOCATION_FACTORY(): CodeFileLocation {\r\n const _location: Location = inject(DOCUMENT)?.location;\r\n\r\n return {\r\n // Note that this needs to be a function, rather than a property, because Angular\r\n // will only resolve it once, but we want the current path on each call.\r\n // getPathname: () => (_location ? _location.pathname + _location.search : ''),\r\n getPathname: () => (_location ? _location.origin : '')\r\n };\r\n}\r\n","import { inject, Pipe, PipeTransform } from '@angular/core';\r\nimport { Observable } from 'rxjs';\r\nimport { CodeLoader } from './code-loader';\r\nimport { HIGHLIGHT_FILE_LOCATION, CodeFileLocation } from './code-file-location';\r\nimport { isUrl } from './gist.model';\r\n\r\n@Pipe({\r\n name: 'codeFromUrl'\r\n})\r\nexport class CodeFromUrlPipe implements PipeTransform {\r\n\r\n private _location: CodeFileLocation = inject(HIGHLIGHT_FILE_LOCATION);\r\n\r\n private _loader: CodeLoader = inject(CodeLoader);\r\n\r\n transform(url: string): Observable<string> {\r\n return this._loader.getCodeFromUrl(isUrl(url) ? url : `${ this._location.getPathname() }/${ url }`);\r\n }\r\n}\r\n","import { NgModule } from '@angular/core';\r\nimport { GistFilePipe, GistDirective } from './gist';\r\nimport { CodeFromUrlPipe } from './code-from-url';\r\n\r\n@NgModule({\r\n imports: [\r\n GistDirective,\r\n GistFilePipe,\r\n CodeFromUrlPipe\r\n ],\r\n exports: [\r\n GistDirective,\r\n GistFilePipe,\r\n CodeFromUrlPipe\r\n ]\r\n})\r\nexport class HighlightPlusModule {\r\n}\r\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public_api';\n"],"names":[],"mappings":";;;;;;AAGM,SAAU,KAAK,CAAC,GAAW,EAAA;IAC/B,MAAM,MAAM,GAAW,+EAA+E;AACtG,IAAA,OAAO,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC;AACzB;MAOa,YAAY,GAAgC,IAAI,cAAc,CAAc,cAAc;AAEjG,SAAU,kBAAkB,CAAC,OAAoB,EAAA;IACrD,OAAO;AACL,QAAA;AACE,YAAA,OAAO,EAAE,YAAY;AACrB,YAAA,QAAQ,EAAE;AACX;KACF;AACH;;MCda,UAAU,CAAA;AAHvB,IAAA,WAAA,GAAA;AAKU,QA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