当前位置: 首页 > news >正文

VuePress 使用并应用 mcommon 模板

VuePress 可以实现用 Markdown 编程,快速便捷开发网站

通过命令行创建项目模板 

npm init vuepress vuepress-starter

你想要创建什么类型的项目? 这个配置选择 docs

启动项目

npm run docs:dev

网站启动了,编辑 README.md 文件可以实时更新页面

这个模板是官方提供的,现在要设计自己的模板。首先需要安装一个自动注册组件的包

npm install unplugin-vue-components -D

添加配置 docs\.vuepress\config.js

import Components from 'unplugin-vue-components/vite'bundler: viteBundler({viteOptions: {plugins: [Components({dirs: ['components'],allowOverrides: true,directoryAsNamespace: true, // 组件命令带路径dts: 'components.d.ts',})]},}),

 docs 目录下添加 components 目录,添加 vd-tag.vue 文件

<template><div class="auto">自动注册组件测试</div>
</template>

 启动项目

npm run docs:dev

生成 components.d.ts

/* eslint-disable */
// @ts-nocheck
// Generated by unplugin-vue-components
// Read more: https://github.com/vuejs/core/pull/3399
// biome-ignore lint: disable
export {}/* prettier-ignore */
declare module 'vue' {export interface GlobalComponents {VdTag: typeof import('./components/vd-tag.vue')['default']}
}

 首页引入自动生成的全局组件 README.md 末尾追加

<VdTag />

那么怎么引入代码块呢

以前版本写法

 <<< ./docs/.vuepress/config.js

 最新版本写法

@[code](./.vuepress/config.js)

但是如果做为组件插槽的内容使用时 @[code](./.vuepress/config.js) 必须上方和下方有一空行才会生效。

<DemoBlock>@[code](./.vuepress/config.js)</DemoBlock>

 DemoBlock 组件从 mcommon 上借鉴的模板

<template><div class="demo-block":class="['demo', { 'hover': hovering }]"@mouseenter="hovering = true"@mouseleave="hovering = false"><div class="source"><slot name="source"></slot></div><div class="meta"ref="meta"><div class="description"v-if="$slots.default"><slot></slot></div><div class="highlight"><slot name="highlight"></slot></div></div><div class="demo-block-control"ref="control":class="{ 'is-fixed': fixedControl }"@click="isExpanded = !isExpanded"><transition name="arrow-slide"><i :class="[iconClass, { 'hovering': hovering }]"></i></transition><transition name="text-slide"><span v-show="hovering">{{ controlText }}</span></transition></div></div>
</template><style lang="scss">
.demo-block {border: solid 1px #ebebeb;border-radius: 3px;transition: 0.2s;&.hover {box-shadow: 0 0 8px 0 rgba(232, 237, 250, 0.6),0 2px 4px 0 rgba(232, 237, 250, 0.5);}.demo-button {float: right;}.source {padding: 24px;}.meta {background-color: #fafafa;border-top: solid 1px #eaeefb;overflow: hidden;height: 0;transition: height 0.2s;}.description {padding: 20px;box-sizing: border-box;border: solid 1px #ebebeb;border-radius: 3px;color: #666;word-break: break-word;margin: 10px;background-color: #fff;p {margin: 0;line-height: 26px;}}.highlight {pre {margin: 0;}code.hljs {margin: 0;border: none;max-height: none;border-radius: 0;&::before {content: none;}}}.demo-block-control {border-top: solid 1px #eaeefb;height: 44px;box-sizing: border-box;background-color: #fff;border-bottom-left-radius: 4px;border-bottom-right-radius: 4px;text-align: center;margin-top: -1px;color: #d3dce6;cursor: pointer;position: relative;&.is-fixed {position: fixed;bottom: 0;width: 718px;z-index: 1;}i {font-size: 16px;line-height: 44px;transition: 0.3s;&.hovering {transform: translateX(-40px);}}> span {position: absolute;transform: translateX(-30px);font-size: 14px;line-height: 44px;transition: 0.3s;display: inline-block;}&:hover {color: #409eff;background-color: #f9fafc;}& .text-slide-enter,& .text-slide-leave-active {opacity: 0;transform: translateX(10px);}.control-button {line-height: 26px;position: absolute;top: 0;right: 0;font-size: 14px;padding-left: 5px;padding-right: 25px;}}table {margin: 0;display: table;}th,td,tr {border: 0;}
}
.el-popper {table {margin: 0;display: table;}th,td,tr {border: 0;}tr:nth-child(2n) {background: none;}
}
</style><script type="text/babel">
export default {name: 'DemoBlock',data () {return {codepen: {script: '',html: '',style: ''},hovering: false,isExpanded: false,fixedControl: false,scrollParent: null}},methods: {scrollHandler () {const { top, bottom, left } = this.$refs.meta.getBoundingClientRect()this.fixedControl = bottom > document.documentElement.clientHeight &&top + 44 <= document.documentElement.clientHeight},removeScrollHandler () {this.scrollParent && this.scrollParent.removeEventListener('scroll', this.scrollHandler)}},computed: {iconClass () {return this.isExpanded ? 'el-icon-caret-top' : 'el-icon-caret-bottom'},controlText () {return this.isExpanded ? '隐藏代码':'显示代码'},codeArea () {return this.$el.getElementsByClassName('meta')[0]},codeAreaHeight () {if (this.$el.getElementsByClassName('description').length > 0) {return this.$el.getElementsByClassName('description')[0].clientHeight +this.$el.getElementsByClassName('highlight')[0].clientHeight + 20}return this.$el.getElementsByClassName('highlight')[0].clientHeight}},watch: {isExpanded (val) {this.codeArea.style.height = val ? `${this.codeAreaHeight + 1}px` : '0'if (!val) {this.fixedControl = falsethis.removeScrollHandler()return}setTimeout(() => {this.scrollParent = windowthis.scrollParent && this.scrollParent.addEventListener('scroll', this.scrollHandler)this.scrollHandler()}, 200)}},created () {const highlight = this.$slots.highlightif (highlight && highlight[0]) {let code = ''let cur = highlight[0]if (cur.tag === 'pre' && (cur.children && cur.children[0])) {cur = cur.children[0]if (cur.tag === 'code') {code = cur.children[0].text}}}},mounted () {this.$nextTick(() => {let highlight = this.$el.getElementsByClassName('highlight')[0]if (this.$el.getElementsByClassName('description').length === 0) {highlight.style.width = '100%'highlight.borderRight = 'none'}})},beforeDestroy () {this.removeScrollHandler()}
}
</script>

demo.vue

<template><ul class="demo-select"><li class="li"><label>基本用法</label><select v-model="value" ></select></li><li class="li"><label>异步获取数据</label><select v-model="value"></select></li></ul>
</template>
<script>
export default {data() {return {value: null};},methods: {}
};
</script>
<style lang="scss">
</style>

README.md

<DemoBlock>
<template #source><Demo />
</template>@[code](./.vuepress/config.js)</DemoBlock>

 

vuepress 模板推荐

瀑布流容器 | Plume 主题

https://theme-hope.vuejs.press/zh/demo/portfolio-home.html

友情链接 | Plume 主题

http://www.lqws.cn/news/599239.html

相关文章:

  • 3D一览通:在线查看3D模型,让协同更简单
  • GPT-1论文阅读:Improving Language Understanding by Generative Pre-Training
  • opencv入门(4)图像创建和赋值
  • 动手学深度学习13.5. 多尺度目标检测-笔记练习(PyTorch)
  • IDE全家桶专用快捷键----------个人独家分享!!
  • MCP 协议使用核心讲解
  • 数据结构day4——栈
  • 板凳-------Mysql cookbook学习 (十一--------1)
  • 杭州来未来科技 Java 实习面经
  • grom使用mysql快速上手
  • SeaTunnel 社区 2 项目中选“开源之夏 2025”,探索高阶数据集成能力!
  • PHP爬虫实战指南:获取淘宝商品详情
  • 【仿muduo库实现并发服务器】eventloop模块
  • 『深度编码』C++中的参数传递
  • 02.SpringBoot常用Utils工具类详解
  • 【学习方法】框架质疑学习法:破解专业学习的“知识厚度”困境
  • 华为云开始了“开发者空间 AI Agent 开发”活动
  • es的读和写-Reading and writing documents
  • Windows 疑难杂症集 - MsMpEng.exe 磁盘占用率持续高占
  • 发布/订阅模式:解耦系统的强大设计模式
  • 第七讲~~测试工具(禅道项目管理系统)
  • 软件测试期末复习之白盒测试
  • FPGA FMC 接口
  • Electron 进程间通信(IPC)深度优化指南
  • SpringBoot计时一次请求耗时
  • 数据库编程-ORM
  • 基于Pandas和FineBI的昆明职位数据分析与可视化实现(四)- 职位数据可视化(FineBI)
  • Java-String类静态成员方法深度解析
  • HDMI 2.1 FRL协议的流控机制:切片传输(Slicing)和GAP插入
  • 开关电源和线性电源Multisim电路仿真实验汇总——硬件工程师笔记