All Articles

Github 代码片段嵌入

1. Github Gist

Github上有一个功能,可以让用户存放很短小的一个个代码片段,这就是gist。现在写技术博客很流行的一个做法就是把代码存在gist上,然后在自己的博客里嵌入对应的github gist。

奇妙的是这东西我没有招到官方的介绍,只是看到别人的博客上有这么做的,然后搜了下做法,搜到的都是三方的解决方案。当然这东西github肯定是支持的,否则三方也没什么方法能拿到对应的gist内容。

搜索靠前的几个:

2. Jekyll插件

使用Jekyll博客系统就不需要自己操心这个事情了,已经有人把这个功能的插件做出来了。见jekyll/jekyll-gist

安装:

gem install jekyll-gist
# ...
# Done installing documentation for multipart-post, faraday, sawyer, octokit, jekyll-gist after 2 seconds
# 5 gems installed

然后编辑_config.yml,添加:

plugins:
  - jekyll-gist

嵌入代码: 找到你的目标gist的编码,在URL上就有:

https://gist.github.com/agreatfool/`9894fc4159d2a1dd2c893250dc4ae0d3`

在博客中插入代码:

{% gist 9894fc4159d2a1dd2c893250dc4ae0d3 %} 记得去掉文本内的back slash转译符

就OK了,剩下的,自动生成站点的时候会自然生成。

3. 例子

EOF

Published 2018/1/18

Some tech & personal blog posts