简体   繁体   English

我该如何编写一个修改`site`对象的Jekyll插件?

[英]How can I write a Jekyll plugin that modifies the `site` object?

I want to write a Jekyll plugin that modifies the site object. 我想编写一个可以修改site对象的Jekyll插件。 The site object is available throughout the Jekyll build process. site对象在Jekyll的整个构建过程中都可用。 In Jekyll, the most direct way to add an attribute to it is to define it in the _config.yml file. 在Jekyll中,向其添加属性的最直接方法是在_config.yml文件中对其进行定义。 That works in some cases, but I need to take it a step further. 在某些情况下,这种方法可行,但是我需要更进一步。

The reason I am asking this question is because I need to do more than I can do in the context of a YAML file. 我问这个问题的原因是因为我需要做的比在YAML文件的上下文中能做的更多。 I want to add a method to the site object. 我想向site对象添加一个方法。 I assumed I'd only need to add the method to the Site class, but it isn't working. 我以为我只需要将方法添加到Site类,但它不起作用。

Non-working plugin: 不起作用的插件:

module Jekyll
  class Site
    def foo
      "foo"
    end
  end
end

I assumed from reading the Jekyll source and from how Ruby classes can be extended that the site object in Jekyll would have the new foo method, but it doesn't. 通过阅读Jekyll的源代码以及如何扩展Ruby类,我认为Jekyll中的site对象将具有新的foo方法,但事实并非如此。 For reference, here is the Site Class in the Jekyll source code . 供参考, 这是Jekyll源代码中的Site类

This is as much a question required because of my poor ability to read the RUby code that Jekyll is constructed in as it is a question about Jekyll itself. 这是一个必需的问题,因为我对Jekyll所构造的RUby代码的读取能力很差,而这是关于Jekyll本身的问题。

Maybe you can do it without a plugin. 也许您可以在没有插件的情况下进行操作。 Inspired by Jekyll Bootstrap you can craft a global variable to target your assets folder. Jekyll Bootstrap的启发,您可以制作全局变量以定位资产文件夹。

_config.yml _config.yml

# can be '' or '/repositoryName'
baseurl: /repositoryName

ASSET_PATH : /assets

_layouts/default.html _layouts / default.html中

---
front mater ...
---
{% capture ASSET_PATH %}{{ site.baseurl }}{{ ASSET_PATH }}{% endcapture %}

Then call assets from post or page : 然后从帖子或页面调用资产:

<link href="{{ ASSET_PATH }}/bootstrap/css/bootstrap.min.css" rel="stylesheet">

or

![img title]({{ ASSET_PATH }}/img/toto.jpg)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM