VUE-UI-DOCS
vue 2.x 组件库自动解析 Markdown 示例,自动构建发布到 github-pages
和 npm
实现目标
- 自动发布
- 使用
Travis
自动构建和发布代码 - 提交代码到
master
分支,自动发布到github-pages
- 打标签
tag
到master
分支,自动发布到npm
- 使用
- 文档解析
-
.md
文件通过编写webpack
加载器解析为vue
代码 -
Markdown
文件解析其中包含snippet::: {code} :::
格式为局部组件
-
- 项目构建
-
travis
自动构建 https://travis-ci.org/github/kitorv/vue-ui-docs -
github-pages
在线访问 https://kitorv.github.io/vue-ui-docs -
npm
包安装 https://www.npmjs.com/package/vue-ui-docs
-
代码示例格式:
:::snippet 通过 `v-button` 标签初始化按钮。 ```html Default ``` :::
解析示例渲染:
创建项目
- 全局安装
Vue CLI
更多内容查看官方文档
npm install -g @vue/cli
vue ui
可视化操作,也可以通过vue crate {project}
vue ui
- 创建项目
- 预设配置
- 手动配置
- 详细配置
- 安装依赖
- 启动项目
- 访问地址
- 浏览器访问
结构调整
- 文件结构调整
├─site //示例网站目录│ └─components //示例网站组件│ └─router //路由配置│ │ └─indexjs //路由配置文件│ └─views //示例页面│ │ └─Homevue //示例网站首页│ └─Appvue //项目入口│ └─mainjs //启动文件├─src //源码目录
- 根目录创建
vue.config.js
配置
moduleexports = pages: index: entry: "site/main.js" ;
- 相关的依赖、路由配置进行调整,
Home.vue
页面调整
<template> <h1>Home</h1></template> <script>export default { name: "Home"};</script>
- 页面访问
组件开发
Button
组件开发
{{ text }}
- 安装配置
src/button/index.js
; Button { Vue;}; ;
- 组件集成安装
src/index.js
; const components = Button; const install = { components;}; install;
- 入口文件引入
site/main.js
;Vue;
文档解析
- 文档示例组件开发
site/components/snippet.vue
,并且在site/main.js
使用Vue.component
全局注册
<!-- 代码生成vue示例 --> <!-- 示例描述说明 --> <!-- 示例代码高亮显示 -->
build/markdown-loader.js
解析 marokdown 为 vue 文件,并且自定义解析代码块:::snippet {content} :::
const MarkdownIt = ;const MarkdownItContainer = ;const VueTemplateComplier = ;const hljs = ;const parse compileTemplate = ; module { // 需要解析成vue代码块集合 const componentCodeList = ; let styleCodeList = ; const globalScript = ; // 初始还MarkdownIt用于转换md文件为html const markdownIt = ; // 解析【:::tip:::】 markdownIt; // 解析【:::warning:::】 markdownIt; // 使用【markdown-it-container】插件解析【:::snippet :::】代码块为vue渲染 markdownIt; // 将所有转换好的代码字符串拼接成vue单组件template、script、style格式 return ` <template> <div class="vc-snippet-doc"> </div> </template> <script> export default { name: 'vc-component-doc', components: { } } </script> <style lang='scss'> </style>`;};
vue.config.js
配置webpack
加载器解析.md
文件
moduleexports = pages: index: // 入口文件 entry: "site/main.js" { // 解析Markdown文件转成vue组件 configmodule options compilerOptions: preserveWhitespace: false ; };
- 文档编写
src/button/index.md
# Button 按钮 按钮用于开始一个即时操作。 ## 基础用法 :::snippet 通过 `v-button` 标签初始化按钮。 ```html Default ``` ::: ## 文本设置 :::snippet 通过 `text` 设置按钮文本。 ```html ``` ::: ## 事件绑定 :::snippet 绑定 `click` 事件。 ```html ``` ::: ## Button Attributes | 参数 | 说明 | 类型 | 可选值 | 默认值 || ---- | -------- | ------ | ------ | ------ || text | 按钮文本 | String | — | — | ## Button Events | 事件名称 | 说明 | 回调参数 || -------- | -------- | -------- || click | 单击触发 | event | ## Button Slots | 名称 | 说明 || ---- | -------- || — | 按钮内容 |
- 路由配置
... path: "/component/button" name: "component-button" import"../../src/button/index.md" ...
- 浏览器访问
Travis 自动构建
Github
授权配置 https://github.com/settings/tokens 并且记录GITHUB_TOKEN
的值
NPM
创建一个token
授权码,记录该授权码
githbub
授权访问 https://www.travis-ci.org
- Travis 环境变量设置
变量 | 描述 |
---|---|
GITHUB_TOKEN | Github 生成的授权 Token |
NPM_EMAIL | NPM 注册邮箱 |
NPM_TOKEN | NPM 授权 Token |
- 构建配置文件
.travis.yml
具体配置参考 https://docs.travis-ci.com
# 编译环境language: node_js # Node 版本node_js: - "10" # 安装依赖install: - npm install # 代码编译script: - npm run build - npm run release # 发布配置deploy: # 发布到 gh-pages - provider: pages local_dir: dist skip_cleanup: true github_token: $GITHUB_TOKEN keep_history: true on: branch: master # 发布到 npm - provider: npm email: $NPM_EMAIL api_key: $NPM_TOKEN skip_cleanup: true on: tags: true branch: master
- 项目构建
vue.config.js
// vue.config.js 部署路径调整...publicPath: processenvNODE_ENV !== "production" ? "/" : "/vue-ui-docs"...
// site/router/index.js 路由调整...base: processenvNODE_ENV !== "production" ? "/" : "/vue-ui-docs"...
package.json
添加script
打包发布指令
..."release": "vue-cli-service build --dest lib --target lib src/index.js"...
- Git 新增标签
v1.0.0
提交推送代码,触发自动构建
Github-Pages
访问 https://kitorv.github.io/vue-ui-docs