使用VitePress创建个人网站并部署到GitHub

news2024/10/7 16:19:24

网站在线预览

参考文档:

  • VitePress

创建 GitHub 远程仓库

image.png

克隆远程仓库到本地

git clone git@github.com:themusecatcher/front-end-notes.git

进入 front-end-notes/ 目录,添加 README.md 并建立分支跟踪

echo "# front-end-notes" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M master
git remote add origin git@github.com:themusecatcher/front-end-notes.git
git push -u origin master

安装 vitepress

推荐使用 pnpm

npm install -g pnpm

安装 vitepress:

pnpm add -D vitepress
# or
yarn add -D vitepress

使用脚手架初始化文档项目

pnpm exec vitepress init

image.png

启动项目,查看网站

pnpm docs:dev

初始化 package.json 文件,填写相关信息

npm init

完整 package.json 文件如下:

{
  "name": "front-end-notes",
  "version": "0.0.1",
  "scripts": {
    "docs:dev": "vitepress dev docs --open",
    "docs:build": "vitepress build docs",
    "docs:preview": "vitepress preview docs",
    "docs:deploy": "sh deploy.sh"
  },
  "devDependencies": {
    "less": "^4.1.3",
    "vitepress": "1.0.0-beta.1"
  },
  "description": "前端笔记",
  "directories": {
    "doc": "docs"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/themusecatcher/front-end-notes.git"
  },
  "keywords": [
    "front-end",
    "notes"
  ],
  "author": "themusecatcher",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/themusecatcher/front-end-notes/issues"
  },
  "homepage": "https://github.com/themusecatcher/front-end-notes#readme"
}

编写网站首页

首页配置参考文档

docs/index.md 中编写首页,其中 fetchVersion() 自定义方法用于在首页 tagline 后添加项目版本标签

---
layout: home

title: Front-end Notes
titleTemplate: Library

hero:
  name: Front-end Notes
  text: ''
  tagline: 前端笔记文档
  image:
    src: /logo-with-shadow.png
    alt: Front-end Notes
  actions:
    - theme: brand
      text: Get Started
      link: /guide/started
    - theme: alt
      text: View on GitHub
      link: https://github.com/themusecatcher/front-end-notes
---

<script setup lang="ts">
import { onMounted } from 'vue'
import { fetchVersion } from './.vitepress/utils/fetchVersion'

onMounted(() => {
  fetchVersion()
})
</script>

docs/.vitepress/utils/ 中创建 fetchVersion.ts 文件

/*
  远程读取 github 仓库中 package.json 文件中的 version 版本号
  方式一:
  读取规则:https://api.github.com/repos/<username>/<repo>/contents/<file>?ref=<branch 可选,默认master>
  return fetch('https://api.github.com/repos/themusecatcher/front-end-notes/contents/package.json?ref=master', {
    headers: {
      // See https://docs.github.com/en/rest/overview/media-types
      Accept: 'application/vnd.github.v3.raw',
      // See https://docs.github.com/en/rest/guides/getting-started-with-the-rest-api#authentication
      // Authorization: 'token ${GITHUB_TOKEN}',
    }
  })
  方式二:
  读取规则:https://raw.githubusercontent.com/<username>/<repo>/<branch>/<file>
  return fetch('https://raw.githubusercontent.com/themusecatcher/front-end-notes/master/package.json')
*/
export function fetchVersion () {
  return fetch('https://api.github.com/repos/themusecatcher/front-end-notes/contents/package.json?ref=master', {
    headers: {
      // See https://docs.github.com/en/rest/overview/media-types
      Accept: 'application/vnd.github.v3.raw',
      // See https://docs.github.com/en/rest/guides/getting-started-with-the-rest-api#authentication
      // Authorization: 'token ${GITHUB_TOKEN}',
    }
  }).then(res => res.json())
    .then(json => json.version ?? '')
    .then(version => {
      if (!version) return
      const tagLineParagragh = document.querySelector('div.VPHero.has-image.VPHomeHero > div > div.main > p.tagline')
      const docsVersionSpan = document.createElement('samp')
      docsVersionSpan.classList.add('version-tag')
      docsVersionSpan.innerText = version
      tagLineParagragh?.appendChild(docsVersionSpan)
    })
}

