Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,40 @@ export function createMarkdownBlockGroup(
}
break;
case 'FormatContainer':
markdownString += createMarkdownBlockQuote(blockGroup, newLinePattern, listCounter);
if (blockGroup.tagName == 'blockquote') {
markdownString += createMarkdownBlockQuote(blockGroup, newLinePattern, listCounter);
} else {
markdownString += createDefaultMarkdownString(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just use existing function createMarkdownBlockGroup recursively?

blockGroup,
newLinePattern,
listCounter
);
}
break;
default:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although it logically works, but it can't pass lint check.

Also would you please add a test case, thanks

const { blocks } = blockGroup;
for (const block of blocks) {
markdownString += createMarkdownBlock(block, newLinePattern, listCounter);
}
markdownString += createDefaultMarkdownString(blockGroup, newLinePattern, listCounter);
break;
}
return markdownString;
}

function createDefaultMarkdownString(
blockGroup: ContentModelBlockGroup,
newLinePattern: MarkdownLineBreaks,
listCounter: ListCounter
): string {
let markdownString = '';
const { blocks } = blockGroup;
for (const block of blocks) {
markdownString += createMarkdownBlock(block, newLinePattern, listCounter, {
table: newLinePattern.newLine,
paragraph: newLinePattern.lineBreak,
divider: newLinePattern.lineBreak,
});
}
return markdownString;
}

function createMarkdownListItem(
listItem: ContentModelListItem,
newLinePattern: MarkdownLineBreaks,
Expand Down Expand Up @@ -92,17 +114,14 @@ function createMarkdownBlockQuote(
listCounter: ListCounter
): string {
let markdownString = '';
if (blockquote.tagName == 'blockquote') {
const { blocks } = blockquote;
for (const block of blocks) {
markdownString +=
'> ' +
createMarkdownBlock(block, newLinePattern, listCounter, undefined /* newLines */, {
ignoreLineBreaks: true,
}) +
newLinePattern.newLine;
}
const { blocks } = blockquote;
for (const block of blocks) {
markdownString +=
'> ' +
createMarkdownBlock(block, newLinePattern, listCounter, undefined /* newLines */, {
ignoreLineBreaks: true,
}) +
newLinePattern.newLine;
}

return `${markdownString}\n`;
}
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,142 @@ describe('createMarkdownBlockGroup', () => {
subListItemCount: 0,
});
});

describe('FormatContainer tests', () => {
it('should handle FormatContainer with blockquote tagName', () => {
const blockGroup = createFormatContainer('blockquote');
const paragraph = createParagraph();
const text = createText('This is a blockquote');
paragraph.segments.push(text);
blockGroup.blocks.push(paragraph);

runTest(blockGroup, `> This is a blockquote\n\n`, {
listItemCount: 0,
subListItemCount: 0,
});
});

it('should handle FormatContainer with blockquote tagName and multiple paragraphs', () => {
const blockGroup = createFormatContainer('blockquote');

const paragraph1 = createParagraph();
const text1 = createText('First paragraph');
paragraph1.segments.push(text1);
blockGroup.blocks.push(paragraph1);

const paragraph2 = createParagraph();
const text2 = createText('Second paragraph');
paragraph2.segments.push(text2);
blockGroup.blocks.push(paragraph2);

runTest(blockGroup, `> First paragraph\n> Second paragraph\n\n`, {
listItemCount: 0,
subListItemCount: 0,
});
});

it('should handle FormatContainer with div tagName (non-blockquote)', () => {
const blockGroup = createFormatContainer('div');
const paragraph = createParagraph();
const text = createText('This is a div content');
paragraph.segments.push(text);
blockGroup.blocks.push(paragraph);

runTest(blockGroup, `This is a div content\n\n`, {
listItemCount: 0,
subListItemCount: 0,
});
});

it('should handle FormatContainer with section tagName (non-blockquote)', () => {
const blockGroup = createFormatContainer('section');
const paragraph = createParagraph();
const text = createText('Section content');
paragraph.segments.push(text);
blockGroup.blocks.push(paragraph);

runTest(blockGroup, `Section content\n\n`, {
listItemCount: 0,
subListItemCount: 0,
});
});

it('should handle FormatContainer with article tagName (non-blockquote)', () => {
const blockGroup = createFormatContainer('article');
const paragraph = createParagraph();
const text = createText('Article content');
paragraph.segments.push(text);
blockGroup.blocks.push(paragraph);

runTest(blockGroup, `Article content\n\n`, {
listItemCount: 0,
subListItemCount: 0,
});
});

it('should handle FormatContainer with non-blockquote tagName and multiple paragraphs', () => {
const blockGroup = createFormatContainer('div');

const paragraph1 = createParagraph();
const text1 = createText('First div paragraph');
paragraph1.segments.push(text1);
blockGroup.blocks.push(paragraph1);

const paragraph2 = createParagraph();
const text2 = createText('Second div paragraph');
paragraph2.segments.push(text2);
blockGroup.blocks.push(paragraph2);

runTest(blockGroup, `First div paragraph\n\nSecond div paragraph\n\n`, {
listItemCount: 0,
subListItemCount: 0,
});
});

it('should handle FormatContainer with blockquote and formatted text', () => {
const blockGroup = createFormatContainer('blockquote');
const paragraph = createParagraph();

const boldText = createText('Bold ');
boldText.format.fontWeight = 'bold';

const italicText = createText('italic ');
italicText.format.italic = true;

const normalText = createText('and normal text');

paragraph.segments.push(boldText);
paragraph.segments.push(italicText);
paragraph.segments.push(normalText);
blockGroup.blocks.push(paragraph);

runTest(blockGroup, `> **Bold ***italic *and normal text\n\n`, {
listItemCount: 0,
subListItemCount: 0,
});
});

it('should handle FormatContainer with non-blockquote and formatted text', () => {
const blockGroup = createFormatContainer('div');
const paragraph = createParagraph();

const boldText = createText('Bold ');
boldText.format.fontWeight = 'bold';

const italicText = createText('italic ');
italicText.format.italic = true;

const normalText = createText('and normal text');

paragraph.segments.push(boldText);
paragraph.segments.push(italicText);
paragraph.segments.push(normalText);
blockGroup.blocks.push(paragraph);

runTest(blockGroup, `**Bold ***italic *and normal text\n\n`, {
listItemCount: 0,
subListItemCount: 0,
});
});
});
});
Loading