Plugin help: Markdown in string processed by plugin not being rendered

Surprised by this:

Basically, I gsub ==text== into <mark>text</text> which works fine but if text contains any other markdown, like a link or bold or whatever, that markdown doesn’t get converted into HTML downstream and is rendered to the page as raw markdown.

Perhaps I am invoking the wrong hook or something?

tia for any insights!

So read up a bit and seocnd attempt probably a better approach but the same problem persists: gsub does its thing, but any markdown contained in between does not get rendered as HTML afterwards (even though we are firing this “pre_render”).

Markdown in “\1” is left intact pre_render, why isn’t Kramdown converting it?

kramdown will only convert documents with a Markdown extension (markdown,mkdown,mkdn,mkd,md). If you put Markdown text inside an HTML file, it’ll be ignored. To ensure Markdown conversion in an HTML file, you’ll need to pass the text through the liquid filter markdownify or have your plugin invoke kramdown converter on the gsubbed content.

thank you @ashmaroli.
my files are all markdown.
this is a plugin attempting to parse the content “as it passes through Jekyll”…

Experiment with a standalone page and see if kramdown gives the desired result:

// ./test.md

<mark>[hello](/hello/)</mark>

If the contents are not converted, then try again by using the markdown attribute:

// ./test.md

<mark markdown="1">[hello](/hello/)</mark>

@ashmaroli thank you.
Do you think I would be here asking for advice if I hadn’t been test running the code? :slight_smile:

The test content is as follow:
// ./test. md
==this **should** be highlighted==

expected result is
<mark>this <strong>should</strong> be highlighted</mark>

with
Jekyll::Hooks.register :pages, :pre_render do |page|
the actual result is
<mark>this **should** be highlighted</mark>

with
Jekyll::Hooks.register :pages, :post_render do |page|
the result is
==this <strong>should</strong> be highlighted==

As you can see, I can’t seem to win here…

Instead of using the markdown="1" attribute I suggested previously, you can use the attribute markdown="span"
The reference documentation is at:
https://kramdown.gettalong.org/syntax.html#html-blocks

Therefore, your generator would now look like the following:

class HighlightsGenerator < Jekyll::Generator
  def generate(site)

    all_docs = site.documents

    # Converts Markdown-plus ==text== to  <mark>text</mark>
    all_docs.each do |current_note|
      current_note.content = current_note.content.gsub(
        /\=\=(.*)\=\=/i,
        '<mark markdown="span">\1</mark>'
      )
    end
  end
end
1 Like

@ashmaroli !!! That worked!
I didn’t catch the suggestion the first time to add markdown="1" (which now that I tried it didn’t work) but yes markdown="span" totally does the trick.

If I understand correctly, my plugin is generating HTML… and kramdown just ignores stuff that is between html tags? Unless one sets the markdown="span" flag explicitly? Ok. Hunh. Dunno if I would have ever caught that on my own.

Thanks a million!

for posterity, final Gist for this is here:

Yes, that is correct.
And it is not always markdown="span". According to the documentation I linked to previously, even markdown="block" may be used based on the use-case.

1 Like