看过不少人,用github
建立我的博客,最近抽空也实现的本身的博客,下面就把摸索过程记录下。css
Node.js下载地址:https://nodejs.org/en/download/html
安装过程一路默认安装便可。前端
详细安装文档参看:http://www.runoob.com/nodejs/...node
Git软件下载地址:https://git-scm.com/downloadgit
安装过程一路默认安装便可。github
关于更多的Git讲解参看:chrome
https://www.liaoxuefeng.com/w...npm
什么是 Hexo?Hexo 是一个快速、简洁且高效的博客框架。Hexo 使用Markdown
(或其余渲染引擎)解析文章,在几秒内,便可利用靓丽的主题生成静态网页。json
Hexo官方网站:https://hexo.io/zh-cn/ ,我用的当前版本是v6.4.0,基本步骤:后端
npm install -g hexo-cli
全局安装hexo;hexo -v
显示hexo的相关信息说明安装成功;hexo init
初始化hexo项目,安装相关依赖:hexo s
或hexo server
,开启服务,成功后,在浏览器访问http://localhost:4000
生成的默认主题博客。PS:在这里能够npm install hexo-browsersync --save
实现热更新,没必要每次更改都去刷新浏览器。安装完成后的目录结构
- node_modules // 依赖包 - public // 存放生成的页面 - scaffolds // 生成页模板 - source // 建立的源文章 - themes // 主题 - _config.yml // 博客配置(站点配置) - db.json // 解析source获得的库文件 - package.json // 项目依赖配置
_config.yml
文件是对整个站点基本信息的配置,好比:
# Site title: // 博客名称 subtitle: // 副标题 description: // 描述 用于seo keywords: // 关键字 用于seo author: // 做者 用于seo language: // 语言 timezone: // 时区
和建立其它git仓库同样,只不过名称必须为yourname.github.io
这种形式,其中yourname
是你github我的帐号名,建立好后,找到站点配置文件(blog下的_config.yml
文件),找到:
# Deployment ## Docs: https://hexo.io/docs/deployment.html deploy: type:
改为你的 博客git仓库地址和分支:
deploy: type: git repo: https://github.com/YourgithubName/YourgithubName.github.io.git branch: master
这样,远程仓库配置完成。接下来
hexo generate
或hexo g
将原markedown
文章生成静态页面,放置在生成的public
目录下;npm install hexo-deployer-git --save
安装hexo
的git
插件;hexo deploy
或hexo d
将生成的静态页面推送到远程仓库;https://yourname.github.io/
,就能看到你构建好的我的博客啦!hexo
支持用markdown
写做,在目录blog/source/_posts
新建markdown
文件,或者使用命令hexo new posts 你的文章标题
。
小坑:{{}}符号编译出错
markdown生成静态页面,{{}}是swig模板符号,属于特殊字符,在编译时解析不了双大括号中间字符串就会报错
FATAL Something's wrong. Maybe you can find the solution here: http://hexo.io/docs/troubleshooting.html Template render error: (unknown path) [Line 3, Column 8] unexpected token: }}
解决方案:用转义字符代替
{ -> { — 大括号左边部分Left curly brace } -> } — 大括号右边部分Right curly brace
hexo
默认主题是landscape
,样式可能不是每一个人都喜好的,官方页提供了一些主题,能够按本身的风格安装,只需在站点配置文件_config.yml
更改主题名称。
Next
主题是目前比较流行的主题,文档相对比较成熟。next主题文档
安装
cd blog git clone https://github.com/theme-next/hexo-theme-next themes/next
更换主题
# Extensions ## Plugins: https://hexo.io/plugins/ ## Themes: https://hexo.io/themes/ theme: next
目录blog/themes/next
找到_config.yml
文件,其中有不少配置项,下面列举几个经常使用的。
# Sidebar Avatar avatar: # in theme directory(source/images): /images/avatar.gif # in site directory(source/uploads): /uploads/avatar.gif # You can also use other linking images. url: /images/avatar.png # If true, the avatar would be dispalyed in circle. rounded: true # The value of opacity should be choose from 0 to 1 to set the opacity of the avatar. opacity: 1 # If true, the avatar would be rotated with the cursor. rotated: false
只需将头像的url换成你本身头像的url或者直接覆盖头像图片便可。
注意这里的根/
的绝对路径是blog/themes/next/source/
,之后写文章引用本地图片的话,注意放到这个目录下!
NexT
使用Tomorrow Theme做为代码高亮,共有5款主题供你选择。 NexT
默认使用的是白色的normal
主题,可选的值有normal
,night
,night blue
, night bright
,night eighties
。
# Code Highlight theme # Available values: normal | night | night eighties | night blue | night bright # https://github.com/chriskempson/tomorrow-theme highlight_theme: normal
文章可能须要分类,添加分类页
cd blog hexo new page categories
此时在blog/source
目录下就生成了categories/index.md
文件:
--- title: 分类 date: 2018-08-28 14:59:48 type: categories comments: false // 分类页不须要添加评论 ---
而后,放开主题配置文件_config.yml
中menu setting
对categories
注释
menu: home: / || home categories: /categories/ || th
之后文章的内容头声明的分类就会在分类页有索引了。
跟添加分类页同样,文章也须要标签
cd blog hexo new page tags
此时在blog/source
目录下就生成了tags/index.md
文件:
--- title: 标签 date: 2018-08-28 14:34:22 type: tags comments: false // 标签页不须要评论 ---
而后,放开主题配置文件_config.yml
中menu setting
对tags
注释
menu: home: / || home tags: /tags/ || tags
之后文章的内容头声明的标签就会在标签页有对应索引了。
当访问当前站点,页面找不到,跳转到404页,推荐用腾讯公益404页面,寻找丢失儿童,让你们一块儿关注此项公益事业!使用方法,新建404.html页面,放到主题的source
目录下,内容以下:
<!DOCTYPE HTML> <html> <head> <meta http-equiv="content-type" content="text/html;charset=utf-8;"/> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" /> <meta name="robots" content="all" /> <meta name="robots" content="index,follow"/> <link rel="stylesheet" type="text/css" href="https://qzone.qq.com/gy/404/style/404style.css"> </head> <body> <script type="text/plain" src="http://www.qq.com/404/search_children.js" charset="utf-8" homePageUrl="/" homePageName="回到个人主页"> </script> <script src="https://qzone.qq.com/gy/404/data.js" charset="utf-8"></script> <script src="https://qzone.qq.com/gy/404/page.js" charset="utf-8"></script> </body> </html>
对于我的站点,咱们须要统计用户访问量,用户分布,跳转率等。Next主题推荐使用的有百度统计、Google分析、腾讯分析了,使用均同样,注册获取站点统计id。
我一直用的百度统计,注册百度统计,管理
> 网站列表
> 新增网站
完成后,代码管理
> 代码获取
,就能获得统计id。
# Baidu Analytics ID baidu_analytics: 统计id
不蒜子统计能够统计站点以及每一个页面的PV
、UV
和站点总的访问数,以小眼睛的形式展示。
编辑主题配置文件中的busuanzi_count
的配置项。当enable: true
时,表明开启全局开关。若total_visitors
、total_views
、post_views
的值均为false时,不蒜子仅做记录而不会在页面上显示。
Next主题还提供了对外提供分享接口,包括百度分享、addthis Share和NeedMoreShare2,要用到哪一个,只需把相应enable: true
,注册帐号获取id便可。
当前版本配置,支持畅言,Disqus,valine,gitment。
App ID
和App Key
便可使用。GitHub Issues
的评论系统。支持在前端直接引入,不须要任何后端代码。能够在页面进行登陆、查看、评论、点赞等操做,同时有完整的Markdown / GFM
和代码高亮支持。尤其适合各类基于GitHub Pages
的静态博客或项目页面。畅言要备案,对于挂靠在GitHub
的博客很是的不友好,放弃!Disqus
,目前国内网络环境访问不了,放弃!valine在用户没有认证登陆能够评论,不能防止恶意注水,放弃!我选择用Gitment
,让用户用本身的GitHub
帐号才能评论,用git的一个仓库来存储评论(评论以该仓库的issue
形式展示)。
Gitment
的所有配置项以下,
# Gitment # Introduction: https://imsun.net/posts/gitment-introduction/ gitment: enable: true mint: true # RECOMMEND, A mint on Gitment, to support count, language and proxy_gateway count: true # Show comments count in post meta area lazy: false # Comments lazy loading with a button cleanly: true # Hide 'Powered by ...' on footer, and more language: zh-CN # Force language, or auto switch by theme github_user: xxx # MUST HAVE, Your Github Username github_repo: xxx # MUST HAVE, The name of the repo you use to store Gitment comments client_id: xxx # MUST HAVE, Github client id for the Gitment client_secret: xxx # EITHER this or proxy_gateway, Github access secret token for the Gitment proxy_gateway: # Address of api proxy, See: https://github.com/aimingoo/intersect redirect_protocol: # Protocol of redirect_uri with force_redirect_protocol when mint enabled
开启enable
为true
就显示评论框了,不过要真正实现评论可用,须要
用本身的github
帐号注册一个应用许可证书,即OAuth application
,注册成功就生成了client_id
和client_secret
。
步骤:你的github首页
> settings
> Developer settings
> OAuth Apps
> New oAuth App
。填写好相关信息,其中,Homepage URL
和Authorization callback URL
都写上你的github
博客首页地址,好比https://xxx.github.io/
,点击Register application
便可完成注册,生成Client ID
和Client Secret
。
这样,用户点击评论框右边登入跳转到github
受权,容许受权跳转回来就能够评论啦!
小坑:有些文章评论初始化弹出validation failed错误
由于GitHub
的每一个issues
有两个lables
,一个是gitment
,另外一个是id
,id
取的是页面pathname
,标签长度限定不超过50个字符,而像通常中文标题文章,转义后字符很容易超过50个字符。
目录blog/themes/next/layout/_third-party/comments
找到文件gitment.swig
,
在这里我用文章建立时间戳来看成id
,这样长度就不会超过50个字符,成功解决!
构建本身的博客并不难,也不须要什么专业代码基础,须要的是耐心而已(┭┮﹏┭┮都是配置)。
PS:个人GitHub博客https://wuwhs.github.io 大佬拍轻点