Table of Contents
0. VuePress
1. Local build
7. 设置语言
8. 开启目录结构
9. 修改主题颜色
10. 自定义修改样式
11. 部署
Home Web Front-end Vue.js One article to quickly teach you how to build a blog using VuePress + Github Pages (practical)

One article to quickly teach you how to build a blog using VuePress + Github Pages (practical)

Dec 20, 2021 pm 06:45 PM
vuepress blog

This article will share with you a VuePress practical experience. Through this article, I will teach you how to quickly build a blog using VuePress Github Pages. I hope it will be helpful to everyone.

One article to quickly teach you how to build a blog using VuePress + Github Pages (practical)

Recently completed the translation of Hanbook, the latest official TypeScript document. There are fourteen articles in total. It can be called one of the best TypeScript4 introductory tutorials in China. In order to make it easier for everyone to read, I used VuePress Github Pages to build a blog. The blog effect is as follows:

0. VuePress

VuePress Needless to say, the static website generator based on Vue has a simple style and relatively simple configuration. The reason why I don’t use VitePress is because I want to use an existing theme, and VitePress is not compatible with the current VuePress ecosystem. As for why I don’t choose VuePress@next, considering that it is still in Beta stage, wait until it stabilizes before starting the migration. [Related recommendations: "vue.js Tutorial"]

1. Local build

Quick startSame as VuePress official website:

1. Create and enter a new directory

// 文件名自定义
mkdir vuepress-starter && cd vuepress-starter
Copy after login

2. Use your favorite package manager to initialize

yarn init # npm init
Copy after login

3. Install VuePress as a local dependency

yarn add -D vuepress # npm install -D vuepress
Copy after login

4 , create your first document, VuePress will use docs as the document root directory, so this README.md is equivalent to the homepage:

mkdir docs && echo '# Hello VuePress' > docs/README.md
Copy after login

5. Add some scripts

{
  "scripts": {
    "docs:dev": "vuepress dev docs",
    "docs:build": "vuepress build docs"
  }
}
Copy after login
## in package.json #6. Start the server locally

yarn docs:dev # npm run docs:dev
Copy after login

VuePress will start a hot-reloaded development server at

http://localhost:8080 (opens new window).

2. Basic configuration

Create a

.vuepress directory in the document directory, where all VuePress related files will be placed. At this time, your project structure may look like this:

.
├─ docs
│  ├─ README.md
│  └─ .vuepress
│     └─ config.js
└─ package.json
Copy after login

Add

config.js under the .vuepress folder to configure the title and description of the website to facilitate SEO:

module.exports = {
  title: 'TypeScript4 文档',
  description: 'TypeScript4 最新官方文档翻译'
}
Copy after login

The interface at this time is similar to:

3. Add navigation bar

We now add a navigation bar in the upper right corner of the top of the page , modify

config.js:

module.exports = {
    title: '...',
    description: '...',
    themeConfig: {
        nav: [
            { text: '首页', link: '/' },
            { 
                text: '冴羽的 JavaScript 博客', 
                items: [
                    { text: 'Github', link: 'https://github.com/mqyqingfeng' },
                    { text: '掘金', link: 'https://juejin.cn/user/712139234359182/posts' }
                ]
            }
        ]
    }
}
Copy after login

The effect is as follows:

For more configuration, please refer to the VuePress navigation bar:

https://vuepress.vuejs.org/zh/theme/default-theme-config.html#Navigation bar

4. Add sidebar

Now we add some md documents. The current document directory is as follows:

.
├─ docs
│  ├─ README.md
│  └─ .vuepress
│     └─ config.js
|  └─ handbook
|  	  └─ ConditionalTypes.md
|	    └─ Generics.md
└─ package.json
Copy after login

We configure it in

config.js as follows:

module.exports = {
    themeConfig: {
        nav: [...],
        sidebar: [
            {
                title: '欢迎学习',
                path: '/',
                collapsable: false, // 不折叠
                children: [
                    { title: "学前必读", path: "/" }
                ]
            },
            {
              title: "基础学习",
              path: '/handbook/ConditionalTypes',
              collapsable: false, // 不折叠
              children: [
                { title: "条件类型", path: "/handbook/ConditionalTypes" },
                { title: "泛型", path: "/handbook/Generics" }
              ],
            }
          ]
    }
}
Copy after login

The corresponding effect is as follows:

5. Change the theme