docs/.vitepress/theme/global.less 中写入标签样式

.version-tag {
  font-size: 14px;
  line-height: 1.571;
  font-weight: bold;
  padding: 4px 6px;
  margin-left: 6px;
  background: #bd34fe;
  color: #FFF;
  border-radius: 10px;
  display: inline-block;
  vertical-align: top;
  margin-top: 4px;
}

由于文档使用 less ,因此需要安装 less 预处理器: 相关文档

pnpm add less -D

效果如下图,版本标签:0.0.1

image.png

global.less 中编写文档全局样式,其中样式皆源自 vite官网 中使用的全局样式,并稍加修改:

/**
 * Colors
 * -------------------------------------------------------------------------- */

 :root {
  --vp-c-brand: #646cff;
  --vp-c-brand-light: #747bff;
  --vp-c-brand-lighter: #9499ff;
  --vp-c-brand-lightest: #bcc0ff;
  --vp-c-brand-dark: #535bf2;
  --vp-c-brand-darker: #454ce1;
  --vp-c-brand-dimm: rgba(100, 108, 255, 0.08);
  --c-brand: #646cff;
  --c-brand-light: #747bff;
}

/**
 * Component: Button
 * -------------------------------------------------------------------------- */

:root {
  --vp-button-brand-border: var(--vp-c-brand-light);
  --vp-button-brand-text: var(--vp-c-white);
  --vp-button-brand-bg: var(--vp-c-brand);
  --vp-button-brand-hover-border: var(--vp-c-brand-light);
  --vp-button-brand-hover-text: var(--vp-c-white);
  --vp-button-brand-hover-bg: var(--vp-c-brand-light);
  --vp-button-brand-active-border: var(--vp-c-brand-light);
  --vp-button-brand-active-text: var(--vp-c-white);
  --vp-button-brand-active-bg: var(--vp-button-brand-bg);
}

/**
 * Component: Home
 * -------------------------------------------------------------------------- */

:root {
  --vp-home-hero-name-color: transparent;
  --vp-home-hero-name-background: -webkit-linear-gradient(
    120deg,
    #bd34fe 30%,
    #41d1ff
  );

  --vp-home-hero-image-background-image: linear-gradient(
    -45deg,
    #bd34fe 50%,
    #47caff 50%
  );
  --vp-home-hero-image-filter: blur(40px);
}

@media (min-width: 640px) {
  :root {
    --vp-home-hero-image-filter: blur(56px);
  }
}

@media (min-width: 960px) {
  :root {
    --vp-home-hero-image-filter: blur(72px);
  }
}

/**
 * Component: Custom Block
 * -------------------------------------------------------------------------- */

:root {
  --vp-custom-block-tip-border: var(--vp-c-brand);
  --vp-custom-block-tip-text: var(--vp-c-brand-darker);
  --vp-custom-block-tip-bg: var(--vp-c-brand-dimm);
}

.dark {
  --vp-custom-block-tip-border: var(--vp-c-brand);
  --vp-custom-block-tip-text: var(--vp-c-brand-lightest);
  --vp-custom-block-tip-bg: var(--vp-c-brand-dimm);
}

/**
 * Component: Algolia
 * -------------------------------------------------------------------------- */

.DocSearch {
  --docsearch-primary-color: var(--vp-c-brand) !important;
}

/**
 * VitePress: Custom fix
 * -------------------------------------------------------------------------- */

/*
  Use lighter colors for links in dark mode for a11y.
  Also specify some classes twice to have higher specificity
  over scoped class data attribute.
*/
.dark .vp-doc a,
.dark .vp-doc a > code,
.dark .VPNavBarMenuLink.VPNavBarMenuLink:hover,
.dark .VPNavBarMenuLink.VPNavBarMenuLink.active,
.dark .link.link:hover,
.dark .link.link.active,
.dark .edit-link-button.edit-link-button,
.dark .pager-link .title {
  color: var(--vp-c-brand-lighter);
}

