<!--
  Refactor the HTML structure.
-->

{% assign _content = include.content %}

<!--
  In order to allow a wide table to scroll horizontally,
  we suround the markdown table with `<div class="table-wrapper">` and `</div>`
-->
{% if _content contains '<table>' %}
  {% assign _content = _content
    | replace: '<table>', '<div class="table-wrapper"><table>'
    | replace: '</table>', '</table></div>'
    | replace: '</table></div></code>', '</table></code>'
  %}
{% endif %}

<!--
  Fixed kramdown code highlight rendering:
  https://github.com/penibelst/jekyll-compress-html/issues/101
  https://github.com/penibelst/jekyll-compress-html/issues/71#issuecomment-188144901
-->
{% if _content contains '<pre class="highlight">' %}
  {% assign _content = _content
    | replace: '<div class="highlight"><pre class="highlight"><code', '<div class="highlight"><code'
    | replace: '</code></pre></div>', '</code></div>'
  %}
{% endif %}

<!-- Add attribute 'hide-bullet' to the checkbox list -->
{% if _content contains '<li class="task-list-item"><' %}
  {% assign _content = _content
    | replace: '"task-list-item"><', '"task-list-item" hide-bullet><'
  %}
{% endif %}

{% if _content contains '<img src="' %}

  {% if site.img_cdn != '' %}
    {% assign img_path_replacement = '<img src="' | append: site.img_cdn | append: '/' %}
  {% else %}
    {% assign img_path_replacement = '<img src="' | append: site.baseurl | append: '/' %}
  {% endif %}

  {% assign _content = _content | replace: '<img src="/', img_path_replacement %}

  <!-- lazy-load images <https://github.com/ApoorvSaxena/lozad.js#usage> -->
  {% assign lozad = true %}
  {% assign img_placehodler
    = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7' %}

  {% assign lozad_replacement = '<img src="'
    | append: img_placehodler
    | append: '" data-src="' %}

  {% assign _content = _content | replace: '<img src="', lozad_replacement %}

{% endif %}

<!-- return -->
{{ _content }}

{% if lozad %}
  {% include lozad.html %}
{% endif %}