Vue中使用Marked

使用marked解析markdown文本

Marked只能解析markdown文本,并不能编辑,倒是可以从数据库中读取markdown文字进行解析

Marked使用也很简单

首先npm install marked -s安装好

然后在你需要使用的组件中直接引入就能用了

完整的代码:

<template>
  <div>
    <div class="context" v-html="compiledMarkdown"></div>
  </div>
</template>

<script>
import { marked } from "marked";
export default {
  name: "index",
  data() {
    return {
      articleDetail: {
        context:
          "[连接](https://maosource.com)<br>" +
          "图片<br>" +
          " <img width='200' height='100' alt=文件 src= https://files.maosource.com/20211108/889e273d2c1c44ecad229dd0100564cc.jpg />",
      }, //数据,可以从数据库中读取
    };
  },
  computed: {
    compiledMarkdown() {
      return marked.parse(this.articleDetail.context);
    },
  },
};
</script>

<style scoped></style>

效果图
在这里插入图片描述
另外代码高亮还要另外解析,用highlight.js

// Create reference instance
import { marked } from 'marked';

// Set options
// `highlight` example uses https://highlightjs.org
marked.setOptions({
  renderer: new marked.Renderer(),
  highlight: function(code, lang) {
    const hljs = require('highlight.js');
    const language = hljs.getLanguage(lang) ? lang : 'plaintext';
    return hljs.highlight(code, { language }).value;
  },
  langPrefix: 'hljs language-', // highlight.js css expects a top-level 'hljs' class.
  pedantic: false,
  gfm: true,
  breaks: false,
  sanitize: false,
  smartLists: true,
  smartypants: false,
  xhtml: false
});

// Compile
console.log(marked.parse(markdownString));

更多详细的使用教程可以去看官方文档:https://marked.js.org/

### 如何在 Vue 项目中集成和使用 marked.js #### 安装依赖库 为了能够在 Vue 应用程序中处理 Markdown 文本并将其渲染为 HTML,需要安装 `marked` 这一 npm 包。 ```bash npm install marked --save ``` #### 创建 Marked 组件 创建一个新的组件来封装 `marked` 的功能。这有助于保持代码整洁以及便于在整个应用程序中的重用[^1]。 ```vue <template> <div v-html="compiledMarkdown"></div> </template> <script> import { computed } from 'vue'; import * as marked from 'marked'; export default { props: ['source'], setup(props) { const compiledMarkdown = computed(() => { return marked(props.source || ''); }); return { compiledMarkdown, }; } }; </script> ``` 此组件接收一个名为 `source` 的属性作为输入,并通过计算属性 `compiledMarkdown` 将其转换成 HTML 字符串。注意这里使用了 `v-html` 指令绑定到最终编译后的 HTML 上。 #### 使用 Marked 组件 现在可以在任何地方引入这个自定义的 `<Marked>` 组件,并传递想要解析的内容给它: ```vue <template> <article> <h1>My Blog Post Title</h1> <!-- Pass markdown string to child component --> <Marked :source="blogPostContent"/> </article> </template> <script> // Import your new Marked component here. import Marked from './components/Marked.vue'; export default { components: { Marked, // Register the custom component locally within this file only. }, data() { return { blogPostContent: '# Hello World\nThis is an example of **bold text** and _italic_.', }; } } </script> ``` 这样就完成了在 Vue 中集成 `marked.js` 并利用它的基本流程介绍。可以按照上述方法轻松地把 Markdown 转换成漂亮的 HTML 页面显示出来。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值