Contents
  1. 1. Initialize VitePress Project
    1. 1.1. Add vitepress dependency
    2. 1.2. Scaffold a basic project
  2. 2. Running the project
  3. 3. Git Configurations
    1. 3.1. Initialize the git repository
    2. 3.2. Config gitignore
  4. 4. VitePress Configurations
    1. 4.1. Site Config
      1. 4.1.1. srcDir
      2. 4.1.2. srcExclude
      3. 4.1.3. lastUpdated
      4. 4.1.4. Head
    2. 4.2. Theme Config
      1. 4.2.1. Logo
      2. 4.2.2. Local Search
      3. 4.2.3. Outline
    3. 4.3. Home Page Config
      1. 4.3.1. Image
      2. 4.3.2. Feature Icons
    4. 4.4. Other Config
      1. 4.4.1. Favicon
  5. 5. Deployment
    1. 5.1. Create a repository on GitHub
    2. 5.2. Commit your local repository to GitHub
    3. 5.3. Deploy your project on Vercel

This post will cover how to create a static website using VitePress.

Before using VitePress you must install Node.js v18 or higher.

Initialize VitePress Project

Add vitepress dependency

1
2
3
4
5
6
7
$ mkdir my-site
$ cd my-site

# add vitepress to devDependencies
$ npm add -D vitepress
# or
$ yarn add -D vitepress

VitePress is used in the development process as a build tool. It converts your Markdown files to HTML files. You don’t need VitePress in runtime.

Scaffold a basic project

1
$ npx vitepress init

npx can run your installed package directly, you don’t need to add any npm script to your package.json. You also can do npx package_command by using npm run your_script.

You need to set some basic configuration for your website.

  • Setting the VitePress directory. ./ means using the root directory.
  • Setting your site title.
  • Setting your site description.
  • Other configurations just use the default settings.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
┌  Welcome to VitePress!

◇ Where should VitePress initialize the config?
│ ./

◇ Site title:
│ My Awesome Project

◇ Site description:
│ A VitePress Site

◇ Theme:
│ Default Theme

◇ Use TypeScript for config and theme files?
│ Yes

◇ Add VitePress npm scripts to package.json?
│ Yes

└ Done! Now run npm run docs:dev and start writing.

Running the project

Start a local server

1
2
3
4
5
npm run docs:dev
# or
yarn docs:dev
# or
npx vitepress dev

Visiting http://localhost:5173 to access the website

Git Configurations

Initialize the git repository

1
2
$ cd my-site
$ git init .

Config gitignore

Add the .vitepress/cache directory to .gitignore

1
.vitepress/cache

Result .gitignore

1
2
3
4
5
6
7
8
9
10
11
12
13
.idea

# macOS
.DS_Store

# vitepress
.vitepress/cache

# Dependency directories
node_modules/

# build output
dist

VitePress Configurations

Site Config

The site configuration file is .vitepress/config.mts

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
export default defineConfig({

srcDir: 'src',
srcExclude: [
'someDir/**',
'someFile',
],
// Whether to get the last updated timestamp for each page using Git.
lastUpdated: true,
head: [
['link', {rel: 'shortcut icon', type: "image/jpeg", href: '/logo.jpeg'}],
// These two are what you want to use by default
['link', {rel: 'apple-touch-icon', type: "image/jpeg", href: '/logo.jpeg'}],
['link', {rel: 'apple-touch-icon', type: "image/jpeg", sizes: "72x72", href: '/logo.jpeg'}],
['link', {rel: 'apple-touch-icon', type: "image/jpeg", sizes: "114x114", href: '/logo.jpeg'}],
['link', {rel: 'apple-touch-icon', type: "image/jpeg", sizes: "144x144", href: '/logo.jpeg'}],
['link', {rel: 'apple-touch-icon-precomposed', type: "image/jpeg", href: '/logo.jpeg'}],
// This one works for anything below iOS 4.2
['link', {rel: 'apple-touch-icon-precomposed apple-touch-icon', type: "image/jpeg", href: '/logo.jpeg'}],
],

})
1
$ mkdir src/ && mv index.md src/

Files

  • src/public/logo.jepg

srcDir

Move the home page index.md to src/index.md

1
2
$ mkdir src/
$ mv index.md src/
1
2
3
4
5
export default defineConfig({

srcDir: 'src',

})

srcExclude

Optional config. If you need to add excluded directories and files.

1
2
3
4
5
6
7
8
export default defineConfig({

srcExclude: [
'someDir/**',
'someFile',
],

})

