igniteui-angular
Version:
Ignite UI for Angular is a dependency-free Angular toolkit for building modern web apps
86 lines (81 loc) • 3.4 kB
JavaScript
import DOMPurify from 'dompurify';
import * as i0 from '@angular/core';
import { Injectable, inject, Pipe } from '@angular/core';
import { Marked } from 'marked';
import markedShiki from 'marked-shiki';
import { createHighlighter, bundledThemes } from 'shiki/bundle/web';
import { DomSanitizer } from '@angular/platform-browser';
const DEFAULT_LANGUAGES = ['javascript', 'typescript', 'html', 'css'];
const DEFAULT_THEMES = {
light: 'github-light',
dark: 'github-dark'
};
class IgxChatMarkdownService {
_initializeMarked() {
this._instance = new Marked({
breaks: true,
gfm: true,
extensions: [
{
name: 'link',
renderer({ href, title, text }) {
return `<a href="${href}" target="_blank" rel="noopener noreferrer" ${title ? `title="${title}"` : ''}>${text}</a>`;
}
}
]
});
}
async _initializeShiki() {
const highlighter = await createHighlighter({
langs: DEFAULT_LANGUAGES,
themes: Object.keys(bundledThemes)
});
this._instance.use(markedShiki({
highlight(code, lang, _) {
try {
return highlighter.codeToHtml(code, {
lang,
themes: DEFAULT_THEMES,
});
}
catch {
return `<pre><code>${code}</code></pre>`;
}
}
}));
}
constructor() {
this._initializeMarked();
this._isInitialized = this._initializeShiki();
}
async parse(text) {
await this._isInitialized;
return await this._instance.parse(text);
}
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.0.2", ngImport: i0, type: IgxChatMarkdownService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "21.0.2", ngImport: i0, type: IgxChatMarkdownService, providedIn: 'root' }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.0.2", ngImport: i0, type: IgxChatMarkdownService, decorators: [{
type: Injectable,
args: [{ providedIn: 'root' }]
}], ctorParameters: () => [] });
class MarkdownPipe {
constructor() {
this._service = inject(IgxChatMarkdownService);
this._sanitizer = inject(DomSanitizer);
}
async transform(text) {
return this._sanitizer.bypassSecurityTrustHtml(DOMPurify.sanitize(await this._service.parse(text ?? '')));
}
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.0.2", ngImport: i0, type: MarkdownPipe, deps: [], target: i0.ɵɵFactoryTarget.Pipe }); }
static { this.ɵpipe = i0.ɵɵngDeclarePipe({ minVersion: "14.0.0", version: "21.0.2", ngImport: i0, type: MarkdownPipe, isStandalone: true, name: "fromMarkdown" }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.0.2", ngImport: i0, type: MarkdownPipe, decorators: [{
type: Pipe,
args: [{ name: 'fromMarkdown' }]
}] });
/**
* Generated bundle index. Do not edit.
*/
export { MarkdownPipe };
//# sourceMappingURL=igniteui-angular-chat-extras.mjs.map