Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

showdownjs,实现MD格式进行HTML渲染 #52

Open
alanhe421 opened this issue Jun 18, 2018 · 1 comment
Open

showdownjs,实现MD格式进行HTML渲染 #52

alanhe421 opened this issue Jun 18, 2018 · 1 comment

Comments

@alanhe421
Copy link
Owner

alanhe421 commented Jun 18, 2018

最新实现md博客写作平台,需要实现MD格式渲染,这里记录下实现

使用类库:showdownjs

@alanhe421
Copy link
Owner Author

alanhe421 commented Jun 19, 2018

  1. CDN资源引入
 <script src="https://cdn.bootcss.com/mark.js/8.11.1/mark.min.js"></script>

如果不想CDN引用在CLI配置文件中加入scripts配置也可。

  1. markdown全局设定
    因为我想全局使用一个markdown设定,所以在APP根组件中进行设定,如果不想全局,直接在对应组件中设定即可
declare let showdown: any;

export class AppComponent {
  constructor() {
    showdown.setFlavor('github');
    showdown.setOption('openLinksInNewWindow', true);
  }

}
  1. 组件使用
declare let showdown: any;
export class EditComponent implements OnInit, OnDestroy {
  renderedCnt: string;
  sourceCnt: string;
  converter = new showdown.Converter();
  updateView() {
    this.renderedCnt = this.converter.makeHtml(this.sourceCnt);
  }
<div class="row">
  <div class="col-md-6">
    <textarea class="form-control sourceCnt" [(ngModel)]="sourceCnt"
              (ngModelChange)="updateView()">
    </textarea>
  </div>
  <div class="col-md-6">
    <div id="rendered" [innerHtml]="renderedCnt|safe" class="rendered-post">

    </div>
  </div>
</div>

image

@alanhe421 alanhe421 changed the title 引入mark.js,实现关键词高亮 引入showdownjs,实现MD格式进行HTML渲染 Jun 19, 2018
@alanhe421 alanhe421 changed the title 引入showdownjs,实现MD格式进行HTML渲染 showdownjs,实现MD格式进行HTML渲染 Jun 20, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant