How to remove empty paragraph tags generated in ckeditor content

I am using ckeditor5 in my django web app. The issue is, if the content contains any blank line, it's become an p tag and taking a default margin. As I am using tailwind css to get the default styles of the content I am using @tailwindcss/typography. I have added prose class in the content container.

I have tried to override the css classes. like,

.prose p:empty{
   margin: 0;
}

But it didn't work. So I created a django custom filter to remove the content.


from django import template
from django.utils.safestring import mark_safe
import re

register = template.Library()

@register.filter
def remove_empty_paragraphs(value):
    # Remove empty <p> tags
    cleaned_value = re.sub(r'<p[^>]*>\s*</p>', '&nbsp;', value)
    return mark_safe(cleaned_value)

I have tried with both '' blank string and &nbsp; because in the developer console, there is showing &nbsp; inside the p tag. Still didn't work.

Вернуться на верх