Search code examples
rubyjekyll

How to embed html using plugin in Jekyll?


I have this problem I want to inject html into markdown file (blog post) but It's little bit long so I want to have plugin with parameters because it will change and I may add it multiple times. When search inject html I only found that you can insert html directly into markdown files, but I want to have one tag that will do this for me, to not duplicate the code, it will also be easier to update if Codepen decide to change the embed code.

This is the code I want to add, Codepen embed demo with button:

<div id="codepen">
  <button class="btn" onclick="document.body.classList.toggle('sticky')">Dock</button>
  <iframe height="265" scrolling="no" title="in browser sql terminal"
    src="//codepen.io/jcubic/embed/dVBaRm/?height=265&amp;theme-id=dark&amp;default-tab=result"
    frameborder="no" allowtransparency="true" allowfullscreen="true">
    See the Pen <a href='https://codepen.io/jcubic/pen/dVBaRm/'>in browser sql terminal</a> by Jakub T. Jankiewicz
    (<a href='https://codepen.io/jcubic'>@jcubic</a>) on <a href='https://codepen.io'>CodePen</a>.
  </iframe>
</div>

I want to have params username and id (maybe also title and full name), what is the easiest way to create such plugin and how to use in my markdown file?


Solution

  • You don't need a plugin to do this.

    You can use a Jekyll include.

    example_page.html

    ---
    layout: page
    title: Codepen include example
    codepen:
      author: jcubic
      name: Jakub T. Jankiewicz
      id: dVBaRm
      title: "in browser sql terminal"
    ---
    {% include codepen.html %}
    

    _includes/codepen.html

    {% if page.codepen %}
    {% assign c = page.codepen %}
    <div id="codepen">
      <button class="btn" onclick="document.body.classList.toggle('sticky')">Dock</button>
      <iframe height="265" scrolling="no" title="{{ c.title }}"
        src="//codepen.io/{{ c.author }}/embed/{{ c.id }}/?height=265&amp;theme-id=dark&amp;default-tab=result"
        frameborder="no" allowtransparency="true" allowfullscreen="true">
        See the Pen <a href='https://codepen.io/{{ c.author }}/pen/{{ c.id }}/'>in browser sql terminal</a> by {{ c.name }}
        (<a href='https://codepen.io/{{ c.author }}'>@{{ c.author }}</a>) on <a href='https://codepen.io'>CodePen</a>.
      </iframe>
    </div>
    {% endif %}
    

    You can also use a parametrized include.

    {% include codepen_param.html
      author="jcubic"
      name="Jakub T. Jankiewicz"
      id="dVBaRm"
      title="in browser sql terminal"
      %}
    

    _includes/codepen_param.html

    {% assign pen = include %}
    <div id="codepen">
      <button class="btn" onclick="document.body.classList.toggle('sticky')">Dock</button>
      <iframe height="265" scrolling="no" title="{{ pen.title }}"
        src="//codepen.io/{{ pen.author }}/embed/{{ pen.id }}/?height=265&amp;theme-id=dark&amp;default-tab=result"
        frameborder="no" allowtransparency="true" allowfullscreen="true">
        See the Pen <a href='https://codepen.io/{{ pen.author }}/pen/{{ pen.id }}/'>in browser sql terminal</a> by {{ pen.name }}
        (<a href='https://codepen.io/{{ pen.author }}'>@{{ pen.author }}</a>) on <a href='https://codepen.io'>CodePen</a>.
      </iframe>
    </div>
    

    Like this, you can call any number of pens from your page.