Giter VIP home page Giter VIP logo

ng2-markdown-to-html's Introduction

ng2-markdown-to-html CircleCI version npm

ng2-markdown-to-html is an Angular 2 library that uses marked to parse markdown to html combined with Prism.js for synthax highlights.

Demo available @ jfcere.github.io/ng2-markdown-to-html

Installation

Use the following command to add ng2-markdown-to-html library to your package.json file.

npm install ng2-markdown-to-html --save

Configuration

You must import MarkdownToHtmlModule inside your module to be able to use markdown-to-html component and/or directive.

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
+ import { MarkdownToHtmlModule } from 'ng2-markdown-to-html';

import { HomeComponent } from './home.component';

@NgModule({
  imports: [
    CommonModule,
+    MarkdownToHtmlModule.forRoot(),
  ],
  declarations: [HomeComponent],
})

To activate Prism.js synthax highlight you will need to choose a css theme file from node_modules/prismjs/themes directory and add it to your application.

If you are using Angular CLI you can add the css file by modifying the angular-cli.json file as below ...

"styles": [
  "styles.css",
+  "../node_modules/prismjs/themes/prism-okaidia.css"
],

You can find additional themes by browsing the web such as ...

Usage

ng2-markdown-to-html provides one component and one directive to parse your markdown to your web application.

Component

You can use markdown-to-html component to either parse static markdown directly from your html markup or load the content from a remote url using src property.

<!-- static markdown -->
<markdown-to-html>
  # Markdown
</markdown-to-html>

<!-- loaded from remote url -->
<markdown-to-html [src]="'path/to/file.md'"></markdown-to-html>

Directive

The same way the component works, you can use markdown-to-html directive to accomplish the same thing.

<!-- static markdown -->
<div markdown-to-html>
  # Markdown
</div>

<!-- loaded from remote url -->
<div markdown-to-html [src]="'path/to/file.md'"></div>

Synthax highlight

When using static markdown you are responsible to provide the code block with related language.

<markdown-to-html>
+  ```typescript
    const myProp: string = 'value';
+  ```
</markdown-to-html>

When using remote url ng2-markdown-to-html will use file extension to automatically resolve the code language.

<!-- will use html highlights -->
<markdown-to-html [src]="'path/to/file.html'"></markdown-to-html>

<!-- will use php highlights -->
<markdown-to-html [src]="'path/to/file.php'"></markdown-to-html>

Demo application

You can find the demo source code inside the src/app/markdown-demo directory.

The following commands will clone the repository, install npm dependencies and serve the application @ http://localhost:4200

git clone https://github.com/jfcere/ng2-markdown-to-html.git

npm install

ng serve

AoT compilation

You will need to exclude test.ts from your tsconfig.json file as loading TestBed pulls on dependencies that are not needed when serving app.

Warning! You should copy your tsconfig.json file and create a new one for AoT compilation like tsconfig-aot.json as stated on Angular official documentation othwerwise your tests won't run after excluding test.ts.

{
  "compilerOptions": {
    "baseUrl": "",
    "declaration": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": ["es2015", "dom"],
    "mapRoot": "./",
    "module": "es2015",
    "moduleResolution": "node",
    "outDir": "../dist/out-tsc",
    "sourceMap": true,
    "target": "es5",
    "typeRoots": [
      "../node_modules/@types"
    ]
  },
  "exclude": [
+    "test.ts"
  ],
  "angularCompilerOptions": {
    "genDir": "aot",
    "skipMetadataEmit" : true
 }
}

Road map

Here is the list of tasks that will be done on this library in a near future ...

  • Add CircleCI integration
  • Publish demo on github pages
  • Support Prism.js customizing options (line-numbers, line-height, ...)
  • Transpile library to Javascript

Contribution

Contributions are always welcome, just make sure that ...

  • Your code style matches with the rest of the project
  • Unit tests pass
  • Linter passes

License

Licensed under MIT.

ng2-markdown-to-html's People

Contributors

jfcere avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.