How can you access the page properties (front YAML element) in the converter plugin

I am writing a converter plugin for Jekyll and need access to some page header properties (YAML). Only the content is passed to the method of the main converter and it is not possible to access the context.

Example:

module Jekyll class UpcaseConverter < Converter safe true priority :low def matches(ext) ext =~ /^\.upcase$/i end def output_ext(ext) ".html" end def convert(content) ########### # # Its here that I need access to the content page header data # # ########### content.upcase end end end 

Any ideas on how I can access the page header data in the converter plugin?

+6
source share
2 answers

Based on the Jekyll source code, it is not possible to get the YAML source material in the converter.

I see two solutions that may work depending on your situation.

  • Your file extension may be descriptive enough to provide information that you would include in a front-end question. It looks like the Converter plugin was designed this way.

  • If the Jekyll modification is an option, you can change the Convertible.transform method to send the front file to Converter.convert. The converters that ship with Jekyll must also be modified. See this on GitHub and see if others like the idea. Here's where to start: https://github.com/mojombo/jekyll/blob/cb1a2d1818770ca5088818a73860198b8ccca27a/lib/jekyll/convertible.rb#L49

Good luck.

+2
source

devnull, I encountered a similar situation, and I decided to do it.

In the converter, I registered a pre-render hook to pull the YAML into a variable, so in the actual convert method I have access to the information I just pulled. In addition, another post_render hook is post_render to remove this piece of information, as this should be data for each record.

Side note. I found that convert would be called twice, once for use in the html <meta> , once for the actual content. The hook will only be called for the second, not the first. You may need to protect the convert function.

One more note. I think that the YAML in the converter is not unreasonable. As in pandoc , where you can specify a bibliography file in the YAML section and perform other fine-tuning, people should also be given the opportunity to configure a single message using YAML.

  def initialize(config) super(config) Jekyll::Hooks.register :posts, :pre_render do |post| if matches(post.data["ext"]) # extract per post metadata, including those in YAML @myconfig["meta"] = post.data # you may need the path to the post: post.path end end Jekyll::Hooks.register :posts, :post_render do |post| if matches(post.data["ext"]) # remove per post metadata @myconfig.delete("meta") end end end def convert(content) return content unless @myconfig["meta"] # actual conversion goes here end 
+2
source

Source: https://habr.com/ru/post/948638/


All Articles