# HTML-koppen met regeleinden -\> geen kop en inconsistentie tussen preview en gerenderde post

**URL:** https://meta.discourse.org/t/html-headings-with-line-breaks-no-heading-and-inconsistency-between-preview-and-rendered-post/261043
**Category:** Bug
**Created:** [8 april 2023 om 16:05 UTC](https://meta.discourse.org/t/html-headings-with-line-breaks-no-heading-and-inconsistency-between-preview-and-rendered-post/261043 "2023-04-08T16:05:44Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![rahim123](https://avatars.discourse-cdn.com/v4/letter/r/df705f/32.png) [@rahim123](https://meta.discourse.org/u/rahim123)
#### Post date: [8 april 2023 om 16:05 UTC](https://meta.discourse.org/t/html-headings-with-line-breaks-no-heading-and-inconsistency-between-preview-and-rendered-post/261043/1 "2023-04-08T16:05:44Z")

</div>

Hi there, I have some posts imported from another forum software with line breaks in headings. These are shown correctly in the post preview, but they are not rendered as headings in the final baked post. I’m seeing this issue on my forum updated yesterday and also here on Discourse Meta:

* * *

## 

Heading test with line breaks

Normal paragraph.

## Heading with no line breaks

* * *

 ![Screenshot from 2023-04-08 11-04-27](https://global.discourse-cdn.com/meta/original/4X/2/9/3/2933e285345227c5c30f166882183e32c04b6358.png)

It’s leaving the `<h2>` blank and is wrapping the text after that in `<p>` :

```plaintext
<h2 dir="ltr">
</h2><p dir="ltr">Heading test with line breaks</p>

```

I’m 99% sure that this is a recent regression, as I specifically checked headings in imported posts from the same user during the migration process, and the user and I both remember that they were rendered correctly.

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [11 april 2023 om 01:16 UTC](https://meta.discourse.org/t/html-headings-with-line-breaks-no-heading-and-inconsistency-between-preview-and-rendered-post/261043/3 "2023-04-11T01:16:39Z")

</div>

Hmm given the ltr stuff there @Osama could this be related to the CSS flipper we recently added?

---

<div class="post-metadata">

### Author: ![Osama](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/osama/32/98013_2.png) [@Osama](https://meta.discourse.org/u/Osama)
#### Post date: [12 april 2023 om 01:56 UTC](https://meta.discourse.org/t/html-headings-with-line-breaks-no-heading-and-inconsistency-between-preview-and-rendered-post/261043/4 "2023-04-12T01:56:27Z")

</div>

> [@sam](#):
>
> could this be related to the CSS flipper we recently added?

I don’t think so because the final/cooked HTML of the post is wrong and the CSS flipper (whether the old or the new one) isn’t involved at all in the post cooking process.

> [@rahim123](#):
>
> It’s leaving the `<h2>` blank and is wrapping the text after that in `<p>` :
> 
> ```plaintext
> <h2 dir="ltr">
> </h2><p dir="ltr">Heading test with line breaks</p>
> 
> ```

^ the text inside the `<p>` should be inside the `<h2>` and there should be no `<p>` at all (I think).

---

<div class="post-metadata">

### Author: ![selase](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/selase/32/270909_2.png) [@selase](https://meta.discourse.org/u/selase)
#### Post date: [8 mei 2023 om 18:15 UTC](https://meta.discourse.org/t/html-headings-with-line-breaks-no-heading-and-inconsistency-between-preview-and-rendered-post/261043/5 "2023-05-08T18:15:47Z")

</div>

This seems to happen during cooking, specifically markdown parsing.

> <https://github.com/discourse/discourse/blob/81aff58d09e46c01670e40e5a4e1471ea0cd3486/lib/pretty_text.rb#L302-L315>

Passing a header with linebreaks to `PrettyText.markdown` returns with the content of header wrapped in a `p` tag.

And then during scrubbing, the paragraph get’s extracted out out of the header tag resulting in the following:

> [@rahim123](#):
>
> ```plaintext
> <h2 dir="ltr">
> </h2><p dir="ltr">Heading test with line breaks</p>
> 
> ```

I suppose this happens since a paragraph in a header tag isn’t spec compliant?

I’m still reviewing the markdown parser code to figure out why the content is being wrapped in `p` tag in the first place.

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [9 mei 2023 om 00:02 UTC](https://meta.discourse.org/t/html-headings-with-line-breaks-no-heading-and-inconsistency-between-preview-and-rendered-post/261043/7 "2023-05-09T00:02:51Z")

</div>

Oh this is very related to:

> [@Markdown preview and result differ](https://meta.discourse.org/t/markdown-preview-and-result-differ/263878/4):
>
> Took a quick look out of curiosity. It looks like an issue with the Nokogiri library. thinking From what uses Discourse here: Loofah.fragment uses Nokogiri’s HTML4 parser. This could be fixed using Loofah.html5\_fragment as long as Nokogiri \>= 1.14.0 and Loofah \>= 2.21.0. Discourse already uses Nokogiri::HTML5.fragment; that would make sense. Note: Loofah [2.21.0](https://github.com/flavorjones/loofah) is not yet released; currently in RC1.

I am OK to wait a few more weeks for Loofah to release

Will shoot out a Tweet to Mike to mention this popped up.

---

<div class="post-metadata">

### Author: ![rahim123](https://avatars.discourse-cdn.com/v4/letter/r/df705f/32.png) [@rahim123](https://meta.discourse.org/u/rahim123)
#### Post date: [9 mei 2023 om 04:28 UTC](https://meta.discourse.org/t/html-headings-with-line-breaks-no-heading-and-inconsistency-between-preview-and-rendered-post/261043/8 "2023-05-09T04:28:21Z")

</div>

> [@selase](#):
>
> I suppose this happens since a paragraph in a header tag isn’t spec compliant?

Hi there, thanks for looking into this. HTML doesn’t care about line breaks, so technically this is correct:

```plaintext
<h2>

Heading test with line breaks

</h2>

```

It simply seems to be a problem of the Markdown parser running after/inside the HTML parser, which is generating `<p>` tags for the line returns.

---

<div class="post-metadata">

### Author: ![nat](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nat/32/235063_2.png) [@nat](https://meta.discourse.org/u/nat)
#### Post date: [20 juni 2023 om 12:40 UTC](https://meta.discourse.org/t/html-headings-with-line-breaks-no-heading-and-inconsistency-between-preview-and-rendered-post/261043/11 "2023-06-20T12:40:21Z")

</div>

Hey there, we’ve fixed this with

[https://github.com/discourse/discourse/pull/21522](https://github.com/discourse/discourse/pull/21522)

as seen here:

## 

Heading test with line breaks

Normal paragraph.

## Heading with no line breaks

* * *

---

<div class="post-metadata">

### Author: ![rahim123](https://avatars.discourse-cdn.com/v4/letter/r/df705f/32.png) [@rahim123](https://meta.discourse.org/u/rahim123)
#### Post date: [20 juni 2023 om 13:01 UTC](https://meta.discourse.org/t/html-headings-with-line-breaks-no-heading-and-inconsistency-between-preview-and-rendered-post/261043/12 "2023-06-20T13:01:16Z")

</div>

Glad to hear, thanks @nat! Does this require rebaking posts?

---

<div class="post-metadata">

### Author: ![nat](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nat/32/235063_2.png) [@nat](https://meta.discourse.org/u/nat)
#### Post date: [20 juni 2023 om 13:21 UTC](https://meta.discourse.org/t/html-headings-with-line-breaks-no-heading-and-inconsistency-between-preview-and-rendered-post/261043/13 "2023-06-20T13:21:14Z")

</div>

Yes, it does require a rebake — we can’t do it for you automatically with this fix since it’s an expensive operation.

---

<div class="post-metadata">

### Author: ![nat](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nat/32/235063_2.png) [@nat](https://meta.discourse.org/u/nat)
#### Post date: [22 juni 2023 om 12:40 UTC](https://meta.discourse.org/t/html-headings-with-line-breaks-no-heading-and-inconsistency-between-preview-and-rendered-post/261043/14 "2023-06-22T12:40:48Z")

</div>

This topic was automatically closed after 2 days. New replies are no longer allowed.
