Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Prevent extra blank lines on list items #685

Merged
merged 2 commits into from Mar 6, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/serializers/markdown/plugins/paragraph.ts
Expand Up @@ -3,7 +3,8 @@ import type Turndown from 'turndown'

/**
* A Turndown plugin which adds a custom rule for paragraphs. This custom rule is required to avoid
* adding unnecessary blank lines between paragraphs to plain-text documents.
* adding unnecessary blank lines between paragraphs to plain-text documents, and to list items in
* rich-text documents.
*
* @param nodeType The node object that matches this rule.
* @param isPlainText Specifies if the schema represents a plain-text document.
Expand All @@ -12,8 +13,14 @@ function paragraph(nodeType: NodeType, isPlainText: boolean): Turndown.Plugin {
return (turndown: Turndown) => {
turndown.addRule(nodeType.name, {
filter: 'p',
replacement(content) {
return isPlainText ? `\n${content}\n` : `\n\n${content}\n\n`
replacement(content, node) {
const useSingleLineSpacing =
isPlainText ||
// Paragraphs within list items should be wrapped with a single line feed to
// maintain proper list formatting in rich-text documents.
(!isPlainText && node.parentNode?.nodeName === 'LI')

return useSingleLineSpacing ? `\n${content}\n` : `\n\n${content}\n\n`
},
})
}
Expand Down