Skip to content

Improve metadata and more generally HTML code for :empty selectors #13537

@mcanouil

Description

@mcanouil

TL;DR

  • The Pandoc/Quarto HTML template inserts empty lines inside <div class="quarto-title-meta"> when no metadata is present.
  • This causes the div to not be considered empty by CSS, so div:empty { display: none; } does not work.
  • As a result, unwanted empty divs are displayed and styling cannot be suppressed using the :empty selector.
  • This issue is unlikely limited to metadata block.

Description/Example

The HTML template for the title block metadata looks like this:

<div class="quarto-title-meta">
  $if(by-affiliation)$
  $elseif(by-author)$
  <div>
    <div class="quarto-title-meta-heading">$labels.authors$</div>
    <div class="quarto-title-meta-contents">
      $for(by-author)$
       <p>$_title-meta-author.html()$</p>
      $endfor$
    </div>
  </div>
  $endif$

  ...
</div>

When no metadata this leads to:

<div class="quarto-title-meta">

</div>

Because of the empty lines in between conditionals.

  • <div class="quarto-title-meta">
    $if(by-affiliation)$
    $elseif(by-author)$
    <div>
    <div class="quarto-title-meta-heading">$labels.authors$</div>
    <div class="quarto-title-meta-contents">
    $for(by-author)$
    <p>$_title-meta-author.html()$</p>
    $endfor$
    </div>
    </div>
    $endif$
    $if(date)$
    <div>
    <div class="quarto-title-meta-heading">$labels.published$</div>
    <div class="quarto-title-meta-contents">
    <p class="date">$date$</p>
    </div>
    </div>
    $endif$
    $if(date-modified)$
    <div>
    <div class="quarto-title-meta-heading">$labels.modified$</div>
    <div class="quarto-title-meta-contents">
    <p class="date-modified">$date-modified$</p>
    </div>
    </div>
    $endif$
    $if(doi)$
    <div>
    <div class="quarto-title-meta-heading">$labels.doi$</div>
    <div class="quarto-title-meta-contents">
    <p class="doi">
    <a href="https://doi.org/$doi$">$doi$</a>
    </p>
    </div>
    </div>
    $endif$
    </div>

Problem

  • The div is not empty in CSS parlance due to whitespace/empty lines.
  • Thus, you cannot hide it with:
div:empty {
  display: none;
}
  • If you add styling to div.quarto-title-meta, it gets displayed every time, even with no metadata.
  • You also get empty lines added.

Proposed Fix

One way to fix this is to remove the empty lines in between metadata in the template.

Workarounds

For now, I’m doing the cleaning in JS:

<script>
  document.querySelectorAll('header#title-block-header div, header#title-block-header p').forEach(function(el) {
    if (!el.textContent.trim()) {
      el.style.display = "none";
      el.textContent = "";
    }
  });
</script>

References

Discussion

  • Some users have experienced “empty” divs being added regardless of template modifications.
  • Removing empty lines makes :empty work.
  • "We arguably should be doing this in our HTML postprocessor."

Metadata

Metadata

Assignees

Labels

enhancementNew feature or requesthtmlIssues with HTML and related web technology (html/css/scss/js)

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions