markdown 的正确使用方式

·1.下载对应插件

pnpm add markdown-it highlight.js

2使用

2.1 markdown-it的使用
<template>
  <div class="text-content markdown-body">
    <div v-highlight v-html="renderedMarkdown" />
  </div>
</template>

<script>
import MarkdownIt from 'markdown-it'
export default {
  components: {
  },
  props: {
    source: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
    }
  },
  computed: {
    renderedMarkdown() {
      return this.md.render(this.source)
    }
  },
  created() {
    this.md = new MarkdownIt()
  }
}
</script>
<style lang="scss" scoped>
// 修改内部样式
.markdown-body::v-deep {
  > p:nth-last-of-type(1) {
    margin: 0 !important;
  }
  table {
    margin-top: 10px !important;
  }
  ul {
    li::before {
      content: '\2022'; /* 使用 Unicode 或其他字符表示小点 */
      color: #3a7fff; /* 设置小点颜色 */
      display: inline-block; /* 使小点与文本在同一行 */
      width: 1em; /* 调整小点的宽度,以适应需要的大小 */
      font-size: 16px;
      margin-left: -1em; /* 负的左边距使小点与文本对齐 */
    }
  }
  ol > li::marker {
    color: #3a7fff;
  }
  pre{
    font-size: 70%;
  }
}
</style>


2.2 highlight 的使用
import '@/assets/css/markdown-css/github-markdown-light.css'
import 'highlight.js/styles/atom-one-dark.css'
import hljs from 'highlight.js'
import Vue from 'vue'
// 通过 import * as hljs from 'highlight.js' 引入
Vue.directive('highlight', function (el) {
  const blocks = el.querySelectorAll('pre code')
  blocks.forEach(block => {
    hljs.highlightElement(block)
    block.classList.add('hljs')
  })
})
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容