Get Jekyll Configuration Inside Plugin

Get Jekyll Configuration Inside Plugin

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']

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.


Another Possible Option

The "Liquid filters" section of the Jekyll Plugins Wiki Page contains the following:

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 access data files from inside a plugin

I had to use a combination of two answers:

How to pass items to my plugin:
Custom Liquid tag with two parameters

parse and access json data:
How to convert ruby formatted json string to json hash in ruby?

Get Jekyll Data Files inside Converter Plugin

Edit : As context.registers[:site] is not available in Converter plugins.
But you can use this dirty trick:

  def initialize(config)
dir = config['data_source']
filePath = File.join(dir, 'links.yml')
data = SafeYAML.load_file(filePath)
end

Jekyll: Specify whether a plugin is active in the development environment

no env variable set in `_config.yml``=> minification

env: production => minification

env: yolo or whatever or dev => no minification

You can have a development command, using a development config file that will override the default _config.yml. See command options

In a new _config_dev.yml, set :

env: dev

To serve minified : jekyll serve.

To serve unminified : jekyll serve --config _config.yml,_config_dev.yml

Can a Jekyll plugin / generator be disabled for non-production environment?

Local Plugins

For switching local plugins, you could use two different folders, and switch between them with a combination of multiple config files overwriting your plugins_dir.

You would set the a different value for plugins_dir field in the _config-dev.yml file which would overwrite your value in _config.yml (or the default _plugins if unset):

$ bundle exec jekyll build --config _config.yml,_config-dev.yml

This way you can have two folders with your development and production plugins separate. This has a maintenance cost when you use a plugin across both environments.


Gemfile Plugins

For switching gem-based plugins, you could use a different Gemfile for development without the production plugins:

BUNDLE_GEMFILE=Gemfile-dev bundle exec jekyll build

This gives you a lot of flexibility at the cost of maintaining two files. You'd want to ensure the versions of plugins across both are the same.

Alternatively, you could use an additional config file for development. You would set the a different value for plugins field in the _config-dev.yml file which would overwrite your value in _config.yml. You'd need to ensure your plugins are not set in the :jekyll_plugins group in your Gemfile for this to work (as this would shortcut the config setting):

$ bundle exec jekyll build --config _config.yml,_config-dev.yml

General Performance

If your site has a large number of posts, it's likely that your biggest time saving would be made by processing less of them at development time. You can do this with the limit_posts command line option (https://jekyllrb.com/docs/configuration/options/#build-command-options):

$ bundle exec jekyll build --limit_posts 5

For general build time improvements, I'd highly recommend profiling your site to find the best place to optimise:

$ bundle exec jekyll build --profile

Jekyll - getting an error when I have Paginate in my project

Jekyll guys have removed Paginate plugin from version 3.x as it did not play nicely with more core features. You can still enable it using any of these three options

  1. In your site source root, make a _plugins directory. Place your plugins here. Any file ending in *.rb inside this directory will be loaded before Jekyll generates your site.
  2. In your _config.yml file, add a new array with the key gems and the values of the gem names of the plugins you’d like to use. An example:


gems: [jekyll-coffeescript, jekyll-watch, jekyll-assets]
# This will require each of these gems automatically.

Then install your plugins using gem install jekyll-paginate-category jekyll-watch jekyll-assets


  1. Add the relevant plugins to a Bundler group in your Gemfile. An example:

    group :jekyll_plugins do
    gem "my-jekyll-plugin"
    gem "jekyll-paginate-category"
    end

    Now you need to install all plugins from your Bundler group by running single command bundle install

You can find more information on jekyll plugins page

jekyll plugin not working, can't find plugin

fixed..I should not add this gems:[jekyll-lunr-js-search] to_config.yml...

How do I configure GitHub to use non-supported Jekyll site plugins?

Depending if you deal with a User/Organization (UO) site or a Project site (P), do :

  1. from your working folder git init
  2. git remote add origin git@github.com:userName/userName.github.io.git (UO) or git remote add origin git@github.com:userName/repositoryName.git (P)
  3. jekyll new . creates your code base
  4. in _config.yml, set the baseurl parameter to baseurl: '' (UO) or baseurl: '/repositoryName' (P)
  5. in .gitignore add _site, it will be versioned in the other branch
  6. jekyll build will create the destination folder and build site.
  7. git checkout -b sources (UO) or git checkout master (P)
  8. git add -A
  9. git commit -m "jekyll base sources" commit your source code
  10. git push origin sources (UO) or git push origin master (P) push your sources in the appropriate branch
  11. cd _site
  12. touch .nojekyll, this file tells gh-pages that there is no need to build
  13. git init init the repository
  14. git remote add origin git@github.com:userName/userName.github.io.git (UO) or git remote add origin git@github.com:userName/repositoryName.git (P)
  15. git checkout master (UO) or git checkout -b gh-pages (P) put this repository on the appropriate branch
  16. git add -A
  17. git commit -m "jekyll first build" commit your site code
  18. git push origin master (UO) or git push origin gh-pages (P)

You now have something like Octopress does. Look at their rake file, there are some nice comments inside.

Jekyll plugin options error when trying to include options

You are supposed to have two entries for jekyll minifier in your _config.yml file :

one to enable the plugin :

plugins:
- jekyll-minifier

and one to configure your plugin :

jekyll-minifier:
preserve_php: true # Default: false
...


Related Topics



Leave a reply



Submit