官方文檔:https://github.com/pandao/editor.md
1.安裝
npm install editor.md
bower install editor.md
2.angular配置`
angular.json 中配置 editor.md 的 css 和 js 路徑
"styles": [
"src/assets/editor.md/css/editormd.css"
],
"scripts": [
"node_modules/jquery/dist/jquery.js",
"src/assets/editor.md/editormd.min.js",
"src/assets/editor.md/lib/marked.min.js",//解析markdown用到的js路徑
"src/assets/editor.md/lib/prettify.min.js"http://解析markdown用到的js路徑
]
config.ts
export class EditorConfig {
public width = '100%';
public height = '400';
public path = './assets/md_editor/lib/';
public codeFold: true;
public searchReplace = true;
public toolbar = true;
public emoji = true;
public taskList = false;
public tex = false;// 數(shù)學(xué)公式類默認(rèn)不解析
public readOnly = false;
public tocm = true;
public watch = true; // 實(shí)時(shí)預(yù)覽
public previewCodeHighlight = true;
public saveHTMLToTextarea = true;
public markdown = '';
public flowChart = false;//流程圖
public syncScrolling = true;
public sequenceDiagram = false;//UML時(shí)序圖
public imageUpload = true;
public imageFormats = ['jpg', 'jpeg', 'gif', 'png', 'bmp', 'webp'];
public imageUploadURL = '';
public htmlDecode = 'style,script,iframe'; // you can filter tags decode
public editorFunction = '';//定義調(diào)用的方法
constructor() {
}
}
//獲取編輯器內(nèi)容
var blogcontent = encodeURIComponent(testEditor.getMarkdown());
component:
通過調(diào)用 editormd 的方法 editormd.markdownToHTML ('editor', this.conf); 其中 editor, 表示需要對該 div 下面的 textarea 里面的 markdown 內(nèi)容進(jìn)行渲染
import { ChangeDetectorRef, Component, OnInit, ViewChild, AfterViewInit } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd/message';
import { Router } from '@angular/router';
import { _HttpClient } from '@delon/theme';
import {EditorConfig} from '../../../shared/utils/editor-config';
declare var editormd: any;
import * as $ from 'jquery';
@Component({
selector: 'app-license-info',
templateUrl: './license-info.component.html',
})
export class LicenseInfoComponent implements OnInit, AfterViewInit {
conf = new EditorConfig();
editor: any;
markdown = '測試語句';
constructor(
private http: _HttpClient,
private msg: NzMessageService,
private cdr: ChangeDetectorRef,
private router: Router
) {}
ngOnInit() {
editormd.markdownToHTML('editor', this.conf)
}
}
html:
<div id="editor">
<textarea style="display: none;" ></textarea>
</div>