Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

vitepress-theme-demoblock

xinlei3166730MIT3.1.3TypeScript support: included

vitepress-theme-demoblock

vitepress, vue, theme, demoblock

readme

vitepress-theme-demoblock

这是 3.x 版本的文档,已经采用 TypeScriptESM 规范重写,2.x 版本文档请看v2文档,1.x 版本文档请看v1文档

简介

vitepress-theme-demoblock 是一个基于 Vitepress 的主题插件,它可以帮助你在编写文档的时候增加 Vue 示例,它的诞生初衷是为了降低编写组件文档时增加一些相关示例的难度。

使用 Vitepress 编写组件示例有以下不足之处:

  • 1.组件示例和示例代码本质上一样,却需要写两遍。
  • 2.Vitepress 无法渲染 Markdown 中的代码块。

查看Demo

提示

由于 Vitepress 版本更新频繁,目前支持版本为 1.0.1

Vue 支持版本为 3.4.21

安装

npm install -D vitepress-theme-demoblock
yarn add -D vitepress-theme-demoblock
pnpm add -D vitepress-theme-demoblock

快速上手

.vitepress/config.js 文件中使用 demoblockPlugindemoblockVitePlugin 插件

import { demoblockPlugin, demoblockVitePlugin } from 'vitepress-theme-demoblock'

markdown: {
  config: (md) => {
    md.use(demoblockPlugin)
  }
},
vite: {
  plugins: [demoblockVitePlugin()]
}

.vitepress/theme/index.js 中使用 vitepress-theme-demoblock 主题,并注册组件(包含主题中默认的组件)。

import DefaultTheme from 'vitepress/theme'
import 'vitepress-theme-demoblock/dist/theme/styles/index.css'
import { useComponents } from './useComponents'

export default {
  ...DefaultTheme,
  enhanceApp(ctx) {
    DefaultTheme.enhanceApp(ctx)
    useComponents(ctx.app)
  }
}

package.json 配置命令 scriptsvitepress-rc 用来注册组件(--docsDir 指定docs目录,--componentsDir 指定组件注册目录), 如果 .vitepress 目录和 package.json 同级,--docsDir 设置为 .

"scripts": {
  "docs:dev": "yarn run register:components && vitepress dev docs",
  "docs:build": "yarn run register:components && vitepress build docs",
  "docs:serve": "vitepress serve docs",
  "register:components": "vitepress-rc"
}

更多用法

.md 文件中 使用 stylescript,参考下面例子:

# code snippet ...

<style>
body { color: red; }
</style>

<script setup>
console.log('vitepress-theme-demoblock setup')
</script>

<script>
console.log('vitepress-theme-demoblock')
</script>

从 v2 迁移

v3 在使用插件时需要使用一个 Vite 插件。

vite: {
  plugins: [demoblockVitePlugin()]
}

v3 不支持 :::demo 后面的描述。

v2 :::demo 使用 `type``plain``round``circle` 属性来定义 Button 的样式。
v3 :::demo

因使用了 Vite 插件,Vue 组件经过 @vitejs/plugin-vue-jsx 插件编译, 很多用法已经支持, 例如:setup、jsx、tsx、css v-bind 等等。插件之前的一些属性和方法都已删除,目前只保留了 customClass 属性。

多语言

.vitepress/config.js 文件中增加 demoblock 字段来支持多语言 (默认中文)

themeConfig: {
  // demoblock locales
  demoblock: {
    'root': {
      'view-source': 'View source',
        'hide-source': 'Hide source',
        'edit-in-editor': 'Edit in Playground',
        'edit-on-github': 'Edit on GitHub',
        'copy-code': 'Copy code',
        'copy-success': 'Copy success',
        'copy-error': 'Copy error',
    },
    'zh': {
      'view-source': '查看源代码',
        'hide-source': '隐藏源代码',
        'edit-in-editor': '在 Playground 中编辑',
        'edit-on-github': '在 Github 中编辑',
        'copy-code': '复制代码',
        'copy-success': '复制成功',
        'copy-error': '复制失败'
    }
  }
}

自定义主题

通过配置 customClass 类名称,自定义 demoblock 样式

markdown: {
  config: (md) => {
    md.use(demoblockPlugin, {
      customClass: 'demoblock-custom'
    })
  }
}

通过重写 css-variables,自定义 demoblock 样式

:root {
  --demoblock-border: var(--vp-c-divider);
  --demoblock-control: #909399;
  --demoblock-control-bg: var(--vp-c-bg);
}

html.dark {
  --demoblock-control: #A3A6AD;
}

配置主题色

:root {
  --vp-c-brand-1: hsl(237, 100%, 70%);
  --vp-c-brand-2: hsl(237, 100%, 73%);
  --vp-c-brand-3: hsl(237, 100%, 70%);
  --vp-c-brand-soft: hsl(237, 100%, 70%, 14%);
}

使用第三方组件库

