博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
搭建jekyll博客
阅读量:6945 次
发布时间:2019-06-27

本文共 5453 字,大约阅读时间需要 18 分钟。

使用jekyll将markdown文件生成静态的html文件,并使用主题有序的进行布局,形成最终的博客页面。

特点

  • 基于ruby
  • 使用Markdown书写文章
  • 无需数据库
  • 可以使用GitHub Pages发布

安装Ruby环境

由于我的是在windows的操作,所以这里用rubyinstaller——。

我使用的版本:rubyinstaller-2.2.3-x64.exe(17.1M)。安装很简单,这里不多描述,但要注意安装的路径。这里我的是D:\Ruby22。安装目录中不要有空格或中文,可能会引发未知的错误。

安装过程中若没有添加到环境变量,需要设置下环境变量:

windows下在计算机属性->系统高级设置->环境变量,PATH中追加:
D:\Ruby22\bin\;。注意不是覆盖!

命令行下输入ruby -v 检测是否安装成功:

D:\Ruby22>ruby -vruby 2.2.3p173 (2015-08-18 revision 51636) [x64-mingw32]

出现版本号就说明安装成功了,接下来就要安装jekyll。

安装jekyll

使用gem install jekyll命令安装jekyll。会安装在D:\Ruby22\lib\ruby\gems\2.0.0\gems\目录下,并把相应的依赖文件一同安装下来。

安装错误,提示:

