Skip to content

Latest commit

 

History

History
108 lines (79 loc) · 2.38 KB

README.md

File metadata and controls

108 lines (79 loc) · 2.38 KB

ngx-markdown-it

An Angular library that renders markdown using markdown-it.

You can try online @StackBlitz.

Installation

Use your favorite package manager to install ngx-markdown-it. For example, with NPM:

npm install ngx-markdown-it

Don't forget to install markdown-it too:

npm install markdown-it

Configuration

As usual, with Angular, you must import the module inside your main application:

import { NgxMarkdownItModule } from "ngx-markdown-it";

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    NgxMarkdownItModule
  ],
  providers: [],
  bootstrap: []
})
export class AppModule { }

Optionally, you can pass a NgxMarkdownItConfig class to the module, to allow to configure the markdown-it library. For example, to use the markdown-it-markmap plugin:

import { NgxMarkdownItModule } from "ngx-markdown-it";
import { NgxMarkdownItConfig } from "ngx-markdown-it";

import { default as markmapPlugin } from 'markdown-it-markmap';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    NgxMarkdownItModule.forRoot({
      plugins: [
        markmapPlugin
      ]
    })
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Usage

The module provides a component and a service.

markdown-it component

A simple component to render the mardown.

<markdown-it [markdown]="markdown"></markdown-it>
<markdown-it ngPreserveWhitespaces># A level 1 header</markdown-it>

markdown-it service

A service to allow more advanced usage of the markdown-it parser.

import { Component, OnInit } from '@angular/core';
import { NgxMarkdownItService } from 'ngx-markdown-it';

@Component({ ... })
export class Foo implements OnInit {
  constructor(private markdownItService: NgxMarkdownItService) { }

  ngOnInit() {
    console.log(this.markdownItService.render('# A level 1 header'));
  }
}

Development

The library is located in projects/ngx-markdown-it. The root app is only a demo to test the library.

To build the library run:

cd lib/ngx-markdown-it && npm i
ng build ngx-markdown-it # can be ran from the root dir, no need to cd into the lib dir