-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
[lexical-markdown][lexical-playground] Feature: Option to include blanklines in markdown render #6020
[lexical-markdown][lexical-playground] Feature: Option to include blanklines in markdown render #6020
Changes from all commits
767ad05
884a1ae
25559fa
3d5a481
921ec14
b815fd9
b39eb63
ff8c5c4
cf42a14
89a7b30
36fa818
d35bc85
6a42853
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,7 @@ import type { | |
TextMatchTransformer, | ||
Transformer, | ||
} from '@lexical/markdown'; | ||
import type {LexicalNode, TextNode} from 'lexical'; | ||
import type {TextNode} from 'lexical'; | ||
|
||
import {$createCodeNode} from '@lexical/code'; | ||
import {$isListItemNode, $isListNode, ListItemNode} from '@lexical/list'; | ||
|
@@ -26,14 +26,16 @@ import { | |
$getRoot, | ||
$getSelection, | ||
$isParagraphNode, | ||
$isTextNode, | ||
ElementNode, | ||
} from 'lexical'; | ||
import {IS_APPLE_WEBKIT, IS_IOS, IS_SAFARI} from 'shared/environment'; | ||
|
||
import {PUNCTUATION_OR_SPACE, transformersByType} from './utils'; | ||
import { | ||
isEmptyParagraph, | ||
PUNCTUATION_OR_SPACE, | ||
transformersByType, | ||
} from './utils'; | ||
|
||
const MARKDOWN_EMPTY_LINE_REG_EXP = /^\s{0,3}$/; | ||
const CODE_BLOCK_REG_EXP = /^[ \t]*```(\w{1,10})?\s?$/; | ||
type TextFormatTransformersIndex = Readonly<{ | ||
fullMatchRegExpByTag: Readonly<Record<string, RegExp>>; | ||
|
@@ -43,6 +45,7 @@ type TextFormatTransformersIndex = Readonly<{ | |
|
||
export function createMarkdownImport( | ||
transformers: Array<Transformer>, | ||
shouldPreserveNewLines = false, | ||
): (markdownString: string, node?: ElementNode) => void { | ||
const byType = transformersByType(transformers); | ||
const textFormatTransformersIndex = createTextFormatTransformersIndex( | ||
|
@@ -77,11 +80,16 @@ export function createMarkdownImport( | |
); | ||
} | ||
|
||
// Removing empty paragraphs as md does not really | ||
// allow empty lines and uses them as delimiter | ||
// By default, removing empty paragraphs as md does not really | ||
// allow empty lines and uses them as delimiter. | ||
// If you need empty lines set shouldPreserveNewLines = true. | ||
const children = root.getChildren(); | ||
for (const child of children) { | ||
if (isEmptyParagraph(child) && root.getChildrenSize() > 1) { | ||
if ( | ||
!shouldPreserveNewLines && | ||
isEmptyParagraph(child) && | ||
root.getChildrenSize() > 1 | ||
) { | ||
child.remove(); | ||
} | ||
} | ||
|
@@ -92,20 +100,6 @@ export function createMarkdownImport( | |
}; | ||
} | ||
|
||
function isEmptyParagraph(node: LexicalNode): boolean { | ||
if (!$isParagraphNode(node)) { | ||
return false; | ||
} | ||
|
||
const firstChild = node.getFirstChild(); | ||
return ( | ||
firstChild == null || | ||
(node.getChildrenSize() === 1 && | ||
$isTextNode(firstChild) && | ||
MARKDOWN_EMPTY_LINE_REG_EXP.test(firstChild.getTextContent())) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this intentional? I believe this regex is particularly important on top of the default Node isEmpty There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i moved this to packages/lexical-markdown/src/utils.ts for reuse |
||
); | ||
} | ||
|
||
function $importBlocks( | ||
lineText: string, | ||
rootNode: ElementNode, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,11 +13,17 @@ import type { | |
TextMatchTransformer, | ||
Transformer, | ||
} from '@lexical/markdown'; | ||
import type {ElementNode, LexicalNode, TextFormatType} from 'lexical'; | ||
|
||
import {$isCodeNode} from '@lexical/code'; | ||
import {$isListItemNode, $isListNode} from '@lexical/list'; | ||
import {$isHeadingNode, $isQuoteNode} from '@lexical/rich-text'; | ||
import { | ||
$isParagraphNode, | ||
$isTextNode, | ||
type ElementNode, | ||
type LexicalNode, | ||
type TextFormatType, | ||
} from 'lexical'; | ||
|
||
type MarkdownFormatKind = | ||
| 'noTransformation' | ||
|
@@ -429,3 +435,19 @@ export function transformersByType(transformers: Array<Transformer>): Readonly<{ | |
} | ||
|
||
export const PUNCTUATION_OR_SPACE = /[!-/:-@[-`{-~\s]/; | ||
|
||
const MARKDOWN_EMPTY_LINE_REG_EXP = /^\s{0,3}$/; | ||
|
||
export function isEmptyParagraph(node: LexicalNode): boolean { | ||
if (!$isParagraphNode(node)) { | ||
return false; | ||
} | ||
|
||
const firstChild = node.getFirstChild(); | ||
return ( | ||
firstChild == null || | ||
(node.getChildrenSize() === 1 && | ||
$isTextNode(firstChild) && | ||
MARKDOWN_EMPTY_LINE_REG_EXP.test(firstChild.getTextContent())) | ||
); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @zurfyx here it is |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think the
preserveNewLines
comment we discussed applies here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i hope i understood u correctly, its been applied in 89a7b30 ,
isNewlineDelimited
replaced withshouldPreserveNewLines
,preserveNewLines
is named asshouldPreserveNewLines
to be consistent with boolean naming conventions