我正在使用markdown 文件的YAML标题向博客帖子添加excerpt个变量,我可以在其他地方使用.在其中一篇摘录中,我通过标记链接标记引用了之前的一篇博客文章,我使用liquid template数据变量{{ site.url }}来代替网站的基本URL.

所以我有点像(稍微修剪一下)

--- 
title: "Decluttering ordination plots in vegan part 2: orditorp()"
status: publish
layout: post
published: true
tags: 
- tag1
- tag2
excerpt: In the [earlier post in this series]({{ site.url }}/2013/01/12/
decluttering-ordination-plots-in-vegan-part-1-ordilabel/ "Decluttering ordination
plots in vegan part 1: ordilabel()") I looked at the `ordilabel()` function
----

然而,jekyll和Maruku md解析器不喜欢这样,这让我怀疑不能在YAML头中使用 liquid 标记.

在jekyll处理的YAML页眉中可以使用 liquid 标记吗?

  1. 如果是,那么我在所示示例中做错了什么?
  2. If it is not allowed, who else can I achieve what I intended? I am currently developing my site on my laptop 和 don't want to hard code the base URL as it'll have to change when I am ready to deploy.

我从Maruku那里得到的错误是:

| Maruku tells you:
+---------------------------------------------------------------------------
| Must quote title
| ---------------------------------------------------------------------------
|  the [earlier post in this series]({{ site.url }}/2013/01/12/decluttering-o
| --------------------------------------|-------------------------------------
|                                       +--- Byte 40

| Maruku tells you:
+---------------------------------------------------------------------------
| Unclosed link
| ---------------------------------------------------------------------------
| the [earlier post in this series]({{ site.url }}/2013/01/12/decluttering-or
| --------------------------------------|-------------------------------------
|                                       +--- Byte 41

| Maruku tells you:
+---------------------------------------------------------------------------
| No closing ): I will not create the link for ["earlier post in this series"]
| ---------------------------------------------------------------------------
| the [earlier post in this series]({{ site.url }}/2013/01/12/decluttering-or
| --------------------------------------|-------------------------------------
|                                       +--- Byte 41

推荐答案

我不相信在YAML中嵌套 liquid 变量是可能的.至少,我还不知道怎么做.

一种有效的方法是使用Liquid's replace filter.具体来说,定义一个要用于变量替换的字符串(例如!SITE_URL!).然后,在输出过程中,使用替换过滤器将其切换到所需的Jekyll变量(例如site.url).这是一个削减.在my jekyll 0.11安装中运行正常的md文件:

---
layout: post

excerpt: In the [earlier post in this series](!SITE_URL!/2013/01/12/)

---

{{ page.excerpt | replace: '!SITE_URL!', site.url }}

在我的机器上测试,URL被正确插入,然后按预期从标记转换为HTML链接.如果要替换多个项,可以将多个替换调用串在一起.

---
layout: post

my_name: Alan W. Smith
multi_replace_test: 'Name: !PAGE_MY_NAME! - Site: [!SITE_URL!](!SITE_URL!)'

---

{{ page.multi_replace_test | replace: '!SITE_URL!', site.url | replace: '!PAGE_MY_NAME!', page.my_name }}

一个重要的注意事项是you must explicitly set the site.url的值.你和杰基尔不是免费的.您可以在_config.yml文件中使用以下选项进行设置:

url: http://alanwsmith.com

或者,当你给杰基尔打电话时,定义一下:

jekyll --url http://alanwsmith.com

Ruby相关问答推荐

如何使用 define_method 指定方法默认参数?

从 ruby​​ 脚本运行另一个 ruby​​ 脚本

如何使用 Ruby 的 optparse 解析没有名称的参数

Rubymine - 启用行号

在ruby中反转数组的顺序

rbenv 没有显示可用的 ruby​​ 版本

使用 Homebrew 安装 Ruby

Rails - RSpec - let和let!之间的区别

如何从最后一个元素开始遍历数组?

为什么整数除法在许多脚本语言中舍入?

在 Pry 中有相当于 next 的吗?

如何在 ruby​​ 中针对正则表达式测试整个字符串?

理解 Ruby 中的私有方法

删除/取消定义类方法

为什么 Ruby 的 Date 类会自动加载,但 DateTime 不会?

如何测试数组中的所有项目是否相同?

Ruby 中的 %w{} 和 %W{} 大写和小写百分比 W 数组文字有什么区别?

Ruby 中的=~运算符是什么?

默认情况下需要查找的路径是什么?

Eclipse 的首选 Ruby 插件?