这个插件主要是针对自己的组件库来使用的,第三方的组件库直接导入使用即可(例如 element-plus )。

.vitepress/theme/index.js 文件中加入以下代码:

import DefaultTheme from 'vitepress/theme'
import 'element-plus/dist/index.css'
// import ElementPlus from 'element-plus'
// import cn from 'element-plus/lib/locale/lang/zh-cn'

export default {
  ...DefaultTheme,
  enhanceApp(ctx) {
    DefaultTheme.enhanceApp(ctx)
    // ctx.app.use(ElementPlus, { locale: cn })
  }
}

使用的时候,导入 element-plus 组件即可:

<template>
  <div class="card-wrap">
    <div class="card">{{ title }}</div>
    <el-button type="primary" @click="onClick">点击</el-button>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { ElMessage, ElButton } from 'element-plus'

const title = ref('vitepress-theme-demoblock')

const onClick = () => {
  ElMessage('消息')
}
</script>

也可以安装 unplugin-auto-importunplugin-vue-components ,配合 Vite 实现自动导入。

感谢

参考:element-ui, element-plus, vite-plugin-markdown-preview, nova-next

changelog

3.1.3 (2025-04-14)

3.1.2 (2025-04-02)

Bug Fixes

  • fix the problem that emojis cannot be displayed properly in VitePress. (ad415fa)

3.1.1 (2025-03-25)

Features

3.0.7 (2024-04-02)

Features

  • demoblock-view add vp-view (5090247)

3.0.6 (2024-04-01)

3.0.5 (2024-03-27)

Bug Fixes

3.0.4 (2024-03-26)

3.0.3 (2023-07-16)

3.0.2 (2023-07-13)

Features

  • add support for Markdown File Inclusion (004965f)

3.0.1 (2023-06-26)

3.0.0 (2023-06-26)

Features

2.0.2 (2022-12-22)

Features

2.0.1 (2022-12-07)

Bug Fixes

  • 临时解决setup 语法报错,锁定 vue 版本为 3.2.44。 (0e0b6e3)

2.0.0 (2022-11-11)

2.0.0-beta.3 (2022-11-11)

Features

2.0.0-beta.2 (2022-11-10)

2.0.0-beta.1 (2022-11-10)

Features

1.4.2 (2021-11-26)

Bug Fixes

  • folded width of the demoblock (83774d1)

1.4.1 (2021-11-10)

Features

1.4.0 (2021-11-10)

Features

  • 复制代码后 toast 展示成功 (d05b64e)

1.3.2 (2021-10-28)

Bug Fixes

  • controlText does not disappear when a block of code is retracted on the mobile client (3cb1040)
  • 删除字体图标替换成css生成三角形 (92d7cb7)

1.3.1 (2021-10-28)

Bug Fixes

  • scriptImports为空时报错 (99235b8)

1.3.0 (2021-10-28)

Features

  • support custom scriptImports and custom scriptReplaces, see readme.md for details (8434eba)

1.2.7 (2021-10-25)

1.2.6 (2021-10-21)

Bug Fixes

  • Demo: 已经有一个展开操作区固定在下方的情况下,再展开一个会出现重叠的现象 (dd9f061), closes #10

1.2.5 (2021-10-21)

Bug Fixes

1.2.4 (2021-10-21)

Bug Fixes

  • Demo: 已经有一个展开操作区固定在下方的情况下,再展开一个会出现重叠的现象 (5dd9608), closes #10

1.2.3 (2021-10-21)

Bug Fixes

  • Demo: demo-block-control样式问题;代码展开/收起的操作区未遮挡语言的问题 (a6f0e55), closes #8 #9

1.2.2 (2021-10-14)

Features

  • remove lodash-es, Optimize package volume, reduce first load time (c7956d9)

1.2.1 (2021-10-12)

Features

1.2.0 (2021-10-12)

Bug Fixes

  • Demo: 修复窗口缩放时,底部 demo-block-control 的宽度和位置不准确问题 (d039003)

Features

  • demo block control style (3106b13)
  • support css preprocessors (c8a335e)

1.1.1 (2021-08-19)

Features

  • code demo copy success text, update README.md (9359f92)
  • 新增了复制成功文案 (e3ff5b7)
  • 新增了复制成功文案以及为demo展示增加溢出隐藏 (2b23d2b)

1.1.0 (2021-08-19)

Features

1.0.10 (2021-08-18)

Bug Fixes

  • when demoblock i18n is empty, the component reports an error (db90f4e)
  • when demoblock i18n is empty, the component reports an error (f84a2e1)

1.0.9 (2021-08-18)

Features

  • support vue script setup syntax (43083a2)

1.0.7 (2021-07-19)

1.0.5 (2021-07-01)

Features

1.0.4 (2021-06-29)

Features

  • rename demo-block to demoblock (7f674b5)

1.0.3 (2021-06-29)

Features

1.0.2 (2021-06-29)

Features

1.0.1 (2021-06-29)

Features

1.0.0 (2021-06-29)

Features