title: 项目结构

项目结构 {#directory-structure}

Slidev 对项目结构进行了一些约定,以尽量减少配置项,使功能扩展更加灵活直观。

基本结构如下所示:

  1. your-slidev/
  2. ├── components/ # 自定义组件
  3. ├── layouts/ # 自定义布局
  4. ├── public/ # 静态资源
  5. ├── setup/ # 自定义 setup / hooks
  6. ├── styles/ # 自定义样式
  7. ├── index.html # 注入的 index.html
  8. ├── slides.md # 幻灯片主入口
  9. └── vite.config.ts # 扩展 vite 配置

以上所有均为可选。

组件 {#components}

约定:./components/*.{vue,js,ts,jsx,tsx,md}

此目录中的组件可以在幻灯片的 Markdown 中直接使用,其组件名与文件名相同。

示例:

  1. your-slidev/
  2. ├── ...
  3. └── components/
  4. ├── MyComponent.vue
  5. └── HelloWorld.ts
  1. <!-- slides.md -->
  2. # My Slide
  3. <MyComponent :count="4"/>
  4. <!-- both namings work -->
  5. <hello-world foo="bar">
  6. Slot
  7. </hello-world>

此特性得益于 vite-plugin-components

Slidev 还提供了一些 内置组件 供你选择。

布局 {#layouts}

约定:./layouts/*.{vue,js,ts,jsx,tsx}

  1. your-slidev/
  2. ├── ...
  3. └── layouts/
  4. ├── cover.vue
  5. └── my-cool-theme.vue

你可以为布局文件使用任何文件名。然后只需在你的 YAML 头部使用文件名引用你的布局。

  1. ---
  2. layout: my-cool-theme
  3. ---

如果你提供的布局与内置布局或主题布局重名的话,你的自定义布局将优先于内置/主题布局。优先级为 本地 > 主题 > 内置

在布局组件中,你可以使用 <slot/> 展示幻灯片内容。比如:

  1. <!-- default.vue -->
  2. <template>
  3. <div class="slidev-layout default">
  4. <slot />
  5. </div>
  6. </template>

静态资源 {#public}

约定:./public/*

开发过程中,此目录中的资源文件将在 / 下提供,并会按原样复制到 dist 目录的根目录中。欲了解更多,请参阅 Vite 的 public 目录

样式 {#style}

约定:./style.css | ./styles/index.{css,js,ts}

遵循上述约定的文件将被注入到 App 的根目录中。如果你需要引入多个 css 入口,你可以按如下方式创建结构并自行管理引入顺序。

  1. your-slidev/
  2. ├── ...
  3. └── styles/
  4. ├── index.ts
  5. ├── base.css
  6. ├── code.css
  7. └── layouts.css
  1. // styles/index.ts
  2. import './base.css'
  3. import './code.css'
  4. import './layouts.css'

样式得益于 Windi CSSPostCSS,因此,你拥有开箱即用的 css 嵌套和 at-directives。示例:

  1. .slidev-layout {
  2. @apply px-14 py-10 text-[1.1rem];
  3. h1, h2, h3, h4, p, div {
  4. @apply select-none;
  5. }
  6. pre, code {
  7. @apply select-text;
  8. }
  9. a {
  10. color: theme('colors.primary');
  11. }
  12. }

了解更多关于此语法的信息

index.html {#index-html}

约定:index.html

index.html 提供了向主 index.html 中注入 meta 标签以及 scripts 标签的能力。

例如,对于以下自定义 index.html 来说:

  1. <!-- ./index.html -->
  2. <head>
  3. <link rel="preconnect" href="https://fonts.gstatic.com">
  4. <link href="https://fonts.googleapis.com/css2?family=Fira+Code:wght@400;600&family=Nunito+Sans:wght@200;400;600&display=swap" rel="stylesheet">
  5. </head>
  6. <body>
  7. <script src="./your-scripts"></script>
  8. </body>

最终部署的 index.html 效果如下:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <link rel="icon" type="image/png" href="https://cdn.jsdelivr.net/gh/slidevjs/slidev/assets/favicon.png">
  7. <!-- injected head -->
  8. <link rel="preconnect" href="https://fonts.gstatic.com">
  9. <link href="https://fonts.googleapis.com/css2?family=Fira+Code:wght@400;600&family=Nunito+Sans:wght@200;400;600&display=swap" rel="stylesheet">
  10. </head>
  11. <body>
  12. <div id="app"></div>
  13. <script type="module" src="__ENTRY__"></script>
  14. <!-- injected body -->
  15. <script src="./your-scripts"></script>
  16. </body>
  17. </html>

全局图层 {#global-layers}

约定:global-top.vue | global-bottom.vue

了解更多:全局图层