mdBook icon indicating copy to clipboard operation
mdBook copied to clipboard

Markdown blockquote parsing issue with inline tags

Open MR-Addict opened this issue 1 year ago • 4 comments

Problem

When I upgrade to 0.4.37, some of my custom tags was broken, and I find that this was releated to mdbook blockqote parsing issue.

It will treat below right opening tag as blockquote while rending:

<button
  type="button"
  class="scroll-to-top"
  onclick="scrollToTop()"
>
  <i class="fas fa-arrow-up"></i>
</button>

And turn it into:

<p>&lt;button
type="button"
class="scroll-to-top"
onclick="scrollToTop()"</p>
<blockquote>
</blockquote>
<p><i class="fas fa-arrow-up"></i>
</button></p>

Steps

For example, I have below custom button element in my markdown:

<button
  type="button"
  class="scroll-to-top"
  onclick="scrollToTop()"
>
  <i class="fas fa-arrow-up"></i>
</button>

And turn it into:

<p>&lt;button
type="button"
class="scroll-to-top"
onclick="scrollToTop()"</p>
<blockquote>
</blockquote>
<p><i class="fas fa-arrow-up"></i>
</button></p>

Possible Solution(s)

However if I move button right opening tag > upper, it will render properly:

<button
  type="button"
  class="scroll-to-top"
  onclick="scrollToTop()" >
  <i class="fas fa-arrow-up"></i>
</button>

Notes

So I think mdbook treat button right opening tag as blockquote.

And I also find that, this issue only appers in button tag, it renders script, style, iframe, footer and div all properly, even these tags have some strange right opening tag positions.

This is okay, which > was separately put under a new line:

<script
>
  const scrollToTop = window.scroll({ top: 0, behavior: "smooth" });
</script>

And this is okay too, which > has a space with closing tag which is every similar to markdown blockquote syntax:

<iframe
  allowfullscreen
  loading="lazy"
  src="https://www.youtube.com/embed/DyTCOwB0DVw"
  allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
> </iframe>

So I think this is releated only to button and some other inline tags like span. It won't able to render below custom element properly too:

<span
> hello world</span>

Even with mdbook 0.4.36, it cannot pase it properly too.

Though above span example is strange and rarely there got any people would write raw html in markdown, but I think it's important to improve mdbook markdown parsing.

Version

0.4.37

MR-Addict avatar Feb 14 '24 11:02 MR-Addict

Thanks for the report! I believe this is working as intended. The parser tries to adhere to commonmark as closely as possible, and the reference implementation agrees here. My guess is that the precedence rules take over here, causing > to be treated as a blockquote (I believe block-structure elements take precedence over inline ones).

ehuss avatar Feb 14 '24 14:02 ehuss

With your reference, it makes sense that block-structure elements take precedence over inline ones. But in version 0.4.36, mdboook is able to correctly parse below custom html:

<button
  type="button"
  class="scroll-to-top"
  onclick="scrollToTop()"
>
  <i class="fas fa-arrow-up"></i>
</button>

So, I think there maybe some changes between 0.4.36 and 0.4.37.

MR-Addict avatar Feb 15 '24 04:02 MR-Addict

Going by the changelog, that seems expected. #2308 fixed some deviations from the spec and was included in 0.4.37

shenef avatar Feb 15 '24 08:02 shenef

I agree that keeping this behavior is acceptable. Thank you for your assistance in clarifying this matter.

MR-Addict avatar Feb 15 '24 10:02 MR-Addict