在 Octopress 中使用 post_url 來簡化內部文章連接
部落格文章常常需要連到其他內部文章,Jekyll/Octopress 提供 post_url 標籤來簡化內部文章連結的建立。由於山姆鍋在使用這個標籤時遇到一些問題,本文是為了協助那些同樣遇到問題的人避免再浪費時間。
安裝 post_url 插件
Jekyll 提供的 post_url 聽說跟 Octopress 不太相容。但我實際使用,這個問題應該已經不存在。如果您使用 Jekyll 的 post_url 內建標籤遇到問題,可以嘗試使用下列方法安裝。
- 從 GitHub 上下載這個 post_url.rb 檔案。
- 放到您 Octopress 的 plugins 目錄。
post_url 使用的語法如下:
1 | [Post Name]({% post_url 2013-05-30-start-up %}) |
其中,'2013-05-30-start-up’要換成被連結文章的檔名,但不要包含副檔名 (.md/.markdown, 等等)。
更換成 kramdown 剖析器 (parser)
按照上述步驟後,在文章中加入 post_url 標籤,您可能會遇到這個錯誤:
1 | Liquid Exception: can't convert nil into String in atom.xml |
Octopress 預設使用的 Markdown 剖析器 (parser),也就是 rdiscount,才會遇到這個問題。所以,只好更換成 kramdown 這個剖析器。
在 _config.yml 檔案中,將 ‘markdown: rdistcount’ 換成 ‘markdown: kramdown’
1 | #markdown: rdiscount |
如果您沒有裝過,使用下列指令安裝 kramdown。
1 | $ gem install kramdown |
小結
本來認為應該很簡單的工作,想不到出了一些意外。幸好,網路上許多先進不吝分享,才能快速解決問題。