在我们开发vue组件的过程中,希望把markdown文档写的插件使用说明,也展现到页面中。那么具体怎么做呢?

1、安装vue-markdown-loader

npm install vue-markdown-loader --save
npm install markdown-it-container --save

2、为webpack配置loader

{test:/\.md$/,loader: 'vue-markdown-loader',options: vueMarkdown}

3、配置options

vue-markdown-loader配置options。

这一部分的代码主要是将markDown的源码转换成,html代码。注意里面有几处用到了自定义的工具类,在utils.js下,后面会附带源码。

这个options是被作为一个独立的模块使用的,建议复制出来后以一个独立的文件保存在build文件夹中。

const striptags = require('./strip-tag');
const vueMarkdown = {preprocess: (MarkdownIt, source) => {MarkdownIt.renderer.rules.table_open = function () {return '<table class="table">'}MarkdownIt.renderer.rules.fence = utils.wrapCustomClass(MarkdownIt.renderer.rules.fence)// ```code`` 给这种样式加个class code_inlineconst code_inline = MarkdownIt.renderer.rules.code_inlineMarkdownIt.renderer.rules.code_inline = function(...args){args[0][args[1]].attrJoin('class', 'code_inline')return code_inline(...args)}return source},use: [[require('markdown-it-container'), 'demo', {validate: params => params.trim().match(/^demo\s*(.*)$/),render: function(tokens, idx) {var m = tokens[idx].info.trim().match(/^demo\s*(.*)$/);if (tokens[idx].nesting === 1) {var desc = tokens[idx + 2].content;const html = utils.convertHtml(striptags(tokens[idx + 1].content, 'script'))// 移除描述,防止被添加到代码块tokens[idx + 2].children = [];return `<demo-block><div slot="desc">${html}</div><div slot="highlight">`;}return '</div></demo-block>\n';}}],[require('markdown-it-container'), 'tip'],/* or */[require('markdown-it-container'), 'warning']]
};
exports.vueMarkdown = vueMarkdown;

在这段代码顶部,我们引入了striptags。这个模块是用来解析示例代码的。strip-tag的具体内容。

const cheerio = require('cheerio')//cheerio是nodejs的抓取页面模块,为服务器特别定制的,快速、灵活、实施的jQuery核心实现。适合各种Web爬虫程序。module.exports = (str, tags) => {const $ = cheerio.load(str, { decodeEntities: false })if (!tags || tags.length === 0) {return str}tags = !Array.isArray(tags) ? [tags] : tagslet len = tags.lengthwhile (len--) {$(tags[len]).remove()}return $.html()
}

在webpack的build目录下的utils.js中添加下面的两个方法

