当前位置:   article > 正文

使用VuePress搭建一个类似element的文档

如何快速生成类似element的在线阅读文档

网站成果样式


项目书写步骤

github地址:github.com/xuhuihui/da…

官网:caibaojian.com/vuepress/gu…

参考文章:www.javascriptcn.com/read-31206.…

前言:我先git clone官方github,运行查看完整效果。 再根据官网介绍和参考文章,结合完整的代码,自己一步步配置内容。最后,参考element的设计样式,修改并增加代码,形成一个平台组件库的网站。

(1)在已有项目中安装

  1. # 安装为本地依赖项
  2. npm install -D vuepress
  3. # 创建一个 docs 目录
  4. mkdir docs
  5. # 创建一个 markdown 文件
  6. echo '# Hello VuePress' > docs/README.md
  7. # 给package.json 添加一些 scripts 脚本:{
  8. "scripts": {
  9. "docs:dev": "vuepress dev docs",
  10. "docs:build": "vuepress build docs"
  11. }
  12. }# 运行项目
  13. yarn run docs:dev
  14. 复制代码

出现显示文档乱码问题,如图所示:


解决方式:修改md文件编码为UTF-8


改变md文件的内容如下:

  1. ---
  2. home: true
  3. actionText: 前往 →
  4. actionLink: /baseComponents/
  5. features:
  6. - title: 布局类组件
  7. details: 基本组件,为常用组件提供快速,可用的组件
  8. - title: 可视化组件
  9. details: 积累将数据可视化的业务组件
  10. - title: 知识库
  11. details: 积累前端相关的知识,涵盖 vue、react、koa2、nodejs 相关的知识点
  12. ---复制代码

(2)配置文件

配置(参考链接:caibaojian.com/vuepress/co…) VuePress 站点的基本文件是 .vuepress/config.js,其中导出一个 JavaScript 对象:

  1. module.exports = {
  2. title: 'data Com', // 设置网站标题
  3. description: 'Just for fun', //描述
  4. dest: './dist', // 设置输出目录
  5. port: 2233, //端口
  6. themeConfig: { //主题配置
  7. // 添加导航栏
  8. nav: [
  9. { text: '主页', link: '/' }, // 导航条
  10. { text: '组件文档', link: '/baseComponents/' },
  11. { text: '知识库', link: '/knowledge/' },
  12. { text: 'github', // 这里是下拉列表展现形式。
  13. items: [
  14. { text: 'focus-outside', link: 'https://github.com/TaoXuSheng/focus-outside' },
  15. { text: 'stylus-converter', link: 'https://github.com/TaoXuSheng/stylus-converter' },
  16. ]
  17. }
  18. ],
  19. // 为以下路由添加侧边栏
  20. sidebar:{
  21. '/baseComponents/': [
  22. {
  23. title: '布局类组件',
  24. collapsable: true,
  25. children: [
  26. 'base/test1',
  27. 'base/test2',
  28. 'base/test3',
  29. 'base/test4',
  30. ]
  31. },
  32. {
  33. title: '可视化组件',
  34. collapsable: true,
  35. children: [
  36. ]
  37. },
  38. {
  39. title: '工具类组件',
  40. collapsable: true,
  41. children: [
  42. ]
  43. },
  44. {
  45. title: '方法类函数',
  46. collapsable: true,
  47. children: [
  48. ]
  49. }
  50. ],
  51. '/knowledge/': [
  52. {
  53. title: 'CSS知识库',
  54. collapsable: false,
  55. children: [
  56. ]
  57. },
  58. {
  59. title: 'JS知识库',
  60. collapsable: false,
  61. children: [
  62. ]
  63. },
  64. {
  65. title: 'node知识库',
  66. collapsable: false,
  67. children: [
  68. ]
  69. },
  70. {
  71. title: 'vue知识库',
  72. collapsable: false,
  73. children: [
  74. ]
  75. }
  76. ]
  77. }
  78. }
  79. }复制代码

主题配置部分:在.vuepress/override.styl修改样式:

  1. $accentColor = #3EB9C8 // 主题色
  2. $textColor = #2c3e50 // 文字颜色
  3. $borderColor = #eaecef // 边框颜色
  4. $codeBgColor = #282c34 // 代码背景颜色
  5. // 代码库重置
  6. .content pre{ margin: 0!important;}复制代码

(3)增加其它扩展插件

插件npm安装:element-ui,vue-echarts,vue-highlight。。

在.vuepress/enhanceApp.js引入:

  1. /**
  2. * 扩展 VuePress 应用
  3. */
  4. import VueHighlightJS from 'vue-highlight.js';
  5. import 'highlight.js/styles/atom-one-dark.css';
  6. import Element from 'element-ui'
  7. import 'element-ui/lib/theme-chalk/index.css'
  8. import VueECharts from 'vue-echarts' //注册图表
  9. import './public/css/index.css' //组件css文件
  10. export default ({
  11. Vue, // VuePress 正在使用的 Vue 构造函数
  12. options, // 附加到根实例的一些选项
  13. router, // 当前应用的路由实例
  14. siteData // 站点元数据
  15. }) => {
  16. // ...做一些其他的应用级别的优化
  17. Vue.use(VueHighlightJS)
  18. Vue.use(Element)
  19. Vue.component('chart', VueECharts)
  20. }
  21. 复制代码

