How can I access un-rendered (markdown) content in Jekyll with liquid tags?

前端 未结 3 415
情深已故
情深已故 2020-12-17 17:50

From reading the documentation Jekyll\'s template data one might think that the way to access un-rendered content would be page.content; but as far as I can tel

相关标签:
3条回答
  • 2020-12-17 18:33

    I ended up renaming my .md file to .html so they don't get rendered by the MarkDown renderer.

    0 讨论(0)
  • 2020-12-17 18:38

    Here's the trouble that I think you'll have: https://github.com/mojombo/jekyll/blob/master/lib/jekyll/convertible.rb https://github.com/mojombo/jekyll/blob/master/lib/jekyll/site.rb

    From my reading, for a given post/page self.content is replaced by the result of running self.content through Markdown and Liquid, at line 79 in convertible.rb:

    self.content = Liquid::Template.parse(self.content).render(payload, info)
    

    Posts are rendered before pages, seen at lines 37-44 and 197-211 in site.rb:

    def process
      self.reset
      self.read
      self.generate
      self.render
      self.cleanup
      self.write
    end
    
    ... ...
    
    def render
      payload = site_payload
      self.posts.each do |post|
        post.render(self.layouts, payload)
      end
    
      self.pages.each do |page|
        page.render(self.layouts, payload)
      end
    
      self.categories.values.map { |ps| ps.sort! { |a, b| b <=> a } }
      self.tags.values.map { |ps| ps.sort! { |a, b| b <=> a } }
    rescue Errno::ENOENT => e
      # ignore missing layout dir
    end
    

    By the time you get to rendering this page, self.content has been rendered to HTML - so it isn't a case of stopping it rendering. It's already done.

    However, Generators (https://github.com/mojombo/jekyll/wiki/Plugins) run before the render stage, so, as far as I can tell from reading the source, you should be able to fairly trivially write a generator which will duplicate self.content into some attribute (such as self.raw_content) which you can later access as raw Markdown in your templates {{ page.raw_content }}.

    0 讨论(0)
  • 2020-12-17 18:44

    This should work.

    # frozen_string_literal: true
    
    module RawContent
      class Generator < Jekyll::Generator
        def generate(site)
          site.posts.docs.each do |post|
            post.data['raw_content'] = post.content
          end
        end
      end
    end
    
    0 讨论(0)
提交回复
热议问题