Now the basic directory and navigation functions have been implemented, but if I also want to load, switch animations, and mode switch (dark mode), As for functions such as returning to the top and comments, in order to simplify development costs, we can use the theme directly. The theme used here is vuepress-theme-rec (https://vuepress-theme-reco.recoluan.com/):

Now we install vuepress-theme-reco:

npm install vuepress-theme-reco --save-dev
# or
yarn add vuepress-theme-reco
Copy after login

Then reference the theme in

config.js:

module.exports = {
  // ...
  theme: 'reco'
  // ...
}
Copy after login

Refresh the page and we will find some details Changes, such as loading animation during loading, and support for switching to dark mode:

6. Add article information

But we will also find, like conditions In this article,

Conditional Types actually appears twice. This is because this topic automatically extracts the first headline as the title of this article. We can add it to the md of each article. Add some information modifications to the file:

---
title: 条件类型
author: 冴羽
date: '2021-12-12'
---
Copy after login

The effect of the article at this time is as follows:

但如果你不想要标题、作者、时间这些信息呢,我们可以在样式里隐藏,这个稍后会讲到。

7. 设置语言

注意,上图的文章时间,我们写入的格式为 2021-12-12 ,但是显示的是 12/12/2021,这是因为 VuePress 默认的 lang 为 en-US,我们修改一下 config.js:

module.exports = {
  // ...
  locales: {
    '/': {
      lang: 'zh-CN'
    }
  },
  // ...
}
Copy after login

可以发现时间换了一种展示方式:

8. 开启目录结构

在原本的主题里,我们发现每篇文章的目录结构出现在左侧:

而 vuepress-theme-reco 将原有的侧边栏的中的多级标题移出,生成子侧边栏,放在了页面的右侧,如果你要全局开启,可在页面 config.js 里设置开启:

module.exports = {
  //...
  themeConfig: {
    subSidebar: 'auto'
  }
  //...
}
Copy after login

此时效果如下:

9. 修改主题颜色

VuePress 基于 Vue,所以主题色用的是 Vue 的绿色,然而 TypeScript 的官方色则是蓝色,那如何修改 VuePress 的主题色呢?

你可以创建一个 .vuepress/styles/palette.styl 文件,文件代码如下:

$accentColor = #3178c6
Copy after login

此时可以发现主题颜色变了:

更多的颜色修改参考 VuePress 的 palette.styl

10. 自定义修改样式

如果你想自定义修改一些 DOM 元素的样式呢?就比如在暗黑模式下:

我们发现用作强调的文字颜色比较暗淡,在暗黑模式下看不清楚,我想改下这个文字的颜色和背景色呢?

而 VuePress 提供了一种添加额外样式的简便方法。你可以创建一个 .vuepress/styles/index.styl 文件。这是一个 Stylus 文件,但你也可以使用正常的 CSS 语法。

我们在 .vupress 文件夹下创建这个目录,然后创建 index.styl 文件,代码如下:

// 通过检查,查看元素样式声明
.dark .content__default code {
    background-color: rgba(58,58,92,0.7);
    color: #fff;
}
Copy after login

此时文字就清晰了很多:

那之前我们提到的隐藏每篇文章的标题、作者、时间呢,其实也是类似的方式:

.page .page-title {
   display: none;
}
Copy after login

最后的效果如下:

11. 部署

我们的博客就算是正式的做好了,接下来我们部署到免费的 Github Pages 上。我们在 Github 上新建一个仓库,这里我取得仓库名为:learn-typescript

对应,我们需要在 config.js 添加一个 base 路径配置:

module.exports = {
  	// 路径名为 "/<REPO>/"
    base: &#39;/learn-typescript/&#39;,
  	//...
}
Copy after login

最终的 config.js 文件内容为:

module.exports = {
    title: &#39;TypeScript4 文档&#39;,
    description: &#39;TypeScript4 最新官方文档翻译&#39;,
    base: &#39;/learn-typescript/&#39;,
    theme: &#39;reco&#39;,
    locales: {
        &#39;/&#39;: {
          lang: &#39;zh-CN&#39;
        }
    },
    themeConfig: {
        // lastUpdated: &#39;上次更新&#39;,
        subSidebar: &#39;auto&#39;,
        nav: [
            { text: &#39;首页&#39;, link: &#39;/&#39; },
            { 
                text: &#39;冴羽的 JavaScript 博客&#39;, 
                items: [
                    { text: &#39;Github&#39;, link: &#39;https://github.com/mqyqingfeng&#39; },
                    { text: &#39;掘金&#39;, link: &#39;https://juejin.cn/user/712139234359182/posts&#39; }
                ]
            }
        ],
        sidebar: [
            {
                title: &#39;欢迎学习&#39;,
                path: &#39;/&#39;,
                collapsable: false,
                children: [
                    { title: "学前必读", path: "/" }
                ]
            },
            {
              title: "基础学习",
              path: &#39;/handbook/ConditionalTypes&#39;,
              collapsable: false,
              children: [
                { title: "条件类型", path: "/handbook/ConditionalTypes" },
                { title: "泛型", path: "/handbook/Generics" }
              ],
            }
          ]
    }
}
Copy after login

然后我们在项目 vuepress-starter 目录下建立一个脚本文件:deploy.sh,注意修改一下对应的用户名和仓库名:

#!/usr/bin/env sh

# 确保脚本抛出遇到的错误
set -e

# 生成静态文件
npm run docs:build

# 进入生成的文件夹
cd docs/.vuepress/dist

git init
git add -A
git commit -m &#39;deploy&#39;

# 如果发布到 https://<USERNAME>.github.io/<REPO>
git push -f git@github.com:mqyqingfeng/learn-typescript.git master:gh-pages

cd -
Copy after login

然后命令行切换到 vuepress-starter 目录下,执行  sh deploy.sh,就会开始构建,然后提交到远程仓库,注意这里提交到了 gh-pages 分支,我们查看下对应仓库分支的代码:

我们可以在仓库的 Settings -> Pages 中看到最后的地址:

Like the last address I generated is https://mqyqingfeng.github.io/learn-typescript/

At this point, we are done Deployment of VuePress and Github Pages.

For more programming-related knowledge, please visit: Introduction to Programming! !

The above is the detailed content of One article to quickly teach you how to build a blog using VuePress + Github Pages (practical). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Start from scratch and guide you step by step to install Flask and quickly establish a personal blog Start from scratch and guide you step by step to install Flask and quickly establish a personal blog Feb 19, 2024 pm 04:01 PM

Starting from scratch, I will teach you step by step how to install Flask and quickly build a personal blog. As a person who likes writing, it is very important to have a personal blog. As a lightweight Python Web framework, Flask can help us quickly build a simple and fully functional personal blog. In this article, I will start from scratch and teach you step by step how to install Flask and quickly build a personal blog. Step 1: Install Python and pip Before starting, we need to install Python and pi first

What are the top ten open source PHP blog systems in 2022? 【recommend】 What are the top ten open source PHP blog systems in 2022? 【recommend】 Jul 27, 2022 pm 05:38 PM

Blog, also translated as web log, blog or blog, is a website that is usually managed by individuals and posts new articles from time to time. So how to set up a blog? What are the PHP blog systems? Which blogging system is best to use? Below, PHP Chinese website will summarize and share the top ten open source PHP blog systems with you. Let’s take a look!

Create a simple blog: using PHP and SQLite Create a simple blog: using PHP and SQLite Jun 21, 2023 pm 01:23 PM

With the development of the Internet, blogs have become a platform for more and more people to share their lives, knowledge and ideas. If you also want to create a blog of your own, then this article will introduce how to use PHP and SQLite to create a simple blog. Determine the needs Before starting to create a blog, we need to determine the functions we want to achieve. For example: Create a blog post Edit a blog post Delete a blog post Display a list of blog posts Display blog post details User authentication and permission control Install PHP and SQLite We need to install PHP and S

Build a blog website using the Python Django framework Build a blog website using the Python Django framework Jun 17, 2023 pm 03:37 PM

With the popularity of the Internet, blogs play an increasingly important role in information dissemination and communication. In this context, more and more people are starting to build their own blog sites. This article will introduce how to use the PythonDjango framework to build your own blog website. 1. Introduction to the PythonDjango framework PythonDjango is a free and open source web framework that can be used to quickly develop web applications. The framework provides developers with powerful tools to help them build feature-rich

How to create a simple blog using PHP How to create a simple blog using PHP Sep 24, 2023 am 08:25 AM

How to create a simple blog using PHP 1. Introduction With the rapid development of the Internet, blogs have become an important way for people to share experiences, record life and express opinions. This article will introduce how to use PHP to create a simple blog, with specific code examples. 2. Preparation Before starting, you need to have the following development environment: a computer with a PHP interpreter and Web server (such as Apache) installed, a database management system, such as MySQL, a text editor or IDE3

How to create a blog How to create a blog Oct 10, 2023 am 09:46 AM

You can create a blog by determining the topic and target audience of the blog, choosing a suitable blogging platform, registering a domain name and purchasing hosting, designing the appearance and layout of the blog, writing quality content, promoting the blog, and analyzing and improving it.

A complete guide to installing and building a blog on CentOS A complete guide to installing and building a blog on CentOS Feb 14, 2024 pm 08:27 PM

This article will introduce in detail how to install and build a blog on CentOS system, including the required software installation, configuration and basic usage. At the end of the article, I will share a little Linux knowledge. With the continuous development of Internet technology, more and more people choose to use blogs to record their lives and share knowledge. As a popular Linux distribution, CentOS is stable and secure and is suitable for building blogs. This article will Detailed introduction to the steps of installing and setting up a blog on CentOS. Preparation before installation 1. Make sure that the CentOS operating system has been installed and can be connected to the Internet. 2. Have basic knowledge of Linux command line operations. Install Apache server 1. Open the terminal and use the following command

golang Websocket Development Guide: Implementing multi-person online blogging function golang Websocket Development Guide: Implementing multi-person online blogging function Dec 02, 2023 pm 01:17 PM

GolangWebsocket Development Guide: Implementing the Multi-person Online Blog Function In today's era of developed Internet, blogs have become an important tool for people to share their opinions and knowledge. In order to improve user experience, implementing multi-person online blogging has become a requirement for many websites. This article will introduce how to use Websocket technology in Golang to implement this function, and give specific code examples. Websocket is a new communication protocol in HTML5, which allows servers and browsers to

See all articles