Skip to content

Commit

Permalink
fix: keep empty line between lists (#2598)
Browse files Browse the repository at this point in the history
* fix: keep empty line between lists

* fix: remove delimiter when add empty line between list items

* test: add test for convertor

* chore: apply code reviews
  • Loading branch information
jajugoguma authored Jul 12, 2022
1 parent 3fc8ac5 commit 78e2184
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
30 changes: 30 additions & 0 deletions apps/editor/src/__test__/unit/convertor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1099,6 +1099,36 @@ describe('Convertor', () => {
expect(result).toBe(`<strong>"test"</strong>a`);
});

it('should convert empty line between lists of wysiwig to <br>', () => {
const wwNodeJson = {
type: 'doc',
content: [
{
type: 'bulletList',
content: [
{
type: 'listItem',
content: [
{ type: 'paragraph', content: [{ type: 'text', text: 'test_1' }] },
{ type: 'paragraph', content: [] },
],
},
{
type: 'listItem',
content: [{ type: 'paragraph', content: [{ type: 'text', text: 'test_2' }] }],
},
],
},
],
};

const wwNode = Node.fromJSON(schema, wwNodeJson);

const result = convertor.toMarkdownText(wwNode);

expect(result).toBe(`* test\\_1\n<br>\n* test\\_2`);
});

it('should escape the backslash, which is a plain chracter in the middle of a sentence', () => {
const markdown = source`
backslash \\in the middle of a sentence
Expand Down
14 changes: 11 additions & 3 deletions apps/editor/src/convertors/toMarkdown/toMdConvertorState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ export default class ToMdConvertorState {
return '';
}

setDelim(delim: string) {
this.delim = delim;
}

getDelim() {
return this.delim;
}

flushClose(size?: number) {
if (!this.stopNewline && this.closed) {
if (!this.isInBlank()) {
Expand Down Expand Up @@ -106,12 +114,12 @@ export default class ToMdConvertorState {
}

wrapBlock(delim: string, firstDelim: string | null, node: Node, fn: () => void) {
const old = this.delim;
const old = this.getDelim();

this.write(firstDelim || delim);
this.delim += delim;
this.setDelim(this.getDelim() + delim);
fn();
this.delim = old;
this.setDelim(old);
this.closeBlock(node);
}

Expand Down
8 changes: 8 additions & 0 deletions apps/editor/src/convertors/toMarkdown/toMdNodeTypeWriters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ export const nodeTypeWriters: ToMdNodeTypeWriterMap = {
if (emptyNode && prevEmptyNode) {
state.write('<br>\n');
} else if (emptyNode && !prevEmptyNode && !firstChildNode) {
if (parent?.type.name === 'listItem') {
const prevDelim = state.getDelim();

state.setDelim('');
state.write('<br>');

state.setDelim(prevDelim);
}
state.write('\n');
} else {
state.convertInline(node);
Expand Down
2 changes: 2 additions & 0 deletions apps/editor/types/convertor.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ export type FirstDelimFn = (index: number) => string;
export interface ToMdConvertorState {
stopNewline: boolean;
inTable: boolean;
getDelim(): string;
setDelim(delim: string): void;
flushClose(size?: number): void;
wrapBlock(delim: string, firstDelim: string | null, node: ProsemirrorNode, fn: () => void): void;
ensureNewLine(): void;
Expand Down

0 comments on commit 78e2184

Please sign in to comment.