Skip to content

Commit

Permalink
standardise shouldPreserveNewLines flag
Browse files Browse the repository at this point in the history
  • Loading branch information
potatowagon committed May 16, 2024
1 parent cf42a14 commit 89a7b30
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 18 deletions.
3 changes: 2 additions & 1 deletion packages/lexical-markdown/src/MarkdownExport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ import {isEmptyParagraph, transformersByType} from './utils';

export function createMarkdownExport(
transformers: Array<Transformer>,
isNewlineDelimited: boolean = true,

This comment has been minimized.

Copy link
@potatowagon

potatowagon May 17, 2024

Author Contributor

@zurfyx here, its already been applied

shouldPreserveNewLines: boolean = false,
): (node?: ElementNode) => string {
const byType = transformersByType(transformers);
const isNewlineDelimited = !shouldPreserveNewLines;

// Export only uses text formats that are responsible for single format
// e.g. it will filter out *** (bold, italic) and instead use separate ** and *
Expand Down
4 changes: 2 additions & 2 deletions packages/lexical-markdown/src/MarkdownImport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type TextFormatTransformersIndex = Readonly<{

export function createMarkdownImport(
transformers: Array<Transformer>,
shouldIncludeBlankLines = false,
shouldPreserveNewLines = false,
): (markdownString: string, node?: ElementNode) => void {
const byType = transformersByType(transformers);
const textFormatTransformersIndex = createTextFormatTransformersIndex(
Expand Down Expand Up @@ -86,7 +86,7 @@ export function createMarkdownImport(
const children = root.getChildren();
for (const child of children) {
if (
!shouldIncludeBlankLines &&
!shouldPreserveNewLines &&
isEmptyParagraph(child) &&
root.getChildrenSize() > 1
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ describe('Markdown', () => {
md: string;
skipExport?: true;
skipImport?: true;
shouldIncludeBlankLines?: true;
isNewlineDelimited?: false;
shouldPreserveNewLines?: true;
}>;

const URL = 'https://lexical.dev';
Expand Down Expand Up @@ -152,13 +151,12 @@ describe('Markdown', () => {
{
html: '<h1><span style="white-space: pre-wrap;">Hello</span></h1><p><br></p><p><br></p><p><br></p><p><b><strong style="white-space: pre-wrap;">world</strong></b><span style="white-space: pre-wrap;">!</span></p>',
md: '# Hello\n\n\n\n**world**!',
shouldIncludeBlankLines: true,
shouldPreserveNewLines: true,
},
{
html: '<h1><span style="white-space: pre-wrap;">Hello</span></h1><p><span style="white-space: pre-wrap;">hi</span></p><p><br></p><p><b><strong style="white-space: pre-wrap;">world</strong></b></p><p><br></p><p><span style="white-space: pre-wrap;">hi</span></p><blockquote><span style="white-space: pre-wrap;">hello</span><br><span style="white-space: pre-wrap;">hello</span></blockquote><p><br></p><h1><span style="white-space: pre-wrap;">hi</span></h1><p><br></p><p><span style="white-space: pre-wrap;">hi</span></p>',
isNewlineDelimited: false,
md: '# Hello\nhi\n\n**world**\n\nhi\n> hello\n> hello\n\n# hi\n\nhi',
shouldIncludeBlankLines: true,
shouldPreserveNewLines: true,
},
{
// Import only: export will use * instead of _ due to registered transformers order
Expand Down Expand Up @@ -238,7 +236,7 @@ describe('Markdown', () => {
html,
md,
skipImport,
shouldIncludeBlankLines,
shouldPreserveNewLines,
} of IMPORT_AND_EXPORT) {
if (skipImport) {
continue;
Expand All @@ -262,7 +260,7 @@ describe('Markdown', () => {
md,
[...TRANSFORMERS, HIGHLIGHT_TEXT_MATCH_IMPORT],
undefined,
shouldIncludeBlankLines,
shouldPreserveNewLines,
),
{
discrete: true,
Expand All @@ -275,7 +273,12 @@ describe('Markdown', () => {
});
}

for (const {html, md, skipExport, isNewlineDelimited} of IMPORT_AND_EXPORT) {
for (const {
html,
md,
skipExport,
shouldPreserveNewLines,
} of IMPORT_AND_EXPORT) {
if (skipExport) {
continue;
}
Expand Down Expand Up @@ -312,7 +315,7 @@ describe('Markdown', () => {
$convertToMarkdownString(
TRANSFORMERS,
undefined,
isNewlineDelimited,
shouldPreserveNewLines,
),
),
).toBe(md);
Expand Down
11 changes: 7 additions & 4 deletions packages/lexical-markdown/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,24 @@ function $convertFromMarkdownString(
markdown: string,
transformers: Array<Transformer> = TRANSFORMERS,
node?: ElementNode,
shouldIncludeBlankLines = false,
shouldPreserveNewLines = false,
): void {
const importMarkdown = createMarkdownImport(
transformers,
shouldIncludeBlankLines,
shouldPreserveNewLines,
);
return importMarkdown(markdown, node);
}

function $convertToMarkdownString(
transformers: Array<Transformer> = TRANSFORMERS,
node?: ElementNode,
isNewlineDelimited: boolean = true,
shouldPreserveNewLines: boolean = false,
): string {
const exportMarkdown = createMarkdownExport(transformers, isNewlineDelimited);
const exportMarkdown = createMarkdownExport(
transformers,
shouldPreserveNewLines,
);
return exportMarkdown(node);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,13 @@ export default function ActionsPlugin({
PLAYGROUND_TRANSFORMERS,
undefined, // node
// TODO: make a playground setting for this in lexical-playground/src/Settings.tsx
true, // include blank lines
true, // shouldPreserveNewLines
);
} else {
const markdown = $convertToMarkdownString(
PLAYGROUND_TRANSFORMERS,
undefined, //node
false, // isNewlineDelimited
true, // shouldPreserveNewLines
);
root
.clear()
Expand Down

0 comments on commit 89a7b30

Please sign in to comment.