.dark .vp-doc a:hover,
.dark .vp-doc a > code:hover {
  color: var(--vp-c-brand-lightest);
  opacity: 1;
}
.vp-doc a {
  font-weight: normal;
}
.vp-doc p {
  margin: 0;
}
/* Transition by color instead of opacity */
.dark .vp-doc .custom-block a {
  transition: color 0.25s;
}
a:hover {
  text-decoration: none !important;
}
summary {
  font-weight: 600;
  &:hover {
    cursor: pointer;
    color: var(--vp-c-brand-lighter);
  }
}
svg {
  fill: var(--vp-c-text-1);
}
.VPNavBarTitle .title {
  transition: all 0.25s;
  &:hover {
    color: var(--vp-c-brand);
  }
}
.version-tag {
  font-size: 14px;
  line-height: 1.571;
  font-weight: bold;
  padding: 4px 6px;
  margin-left: 6px;
  background: #bd34fe;
  color: #FFF;
  border-radius: 10px;
  display: inline-block;
  vertical-align: top;
  margin-top: 4px;
}

引入默认主题与全局样式

theme/index.ts 中引入默认主题与全局样式

import DefaultTheme from 'vitepress/theme'
import './global.less' // global less

export default {
  extends: DefaultTheme // or ...DefaultTheme
}

配置网站

docs/.vitepress/config.ts 中对网站进行整体配置

import { defineConfig } from 'vitepress'

export default defineConfig({
  title: `Front-end Notes`,
  description: '前端笔记文档',
  base: '/front-end-notes/',

  head: [ // 网站图标
    ['link', { rel: 'icon', type: 'image/svg+xml', href: 'logo.svg' }],
    // ['link', { rel: 'icon', type: 'image/x-icon', href: 'favicon.ico' }],
  ],
  appearance: true, // 默认 true,设为 false 则无法切换dark/light主题,可选 'dark' true false
  markdown: {
    lineNumbers: false // 是否显示行数,默认false
  },
  themeConfig: {
    logo: '/logo.svg',

    editLink: {
      pattern: 'https://github.com/themusecatcher/front-end-notes/tree/master/docs/:path',
      text: 'Suggest changes to this page',
    },
    // 默认支持icon包括:'discord'|'facebook'|'github'|'instagram'|'linkedin'|'mastodon'|'slack'|'twitter'|'youtube'
    socialLinks: [
      { icon: 'github', link: 'https://github.com/themusecatcher/front-end-notes' },
      // 自定义icon
      // {
      //   icon: {
      //     svg: '<svg role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title>Dribbble</title><path d="M12...6.38z"/></svg>'
      //   },
      //   link: 'https://www.npmjs.com/package/front-end-notes'
      // }
    ],

    // search: { // vitepress 内置 search
    //   provider: 'local'
    // },

    algolia: { // algolia 搜索服务 与 内置 search 可二选一
      appId: 'LPTNA0E8HM',
      apiKey: '8f1b68dfab6b0320adef728a1c3a77cc',
      indexName: 'themusecatcher_front-end'
    },

    footer: {
      message: 'Released under the MIT License.',
      copyright: 'Copyright © 2023-present The Muse Catcher',
    },

    nav: [
      { text: 'Vue2 Notes', link: '/vue2/note-1', activeMatch: '/vue2/' },
      { text: 'Vue3 Notes', link: '/vue3/note-1', activeMatch: '/vue3/' },
      {
        text: 'links',
        items: [
          { text: 'My Github', link: 'https://github.com/themusecatcher' },
          { text: 'My CSDN', link: 'https://blog.csdn.net/Dandrose?type=blog' },
          {
            items: [
              {
                text: 'Vue 2 Docs',
                link: 'https://v2.cn.vuejs.org/v2/guide/',
              },
              {
                text: 'Vue 3 Docs',
                link: 'https://cn.vuejs.org/guide/introduction.html',
              },
              {
                text: 'TypeScript Docs',
                link: 'https://www.tslang.cn/docs/home.html',
              },
              {
                text: 'MDN Web Docs',
                link: 'https://developer.mozilla.org/zh-CN/',
              }
            ]
          },
          {
            items: [
              {
                text: 'npm',
                link: 'https://www.npmjs.com/',
              },
              {
                text: 'vite',
                link: 'https://cn.vitejs.dev/',
              },
              {
                text: 'markdown',
                link: 'https://markdown.com.cn/',
              },
              {
                text: 'vitepress',
                link: 'https://vitepress.dev/',
              }
            ]
          }
        ]
      }
    ],

    sidebar: {
      '/vue2/': [
        {
          text: '指引',
          items: [
            {
              text: '开始',
              link: '/vue2/started'
            }
          ]
        },
        {
          text: 'Vue2 Notes',
          items: [
            {
              text: 'note-1',
              link: '/vue2/note-1'
            },
            {
              text: 'note-2',
              link: '/vue2/note-2'
            },
            {
              text: 'note-3',
              link: '/vue2/note-3'
            },
            {
              text: 'note-4',
              link: '/vue2/note-4'
            },
            {
              text: 'note-5',
              link: '/vue2/note-5'
            },
            {
              text: 'note-6',
              link: '/vue2/note-6'
            }
          ]
        }
      ],
      '/vue3/': [
        {
          text: '指引',
          items: [
            {
              text: '开始',
              link: '/vue3/started'
            }
          ]
        },
        {
          text: 'Vue3 Notes',
          items: [
            {
              text: 'note-1',
              link: '/vue3/note-1'
            },
            {
              text: 'note-2',
              link: '/vue3/note-2'
            },
            {
              text: 'note-3',
              link: '/vue3/note-3'
            },
            {
              text: 'note-4',
              link: '/vue3/note-4'
            },
            {
              text: 'note-5',
              link: '/vue3/note-5'
            },
            {
              text: 'note-6',
              link: '/vue3/note-6'
            }
          ]
        }
      ]
    }
  }
})

