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

4

14

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 tell, this is providing the content of the post as already rendered by the markdown parser.

I need a solution that accesses the raw (original markdown) content directly, rather than simply trying to convert the html back to markdown.

Background on use case

My use case is the following: I use the pandoc plugin to render markdown for my Jekyll site, using the 'mathjax' option to get pretty equations. However, mathjax requires javascript, so these do not display in the RSS feed, which I generate by looping over page.content like so:

 {% for post in site.posts %}
 <entry>
   <title>{{ post.title }}</title>
   <link href="{{ site.production_url }}{{ post.url }}"/>
   <updated>{{ post.date | date_to_xmlschema }}</updated>
   <id>{{ site.production_url }}{{ post.id }}</id>
   <content type="html">{{ post.content | xml_escape }}</content>
 </entry>
 {% endfor %}

As the xml_escape filter implies, post.content here appears in html. If I could get the raw content (imagine post.contentraw or such existed) then I could easily add a filter that would use pandoc with the "webtex" option to generate images for equations when parsing the RSS feed, e.g:

require 'pandoc-ruby'
module TextFilter
  def webtex(input)
    PandocRuby.new(input, "webtex").to_html
  end
end
Liquid::Template.register_filter(TextFilter)

But as I get content with the equations already rendered in html+mathjax instead of the raw markdown, I'm stuck. Converting back to markdown doesn't help, since it doesn't convert the mathjax (simply garbles it).

Any suggestions? Surely there's a way to call the raw markdown instead?

Chimb answered 31/10, 2012 at 13:37 Comment(1)
An entirely different way to address my use case is here: stackoverflow.com/questions/13166112, though it does not address how to access the raw markdown.Chimb
G
11

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 }}.

Gardol answered 1/11, 2012 at 20:5 Comment(7)
great explanation of what's going on here, and great suggestion. My ruby is poor (partic variable scope) -- is this a simple as writing the generator with the line: self.raw_content = self.content and then page.raw_content would become available automatically?Chimb
Close: take a look at this gist for something that will work: gist.github.com/4025507. You can then access the raw content in your layouts with {{ page.raw_content }}.Gardol
Notice that your own plugin (generator in this case) will not run on Github pages. So if you intend to use it there, you need to find an other solution.Lavenialaver
@Gardol I tried the plugin from the gist, but I get errors, it looks like the data structure has changed since 2012…Christophany
i'm sure it has! to be frank, i haven't paid any attention to really anything at all in jekyll in the past six years, so i can't really help.Gardol
anyone aware of a solution to this problem that works with github pages?Panpsychist
I am having the same problem with GitHub pages - are GitHub pages not able to have their own custom gems?Friable
P
1

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

Polliwog answered 22/3, 2019 at 15:39 Comment(0)
I
0

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
Impossibility answered 26/6, 2020 at 8:36 Comment(0)
W
0

Try using page.content!

For example,

---
layout: default
---

Markdown source:
<pre>
{{ page.content }}
</pre>

Rendered HTML
{{ content }}

See the Jekyll documentation's section on Liquid variables.

Wizardly answered 24/5, 2022 at 6:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.