Search code examples
rubyyamljekyllhook

How to read values from _config.yml file in Jekyll hook


I am trying to read specific part of _config.yml in my Jekyll hook method something like this:

Jekyll::Hooks.register :site, :after_init do
    lm = Jekyll.config("latex-macros")
end

in _config.yml is:

latex-macros:
  - ["\\RR", "\\mathbb{R}"]

so in lm variable should be:

[["\\RR", "\\mathbb{R}"]]

I already tried to to use Jekyll.configuration({})["latex-macros"] and it kinda worked but it ignores --config terminal option and reads file everytime it is called. This makes it unusable for me.


I also tried

Jekyll::Hooks.register :site, :after_init do
    lm = context.registers[:site].config["latex-macros"]
end

but it throws run time error:

katex.rb:8:in '<top (required)>': undefined local variable or method 'context' for main:Object (NameError)


My question is, how to read _config.yml values in jekyll hook properly? How do I fix second method?

Thank you for your help


Solution

  • I am writing this from top of my head since it's been a long time I used Jekyll. You need to pass site variable into hook.

    Jekyll::Hooks.register :site, :after_init do |site|
      # Access using site.config[key]
      puts site.config['latex-macros']
    end