创建网站内容目录

docs/ 下新建网站目录,例如:vue2/vue3/

注意:目录结构需要与 docs/.vitepress/config.ts 配置文件中的 sidebar 属性相对应

vue2/vue3 下按自己需要创建目录或文件

打包静态网站并部署到 GitHub

编写打包部署脚本

在项目根目录创建 deploy.sh 脚本,用于自动化打包、部署流程

deploy.sh 文件:

# /bin/bash

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

# 打包生成静态文件
pnpm docs:build

# 进入待发布的 dist/ 目录
cd docs/.vitepress/dist

# 提交打包静态网站到 github-pages 分支
git init
git add .
git commit -m 'deploy'

# 部署到 https://<username>.github.io/<repo>
git push -f git@github.com:themusecatcher/front-end-notes.git master:github-pages

# 提交所有代码到github
cd ../../../
git add .
git cm -m 'update'
git push

package.json 添加指令

"scripts": {
    "docs:deploy": "sh deploy.sh"
}

打包部署

pnpm docs:deploy
# or
sh deploy.sh

配置 github pages

image.png

查看网站

save 成功之后,点击 Visit site 打开查看网站:

image.png

配置 Algolia 搜索(可选内置 search)

申请搜索服务

申请地址

填写部署到公网的网站地址、邮箱和代码仓库地址,全部勾选,然后提交!

image.png

等待申请通过的邮件

image.png

回复邮件

I am the maintainer of the website, I can modify the code ~

image.png

等待回复邮件

获取 appIdapiKeyindexName

image.png

配置并引入 algolia 搜索服务

docs/.vitepress/config.ts 中写入以下配置

import { defineConfig } from 'vitepress'

export default defineConfig({
  themeConfig: {
    algolia: {
      appId: 'LPTNA0E8HM',
      apiKey: '8f1b68dfab6b0320adef728a1c3a77cc',
      indexName: 'themusecatcher_front-end'
    }
  }
})

algolia 搜索效果如下图:

image.png

内置 search 搜索效果如下图:

image.png

网站整体目录结构如下图:

image.png

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/644743.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

配置Kettle连接大数据HDFS

需求&#xff1a;配置Kettle连接大数据HDFS Kettle对接大数据平台的配置 一&#xff0e;软件环境 1.Hadoop集群,版本&#xff1a;Hadoop3.3.0 2.ETL工具Kettle&#xff0c;版本&#xff1a;pdi-ce-7.0.0.0-25 &#xff08;解压命令&#xff1a;*.zip 用 unzip 解压&#xf…

