ngx-markdown-it
Version:
An Angular library that renders markdown using [markdown-it](https://github.com/markdown-it/markdown-it).
316 lines (308 loc) • 12.5 kB
JavaScript
import * as i0 from '@angular/core';
import { Injectable, Optional, EventEmitter, Component, Output, Input, NgModule, SkipSelf } from '@angular/core';
import MarkdownIt from 'markdown-it';
/*
* @file ngx-markdown-it-config.class.ts
*
* @brief Config class
* @author David Suárez
* @date Mon, 21 Jun 20 19:45:15 +0200
*
* @license
*
* ngx-markdown-it: angular markdown-it module
*
* Copyright (c) 2021 David Suárez
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
*/
class NgxMarkdownItConfig {
/**
* Syntax rules and options for common use cases:
*
* - default - similar to GFM, used when no preset name given.
* - "commonmark" - configures parser to strict CommonMark mode.
* - "zero" - all rules disabled.
*/
presetName;
/**
* Plugins to apply.
*/
plugins;
}
;
/*
* @file ngx-markdown-it.service.ts
*
* @brief Markdown It service
* @author David Suárez
* @date Mon, 21 Jun 20 19:45:15 +0200
*
* @license
*
* ngx-markdown-it: angular markdown-it module
*
* Copyright (c) 2021 David Suárez
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
*/
class NgxMarkdownItService {
markdownIt;
constructor(config) {
var presetName = 'default';
if (config && config.presetName) {
presetName = config.presetName;
}
this.markdownIt = new MarkdownIt(presetName);
if (config && config.plugins) {
config.plugins.forEach(plugin => this.markdownIt.use(plugin));
}
}
/**
* Renders a markdown string to HTML
*
* @param {string} markdown Markdown string that you want to render.
* @returns {string}
*/
render(markdown) {
return `${this.markdownIt.render(markdown)}`;
}
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.7", ngImport: i0, type: NgxMarkdownItService, deps: [{ token: NgxMarkdownItConfig, optional: true }], target: i0.ɵɵFactoryTarget.Injectable });
static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "16.2.7", ngImport: i0, type: NgxMarkdownItService, providedIn: 'root' });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.7", ngImport: i0, type: NgxMarkdownItService, decorators: [{
type: Injectable,
args: [{
providedIn: 'root'
}]
}], ctorParameters: function () { return [{ type: NgxMarkdownItConfig, decorators: [{
type: Optional
}] }]; } });
/*
* @file ngx-markdown-it.component.ts
*
* @brief Component to render markdown to html
* @author David Suárez
* @date Mon, 21 Jun 20 19:45:15 +0200
*
* @license
*
* ngx-markdown-it: angular markdown-it module
*
* Copyright (c) 2021 David Suárez
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
*/
class NgxMarkdownItComponent {
element;
markdownService;
_markdown;
ready = new EventEmitter();
set markdown(val) {
this._markdown = val;
this.render(this._markdown);
}
constructor(element, markdownService) {
this.element = element;
this.markdownService = markdownService;
}
ngAfterViewInit() {
if (this._markdown == null) {
this.render(this.element.nativeElement.innerHTML.trim());
return;
}
else {
/*
* This is probably done in markdown() setter, but because before the view is init, our component or parents ones
* are not in a safe state, we should signal again.
*/
this.ready.emit();
return;
}
}
render(markdown) {
this.element.nativeElement.innerHTML = this.markdownService.render(markdown);
this.ready.emit();
}
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.7", ngImport: i0, type: NgxMarkdownItComponent, deps: [{ token: i0.ElementRef }, { token: NgxMarkdownItService }], target: i0.ɵɵFactoryTarget.Component });
static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "16.2.7", type: NgxMarkdownItComponent, selector: "markdown-it", inputs: { markdown: "markdown" }, outputs: { ready: "ready" }, ngImport: i0, template: `<ng-content></ng-content>`, isInline: true });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.7", ngImport: i0, type: NgxMarkdownItComponent, decorators: [{
type: Component,
args: [{ selector: 'markdown-it', template: `<ng-content></ng-content>` }]
}], ctorParameters: function () { return [{ type: i0.ElementRef }, { type: NgxMarkdownItService }]; }, propDecorators: { ready: [{
type: Output
}], markdown: [{
type: Input
}] } });
/*
* @file ngx-markdown-it.module.ts
*
* @brief Library module
* @author David Suárez
* @date Mon, 21 Jun 20 19:45:15 +0200
*
* @license
*
* ngx-markdown-it: angular markdown-it module
*
* Copyright (c) 2021 David Suárez
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
*/
class NgxMarkdownItModule {
constructor(parentModule) {
if (parentModule) {
throw new Error('NgxMarkdownItModule is already loaded. Import it in the AppModule only');
}
}
static forRoot(config) {
return {
ngModule: NgxMarkdownItModule,
providers: [{
provide: NgxMarkdownItConfig,
useValue: config
}
]
};
}
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.7", ngImport: i0, type: NgxMarkdownItModule, deps: [{ token: NgxMarkdownItModule, optional: true, skipSelf: true }], target: i0.ɵɵFactoryTarget.NgModule });
static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "16.2.7", ngImport: i0, type: NgxMarkdownItModule, declarations: [NgxMarkdownItComponent], exports: [NgxMarkdownItComponent] });
static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "16.2.7", ngImport: i0, type: NgxMarkdownItModule });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.7", ngImport: i0, type: NgxMarkdownItModule, decorators: [{
type: NgModule,
args: [{
declarations: [
NgxMarkdownItComponent
],
imports: [],
exports: [
NgxMarkdownItComponent
]
}]
}], ctorParameters: function () { return [{ type: NgxMarkdownItModule, decorators: [{
type: Optional
}, {
type: SkipSelf
}] }]; } });
/*
* @file public-api.ts
*
* @brief Public API Surface of ngx-markdown-it
* @author David Suárez
* @date Mon, 21 Jun 20 19:45:15 +0200
*
* @license
*
* ngx-markdown-it: angular markdown-it module
*
* Copyright (c) 2021 David Suárez
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
*/
/**
* Generated bundle index. Do not edit.
*/
export { NgxMarkdownItComponent, NgxMarkdownItConfig, NgxMarkdownItModule, NgxMarkdownItService };
//# sourceMappingURL=ngx-markdown-it.mjs.map