Jekyll does not render md tables correctly if the table contains liquid tags

82 Views Asked by At

I am using a custom Liquid tag that, when building my site from .md files with Jekyll, replaces the value in the tag with a corresponding <span> element from a predefined list (similarly to how it works with custom Docbook entities).

The problem I've encountered is as follows: if this tag is in a table written with Markdown syntax, then this table will not render as such; instead, it transforms into a <p>...</p> element whose textContent is, well, the original markdown-formatted table as plain text. The <span> elements substituted by my Liquid tag are rendered correctly.

If I enclose the md table in {%raw%}{%endraw%}, then the table is rendered correctly except that, obviously, my custom tag is ignored.

Here's the Liquid tag I am using:

module Jekyll
    class EntityHandler < Liquid::Tag

        @@dictionary = {}
        file = File.open("./assets/definitions/entities.txt")
        strings = file.readlines
        strings.each do |s|
            key, value = s.split("|")
            @@dictionary[key] = value.to_s
        end

        file.close

        def initialize(name, input, tokens)
            super
            @input = input.strip
        end

        def render(context)

            dictionary = @@dictionary

            if dictionary.has_key?(@input.to_s)
                output = dictionary.fetch_values(@input)
            else
                output = "<span class='btn-false'>ERROR: entity not found</span>"
            end

            return output
        end
    end
end

Liquid::Template.register_tag('ent', Jekyll::EntityHandler)

When used anywhere in .md files, {% ent X %} renders as <span>X</span>, which is what I expect. However, it does not work with .md tables. Thus, if I make a table like this:

| Cell one | Cell two |
| === | === |
| Cell three | {% ent X %} |

it returns:

<p>| Cell one | Cell two || === | === || Cell three | <span>X</span> <!-- actual entity value --> |</p>

instead of a <table/>.

0

There are 0 best solutions below