4自由度并联机器狗实现下蹲功能

1. 功能说明 本文示例将实现R328a样机4自由度并联机器狗下蹲的功能。 2. 结构说明 本样机的并联驱动结构与 【R082】4自由度并联四足 类似&#xff0c;两款样机可以对比来看。 本样机腿部的结构如下图所示&#xff1a;驱动核心部分是两个5杆结构的组合。 两个五杆结构图 驱动核…

ASP.NET MVC下的四种验证编程方式

ASP.NET MVC采用Model绑定为目标Action生成了相应的参数列表&#xff0c;但是在真正执行目标Action方法之前&#xff0c;还需要对绑定的参数实施验证以确保其有效性&#xff0c;我们将针对参数的验证成为Model绑定。总地来说&#xff0c;我们可以采用4种不同的编程模式来进行针…

手机号码篡改测试-业务安全测试实操(6)

手机号码篡改测试, 用户ID篡改测试 订单ID篡改测试-业务安全测试实操(5)_luozhonghua2000的博客-CSDN博客 手机号码篡改测试 测试原理和方法 手机号通常可以代表一个用户身份。当请求中发现有手机号参数时,我们可以试着修改它,测试是否存在越权漏洞。系统登录功能一般先判断…

计算机视觉研究院重新开启知识星球(前期我们免费加入)

点击蓝字 关注我们 关注并星标 从此不迷路 计算机视觉研究院 公众号ID&#xff5c;计算机视觉研究院 学习群&#xff5c;扫码在主页获取加入方式 计算机视觉研究院专栏 Column of Computer Vision Institute 满足广大兴趣关注者&#xff0c;最近我们平台重启了”知识星球“&…

聚观早报 |梅西将于14日淘宝开播;李斌回应蔚来全系车型降3万元

今日要闻&#xff1a;梅西将于6月14日上淘宝开播&#xff1b;李斌回应"蔚来全系车型降价3万元”&#xff1b;美国联邦贸易委员会阻止微软收购暴雪&#xff1b;称iPhone15洗系列最高涨价200美元&#xff1b;极兔正与顺丰洽谈入股 梅西将于6月14日上淘宝开播 据悉&#xff…

微服务springcloud 02 创建项目中的三个service子系统,springcloud中注册中心Eureka介绍和把三个系统注册到Eureka中

item service项目 01.使用springboot创建项目 02.选择依懒项在这里插入代码片 spring web 03.添加sp01-commons依赖 在pom.xml文件中 <?xml version"1.0" encoding"UTF-8"?> <project xmlns"http://maven.apache.org/POM/4.0.0" x…

「深度学习之优化算法」笔记(一):优化算法概述

优化算法笔记&#xff08;一&#xff09;优化算法的介绍 &#xff08;一&#xff09;优化算法的介绍 1.1&#xff08;what&#xff09;什么是优化算法&#xff1f; 我们常见常用的算法有排序算法,字符串遍历算法,寻路算法等。这些算法都是为了解决特定的问题而被提出。 算法本质…

基础知识学习---牛客网C++面试宝典(七)操作系统--第二节

1、本栏用来记录社招找工作过程中的内容&#xff0c;包括基础知识学习以及面试问题的记录等&#xff0c;以便于后续个人回顾学习&#xff1b; 暂时只有2023年3月份&#xff0c;第一次社招找工作的过程&#xff1b; 2、个人经历&#xff1a; 研究生期间课题是SLAM在无人机上的应…

MUR80120PT-ASEMI大电流快恢复二极管MUR80120PT

编辑&#xff1a;ll MUR80120PT-ASEMI大电流快恢复二极管MUR80120PT 型号&#xff1a;MUR80120PT 品牌&#xff1a;ASEMI 封装&#xff1a;TO-247 正向电流&#xff1a;80A 反向电压&#xff1a;1200V 引脚数量&#xff1a;3 恢复时间&#xff1a;35ns 正向压降&#…

ai绘画生成器有哪些?分享3款好用的ai自动绘画生成器

