Why is my HTML table inside a shortcode not showing up?

Hi, I created a shortcode to add captioned tables. When I use md tables, they show up OK, but when I use HTML directly, nothing shows up.

There are no errors, even with --verbose, there is simply nothing between the figure and figcaption tags.

Here is the shortcode:

<figure>
    {{ .Inner | markdownify}}
    <figcaption><p>{{ .Get "caption" | markdownify}}</p></figcaption>
</figure>

Which works like so,

{{<table caption="Ensemble times with a Bias of 5.">}}
Ensemble Size | 10 |5 |1 |
-|-|-|-|
Time (s) |85.94 |86.01 |87.80 |
{{</table>}}

But not with,

{{<table caption="Ensemble times with a Bias of 5.">}}
<table>
<thead>
<tr>
<th>Ensemble Size</th>
<th>10</th>
<th>5</th>
<th>1</th>
</tr>
</thead>
<tbody>
<tr>
<td>Time (s)</td>
<td>85.94</td>
<td>86.01</td>
<td>87.80</td>
</tr>
</tbody>
</table>
{{</table>}}

The latter is rendered as,

<figure>
<figcaption><p>Ensemble times with a Bias of 5.</p></figcaption>
</figure>

I thought HTML could be included directly in Markdown, so don’t see why this wouldn’t work?

I’ve tried to add a <div> tag as suggested

Without minifying (which removes HTML comments by default), the HTML table is actually rendered as:

<figure>
  <!-- raw HTML omitted -->
  <figcaption><p>Ensemble times with a Bias of 5.</p></figcaption>
</figure>

Why?

  1. You are passing .Inner through markdownify, AND
  2. By default, the markdown processor (Goldmark) does not render raw HTML (see docs)

Assuming that you want to use the same shortcode for both markdown and HTML tables…

Option 1: Configure your site to render raw HTML

[markup.goldmark.renderer]
unsafe = true

Option 2: Detect type of table

<figure>
  {{ if in .Inner "<table>" }}
    {{ .Inner }}
  {{ else }}
    {{ .Inner | markdownify }}
  {{ end }}
  <figcaption><p>{{ .Get "caption" | markdownify}}</p></figcaption>
</figure>

I personally prefer option #2.

3 Likes

That (Option #2) worked great. Thank you jmooring!

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.