在插件中获取Jekyll配置 [英] Get Jekyll Configuration Inside Plugin
问题描述
我想对Jekyll进行更改仅First Paragraph插件,以生成更多内容". '链接一个可配置的选项.
I'd like to make changes to the Jekyll Only First Paragraph plugin to make the generation of a 'read more ' link a configurable option.
要执行此操作,我需要能够访问插件的AssetFilter
中的Jekyll站点配置.使用可用的配置,我可以进行更改.我不知道如何使站点配置可用于该插件.
To do this I'd need to be able to access the Jekyll site config inside the plugin's AssetFilter
. With the configuration available I can make the change. I don't know how to make the site configuration available to the plugin.
下面的代码演示了我希望在哪里使用site.config
:
The code below demonstrates where I'd like site.config
available:
require 'nokogiri'
module Jekyll
module AssetFilter
def only_first_p(post)
# site.config needs to be available here to modify the output based on the configuration
output = "<p>"
output << Nokogiri::HTML(post["content"]).at_css("p").inner_html
output << %{</p><a class="readmore" href="#{post["url"]}">Read more</a>}
output
end
end
end
Liquid::Template.register_filter(Jekyll::AssetFilter)
能做到吗?
Can this be achieved?
推荐答案
概述
您可以使用以下命令访问插件中的Jekyll配置选项:
Overview
You can access Jekyll config options in plugins with:
Jekyll.configuration({})['KEY_NAME']
如果配置键包含嵌套级别,则格式为:
If the config key contains nested levels, the format is:
Jekyll.configuration({})['KEY_LEVEL_1']['KEY_LEVEL_2']
示例
如果_config.yml包含:
Example
If a _config.yml contains:
testvar: new value
custom_root:
second_level: sub level data
简单输出这些值的基本示例如下:
A basic example that simply outputs those values would look like:
require 'nokogiri'
module Jekyll
module AssetFilter
def only_first_p(post)
@c_value = Jekyll.configuration({})['testvar']
@c_value_nested = Jekyll.configuration({})['custom_root']['second_level']
output = "<p>"
### Confirm you got the config values
output << "<br />"
output << "c_value: " + @c_value + "<br />"
output << "c_value_nested: " + @c_value_nested + "<br />"
output << "<br />"
###
output << Nokogiri::HTML(post["content"]).at_css("p").inner_html
output << %{</p><a class="readmore" href="#{post["url"]}">Read more</a>}
output
end
end
end
Liquid::Template.register_filter(Jekyll::AssetFilter)
当然,您想要在尝试使用配置键/值之前进行检查,以确保配置键/值已定义.留给读者练习.
Of course, you would want to put checks in that verify that the config key/values are defined before trying to use them. That's left as an exercise for the reader.
Jekyll插件Wiki页面的液体过滤器"部分包含以下内容:
The "Liquid filters" section of the Jekyll Plugins Wiki Page contains the following:
在Jekyll中,您可以通过寄存器访问站点对象.例如,您可以像这样访问全局配置(_config.yml):@ context.registers [:site] .config ['cdn'].
In Jekyll you can access the site object through registers. As an example, you can access the global configuration (_config.yml) like this: @context.registers[:site].config['cdn'].
我没有花时间让它开始工作,但是也许也值得一试.
I haven't spent the time to get that to work, but it might be worth checking out as well.
这篇关于在插件中获取Jekyll配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!