(4)Markdown 拓展

调用别人写好的轮子:www.npmjs.com/package/vue…

  1. <highlight-code slot="codeText" lang="vue">
  2. <template>
  3. <div class="demo-button">
  4. <div>
  5. <dt-button>默认按钮</dt-button>
  6. <dt-button type="primary">主要按钮</dt-button>
  7. <dt-button type="success">成功按钮</dt-button>
  8. <dt-button type="info">信息按钮</dt-button>
  9. <dt-button type="warning">警告按钮</dt-button>
  10. <dt-button type="danger">危险按钮</dt-button>
  11. </div>
  12. </template>
  13. </highlight-code>复制代码

(5)在Markdown 使用Vue-----插入按钮样式

#先写一个按钮组件.\vuepress\docs\.vuepress\components\src\button.vue

  1. <template>
  2. <button
  3. class="dt-button"
  4. @click="handleClick"
  5. :disabled="disabled"
  6. :autofocus="autofocus"
  7. :type="nativeType"
  8. :class="[
  9. size ? 'dt-button--' + size : '',
  10. type ? 'dt-button--' + type : '',
  11. {
  12. 'is-disabled': disabled,
  13. 'is-round': round,
  14. 'is-plain': plain
  15. }
  16. ]">
  17. <i :class="icon" v-if="icon"></i>
  18. <span v-if="$slots.default"><slot></slot></span>
  19. </button>
  20. </template>
  21. <script>
  22. export default {
  23. name: 'DtButton',
  24. props: {
  25. size: String,
  26. type: {
  27. type: String,
  28. default: 'default'
  29. },
  30. nativeType: {
  31. type: String,
  32. default: 'button'
  33. },
  34. disabled: Boolean,
  35. round: Boolean,
  36. plain: Boolean,
  37. autofocus: Boolean,
  38. icon: {
  39. type: String,
  40. default: ''
  41. }
  42. },
  43. methods: {
  44. handleClick (event) {
  45. this.$emit('click', event)
  46. }
  47. },
  48. mounted () {
  49. this.$emit('click', event)
  50. }
  51. }
  52. </script>复制代码

#css样式,在.\vuepress\docs\.vuepress\public\css\button.css,写法参考饿了么。

#在.\study\vuepress\docs\.vuepress\public\css\index.css汇总

  1. @import './iconfont.css';
  2. @import './icon.css';
  3. @import './card.css';
  4. @import './button.css'; //按钮组件
  5. @import './checkbox.css';
  6. 复制代码

#在.\vuepress\docs\.vuepress\components\test\test1.vue文件夹下面调用button

  1. <template>
  2. <div class="demo-button">
  3. <div>
  4. <dt-button>默认按钮</dt-button>
  5. <dt-button type="primary">主要按钮</dt-button>
  6. <dt-button type="success">成功按钮</dt-button>
  7. <dt-button type="info">信息按钮</dt-button>
  8. <dt-button type="warning">警告按钮</dt-button>
  9. <dt-button type="danger">危险按钮</dt-button>
  10. </div>
  11. </div>
  12. </template>
  13. <script>
  14. import DtButton from '../src/button'
  15. export default {
  16. name: 'buttonWrap',
  17. components: {
  18. DtButton
  19. }
  20. }
  21. </script>
  22. <style lang="less" scoped>
  23. .demo-button{
  24. width: 100%;
  25. text-align: center;
  26. div {
  27. margin: 10px 0;
  28. }
  29. }
  30. </style>
  31. 复制代码

#vuepress会自动根据路径注册组件,我们只要映射文件路径,就可以调用组件。

在.\vuepress\docs\baseComponents\base\test1.md

  1. # 测试案例1
  2. ---
  3. <Common-Democode title="基本用法" description="基本按钮用法">
  4. <test-test1></test-test1>
  5. <highlight-code slot="codeText" lang="vue">
  6. <template>
  7. <div class="demo-button">
  8. <div>
  9. <dt-button>默认按钮</dt-button>
  10. <dt-button type="primary">主要按钮</dt-button>
  11. <dt-button type="success">成功按钮</dt-button>
  12. <dt-button type="info">信息按钮</dt-button>
  13. <dt-button type="warning">警告按钮</dt-button>
  14. <dt-button type="danger">危险按钮</dt-button>
  15. </div>
  16. </div>
  17. </template>
  18. </highlight-code>
  19. </Common-Democode>
  20. | Tables | Are | Cool |
  21. | ------------- |:-------------:| -----:|
  22. | col 3 is | right-aligned | $1600 |
  23. | col 2 is | centered | $12 |
  24. | zebra stripes | are neat | $1 |复制代码

#展示效果如图:




转载于:https://juejin.im/post/5aefc37a6fb9a07ab83df131

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/744477
推荐阅读
相关标签
  

闽ICP备14008679号