ERROR:  While executing gem ... (Gem::RemoteFetcher::FetchError)Errno::ECONNABORTED: An established connection was aborted by the software in your host machine. - SSL_connect (https://api.rubygems.org/specs.4.8.gz)

解决:

FetchError 明显是连接错误,使用国内的镜像源即可。
gem source -a https://ruby.taobao.org换个源,再安装。

或者直接改源配置文件:

在用户主目录下,Linux 是 ~,Windows 是 C:\Users\USERNAME (也可能是 Administrator 或 ProgramData) 下面有一个 .gemrc 文件(若没有手动新建),写入下面内容试试:

:sources:- https://ruby.taobao.org:update_sources: true

然后重新gem install jekyll:

Fetching: ffi-1.9.10-x64-mingw32.gem (100%)Successfully installed ffi-1.9.10-x64-mingw32Fetching: rb-inotify-0.9.5.gem (100%)Successfully installed rb-inotify-0.9.5Fetching: rb-fsevent-0.9.7.gem (100%)Successfully installed rb-fsevent-0.9.7Fetching: listen-3.0.5.gem (100%)Successfully installed listen-3.0.5Fetching: jekyll-watch-1.3.0.gem (100%)Successfully installed jekyll-watch-1.3.0Fetching: sass-3.4.20.gem (100%)Successfully installed sass-3.4.20Fetching: jekyll-sass-converter-1.4.0.gem (100%)Successfully installed jekyll-sass-converter-1.4.0Fetching: rouge-1.10.1.gem (100%)Successfully installed rouge-1.10.1Fetching: colorator-0.1.gem (100%)Successfully installed colorator-0.1Fetching: safe_yaml-1.0.4.gem (100%)Successfully installed safe_yaml-1.0.4Fetching: mercenary-0.3.5.gem (100%)Successfully installed mercenary-0.3.5Fetching: kramdown-1.9.0.gem (100%)Successfully installed kramdown-1.9.0Fetching: liquid-3.0.6.gem (100%)Successfully installed liquid-3.0.6Fetching: jekyll-3.0.1.gem (100%)Successfully installed jekyll-3.0.1Parsing documentation for ffi-1.9.10-x64-mingw32Installing ri documentation for ffi-1.9.10-x64-mingw32Parsing documentation for rb-inotify-0.9.5Installing ri documentation for rb-inotify-0.9.5Parsing documentation for rb-fsevent-0.9.7Installing ri documentation for rb-fsevent-0.9.7Parsing documentation for listen-3.0.5Installing ri documentation for listen-3.0.5Parsing documentation for jekyll-watch-1.3.0Installing ri documentation for jekyll-watch-1.3.0Parsing documentation for sass-3.4.20Installing ri documentation for sass-3.4.20Parsing documentation for jekyll-sass-converter-1.4.0Installing ri documentation for jekyll-sass-converter-1.4.0Parsing documentation for rouge-1.10.1Installing ri documentation for rouge-1.10.1Parsing documentation for colorator-0.1Installing ri documentation for colorator-0.1Parsing documentation for safe_yaml-1.0.4Installing ri documentation for safe_yaml-1.0.4Parsing documentation for mercenary-0.3.5Installing ri documentation for mercenary-0.3.5Parsing documentation for kramdown-1.9.0Installing ri documentation for kramdown-1.9.0Parsing documentation for liquid-3.0.6Installing ri documentation for liquid-3.0.6Parsing documentation for jekyll-3.0.1Installing ri documentation for jekyll-3.0.1Done installing documentation for ffi, rb-inotify, rb-fsevent, listen, jekyll-watch, sass, jekyll-sass-converter, rouge, colorator, safe_yaml, mercenary, kramdown, liquid, jekyll after 23 seconds14 gems installed

如果需要安装指定版本,可以使用

gem install jekyll -v 3.0.1

检测是否安装成功:

$ jekyll -vjekyll 3.0.1

ok!

如需卸载使用:

gem uninstall jekyll

新建博客

执行jekyll new xxx会在当前的目录下创建一个xxx的目录,里面就是网站的所有文件了:

jekyll new myblog

之后我们在生成的博客文件夹内执行jekyll serve --watch(不加--watch则不会检测文件夹内的变化,即修改后需要重新启动jekyll),即可以通过http://localhost:4000看到默认的页面。

cd myblogjekyll serve --watch

发布文章

jekyll的所有文章都放在_posts目录下,分类的话暂时没涉及到,有兴趣的朋友可以先去看看。只需要在此目录内新建一个文件,后缀名为markdown即可。

我们新建一个文件,名为:first-post.markdown,内容如下:

---  layout: post  title: "First post"  date: 2016-1-2 21:55:48categories: mypost  ---    >> Here is my first jekyll post    + Just for test  * Just for Test  I'm trying to write some code

注意,此文件上的日期跟实际页面显示的日期没关系,页面的日期由内容中的date来决定。至于其他值,肯定也有相应的用处,大家有兴趣就慢慢研究。

就这样,我们的第一篇文章也创建完成了。

jekyll目录结构分析

生成的目录结构如下:

1、文件夹_layouts:用于存放模板的文件夹,里面有两个模板,default.html和post.html
2、文件夹_posts:用于存放博客文章的文件夹
3、文件夹css:存放博客所用css的文件夹
4、_site:jekyll自动生成的目录,为静态的页面
5、_coinfig.yml:jekyll的配置文件,里面可以定义相当多的配置参数,具体配置参数可以参照其官网

根据实际需要,可能还需要创建如下文件或文件夹:

1、 _includes:用于存放一些固定的HTML代码段,文件为.html格式,可以在模板中通过liquid标签引入,常用来在各个模板中复用如 导航条、标签栏、侧边栏 之类的在每个页面上都一样不变的内容,需要注意的是,这个代码段也可以是未被编译的,也就是说也可以使用liquid标签放在这些代码段中
2、 image和js等自定义文件夹:用来存放一些需要的资源文件,如图片或者javascript文件,可以任意命名
3、 CNAME文件:用来在github上做域名绑定的,将在后面介绍
4、 favicon.ico:网站的小图标

更换主题

更换主题比较简单,直接将主题文件作为一个应用即可。没有专门的主题目录。也就是说下载了主题,相当于已经jekyll new my-awesome-site了。jekyll主题站::

1、Huxpro/huxpro.github.io

2、Phlow/feeling-responsive

可能出现的问题

1、提示分页错误

Deprecation: You appear to have pagination turned on, but you haven't included the `jekyll-paginate` gemguration file.

解决:只需要在_config.yml里面加一句: gems: [jekyll-paginate]就ok了。

permalink: prettygems: [jekyll-paginate]paginate: 10

提交至github pages

新建xx.github.io仓库,将_site下文件提交至仓库。原理是在本地,jekyll将markdown转化成html静态文件。

命令概览

gem install jekyll #使用ruby的gem安装器安装jekyllgem uninstall jekyll #卸载jekylljekyll new my-awesome-site #新建应用jekyll serve #启动应用(需进入应用目录)jekyll serve --watch #启动应用

转载地址:http://wlonl.baihongyu.com/

你可能感兴趣的文章
&nbsp|&quot|&amp|&lt|&gt等html字符转义
查看>>
javascript-在HTML中引入js
查看>>
VLAN基础
查看>>
JVM系列一:JVM内存组成及分配
查看>>
mysqldump导入导出mysql数据库
查看>>
搜狗五笔输入法:“分号模式”(中文下快速输入英文的另类方法)
查看>>
Mac 下安装使用 Love2D
查看>>
史上最全的机器学习资料(下)
查看>>
#SORA#celery原生配置文件研究
查看>>
python 详解re模块
查看>>
程序员之路——一个老程序员对刚上大学的学弟学妹的忠告
查看>>
oninput & onpropertychange 实现监听input的键盘事件
查看>>
famous
查看>>
PHP加密扩展 (php-beast) 1.5版本 常见问题解答
查看>>
C++ primer(笔记更新中)
查看>>
CNPM搭建私有的NPM服务
查看>>
centos6网卡自动启动设置
查看>>
Python各种流程语句
查看>>
any-enter是亮点
查看>>
Web压力测试记录
查看>>