Use CommonMark Markdown in Angular 2+

problem

CommonMark is a strongly defined variant of Markdown, highly compatible, aimed at resolving rendering ambiguities. It has a reference JavaScript implementation at https://github.com/commonmark/commonmark.js

How exactly can the JavaScript library be used in Angular?

solution

As the docs say, first install it with npm:

npm install commonmark

In the TypeScript file, import what classes you need from the library, for example:

import { HtmlRenderer, Parser } from 'commonmark';

Then to use it:

const reader = new Parser();
const writer = new HtmlRenderer();
const parsed = reader.parse('Hello *world*');
const result = writer.render(parsed);

The key is here to use Parser instead of the commonmark.Parser found in the docs.

Gravatar
Author: Dan Dumitru
Last Edit: September 19, 2020
Last Edit September 19, 2020
Created April 20, 2020
Views 41
Tags
Markdown
Angular

Join in!

I'm building here a library of useful solutions to software development problems.

If you like this project, create an account or add a bookmark, then come back here to write a post yourself when you run into something that might be useful to others...

>> read more

Your Comment

Feel free to post additional info or improvement suggestions.
preview
Optional, never shown, displays gravatar.

Formatting Tips

This editor uses Markdown to easily add code in your posts.

Triple backticks for full line(s) of code (or indent 4 spaces)

```
let foo = 'bar';
```

[link text](http://a.com)

*italic* **bold**

More Tips