---
name: init-vitepress
description: Standardize VitePress setup or sync, with Mermaid diagram rendering enabled by default
---

# init-vitepress Standard Skill

## When To Use

Use this skill first when:

- the user wants to bootstrap a VitePress documentation site from scratch
- an existing project needs a `docs/` site created or repaired
- Mermaid diagrams should render by default from Markdown code fences
- a legacy project needs compatibility handling for modern doc tooling

## Core Objectives

1. Create or repair a working VitePress documentation site
2. Enable Mermaid support by default so `flowchart`, `sequenceDiagram`, and similar diagrams render in both dev and build output
3. Reuse the project's existing package manager, structure, repository metadata, and branding where possible
4. Delegate Node.js compatibility handling to `node-env-guide` when the runtime is below Node 18, with support down to Node `16.14.2`
5. Deliver a setup that can complete at least one `docs:build`

## Compatibility Decision Matrix

Before making changes, always select a track based on the runtime. Do not default every repository to the newest VitePress major line.

### Track A: Modern Mainline

- Use when the documentation runtime is `>= 18`
- Recommended version: `vitepress@^1`
- Recommended config: `docs/.vitepress/config.mjs` or another ESM config file already proven in the repo
- Best for teams already standardized on Node 18+ or a dedicated docs runtime

### Track B: Node 16 Compatible Line

- Use when the project baseline is Node `16.14.2` to `<18`
- Recommended version: pin `vitepress@0.22.4`
- Reason: this release declares `engines.node >=14.0.0`, making it a safer compatibility line for legacy projects
- Recommended config: prefer `docs/.vitepress/config.mjs` or `config.js`
- Forbidden approach: do not install the newest VitePress line and force it through with `--ignore-engines`

### Track C: Split Runtime Isolation

- Use when the application must stay on Node 16 but docs may run on a dedicated Node 18+ runtime
- Recommended approach: wrap docs commands in an explicit launcher script that loads `nvm.sh`, Volta, or another detected version manager
- Important: do not put raw `nvm exec 18 vitepress ...` directly in `package.json`
- Reason: `npm run` and `yarn run` usually execute through `/bin/sh`, which often does not load `nvm`

## Execution Standards

### 1. Environment Preflight

- Check whether `package.json` exists at the project root
- Run `node -v`
- If the version is below `16.14.2`, stop automatic setup and tell the user to upgrade Node first
- If the version is between `16.14.2` and `<18`, switch to either the Node 16 compatible line or split runtime isolation
- If the version is below 18, invoke `node-env-guide`
- Detect the package manager from lockfiles such as `package-lock.json`, `pnpm-lock.yaml`, or `yarn.lock`
- Run `git remote get-url origin` to populate edit links and repository links

### 2. Directory and Config Initialization

- Ensure `docs/`, `docs/.vitepress/`, and a site entry page exist
- Prefer creating or updating `docs/.vitepress/config.mjs`
- Only use `config.ts` when the repository already has a proven ESM TypeScript config pipeline
- In Node 16 or legacy CJS environments, avoid generating `config.ts` by default because it is often executed as CJS by surrounding tooling
- If the repository already has VitePress config, merge incrementally instead of overwriting nav, sidebar, or theme settings
- Preserve existing documentation architecture whenever possible

### 3. Mermaid Enabled By Default

This is mandatory:

- ` ```mermaid ` fenced code blocks must render as diagrams rather than raw code
- Reuse the team's existing Mermaid approach if one already exists
- If the project has no existing setup, add a Mermaid renderer compatible with VitePress and place any required client initialization under `docs/.vitepress/theme/`
- Provide at least one Mermaid example page or homepage example for validation
- Mermaid support must work in both local preview and production build output

Suggested verification sample:

````md
```mermaid
flowchart TD
  A[Input] --> B[Initialize VitePress]
  B --> C[Enable Mermaid]
  C --> D[Sync Docs]
  D --> E[Run docs:build]
```
````

### 4. Content Sync and Compatibility

- Scan existing Markdown content and sync it into `docs/`
- Escape `<...>`-style placeholders where Vue template parsing could break the build
- If the project already has README, changelog, or design docs, add entry links or navigation where appropriate

### 5. Script Injection

Fill in the standard doc scripts according to the project context:

```json
{
  "scripts": {
    "docs:dev": "vitepress dev docs",
    "docs:build": "vitepress build docs",
    "docs:preview": "vitepress preview docs"
  }
}
```

If the repository follows the Node 16 compatible line:

- Pin a compatible version such as `vitepress@0.22.4`
- Keep scripts as direct `vitepress ...` calls

If the repository follows split runtime isolation:

- Do not write raw `nvm exec 18 vitepress ...` into `package.json`
- Create an explicit launcher script such as `scripts/run-vitepress.sh`
- The launcher must source `nvm.sh` itself before calling `nvm exec 18 npx vitepress ...`
- If Volta / fnm / asdf is the team standard, adapt the launcher accordingly

Suggested launcher:

```bash
#!/usr/bin/env bash
set -e

if [ -s "${NVM_DIR:-$HOME/.nvm}/nvm.sh" ]; then
  . "${NVM_DIR:-$HOME/.nvm}/nvm.sh"
  exec nvm exec 18 npx vitepress "$@"
fi

exec npx vitepress "$@"
```

Suggested `package.json`:

```json
{
  "scripts": {
    "docs:dev": "bash ./scripts/run-vitepress.sh dev docs",
    "docs:build": "bash ./scripts/run-vitepress.sh build docs",
    "docs:preview": "bash ./scripts/run-vitepress.sh preview docs"
  }
}
```

### 6. Verification and Feedback

- Prefer running `docs:build`
- If the build fails, explain clearly whether the issue is caused by Node.js version, dependency selection, Mermaid setup, config file format, or Markdown content
- For Node 16 repositories, also check:
  - whether a Node 18-only VitePress / Vite line was installed by mistake
  - whether `nvm exec` was written directly into `package.json`
  - whether `config.ts` was generated in a legacy environment
- The summary should state:
  - the site entry path
  - whether Mermaid was enabled
  - which compatibility track was selected
  - the next commands the user should run
  - any compatibility risks that still need follow-up

## Final Delivery Checklist

- `docs/` is recognized as a VitePress site
- `package.json` contains the documentation scripts
- Mermaid diagrams render by default
- Existing docs were preserved rather than overwritten blindly
- there is at least one viable path for Node 16.14.2 projects
- The build command was verified once, or the reason it was not verified is stated explicitly
