Who's Studio.

Octopress的个性化设置

Word count: 796Reading time: 3 min
2016/05/07

写在前面

本文主要记录一些关于octopress的个性化配置方法。

个性化配置

1. 增加Categories列表

首先在plugins目录下添加category_list_tag.rb文件,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
module Jekyll 
class CategoryListTag < Liquid::Tag
def render(context)
html = ""
categories = context.registers[:site].categories.keys
categories.sort.each do |category|
posts_in_category = context.registers[:site].categories[category].size
category_dir = context.registers[:site].config['category_dir']
category_url = File.join(category_dir, category.gsub(/_|\P{Word}/, '-').gsub(/-{2,}/, '-').downcase)
html << "<li class='category'><a href='/#{category_url}/'>#{category} (#{posts_in_category})</a></li>\n"
end
html
end
end
end

Liquid::Template.register_tag('category_list', Jekyll::CategoryListTag)
然后在\source\_includes\asides目录(或\source\_includes\custom\asides目录)下添加category_list.html文件,代码如下:
1
2
3
4
5
6
<section>
<h1>Categories</h1>
<ul id="categories">
{% raw %}{% category_list %}{% endraw %}
</ul>
</section>
最后在_config.ymldefault_asides:一栏添加asides/category_list.html(或custom/asides/category_list.html)即可。
注意:上述步骤有一个问题,那就是如果category包含中文的话生成的侧边栏链接会是404页面(因为链接中不能包含中文),需要将category_list_tag.rb文件中第11行代码里的category_url改成category.to_url.downcase,该代码会将中文转换成拼音。
另外,上面代码中第4行{% category_list %}若想在网页上正确显示必须使用{% RAW %}your_content{% ENDRAW %}(将大写改成小写)。这是markdown里强制显示原始字符串而不进行解释的语法

2. 首页文章缺省显示

只需要在每篇你写的markdown格式的博文中在你想要在首页显示的文字后面加上<!--more-->即可。

3. 设置链接默认在新窗口打开

\source\_includes\custom\head.html文件中添加如下代码:

1
2
3
4
5
6
7
8
9
10
11
<script>
function addBlankTargetForLinks () {
$('a[href^="http"]').each(function(){
$(this).attr('target', '_blank');
});
}

$(document).bind('DOMNodeInserted', function(event) {
addBlankTargetForLinks();
});
</script>

4. 在首页添加选项卡

首先运行rake new_page['about'],在source目录下会新生成一个about目录,里面包含一个index.markdown文件。编辑该文件,然后运行rake generate将会在public目录下生成一个对应的index.html文件。然后编辑\source\_includes\custom\navigation.html文件,即可将生成的about页面链接添加到首页选项卡。

5. 添加disqus评论功能

只需要编辑_config.yml中相应部分即可。具体操作为在_config.yml文件中对应位置填写自己的账号。

6. 添加访客计数功能

我采用了Flag Counter,直接去官网生成html代码,然后拷贝到source\_includes\asides\flag_counter.html文件(需新建),最后在_config.ymldefault_asides:一栏添加asides/flag_counter.html即可。

7. 优化加载速度

由于国内墙的原因,请求墙外网站资源的速度极慢,因此需要删除某些用到国外网站资源的功能。具体如下:
1. 将\source\_includes\head.html文件中的google的jquery脚本地址改成百度的。
2. 将\source\_includes\custom\head.html文件中的google font请求注释掉。
3. 将_config.yml文件中的twitter内容注释掉。

参考文献

Octopress博客的个性化配置
Octopress 精益修改 (5)

CATALOG
  1. 1. 写在前面
  2. 2. 个性化配置
    1. 2.1. 1. 增加Categories列表
    2. 2.2. 2. 首页文章缺省显示
    3. 2.3. 3. 设置链接默认在新窗口打开
    4. 2.4. 4. 在首页添加选项卡
    5. 2.5. 5. 添加disqus评论功能
    6. 2.6. 6. 添加访客计数功能
    7. 2.7. 7. 优化加载速度
  3. 3. 参考文献