For example:

  • docs/**,
  • index-en.md

lastUpdated

1
2
3
4
5
6
export default defineConfig({

// Whether to get the last updated timestamp for each page using Git.
lastUpdated: true,

})

Shortcut icon

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
export default defineConfig({

head: [
['link', {rel: 'shortcut icon', type: "image/jpeg", href: '/logo.jpeg'}],
// These two are what you want to use by default
['link', {rel: 'apple-touch-icon', type: "image/jpeg", href: '/logo.jpeg'}],
['link', {rel: 'apple-touch-icon', type: "image/jpeg", sizes: "72x72", href: '/logo.jpeg'}],
['link', {rel: 'apple-touch-icon', type: "image/jpeg", sizes: "114x114", href: '/logo.jpeg'}],
['link', {rel: 'apple-touch-icon', type: "image/jpeg", sizes: "144x144", href: '/logo.jpeg'}],
['link', {rel: 'apple-touch-icon-precomposed', type: "image/jpeg", href: '/logo.jpeg'}],
// This one works for anything below iOS 4.2
['link', {rel: 'apple-touch-icon-precomposed apple-touch-icon', type: "image/jpeg", href: '/logo.jpeg'}],
],

})

src/public/logo.jepg

Theme Config

The theme configuration file is also .vietpress/config.mts

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import {type DefaultTheme, defineConfig} from 'vitepress'

export default defineConfig({
themeConfig: {
nav: nav(),
sidebar: {
'/example/': sidebarExample()
},

logo: {src: '/your-logo.png', width: 24, height: 24},
search: {
provider: 'local'
},
outline: {
level: "deep"
},
}
})

function nav(): DefaultTheme.NavItem[] {
return [
{text: 'Home', link: '/'},
{text: 'Example', link: '/example/'},
];
}

function sidebarExample(): DefaultTheme.SidebarItem[] {
return [
{text: 'Examples', link: '/examples.md'},
]
}

Files

  • src/public/your-logo.png
1
2
3
4
5
6
7
export default defineConfig({
themeConfig: {

logo: {src: '/your-logo.png', width: 24, height: 24},

}
})

src/public/your-logo.png

1
2
3
4
5
6
7
8
9
export default defineConfig({
themeConfig: {

search: {
provider: 'local'
},

}
})

Outline

1
2
3
4
5
6
7
8
9
export default defineConfig({
themeConfig: {

outline: {
level: "deep"
},

}
})

Home Page Config

Config home page in src/index.md

Image

1
2
3
4
hero:
image:
src: your-home-image.png
alt: your-home-image

src/public/your-home-image.png

Feature Icons

1
2
3
4
features:
- icon: 🚀
title:
details:

Other Config

Favicon

Put your website icon to src/public/favicon.ico

Deployment

Create a repository on GitHub

Create button on the right top bar of GitHub -> New repository

Enter your repository name. For example, my-site

Click the “Create Repository” button.

Commit your local repository to GitHub

1
2
3
4
5
git add .
git commit -m '🎉feat: First commit'
git remote add origin git@github.com:{your_username}/{repo_name}.git
git branch -M main
git push -u origin main

Deploy your project on Vercel

Go to Vercel website.

Click the “Add New” button -> Project -> Import Git Repository

Add permission to access the new GitHub repository to Vercel

After finishing the permission configuration, you can find the new GitHub repository on Vercel.

Select the repository. And click “Import”

Override “Build and Output Settings”

  • Build Command: npm run docs:build or yarn docs:build
  • Output Directory: .vitepress/dist

Click “Deploy”

After the deployment process is finished, you can visit your website by Vercel provided URL.

Contents
  1. 1. Initialize VitePress Project
    1. 1.1. Add vitepress dependency
    2. 1.2. Scaffold a basic project
  2. 2. Running the project
  3. 3. Git Configurations
    1. 3.1. Initialize the git repository
    2. 3.2. Config gitignore
  4. 4. VitePress Configurations
    1. 4.1. Site Config
      1. 4.1.1. srcDir
      2. 4.1.2. srcExclude
      3. 4.1.3. lastUpdated
      4. 4.1.4. Head
    2. 4.2. Theme Config
      1. 4.2.1. Logo
      2. 4.2.2. Local Search
      3. 4.2.3. Outline
    3. 4.3. Home Page Config
      1. 4.3.1. Image
      2. 4.3.2. Feature Icons
    4. 4.4. Other Config
      1. 4.4.1. Favicon
  5. 5. Deployment
    1. 5.1. Create a repository on GitHub
    2. 5.2. Commit your local repository to GitHub
    3. 5.3. Deploy your project on Vercel