<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Simon Hanukaev</title>
    <description>The latest articles on DEV Community by Simon Hanukaev (@smnh).</description>
    <link>https://dev.to/smnh</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F94739%2Fdcb85dda-59ad-417f-9814-ecd1448fdde4.JPG</url>
      <title>DEV Community: Simon Hanukaev</title>
      <link>https://dev.to/smnh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/smnh"/>
    <language>en</language>
    <item>
      <title>The Tale of Markdownify</title>
      <dc:creator>Simon Hanukaev</dc:creator>
      <pubDate>Tue, 21 Aug 2018 20:26:46 +0000</pubDate>
      <link>https://dev.to/smnh/the-tale-of-markdownify-4lcl</link>
      <guid>https://dev.to/smnh/the-tale-of-markdownify-4lcl</guid>
      <description>&lt;p&gt;While I was playing around with &lt;a href="https://gohugo.io"&gt;Hugo&lt;/a&gt; and &lt;a href="https://jekyllrb.com/"&gt;Jekyll&lt;/a&gt;, I found out that their &lt;a href="https://gohugo.io/functions/markdownify/"&gt;&lt;code&gt;markdownify&lt;/code&gt;&lt;/a&gt; filter is somewhat confusing.&lt;/p&gt;

&lt;p&gt;To describe the problem, first I want to show you how Jekyll's &lt;code&gt;markdownify&lt;/code&gt; filter works.&lt;/p&gt;

&lt;p&gt;Assume you have defined &lt;code&gt;my_text&lt;/code&gt; parameter, for example inside &lt;code&gt;_config.yml&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;my_text&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;consectetur **adipiscing** [elit](https://www.example.com)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, this variable is a string in a &lt;a href="https://daringfireball.net/projects/markdown/"&gt;markdown&lt;/a&gt; format. Now, let's try to use that variable inside a template file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight liquid"&gt;&lt;code&gt;&amp;lt;div&amp;gt;Lorem ipsum dolor sit amet, &lt;span class="p"&gt;{{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;site&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;my_text&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;markdownify&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}}&lt;/span&gt;&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, if you try to generate an HTML from this template using Jekyll, you will get something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;Lorem ipsum dolor sit amet,
  &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;consectetur &lt;span class="nt"&gt;&amp;lt;strong&amp;gt;&lt;/span&gt;adipiscing&lt;span class="nt"&gt;&amp;lt;/strong&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"https://www.example.com"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;elit&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Although this is probably not what we wanted, because the content of our variable is wrapped with &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; tag. It is totally predictable behaviour because markdown always wraps its content with block elements.&lt;/p&gt;

&lt;p&gt;But in Hugo, the behaviour is less predictable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight liquid"&gt;&lt;code&gt;&amp;lt;div&amp;gt;Lorem ipsum dolor sit amet, &lt;span class="p"&gt;{{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;Site&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;Params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;my_text&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;markdownify&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}}&lt;/span&gt;&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Results in:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;Lorem ipsum dolor sit amet, consectetur &lt;span class="nt"&gt;&amp;lt;strong&amp;gt;&lt;/span&gt;adipiscing&lt;span class="nt"&gt;&amp;lt;/strong&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"https://www.example.com"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;elit&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is actually what we wanted, but wait. What if our variable had two paragraphs?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;params&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;my_text&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;|-&lt;/span&gt;
    &lt;span class="s"&gt;first paragraph&lt;/span&gt;

    &lt;span class="s"&gt;second paragraph&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we get this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;Lorem ipsum dolor sit amet,
  &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;first paragraph&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;second paragraph&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Wait... but I thought Hugo was stripping these &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; tags. Well, not if you have more than one paragraph.&lt;/p&gt;

&lt;p&gt;But what if I really wanted to wrap a paragraph with &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt;? According to &lt;a href="https://github.com/gohugoio/hugo/pull/3786"&gt;this&lt;/a&gt; thread, you should use &lt;code&gt;blocks&lt;/code&gt; filter. But in my opinion, it is better to have some kind of &lt;code&gt;inline&lt;/code&gt; parameter to strip out &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; tags rather than stripping them by default and making &lt;code&gt;markdownify&lt;/code&gt; filter to behave incorrectly. Even if that means that we won't always get what we want. Standards are above us all :)&lt;/p&gt;

&lt;p&gt;And what about Jekyll? Well, although there were some attempts (see &lt;a href="https://github.com/jekyll/jekyll/issues/3571"&gt;this issue&lt;/a&gt; and &lt;a href="https://github.com/jekyll/jekyll/pull/5509"&gt;this pull request&lt;/a&gt;) to provide an &lt;code&gt;inline&lt;/code&gt; parameter to the &lt;code&gt;markdownify&lt;/code&gt; filter, they didn't succeed. And I didn't find any nice way to markdownify that variable without making it being wrapped with &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; tag. The only solution I found is just to set the variable to raw HTML and use it without &lt;code&gt;markdownify&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;I think the real problem here is that &lt;a href="https://daringfireball.net/projects/markdown/syntax"&gt;markdown syntax&lt;/a&gt; does not define any standard way to create inline content, that wouldn't be wrapped with a block level elements such as &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt;. But if we had something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;This will be wrapped placed inside paragraph

!!!But this &lt;span class="gs"&gt;**will not**&lt;/span&gt;!!!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That would produce:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;This will be wrapped with paragraph&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;

But this &lt;span class="nt"&gt;&amp;lt;strong&amp;gt;&lt;/span&gt;will not&lt;span class="nt"&gt;&amp;lt;/strong&amp;gt;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then it would make our lives much better.&lt;/p&gt;

&lt;p&gt;Live long and prosper 🖖&lt;/p&gt;

</description>
      <category>markdown</category>
      <category>hugo</category>
      <category>jekyll</category>
      <category>markdownify</category>
    </item>
  </channel>
</rss>
