Skip to content

在Octopress中使用post_url來簡化內部文章連接

Published: 3 分鐘

部落格文章常常需要連到其他內部文章,Jekyll/Octopress 提供 post_url 標籤來簡化內部文章連結的建立。由於山姆鍋在使用這個標籤時遇到一些問題,本文是為了協助那些同樣遇到問題的人避免再浪費時間。

安裝 post_url 插件

Jekyll 提供的 post_url 聽說跟 Octopress 不太相容。但我實際使用,這個問題應該已經不存在。如果您使用 Jekyll 的 post_url 內建標籤遇到問題,可以嘗試使用下列方法安裝。

  • GitHub上下載這個 post_url.rb 檔案。
  • 放到您 Octopress 的 plugins 目錄。

post_url 使用的語法如下:

[Post Name]({% post_url 2013-05-30-start-up %})

其中, ‘2013-05-30-start-up’要換成被連結文章的檔名,但不要包含副檔名(.md/.markdown, 等等)。

更換成 kramdown 剖析器(parser)

按照上述步驟後,在文章中加入 post_url 標籤,您可能會遇到這個錯誤:

Liquid Exception: can't convert nil into String in atom.xml
/Users/sam/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/time.rb:265:in `_parse'
/Users/sam/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/time.rb:265:in `parse'
/Users/sam/workspace/blog.eavatar.com/plugins/post_link.rb:11:in `initialize'
/Users/sam/workspace/blog.eavatar.com/plugins/post_link.rb:31:in `new'
.
.
.

Octopress 預設使用的 Markdown 剖析器(parser),也就是 rdiscount,才會遇到這個問題。所以,只好更換成 kramdown 這個剖析器。 在 _config.yml 檔案中,將 ‘markdown: rdistcount’ 換成 ‘markdown: kramdown’

#markdown: rdiscount
markdown: kramdown

如果您沒有裝過,使用下列指令安裝 kramdown。

$ gem install kramdown

小結

本來認為應該很簡單的工作,想不到出了一些意外。幸好,網路上許多先進不吝分享,才能快速解決問題。

參考來源

Internal links to posts

郭信義 (Sam Kuo)

奔騰網路科技技術長,專長分散式系統、Web 應用與雲端服務架構、設計、開發、部署與維運。工作之餘,喜歡關注自由軟體的發展與應用,偶爾寫一下部落格文章。