VitePress Documentation

website·Indexed 40 days ago

https://vitepress.dev/

A static site generator designed for building fast, content-centric sites. Documentation covers getting started, site configuration, routing, internationalization, and the default theme. Includes guides on using Vue in Markdown, build-time data loading, SSR compatibility, and deploying to platforms like Netlify, Vercel, and GitHub Pages.

Tokens
28.4K
Snippets
201
Records
226
Agent score
71%

What's inside VitePress

  1. Overview of VitePress

    VitePress is a Static Site Generator (SSG) designed for building fast, content-centric websites. It converts Markdown source content into static HTML pages by applying a theme. It is powered by Vite and Vue, making it particularly suitable for technical documentation, blogs, portfolios, and marketing sites.
  2. Understand VitePress file-based routing

    VitePress uses file-based routing where the directory structure of source Markdown files maps directly to the generated HTML pages. For example, index.md becomes /index.html (accessible as /), and guide/getting-started.md becomes /guide/getting-started.html.
  3. Use Vue features in VitePress Markdown

    VitePress compiles each Markdown file into HTML and then processes it as a Vue Single-File Component (SFC). This allows you to use dynamic templating, Vue components, and custom logic directly within your Markdown files. Static content is automatically optimized and skipped during client-side hydration to reduce the JavaScript payload. All Vue usage must be SSR-compatible.
  4. Include external Markdown files or sections

    Include the content of another Markdown file using the <!--@@include: path--> directive. This supports nested inclusions and relative paths. You can prefix paths with @ to reference the source root (usually the project root).

    Selection Methods:

    • Line Ranges: Use {start,end} (e.g., {3,} for line 3 onwards, {,10} for up to line 10, or {1,10}).
    • VS Code Regions: Use #region-name to include content between <!-- #region name --> and <!-- #endregion name --> tags.
    • Header Anchors: Use #anchor-id to include a specific section. The ID is the generated heading ID or a custom one defined via {#custom-id}.

    Configuration:

    • markdown.include.silent: true: Logs a warning instead of throwing a build error if a file or region is missing.
    • markdown.include.rebaseRelativeUrls: false: Makes relative links/images in the included file resolve relative to the including page instead of the included file's location.
    # Docs
    
    <!-- Include relative file -->
    <!--@@include: ./parts/basics.md-->
    
    <!-- Include specific line range -->
    <!--@@include: ./parts/basics.md{3,10}-->
    
    <!-- Include VS Code region with line range -->
    <!--@@include: ./parts/basics.md#basic-usage{,2}-->
    
    <!-- Include by heading anchor -->
    <!--@@include: ./parts/basics.md#my-base-section-->
  5. Isolate styles using the raw container

    The ::: raw container wraps content in a <div class="vp-raw"> to prevent style and router conflicts, which is useful for documenting component libraries. Style isolation is opt-in via a PostCSS plugin.
    npm add -D postcss
    // docs/postcss.config.mjs
    import { postcssIsolateStyles } from 'vitepress'
    
    export default {
      plugins: [
        postcssIsolateStyles({
          includeFiles: [/custom\.css/] // defaults to [/vp-doc\.css/, /base\.css/]
        })
      ]
    }
  6. Exclude pages from local search

    To prevent a page from appearing in the local search results, add search: false to the page's frontmatter. If using a custom _render function, this must be handled manually within that function.
  7. Create dynamic routes with a paths loader

    Dynamic routes allow generating multiple pages from a single Markdown template using route parameters (e.g., [pkg].md). Because VitePress is a static site generator, every dynamic route file must have a corresponding paths loader file (e.g., [pkg].paths.js or [pkg].paths.ts).

    The loader must export a default object with a paths method that returns an array of objects containing a params property.

    // packages/[pkg].paths.js
    export default {
      paths() {
        return [
          { params: { pkg: 'foo' } },
          { params: { pkg: 'bar' } }
        ]
      }
    }
  8. Update static image syntax when migrating from VuePress

    VitePress automatically handles the base configuration for static images, eliminating the need for the :src="$withBase(...)" syntax used in VuePress. You can now use standard Markdown image syntax for static assets. However, for dynamic images, you must still use the withBase helper.
    - <img :src="$withBase('/foo.png')" alt="foo">
    + ![foo](/foo.png)
  9. Migrate VitePress 0.x Frontmatter Configuration

    Update the following frontmatter settings when migrating from VitePress 0.x:

    • Home Page Layout: Change home: true to layout: home. Additional homepage settings are now available in the Home Page guide.
    • Footer: The footer option has been moved from individual page frontmatter to the global themeConfig.footer configuration.
  10. Add attributes to custom containers

    Custom containers support additional attributes using curly braces. For example, {open} makes a details block open by default, and {no-title} removes the title element (except for details blocks).
    ::: details Click me to toggle the code {open}
    ```js
    console.log('Hello, VitePress!')

    :::

    ::: tip {no-title} Just want to try it out? Skip to the Quickstart. :::

  11. Set up basic internationalization (i18n) in VitePress

    To support multiple languages, organize your documentation into locale-specific directories and configure the locales object in your VitePress config. The root locale represents the default language, while other keys (e.g., fr) correspond to the directory names for those languages.
    // docs/.vitepress/config.ts
    import { defineConfig } from 'vitepress'
    
    export default defineConfig({
      locales: {
        root: {
          label: 'English',
          lang: 'en'
        },
        fr: {
          label: 'French',
          lang: 'fr', // Added as `lang` attribute on <html> tag
          link: '/fr/guide' // Link for the navbar translations menu
        }
      }
    })

    // Recommended Directory Structure: // docs/ // ├─ es/ // │ └─ foo.md // ├─ fr/ // │ └─ foo.md // └─ foo.md

  12. Scaffold a VitePress project using the Setup Wizard

    VitePress provides a CLI setup wizard to help scaffold a basic project. You can run this in the current directory for a standalone site or in a nested directory (e.g., ./docs) if integrating into an existing project.
    # npm
    npx vitepress init
    
    # pnpm
    pnpm vitepress init
    
    # yarn
    yarn vitepress init
    
    # bun
    bun vitepress init