|
1 | 1 | # 介绍
|
2 |
| -这里写点啥... |
| 2 | +本项目 fork 自 [vuese](https://vuese.github.io/website/zh/),用于 [mpx](https://www.mpxjs.cn/) 项目的文档定制化生成。(感谢 [vuese](https://vuese.github.io/website/zh/) 的相关开发者) |
| 3 | + |
| 4 | +## 快速上手 |
| 5 | + |
| 6 | +<card> |
| 7 | + |
| 8 | +### 安装 |
| 9 | + |
| 10 | +```bash |
| 11 | +npm install @mpxjs/vuese-website -g |
| 12 | +``` |
| 13 | + |
| 14 | +### 项目配置 |
| 15 | + |
| 16 | +假设你所在的目录为基于组件库搭建的`example`目录,这个目录主要是为了演示组件库组件的用法,启动后的效果如右边模拟器所示。👉 |
| 17 | + |
| 18 | +在`example`目录下新建`website.js`,并写入以下内容: |
| 19 | +```javascript |
| 20 | +const path = require('path') |
| 21 | +const website = require('@mpxjs/vuese-website').default |
| 22 | + |
| 23 | +website({ |
| 24 | + srcDirPath: path.resolve(__dirname, '组件库src目录下的components目录'), |
| 25 | + exampleDirPath: path.resolve(__dirname, './pages'), |
| 26 | + outputPath: path.resolve(__dirname, './docs/components'), |
| 27 | + doscPath: path.resolve(__dirname, './docs'), |
| 28 | +}) |
| 29 | + |
| 30 | +``` |
| 31 | + |
| 32 | +其中`组件库src目录下的components目录`结构如下所示: |
| 33 | +```plaintext |
| 34 | +src/components |
| 35 | +├── button |
| 36 | +| ├── index.mpx |
| 37 | +| ├── index.ts |
| 38 | +└── button-group |
| 39 | + ├── index.mpx |
| 40 | + └── index.ts |
| 41 | +``` |
| 42 | +`exampleDirPath`中的`pages`目录结构如下所示: |
| 43 | +```plaintext |
| 44 | +example/pages |
| 45 | +├── button |
| 46 | +| ├── README.md |
| 47 | +| ├── btn-bolder.mpx |
| 48 | +| ├── btn-choose-avatar.mpx |
| 49 | +| ├── btn-contact.mpx |
| 50 | +| ├── btn-disabled.mpx |
| 51 | +| ├── btn-follow-lifestyle.mpx |
| 52 | +| ├── btn-get-phone-number.mpx |
| 53 | +| ├── btn-get-user-info.mpx |
| 54 | +| ├── btn-icon.mpx |
| 55 | +| ├── btn-inline-icon.mpx |
| 56 | +| ├── btn-inline-outline.mpx |
| 57 | +| ├── btn-inline-primary.mpx |
| 58 | +| ├── btn-inline.mpx |
| 59 | +| ├── btn-launch-app.mpx |
| 60 | +| ├── btn-light-active.mpx |
| 61 | +| ├── btn-light-disabled.mpx |
| 62 | +| ├── btn-light.mpx |
| 63 | +| ├── btn-loading.mpx |
| 64 | +| ├── btn-open-setting.mpx |
| 65 | +| ├── btn-outline-active.mpx |
| 66 | +| ├── btn-outline-disabled.mpx |
| 67 | +| ├── btn-outline-loading.mpx |
| 68 | +| ├── btn-outline-primary-active.mpx |
| 69 | +| ├── btn-outline-primary-disabled.mpx |
| 70 | +| ├── btn-outline-primary.mpx |
| 71 | +| ├── btn-outline.mpx |
| 72 | +| ├── btn-primary-active.mpx |
| 73 | +| ├── btn-primary-disabled.mpx |
| 74 | +| ├── btn-primary.mpx |
| 75 | +| ├── btn-secondary-active.mpx |
| 76 | +| ├── btn-secondary.mpx |
| 77 | +| ├── btn-share.mpx |
| 78 | +| ├── btn-with-tip.mpx |
| 79 | +| └── index.mpx |
| 80 | +├── button-group |
| 81 | +| ├── README.md |
| 82 | +| ├── button-group-one.mpx |
| 83 | +| ├── button-group-two.mpx |
| 84 | +| ├── button-group-vertical-two.mpx |
| 85 | +| ├── button-group-with-context.mpx |
| 86 | +| └── index.mpx |
| 87 | +├── index.mpx |
| 88 | +└── index.ts |
| 89 | +``` |
| 90 | + |
| 91 | +### 文档生成 |
| 92 | + |
| 93 | +文档由[vitepress](https://vitepress.dev/zh/guide/what-is-vitepress)驱动,需要在`example`目录中安装对应的依赖: |
| 94 | +```bash |
| 95 | +npm install vitepress@^1.2.3 |
| 96 | +``` |
| 97 | + |
| 98 | +然后在`example`目录下执行以下命令: |
| 99 | + |
| 100 | +```bash |
| 101 | +vuese-website |
| 102 | +``` |
| 103 | +即可在项目根目录下生成基础的docs配置: |
| 104 | +```plaintext |
| 105 | +docs |
| 106 | +├── .vitepress |
| 107 | +| ├── config.mjs |
| 108 | +| ├── sidebar |
| 109 | +| | └── sidebar.js |
| 110 | +| └── theme |
| 111 | +| └── index.mjs |
| 112 | +├── components |
| 113 | +| ├── button-group.md |
| 114 | +| └── button.md |
| 115 | +├── guide |
| 116 | +| └── intro.md |
| 117 | +└── index.md |
| 118 | +``` |
| 119 | + |
| 120 | +在`example`的`package.json`中添加一条命令: |
| 121 | +```diff |
| 122 | ++ "serve:doc": "node website.js" |
| 123 | +``` |
| 124 | + |
| 125 | +然后在目录下执行以下命令即可构建文档。 |
| 126 | + |
| 127 | +```bash |
| 128 | +npm run serve:doc |
| 129 | +``` |
| 130 | + |
| 131 | +文档启动后,默认的访问路径为: |
| 132 | +```bash |
| 133 | +http://localhost:5173/mpx-ui/guide/intro.html |
| 134 | +``` |
| 135 | + |
| 136 | +### 模拟器 |
| 137 | + |
| 138 | +模拟器的默认路由为`http://localhost:8090`,可以通过更改`docs/.vitepress/config.mjs`中的`iframeConfig`来配置预览路径: |
| 139 | +```javascript |
| 140 | +iframeConfig: { |
| 141 | + path: { |
| 142 | + dev: 'http://localhost:8090', |
| 143 | + prod: '8090' |
| 144 | + } |
| 145 | +} |
| 146 | +``` |
| 147 | + |
| 148 | +**注意:这里的端口要和你的`example`项目`vue.config.js`中配置的端口号一致** |
| 149 | +```javascript |
| 150 | +devServer: { |
| 151 | + port: 8090 |
| 152 | +} |
| 153 | +``` |
| 154 | + |
| 155 | +检查无误后启动example项目即可完成文档中模拟器的展示。 |
| 156 | + |
| 157 | +### 父子路由同步 |
| 158 | + |
| 159 | +父子路由同步涉及到`iframe`通信,为此我们提供了以下方法来进行路由同步: |
| 160 | +```javascript |
| 161 | +declare module '@mpxjs/vuese-website/iframe-sync' { |
| 162 | +/** |
| 163 | + * 监听子路由的变化 |
| 164 | + * @param callback |
| 165 | + * @returns |
| 166 | + */ |
| 167 | +export const onPathchange: (callback?: (path: string) => void) => () => void; |
| 168 | +/** |
| 169 | + * 同步路由到子窗口 |
| 170 | + * @param to |
| 171 | + */ |
| 172 | +export const syncPathToChild: (to: string) => void; |
| 173 | +} |
| 174 | +``` |
| 175 | + |
| 176 | +你需要在项目的`app.ts`中,监听主窗口路由的变化: |
| 177 | +```typescript |
| 178 | +import mpx, { createApp } from '@mpxjs/core' |
| 179 | +import { onPathchange } from '@mpxjs/vuese-website/dist/iframe-sync' |
| 180 | + |
| 181 | +const isIframe = __mpx_mode__ === 'web' && window.parent !== window |
| 182 | +if (isIframe) { |
| 183 | + onPathchange((to) => { |
| 184 | + mpx.redirectTo({ |
| 185 | + // 这里需要适配你的项目路径 |
| 186 | + url: `/pages/${to && to !== 'intro' ? `${to}/` : ''}index` |
| 187 | + }) |
| 188 | + }) |
| 189 | +} |
| 190 | +``` |
| 191 | + |
| 192 | +同时如果`example`中涉及到路由变化,也需要同步给主窗口: |
| 193 | +```typescript |
| 194 | +const isIframe = __mpx_mode__ === 'web' && window.parent !== window |
| 195 | +if (isIframe) { |
| 196 | + syncPathToChild(componentName) |
| 197 | +} |
| 198 | +``` |
| 199 | + |
| 200 | +### markdown扩展语法 |
| 201 | +为了完成[文档示例一体化](https://www.mpxjs.cn/mpx-cube-ui/guide/intro.html#%E6%96%87%E6%A1%A3%E7%A4%BA%E4%BE%8B%E4%B8%80%E4%BD%93%E5%8C%96)的目标,我们制定了一些语法来提取模块儿源代码。 |
| 202 | + |
| 203 | +假设你的`example`目录有如下结构: |
| 204 | +```plaintext |
| 205 | +example/pages |
| 206 | +├── button |
| 207 | +| ├── README.md |
| 208 | +| ├── btn-icon.mpx |
| 209 | +| ├── btn-inline.mpx |
| 210 | +| ├── btn-light.mpx |
| 211 | +| ├── btn-loading.mpx |
| 212 | +| ├── btn-outline.mpx |
| 213 | +| ├── btn-primary.mpx |
| 214 | +| ├── btn-secondary.mpx |
| 215 | +| └── index.mpx |
| 216 | +├── button-group |
| 217 | +| ├── README.md |
| 218 | +| ├── button-group-one.mpx |
| 219 | +| ├── button-group-two.mpx |
| 220 | +| ├── button-group-vertical-two.mpx |
| 221 | +| ├── button-group-with-context.mpx |
| 222 | +| └── index.mpx |
| 223 | +├── index.mpx |
| 224 | +└── index.ts |
| 225 | +``` |
| 226 | + |
| 227 | +基本的替换语法如下: |
| 228 | + |
| 229 | +```markdown |
| 230 | +<!-- @example: ${README.md同级目录下的文件名称} --> |
| 231 | +``` |
| 232 | + |
| 233 | + |
| 234 | +#### 提取文件代码 |
| 235 | +用于提取目标文件的所有代码 |
| 236 | + |
| 237 | +```markdown |
| 238 | +<!-- @example: btn-primary --> |
| 239 | +``` |
| 240 | + |
| 241 | +#### 提取`template`代码 |
| 242 | +用于提取目标文件中的`template`代码 |
| 243 | + |
| 244 | +```markdown |
| 245 | +<!-- @example: btn-primary -> template --> |
| 246 | +``` |
| 247 | + |
| 248 | +#### 提取`script`代码 |
| 249 | +用于提取目标文件中的`script`代码 |
| 250 | + |
| 251 | +```markdown |
| 252 | +<!-- @example: btn-primary -> script --> |
| 253 | +``` |
| 254 | + |
| 255 | +#### 提取`style`代码 |
| 256 | +用于提取目标文件中的`style`代码 |
| 257 | + |
| 258 | +```markdown |
| 259 | +<!-- @example: btn-primary -> style --> |
| 260 | +``` |
| 261 | + |
| 262 | +#### 代码成组 |
| 263 | +用于把不同文件的代码提取到同一个代码块儿中 |
| 264 | + |
| 265 | +```markdown |
| 266 | +<!-- @group: group-name -> start --> |
| 267 | + |
| 268 | +<!-- @example: btn-primary -> template no-wrap --> |
| 269 | +<!-- @example: btn-secondary -> template no-wrap --> |
| 270 | +<!-- @example: btn-light -> template --> |
| 271 | + |
| 272 | +<!-- @group: group-name -> end --> |
| 273 | +``` |
| 274 | + |
| 275 | +其中`no-wrap`会去掉代码块的包裹,如: |
| 276 | + |
| 277 | +```markdown |
| 278 | +<!-- @example: btn-primary -> template no-wrap --> |
| 279 | +``` |
| 280 | + |
| 281 | +渲染结果如下: |
| 282 | + |
| 283 | +```html |
| 284 | +<cube-button primary>主要按钮</cube-button> |
| 285 | +``` |
| 286 | + |
| 287 | +而去掉`no-wrap`后,则会渲染如下代码: |
| 288 | + |
| 289 | +```html |
| 290 | +<template> |
| 291 | + <cube-button primary>主要按钮</cube-button> |
| 292 | +</template> |
| 293 | +``` |
| 294 | + |
| 295 | +这样能够更好的将多个模块儿的代码组织到一起。 |
| 296 | + |
| 297 | +</card> |
0 commit comments