Vue Showdown 默认类

问题描述

我想实现以下说我作为MD

md:'#H1'

我想把它渲染成

<h1>H1</h1>

我能够使用 VueShowdown

实现这一目标

但我想为每个 h1 标签添加认类

<h1 class="custom">H1</h1>

我得到了类似于这个 here 的东西。

但我不知道如何在 Vue 中使用它。

甚至可以在 VueShowdown 中使用吗?

有没有更好的库有这个功能

解决方法

您可以创建一个简单的指令:

Vue.directive('default-classes',(parentElement) {
  const els = parentElement.querySelectorAll('h1')

  els.forEach((el) => {
    el.classList.add('custom')
  })
  
})

然后将该指令应用于 VueShowdown 组件:

<VueShowdown v-default-classes :markdown="markdownBinding" />