文字和绘画是两种看似不同的艺术形式&#xff0c;但它们之间却有着一种神奇的联系。你或许曾经在一些创意绘画作品中发现过使用文字进行表达的元素&#xff0c;那么&#xff0c;文本究竟如何生成这些令人惊叹的艺术作品呢&#xff1f;今天&#xff0c;我们就来探索一下文字怎样…

Vue中如何进行颜色选择与取色器?

Vue中如何进行颜色选择与取色器&#xff1f; 在Web开发中&#xff0c;颜色选择器是一个非常常见的功能。在Vue.js中&#xff0c;我们可以使用现成的颜色选择器组件或者自己编写一个颜色选择器组件。本文将介绍如何在Vue.js中实现颜色选择器组件和取色器功能。 颜色选择器组件 …

AI技术实现人工客服的开发流程

AI技术取得重大突破后&#xff0c;典型的应用场景就是人机交互效率极大提高&#xff0c;甚至在很多方面好于人和人的交互。使用AI技术实现人工智能客服是非常适合大规模商用的业务场景&#xff0c;今天和大家分享这方面的知识&#xff0c;希望对大家有所帮助。北京木奇移动技术…

Vue让你轻松实现盒子的显示隐藏和双向数据绑定!

Vue让你轻松实现盒子的显示隐藏和双向数据绑定&#xff01; 一、Vue让你轻松实现盒子的显示隐藏和双向数据绑定&#xff01;&#xff08;一&#xff09;v-if 和 v-show1. v-show2. v-if3. 应用场景:4. 案例&#xff1a;展开折叠盒子 &#xff08;二&#xff09;v-else 和 v-els…

为什么年龄越大工作失误越多水平越低能力越差-个人案例

此为内容创作模板&#xff0c;在发布之前请将不必要的内容删除 在日复一日的工作中&#xff0c;我们免不了会产生一些失误&#xff0c;会因此感到沮丧和失望。但如何正确地对待和处理这些失误才是最重要的&#xff0c;它直接影响到我们的工作表现和个人成长。一起来谈谈作为职…

Unity3D:自定义 Editor 工具

推荐&#xff1a;将 NSDT场景编辑器 加入你的3D工具链 3D工具集&#xff1a; NSDT简石数字孪生 使用自定义 Editor 工具 从 Scene 视图中可以访问使用工具模式 API 创建的自定义工具。 您可以通过以下方式来访问自定义工具&#xff1a; 单击场景视图工具工具条叠加中的可用自…

KaiwuDB 发布智慧矿山解决方案

5月21日&#xff0c;天津第七届世界智能大会&#xff08;WIC&#xff09;圆满落幕。作为智能领域的国家级盛会&#xff0c;WIC 汇聚了全球知名院士、顶级学者、产业领袖分享先进技术和实践经验&#xff0c;推进智能技术创新合作。KaiwuDB 受邀出席大会并正式发布智慧矿山解决方…

Seata Saga 模式快速入门和最佳实践

文&#xff5c;王特&#xff08;花名&#xff1a;亦夏&#xff09; Email&#xff1a;yixia.wtantgroup.com 蚂蚁集团数据中间件核心开发 本文 4927 字 阅读 13 分钟 Seata 是一款开源的分布式事务解决方案&#xff0c;致力于在微服务架构下提供高性能和简单易用的分布式事务服…

ssm+java高校图书馆图书借阅导航系统

智能图书馆导航管理系统是一款基于BS架构模式开发的图书馆宣传网站&#xff0c;网页端采用SSM框架技术开发&#xff0c;MySQL作为数据库&#xff0c;同时使用了JSP、java web等技术进行开发&#xff0c;最终达到智能图书导航的实现&#xff0c;能够实现用户搜索书籍&#xff0c…

Vue中如何进行图片处理与滤镜效果?

Vue中如何进行图片处理与滤镜效果&#xff1f; 在 Vue 应用程序中&#xff0c;处理图片和应用滤镜效果是非常常见的需求。这可以让你的应用程序更加生动而丰富&#xff0c;吸引更多用户的眼球。Vue 提供了多种方式来处理图片和应用滤镜效果&#xff0c;让你可以轻松地实现这些…