实现Markdown显示
-
安装 ngx-markdown npm install ngx-markdown --save
-
再angular.json 中引入对应的css文件和js文件 "styles": [
"src/styles.css",
"node_modules/prismjs/themes/prism-okaidia.css",
"node_modules/prismjs/plugins/line-numbers/prism-line-numbers.css",
"node_modules/prismjs/plugins/line-highlight/prism-line-highlight.css",
"node_modules/katex/dist/katex.min.css"
],
"scripts": [
"node_modules/marked/lib/marked.js",
"node_modules/prismjs/prism.js",
"node_modules/prismjs/components/prism-csharp.min.js",
"node_modules/prismjs/components/prism-css.min.js",
"node_modules/prismjs/plugins/line-numbers/prism-line-numbers.js",
"node_modules/prismjs/plugins/line-highlight/prism-line-highlight.js",
"node_modules/prismjs/components/prism-typescript.min.js",
"node_modules/emoji-toolkit/lib/js/joypixels.min.js",
"node_modules/katex/dist/katex.min.js"
]
-
app.module.ts文件 import { NgModule } from '@angular/core';
+ import { MarkdownModule } from 'ngx-markdown';
import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { HttpClient, HttpClientModule } from '@angular/common/http';
+ import { MdShowComponent } from './md-show/md-show.component';
@NgModule({
declarations: [AppComponent, MdShowComponent],
imports: [
BrowserModule,
AppRoutingModule,
HttpClientModule,
+ MarkdownModule.forRoot({ loader: HttpClient }),
],
providers: [],
bootstrap: [AppComponent],
})
export class AppModule {}
-
通过如下方式显示code
-
管道
- typescritpMarkdown的值
public typescriptMarkdown = `
\`\`\`typescript
import { Component } from '@angular/core';
@Component({
selector: 'markdown-demo',
templateUrl: './markdown-demo.component.html',
styleUrls: ['./markdown-demo.component.scss'],
})
export class MarkdownDemoComponent {
public pipeMarkdown = '# Markdown';
}
\`\`\``;
-
页面 <div [innerHTML]="typescriptMarkdown | language: 'typescript' |markdown"></div>
-
显示效果
Markdown编辑器,angular集成editor
-
下载editor放到asset文件中 https://codeload.github.com/pandao/editor.md/zip/master.zip -
修改angular.json "styles": [
"src/styles.css",
"node_modules/prismjs/themes/prism-okaidia.css",
"node_modules/prismjs/plugins/line-numbers/prism-line-numbers.css",
"node_modules/prismjs/plugins/line-highlight/prism-line-highlight.css",
"node_modules/katex/dist/katex.min.css",
"src/assets/md_editor/css/editormd.css",
"src/assets/md_editor/lib/codemirror/codemirror.min.css"
],
"scripts": [
"node_modules/marked/lib/marked.js",
"node_modules/prismjs/prism.js",
"node_modules/prismjs/components/prism-csharp.min.js",
"node_modules/prismjs/components/prism-css.min.js",
"node_modules/prismjs/plugins/line-numbers/prism-line-numbers.js",
"node_modules/prismjs/plugins/line-highlight/prism-line-highlight.js",
"node_modules/prismjs/components/prism-typescript.min.js",
"node_modules/emoji-toolkit/lib/js/joypixels.min.js",
"node_modules/katex/dist/katex.min.js",
"node_modules/jquery/dist/jquery.js",
"src/assets/md_editor/lib/flowchart.min.js",
"src/assets/md_editor/lib/jquery.flowchart.min.js",
"src/assets/md_editor/editormd.min.js",
"src/assets/md_editor/lib/codemirror/codemirror.min.js",
"src/assets/md_editor/lib/marked.min.js",
"src/assets/md_editor/lib/prettify.min.js",
"src/assets/md_editor/lib/underscore.min.js",
"src/assets/md_editor/lib/raphael.min.js",
"src/assets/md_editor/lib/sequence-diagram.min.js"
]
-
添加editor的配置文件editor-config.ts editor的配置在该文件中修改 declare var $: any;
export class EditorConfig {
public path = './assets/md_editor/lib/';
public tocm = true;
public tocContainer = '';
public tocDropdown = false;
public width = '90%';
public height = 720;
public tocStartLevel = 2;
constructor() {}
}
-
创建editor-md.directive.ts import {AfterViewInit, Attribute, Directive, EventEmitter, Input, Output} from '@angular/core';
import {EditorConfig} from './model/editor-config';
declare var editormd: any;
declare var $: any;
@Directive({
selector: '[appEditorMd]'
})
export class EditorMdDirective implements AfterViewInit {
@Input() editormdConfig: EditorConfig;
@Output() onEditorChange: EventEmitter<string> = new EventEmitter<string>();
editor: any;
constructor(@Attribute('id') private id: string) {
}
ngAfterViewInit(): void {
this.editor = editormd(this.id, this.editormdConfig);
const out = this.onEditorChange;
const textarea = $('#' + this.id + ' :first');
this.editor.on('change', function () {
out.emit(textarea.val());
});
}
}
-
editor.module.ts import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { EditormdRoutingModule } from './editormd-routing.module';
import { EditormdComponent } from './editormd.component';
import { FormsModule } from '@angular/forms';
@NgModule({
declarations: [EditormdComponent],
imports: [CommonModule, EditormdRoutingModule, FormsModule],
})
export class EditormdModule {}
-
editormd.component.ts import { Component, OnInit } from '@angular/core';
import { EditorConfig } from '../editor/model/editor-config';
declare var $: any;
declare var editormd: any;
@Component({
selector: 'app-editormd',
templateUrl: './editormd.component.html',
styleUrls: ['./editormd.component.css'],
})
export class EditormdComponent implements OnInit {
constructor() {}
ngOnInit(): void {}
editor: any;
conf = new EditorConfig();
public showInfo: string = '';
ngAfterViewInit(): void {
this.createEditor();
}
createEditor() {
this.editor = editormd('markdown', this.conf);
$('#markdown.editormd-preview-container')[0].innerHTML = this.showInfo;
this.editor.on('change', () => {
const textarea = $('#editText');
const value = {
textarea: textarea.val(),
};
this.showInfo = value.textarea;
});
}
}
-
editormd.cmponent.html <div id="markdown">
<textarea id="editText" style="display: block"></textarea>
</div>
-
显示效果
源码路径 gitee路径
|