Preserving Line Breaks with @tiptap/markdown - Complete Working SolutionCommunity Extension by a Designer/Non-Developer #7209
jacob-cotten
started this conversation in
Showcase
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Description
After 17+ hours of debugging, found a working solution for blank line preservation with @tiptap/markdown v3. Includes:
Full code examples and explanation in the discussion.
URL
No response
About
Preserving Blank Lines with TipTap Markdown Extension
The Problem
When using TipTap with the Markdown extension and external markdown file storage, blank lines are not preserved correctly:
turndownlibrary adds unwanted\n\nafter every block elementThe Challenge
This happens because:
\n\nafter every block element (paragraphs, headings, lists, etc.)\n\nas just a paragraph separator, not a visual blank line<p></p>tags (which represent intentional blank lines) get lost in translationThe Solution
A three-part solution that gives you complete control over spacing:
Part 1: HTML → Markdown (Saving)
Strip all automatic spacing from Turndown, then restore only intentional blank lines.
File:
lib/markdown-helper.tsKey insight: After stripping
\n\n→\n, placeholders becomeXBLANKLINEPLACEHOLDERX\n. Removing the text leaves\n, which combines with the previous line's\nto create\n\n(a blank line).Part 2: Markdown → HTML (Loading/Pasting)
Convert markdown blank lines to empty
<p></p>tags before Marked processes them.File:
lib/markdown-helper.tsWhy HTML comments? Marked ignores them (doesn't strip them), so we can find and replace them after Marked runs.
Part 3: Paste Handler
Detect markdown content and use our conversion function to preserve spacing.
File:
components/tiptap-editor-markdown.tsxTurndown Configuration
Configure Turndown to not add extra spacing on its own:
Results
✅ No automatic spacing - Elements sit tight by default
✅ Intentional blank lines preserved - Press Enter twice = blank line
✅ Paste markdown, rich text, and regular text perfectly - Spacing preserved when pasting
✅ Load files correctly - Opening markdown files preserves blank lines
✅ Works with all elements - Paragraphs, headings, lists, code blocks
✅ Bidirectional - Both HTML→Markdown and Markdown→HTML work correctly
Example
Before: (automatic spacing everywhere)
After: (control exactly where spacing appears)
Files Changed
Only 2 files need modification:
lib/markdown-helper.ts- Both conversion functions (150 lines total)components/tiptap-editor-markdown.tsx- Paste handler (40 lines)Tech Stack
markedlibrary (Markdown → HTML)turndownlibrary (HTML → Markdown)Questions
After extensive testing (11+ hours of debugging!), this approach works reliably. Would love feedback from the community on whether there's a more elegant solution or if this pattern could help others facing similar issues.
A Note from the Author (The Meat Puppet Steering Claude Code)
I'm a Principal/Founding Product Designer, not an engineer—I just work with them constantly. I was building a custom front-end and couldn't solve this problem for the life of me. It cost maybe $50 in AI credits to finally sort through it. Shameful? Maybe. But now someone else could upload this explanation and/or files to their repository and implement the solution in ~30 seconds.
Is this perfect? Nope. You'll do an update someday and might break it. Is it still useful? Yep! Should the core team consider this? I dunno! Maybe!?
As a designer, I find it INCREDIBLY important to preserve accurate line breaks. I use markdown instead of JSON for agent artifact creation. This was the biggest obstacle for me and the thing that made TipTap fully adoptable in my use cases.
I'm using frozen versions locally anyway. Best of luck to anyone who finds this useful!
If this in the wrong place maybe someone can move it there.
Sincerely,
Some guy
Tested with:
Type
Document Editor
Other
No response
Beta Was this translation helpful? Give feedback.
All reactions