DevExtreme v26.1 is now available.

Explore our newest features/capabilities and share your thoughts with us.

Your search did not match any results.

Angular Html Editor - Markdown Support

The DevExtreme HTML Editor uses HTML markup to format document content. This demo module converts content to Markdown using the unified plugin library.

This sample implementation sets the HTML Editor's converter property to an object that implements two functions: toHtml and fromHtml.

To review our implementation, edit or format text within the HTML Editor control and see how the Markdown Preview section reflects changes.

Backend API
<dx-html-editor [height]="300" [converter]="converter" [(value)]="valueContent"> <dxo-html-editor-toolbar> <dxi-html-editor-item name="undo"></dxi-html-editor-item> <dxi-html-editor-item name="redo"></dxi-html-editor-item> <dxi-html-editor-item name="separator"></dxi-html-editor-item> <dxi-html-editor-item name="bold"></dxi-html-editor-item> <dxi-html-editor-item name="italic"></dxi-html-editor-item> <dxi-html-editor-item name="separator"></dxi-html-editor-item> <dxi-html-editor-item name="header" [acceptedValues]="[false, 1, 2, 3, 4, 5]" [options]="{ inputAttr: { 'aria-label': 'Header' } }" ></dxi-html-editor-item> <dxi-html-editor-item name="separator"></dxi-html-editor-item> <dxi-html-editor-item name="orderedList"></dxi-html-editor-item> <dxi-html-editor-item name="bulletList"></dxi-html-editor-item> </dxo-html-editor-toolbar> </dx-html-editor> <div class="options"> <div class="value-title"> Markdown Preview </div> <div class="value-content" tabindex="0">{{ valueContent }}</div> </div>
import { bootstrapApplication } from '@angular/platform-browser'; import { Component, enableProdMode, provideZoneChangeDetection } from '@angular/core'; import { unified } from 'unified'; import remarkParse from 'remark-parse'; import remarkRehype from 'remark-rehype'; import remarkStringify from 'remark-stringify'; import rehypeParse from 'rehype-parse'; import rehypeRemark from 'rehype-remark'; import rehypeStringify from 'rehype-stringify'; import { DxHtmlEditorModule } from 'devextreme-angular/ui/html-editor'; import { Service } from './app.service'; interface Converter { toHtml: (value: string) => string; fromHtml: (value: string) => string; } if (!/localhost/.test(document.location.host)) { enableProdMode(); } @Component({ selector: 'demo-app', templateUrl: './app.component.html', styleUrls: ['./app.component.css'], providers: [Service], imports: [ DxHtmlEditorModule, ], }) export class AppComponent { valueContent: string; converter: Converter; constructor(service: Service) { this.valueContent = service.getMarkup(); this.converter = { toHtml(value) { const result = unified() .use(remarkParse) .use(remarkRehype) .use(rehypeStringify) .processSync(value) .toString(); return result; }, fromHtml(value) { const result = unified() .use(rehypeParse) .use(rehypeRemark) .use(remarkStringify) .processSync(value) .toString(); return result; }, }; } } bootstrapApplication(AppComponent, { providers: [ provideZoneChangeDetection({ eventCoalescing: true, runCoalescing: true }), ], });
::ng-deep .dx-htmleditor-content img { vertical-align: middle; padding-right: 10px; margin-bottom: 22px; } ::ng-deep .value-title { font-size: var(--dx-font-size-sm); font-weight: 500; } ::ng-deep .value-content { margin-top: 20px; overflow: auto; height: 210px; white-space: pre-wrap; border: 1px solid var(--dx-color-border); padding: 16px; background-color: var(--dx-color-main-bg); } .options { margin-top: 20px; padding: 20px; background-color: rgba(191, 191, 191, 0.15); box-sizing: border-box; width: 100%; }
import { Injectable } from '@angular/core'; const markup = `## ![HtmlEditor](../../../../images/widgets/HtmlEditor.svg) Formatted Text Editor (HTML Editor) Lists: 1. Use numbers followed by a period for an ordered list. 1. Use a single asterisk for a bullet list. Formats: * Enclose a word in single asterisks for *italic*. * Enclose a word in double asterisks for **bold**. `; @Injectable() export class Service { getMarkup(): string { return markup; } }
<!DOCTYPE html> <html lang="en"> <head> <title>DevExtreme Demo</title> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0" /> <link rel="stylesheet" type="text/css" href="https://cdn3.devexpress.com/jslib/26.1.5/css/dx.light.css" /> </head> <body class="dx-viewport"> <div class="demo-container"> <demo-app>Loading...</demo-app> </div> <script src="https://cdn.jsdelivr.net/npm/zone.js@0.16.2/bundles/zone.umd.js"></script> <script src="./bundle.js" type="module"></script> </body> </html>