You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add <br> tag to prevent empty <p></p> getting stripped
Normalize the empty block's content
exportasyncfunctionhtmlToBlockNote(editor: BlockNoteEditor,html: string): Promise<Block[]>{if(!html)return[]try{// Step 1 - Pre-process: add <br> to empty paragraphs so they're not strippedconstprocessedHtml=preserveEmptyParagraphs(html)constblocks=awaiteditor.tryParseHTMLToBlocks(processedHtml)// Step 2 - Post-process: normalize whitespace paragraphs back to emptyreturnnormalizeEmptyParagraphs(blocks)}catch(error){return[]}}functionpreserveEmptyParagraphs(html: string): string{// Replace empty <p></p> (with or without attributes) with <p...><br></p>// Also handles self-closing <p /> tagsreturnhtml.replace(/<p([^>]*)><\/p>/gi,'<p$1><br></p>').replace(/<p([^>]*)\/>/gi,'<p$1><br></p>')}constisEmptyOrWhitespaceParagraph=(block: Block): boolean=>{if(block.type!=='paragraph')returnfalse// Type guard: paragraph blocks have inline content arrayconstcontent=block.contentasArray<{type: string;text?: string}>|undefinedif(!content||content.length===0)returntrue// Check if all content items are whitespace-only textreturncontent.every((item)=>item.type==='text'&&typeofitem.text==='string'&&/^\s*$/.test(item.text))}functionnormalizeEmptyParagraphs(blocks: Block[]): Block[]{returnblocks.map((block)=>{if(isEmptyOrWhitespaceParagraph(block)){return{ ...block,content: []}asBlock}returnblock})}
Activity
aespinoza96 commentedon Aug 6, 2024
Did you try by adding like a blank space? I know it is not the optimal solution but it might be a workaround
nchanged commentedon Aug 14, 2024
that doesn't sound a look a good idea. The formatting will be inconsistent when loading new docs, or if the user just inserts a new line 2 times.
meteorSD commentedon Oct 26, 2024
Found a workaround using turndown lib :
elitan commentedon Jan 9, 2025
@meteorSD do you have a more complete example. I'm not sure I understand how to use the lib for this use case.
Ideally i want BlockNote to take markdown as input, and mardown as output (in the
onChangehandler).Would be great if you could help out here 🙏
dh4bu commentedon Jun 12, 2025
problem still persists
davehuang-tw commentedon Jan 22, 2026
My approach was to:
<br>tag to prevent empty<p></p>getting stripped