/*** 增加 hljs 的 classname*/
exports.wrapCustomClass = function (render) {return function (...args) {return render(...args).replace('<code class="', '<code class="hljs ').replace('<code>', '<code class="hljs">')}
};/*** Format HTML string*/
exports.convertHtml = function (str) {return str.replace(/(&#x)(\w{4});/gi, $0 => String.fromCharCode(parseInt(encodeURIComponent($0).replace(/(%26%23x)(\w{4})(%3B)/g, '$2'), 16)))
};

4、配置展示mark-down文档的vue页面

准备一个名叫demo-block的vue文件,放在你喜欢的地方。然后注册全局组件。

注意,在注册全局组件时,一定是叫demo-block。

import demoBlock from './components/demo-block.vue';
Vue.component('demo-block', demoBlock);

demo-block.vue文件的源码。

<template><div            class="demo-block":class="[{ 'hover': hovering }]"@mouseenter="hovering = true"@mouseleave="hovering = false"><slot name="source"></slot><div class="meta" ref="meta"><div class="description" v-if="$slots.default"><slot></slot></div><slot name="highlight"></slot></div></div>
</template><script type="text/babel">export default {data () {return {hovering: false,}},
}</script>

5、设置路由

大家会发现,每一个markddow文档对应一个路由,所以我们切换markDown的时候,其实是切换的是子路由页面。

import Vue from 'vue';
import Router from 'vue-router';
import loadMap from '@/components/loadMap.vue';Vue.use(Router)export default new Router({routes: [{path: '/',name: 'loadMap',component: loadMap,redirect:{ name : 'home' },children: [{path: '/home',name: 'home',component: r=> require.ensure([], () => r(require('../docs/readme.md')))},{path: '/test',name: 'test',component: r => require.ensure([], () => r(require('../docs/hello.md')))}]}]
})

6、编写一个模板文档

@这是我们展示的说明文档
**三维测量 measure**
### 基本用法* @param type 测量类型
* line 直线测量
* area 面积测量
* vertical 垂直测量
* horizontal 水平测量-----
@这部分是页面上可以交互的代码<div class="measure-ct"><button @click="measure('line')">直线测量</button><button @click="measure('area')">面积测量</button><button @click="measure('vertical')">垂直测量</button><button @click="measure('horizontal')">水平测量</button><button @click="measureCancel">清除测量结果</button></div><br>::: demo@这部分是我们展示的示例代码
```shell
import {cw5} from 'cw3d/index.js';
cw5.measure.measureMent(type)
```
```html
<template><div><div class="measure-ct"><button @click="measure('line')">直线测量</button><button @click="measure('area')">面积测量</button><button @click="measure('vertical')">垂直测量</button><button @click="measure('horizontal')">水平测量</button><button @click="measureCancel">取消测量模式</button></div></div>
</template>
<script>
import {cw5} from 'cw3d/index.js';
export default {name: 'measure',methods: {measure(type){cw5.measure.measureMent(type)},measureCancel(){cw5.measure.measureCancel()}}
}
</script>```
:::

7、优化样式—-引入文档模块的css文件

这个css文件有两个版本,一个lass、一个css。通常css引入就可以。

可以在任意需要css渲染的文件中,引入这个css文件。

<style>@import "../assets/docs.css";.example-block{display:flex;}</style>

要引入的docs.css文件。

body, h1, h2, h3, h4, h5, h6, hr, p, blockquote, dl, dt, dd, ul, ol, li, pre, form, fieldset, legend, button, input, textarea, th, td,
iframe {margin:0;padding:0;border:0;vertical-align:baseline;
}
body {font-family:'Helvetica Neue', Helvetica, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', SimSun, sans-serif;font-weight:400;-webkit-font-smoothing:antialiased;background-color:#f8f8f8;
}
a {color:#4078c0;text-decoration:none;
}
button, input, select,
textarea {font-family:inherit;font-size:inherit;line-height:inherit;color:inherit;
}
ul, ol {list-style:none;
}
code.hljs {line-height:1.5;font-family:Source Code Pro, Menlo, Monaco, Consolas, Courier, monospace;font-size:12px;padding:20px;background-color:#2a3138;border:solid 1px #E5E5E5;margin-bottom:25px;border-radius:4px;-webkit-font-smoothing:auto;
}
.clearfix::before {display:table;content:"";
}
.clearfix::after {display:table;content:"";clear:both;
}
.main-content {width:50%;margin:50px 20px 40px;border:1px solid #f2f2f2;
}
.main-content .page-tip {position:absolute;left:50%;top:60px;transform:translatex(-50%);height:60px;line-height:60px;font-size:24px;color:#0067ed;
}
.page-container {background-color:#fff;position:relative;display:flex;width:100%;overflow:hidden;
}
.page-content {box-sizing:border-box;flex:1;
}
.page-content section {padding:0 40px;
}
.page-content section > h1,
.page-content section > h2,
.page-content section > h3,
.page-content section > h4,
.page-content section > h5,
.page-content section > h6 {color:#333;line-height:1.5;margin:20px 0;font-weight:normal;
}
.page-content section > h1:hover a,
.page-content section > h2:hover a,
.page-content section > h3:hover a,
.page-content section > h4:hover a,
.page-content section > h5:hover a,
.page-content section > h6:hover a {opacity:.4;
}
.page-content section > h1 a,
.page-content section > h2 a,
.page-content section > h3 a,
.page-content section > h4 a,
.page-content section > h5 a,
.page-content section > h6 a {float:left;margin-left:-20px;opacity:0;cursor:pointer;
}
.page-content section > h1 a:hover,
.page-content section > h2 a:hover,
.page-content section > h3 a:hover,
.page-content section > h4 a:hover,
.page-content section > h5 a:hover,
.page-content section > h6 a:hover {opacity:.4;
}
.page-content section > h1 {font-size:34px;
}
.page-content section > h2 {font-size:28px;
}
.page-content section > h3 {font-size:22px;
}
.page-content section > h4 {font-size:18px;
}
.page-content section > h5 {font-size:16px;
}
.page-content section > h6 {font-size:14px;color:#666;
}
.page-content section > p {font-size:14px;line-height:20px;color:#666;margin:14px 0;
}
.page-content section > p a {color:#38f;
}
.page-content section > ul li,
.page-content section > ol li {color:#666;font-size:14px;line-height:20px;margin:10px 0 10px 20px;padding-left:20px;position:relative;
}
.page-content section > ul li a,
.page-content section > ol li a {color:#38f;
}
.page-content section > ul li::before,
.page-content section > ol li::before {content:'';position:absolute;width:8px;height:8px;box-sizing:border-box;border:2px solid #999;border-radius:50%;top:6px;left:0;
}
.page-content section > ul li li,
.page-content section > ol li li {margin-left:0;
}
.page-content p > code,
.page-content .table code,
.page-content li > code {background-color:#F2F2F2;display:inline-block;border:1px solid #E5E5E5;border-radius:3px;padding:1px 3px;color:#333;margin:0 2px;
}
.table {border-collapse:collapse;width:100%;background-color:#fff;color:#333;font-size:14px;margin-bottom:45px;
}
.table th {text-align:left;border:1px solid #E5E5E5;background-color:#F2F2F2;padding:10px;
}
.table th:first-child {padding-left:10px;
}
.table td {border:1px solid #E5E5E5;padding:10px;
}
.l {float:left;
}
.r {float:right;
}
.gray {color:#999;
}
.placeholder {font-size:14px;color:#cccccc;
}
.vui-intro {text-align:center;
}
.vui-intro__logo img {margin:0 auto;
}
.vui-intro__title {font-size:45px;line-height:60px;font-weight:400;font-family:monospace;
}
.vui-intro__subtitle {font-size:15px;color:#455a64;
}

前端页面渲染markDown文件相关推荐

  1. html展示markdown文件,在前端页面展示Markdown文件

    常我们都会在GitHub上浏览很多的readme文件,这些都是Markdown语法写成的Markdown文件,HTML中并没有用于展示Markdown文件的元素,那么为什么可以在前端展示呢? 有别于G ...

  2. react引入渲染markdown文件

    因为需要在项目中添加更新日志,而一般更新日志都是markdown文件,所以需要实现在react中引入并渲染markdown文件. 1. 安装依赖 npm install react-markdown ...

  3. 前端页面渲染方式CSR、SSR、SSG

    三种渲染方式: 使用预渲染之前,首先要考虑是否真的需要它. 如果初始加载时的额外几百毫秒并不会对应用产生影响,这种情况下去使用预选染将是一个小题大作之举. 首屏加载时间是绝对关键的指标,在这种情况下, ...

  4. 前端页面渲染的几种方式(CSR/SSG/SSR)

    客户端渲染 CSR (Croswer Side Render) 是在单页应用中使用最多的渲染方式,我们现在的项目常用使用了react.vue之类的单页应用的框架,这类框架在进行页面渲染时,会到首先对h ...

  5. 前端页面导出excel文件功能(导出)

    方法一: 一.后台管理系统中常有导出excel功能,可定义一个函数,在其他.vue文件中导入即可用. 1.1定义函数 export function excelDown(res, name = &qu ...

  6. 【Vue】解析渲染markdown文件

    1 安装依赖 npm i vue-markdown-loader -s npm i vue-loader vue-template-compiler -snpm i github-markdown-c ...

  7. 在React中解析渲染markdown文件

    解决办法 安装marked对md文件进行解析 npm install marked --save 基本使用 import { marked } from 'marked';const renderer ...

  8. flask中使用FileField上传文件的两种方式+前端页面上传文件(flask三种上传文件方式)

    文章目录 上传文件方式一: 1.index.html文件: 2.主文件main.py: 上传文件方式二: 1.index2.html文件: 2.main.py文件: 上传文件方式三: 1.index3 ...

  9. 前端无法渲染CSS文件

最新文章

  1. 【项目介绍】FTP服务器
  2. 手机1像素线粗_关于移动端开发 1px 线的一些理解和解决办法
  3. method=post 怎么让查看源代码看不到_网上文档无法复制怎么办?试试这几个方法!...
  4. java开发,年薪15W的和年薪50W的差距
  5. sqlserver date类型和字符串比较_基于SQL Server数据库搭建主从复制实现读写分离实战演练...
  6. 微软开源深度学习优化库 DeepSpeed,可训练 1000 亿参数的模型
  7. C++primer 6.7节练习
  8. 清新脱俗的Java选课系统
  9. 【转】 解决IllegalStateException: Can not perform this action after onSaveInstanceState
  10. 判断素数的程序代码c语言,C语言中判断素数的程序代码是什么?
  11. 《通关!游戏设计之道》学习笔记
  12. Google登录提示错误码12501
  13. mysql8.0.17 汉化
  14. 数据分析案例-航班准点分析
  15. PMBOK(第4版)--项目管理九大知识领域的【输入】、【工具与技术】、【输出】一览表
  16. JB的Shell之旅-30分钟带你入门
  17. 练习:罗马数字转整数
  18. 技术总监被开除了....
  19. SQL 添加、删除、更改字段(属性)
  20. 拥抱AI,“纳德拉式”的微软复兴之路 | 人工智能观察

热门文章

  1. WIN SECS .NET 2.7
  2. 在windows系统中硬盘安装Fedora20
  3. TemplateInputException:template might not exist or might not be accessible
  4. 综述:Reading, writing and erasing mRNA methylation
  5. 静态HTML网页设计作品——生鲜超市网站设计(5页)HTML+CSS+JavaScript 学生DW网页设计作业成品 美食站
  6. 一、STM32简介、选型及其目标
  7. 互联网公司常用的DevOps 工具,你都认识吗?
  8. 小猪的C语言快速入门系列(三)
  9. springboot自习室管理系统 毕业设计-附源码221535
  10. Docker File