利用github hexo搭建个人博客
时间: 2023-11-17 10:56:55 浏览: 294
好的,下面是利用 Github 和 Hexo 搭建个人博客的步骤:
1. 注册 Github 账号并创建一个新的仓库,仓库名为 `username.github.io`,其中 `username` 是你的 Github 用户名。
2. 安装 Node.js 和 Git。
3. 在命令行中输入以下命令安装 Hexo:
```
npm install -g hexo-cli
```
4. 在本地创建一个新的 Hexo 博客:
```
hexo init blog
cd blog
npm install
```
5. 修改 Hexo 的配置文件 `_config.yml`,将 `deploy` 部分的 `type` 改为 `git`,并将 `repo` 改为你在第一步中创建的仓库地址:
```
deploy:
type: git
repo: git@github.com:username/username.github.io.git
branch: master
```
6. 生成静态文件并部署到 Github Pages:
```
hexo generate
hexo deploy
```
7. 打开浏览器,访问 `https://username.github.io`,就可以看到你的个人博客了。
相关问题
hexo搭建个人博客 github搜索功能
### 添加GitHub搜索功能到基于Hexo的个人博客
为了向由Hexo驱动的个人博客添加GitHub搜索功能,可以采用集成第三方服务的方式实现这一目标。一种常见方法是利用GitHub API来获取仓库信息并将其展示在博客页面上。
#### 使用JavaScript调用GitHub API
通过编写自定义JavaScript脚本,在网页加载时自动请求特定用户的公共仓库列表,并显示于前端界面中。下面是一个简单的例子:
```javascript
async function fetchRepos(username) {
const response = await fetch(`https://api.github.com/users/${username}/repos`);
const data = await response.json();
let repoListHtml = '<ul>';
for (const item of data) {
repoListHtml += `<li><a href="${item.html_url}" target="_blank">${item.name}</a></li>`;
}
repoListHtml += '</ul>';
document.getElementById('github-repos').innerHTML = repoListHtml;
}
```
此代码片段会创建一个函数`fetchRepos()`接收用户名作为参数,随后发起HTTP GET请求至GitHub RESTful Web Service接口以检索该账户下的所有公开存储库。最后遍历返回的数据对象数组构建HTML无序列表结构用于呈现链接集合[^1]。
#### 将上述逻辑嵌入Hexo模板文件内
编辑主题目录下对应的布局文件(如layout/default.ejs),找到适当位置插入如下代码段以便渲染出包含GitHub项目的区域:
```html
<div id="github-section">
<h2>My GitHub Repositories</h2>
<div id="github-repos"></div>
<!-- 调用之前定义好的JS函数 -->
<script type="text/javascript">fetchRepos('your-github-username');</script>
</div>
```
注意替换 `'your-github-username'` 成实际使用的GitHub账号名称即可完成初步配置工作[^2]。
对于更高级的需求比如支持关键词过滤等功能,则可能涉及到引入额外插件或框架辅助开发;此外还需考虑跨域资源共享(CORS)策略的影响以及API速率限制等问题[^3]。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)