How the post content is modified before the output?

Content generated in the post editor needs some improvements before displaying on your static website. That's why Publii applies some modifications to cover most common issues and problems. Below there is a complete list of all modifications applied to the content before display in your theme.

The operations listed below are in the same order as they are done by the Publii renderer, because some operations must be processed before others.

Parse publii-amp 

On AMP version Publii just removes the <publii-amp> and </publii-amp> tags as their content is dedicated for AMP-only views. The <publii-non-amp> tags and theirs content are totally removed.

On non-AMP views there is done a reverse operation: <publii-non-amp> and </publii-non-amp> tags are removed to left their content and the <publii-amp> tags and theirs content are totally removed.

For example:

<publii-amp>This text will appear on AMP pages only</publii-amp>
<publii-non-amp>This text will appear on non-AMP pages only</publii-non-amp>

Will result in AMP views:

This text will appear on AMP pages only

And on non-AMP views will produce:

This text will appear on non-AMP pages only

Remove Table of Contents id attribute if there is no ToC

If user will add table of contents and later will remove it, the post content contains unnecessary id attributes so they should be removed from the content. Remember: this operation is performed only if table of contents not exists in the post content.

For example:

<h2 id='mcetoc_1eoa987if5'>Heading in post</h2>

Will produce:

<h2>Heading in post</h2>

Improve download attributes

Post editors usually generates the download attribute as download='download'. It is shortened just to the download.

For example:

<a href="test.pdf" download='download'>Link</a>

Is transformed into:

<a href="test.pdf" download>Link</a>

Remove contenteditable attributes

TinyMCE generates sometimes the contenteditable attributes in the output, which allows edit of the specific container content. Publii removes all occurrences of such attributes in the final output of the post content.

For example:

<figcaption contenteditable>Test caption</figcaption>

Is transformed into:

<figcaption>Test caption</figcaption>

Remove the last empty paragraph

If your post content contains at the end an empty paragraph - Publii will remove it. Usually such paragraphs are unintended, that's why they are automatically removed.

For example:

<p>Lorem ipsum dolor</p>
<p>Sit amet</p>
<p> </p>

Will produce:

<p>Lorem ipsum dolor</p>
<p>Sit amet</p>

Add srcset and sizes attributes to images

Every image in the post content is analyzed and the proper values of the srcset and sizes attributes are added to images. Thanks to this operation it is possible to support responsive images in Publii.

For example:

<img 
  width="1482" 
  height="939" 
  src="test-image.png" 
  alt="Test" 
  loading="lazy" />

Will produce (the output depends from your responsive images settings in your theme config):

<img 
  width="1482" 
  height="939" 
  src="test-image.png" 
  alt="Test" 
  loading="lazy" 
  srcset="test-image.png 1482w, 
          test-image-480x304.png 480w, 
          test-image-748x474.png 748w, 
          test-image-768x487.png 768w, 
          test-image-300x190.png 300w, 
          test-image-1024x649.png 1024w" 
  sizes="(min-width:160em) 20vw, 
         (min-width:75em) 33vw, 
         (min-width: 37.5em) 50vw, 
         480px" />

Add lazy loading

For every <img>, <video>, <audio> and <iframe> elements the loading="lazy" attribute is added automatically to improve the page speed of your static websites generated with Publii.

For example:

<img src="test.png" alt="" />
<video src="test.mp4"></video>
<audio src="test.mp3"></audio>
<iframe src="test.html"></iframe>

Will produce:

<img loading="lazy" src="test.png" alt="" />
<video loading="lazy" src="test.mp4"></video>
<audio loading="lazy" src="test.mp3"></audio>
<iframe loading="lazy" src="test.html"></iframe>

Wrap images into figure elements

For posts built with TinyMCE and Markdown editors there is performed an additional operation of wrapping the images inside paragraphs into figure elements with the same CSS classes as the image contains.

For example:

<p><img src="#" class="test-image" alt="" /></p>

Is transformed into:

<figure class="test-image">
  <img src="#" alt="" />
</figure>

Wrap galleries into additional div

For posts built with TinyMCE editor galleries are wrapped into an additional div with gallery-wrapper class:

<div class="gallery gallery-wrapper--wide">
  <!-- gallery content -->
</div>

Will produce:

<div class="gallery-wrapper gallery-wrapper-wide">
  <div class="gallery">
    <!-- gallery content -->
  </div>
</div>

Remove double slashes from gallery images URLs

Sometimes post editors creates URLs with double slashes in galleries - Publii filters these URLs to achieve a proper URL without additional slashes:

https://www.example.com/media//gallery/image.png

Will output:

https://www.example.com/media/gallery/image.png

Remove paragraphs around iframes

Sometimes the <iframe> tags are wrapped into paragraphs. These paragraphs will be automatically removed during creating post content output:

<p><iframe src="test.html"></iframe></p>

Will be transformed into:

<iframe src="test.html"></iframe>

Make iframes responsive

To make <iframe> elements responsive - they are wrapped into additional <div> element with post__iframe CSS class:

<iframe src="test.html"></iframe>

Will be transformed into:

<div class="post__iframe"><iframe src="test.html"></iframe></div>

If you want to disable this feature for some <iframe> elements please add data-responsive="false" attribute to iframes which should not be wrapped into an additional <div> element.

Remove CDATA sections

TinyMCE adds additional CDATA sections to the scripts inside the post content - they are removed in the final output:

<script><![CDATA[/*script content*/]]></script>

Will be transformed into:

<script>/*script content*/</script>

AMP-specific changes

If the AMP version of your website is enabled, for the AMP views there are additional operations performed over the post content:

Remove style attributes

All inline CSS styles attributes are removed:

<div style="text-align: center;">Centered text</div>

Will be transformed into:

<div>Centered text</div>

Prepare images

AMP requires a special syntax for images - all images are transformed as below:

<img src="test.png" alt="" />

Will be transformed into:

<amp-img src="test.png" alt="" layout="responsive"></amp-img>

Prepare videos

AMP videos requires use of the <amp-video> tag and the fallback information, so all <video> elements are transformed as below:

<video src="test.mp4"></video>

Will be transformed into:

<amp-video src="test.mp4">
  <div fallback>
    <p>Your browser doesn't support HTML5 video</p>
  </div>
</amp-video>

Prepare audio

Similarly to the video elements, also the audio elements in AMP mode requires a special syntax:

<audio src="test.mp3"></audio>

Will be transformed into:

<amp-audio src="test.mp4">
  <div fallback>
    <p>Your browser doesn't support HTML5 audio</p>
  </div>
</amp-audio>

Prepare iframes

Also <iframe> elements requires additional adjustments for use in the AMP mode:

<iframe src="test.html"></iframe>

Will output:

<amp-iframe 
  sandbox="allow-scripts allow-same-origin" 
  layout="responsive" 
  src="test.html">
</amp-iframe>