fix: Renders single line breaks properly on Super editor Show markdown (#2988)
This commit is contained in:
committed by
GitHub
parent
449a37a7d7
commit
ac07cea06e
@@ -30,18 +30,14 @@ import {
|
|||||||
Transformer,
|
Transformer,
|
||||||
} from '@lexical/markdown'
|
} from '@lexical/markdown'
|
||||||
|
|
||||||
import { isEmptyParagraph, TRANSFORMERS, transformersByType } from './MarkdownImportExportUtils'
|
import { TRANSFORMERS, transformersByType } from './MarkdownImportExportUtils'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Renders string from markdown. The selection is moved to the start after the operation.
|
* Renders string from markdown. The selection is moved to the start after the operation.
|
||||||
*/
|
*/
|
||||||
function createMarkdownExport(
|
function createMarkdownExport(transformers: Array<Transformer>): (node?: ElementNode) => string {
|
||||||
transformers: Array<Transformer>,
|
|
||||||
shouldPreserveNewLines: boolean = false,
|
|
||||||
): (node?: ElementNode) => string {
|
|
||||||
const byType = transformersByType(transformers)
|
const byType = transformersByType(transformers)
|
||||||
const elementTransformers = [...byType.multilineElement, ...byType.element]
|
const elementTransformers = [...byType.multilineElement, ...byType.element]
|
||||||
const isNewlineDelimited = !shouldPreserveNewLines
|
|
||||||
|
|
||||||
// Export only uses text formats that are responsible for single format
|
// Export only uses text formats that are responsible for single format
|
||||||
// e.g. it will filter out *** (bold, italic) and instead use separate ** and *
|
// e.g. it will filter out *** (bold, italic) and instead use separate ** and *
|
||||||
@@ -56,16 +52,9 @@ function createMarkdownExport(
|
|||||||
const result = exportTopLevelElements(child, elementTransformers, textFormatTransformers, byType.textMatch)
|
const result = exportTopLevelElements(child, elementTransformers, textFormatTransformers, byType.textMatch)
|
||||||
|
|
||||||
if (result != null) {
|
if (result != null) {
|
||||||
output.push(
|
output.push(result)
|
||||||
// separate consecutive group of texts with a line break: eg. ["hello", "world"] -> ["hello", "/nworld"]
|
|
||||||
isNewlineDelimited && i > 0 && !isEmptyParagraph(child) && !isEmptyParagraph(children[i - 1])
|
|
||||||
? '\n'.concat(result)
|
|
||||||
: result,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Ensure consecutive groups of texts are at least \n\n apart while each empty paragraph render as a newline.
|
|
||||||
// Eg. ["hello", "", "", "hi", "\nworld"] -> "hello\n\n\nhi\n\nworld"
|
|
||||||
return output.join('\n')
|
return output.join('\n')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -277,11 +266,7 @@ function hasFormat(node: LexicalNode | null | undefined, format: TextFormatType)
|
|||||||
return $isTextNode(node) && node.hasFormat(format)
|
return $isTextNode(node) && node.hasFormat(format)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function $convertToMarkdownString(
|
export function $convertToMarkdownString(transformers: Array<Transformer> = TRANSFORMERS, node?: ElementNode): string {
|
||||||
transformers: Array<Transformer> = TRANSFORMERS,
|
const exportMarkdown = createMarkdownExport(transformers)
|
||||||
node?: ElementNode,
|
|
||||||
shouldPreserveNewLines: boolean = false,
|
|
||||||
): string {
|
|
||||||
const exportMarkdown = createMarkdownExport(transformers, shouldPreserveNewLines)
|
|
||||||
return exportMarkdown(node)
|
return exportMarkdown(node)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user