Skip to content
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

fix: keep empty line between lists #2598

Merged
merged 5 commits into from
Jul 12, 2022
Merged
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
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);
Comment on lines +87 to +92

Choose a reason for hiding this comment

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

λ™μž‘μ΄ wrapBlock μ΄λΌλŠ” λ©”μ„œλ“œμ™€ μœ μ‚¬ν•΄ λ³΄μ΄λ„€μš”.

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

μ†μ‰½κ²Œ μ™ΈλΆ€μ—μ„œλ„ delim을 닀루기 μœ„ν•΄ λ©”μ„œλ“œλ₯Ό λ§Œλ“  것 κΉŒμ§„ 쒋은데, ToMdConvertorState 클래슀 λ‚΄λΆ€μ—μ„œλŠ”
setDelim λ©”μ„œλ“œλ₯Ό 쓰지 μ•Šκ³  this.delim 에 할당을 ν•˜λŠ” λ™μž‘μ΄ 마ꡬ μΌμ–΄λ‚˜κ³  μžˆμœΌλ‹ˆ μ½”λ“œμ— 일관성이 μ—†μ–΄ λ³΄μ—¬μš”.

Choose a reason for hiding this comment

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

λ‚΄μš©μ΄ λ‘κ°œλ‘œ λ‚˜λ‰˜μ–΄μ•Όν–ˆλŠ”λ° 리뷰λ₯Ό 읽닀가 ν•˜λ‚˜λ‘œ λ­‰μ³μ‘Œλ‚˜λ³΄λ„€μš”.

  1. μ§€κΈˆ μž‘μ„±ν•œ 뢀뢄은 wrapBlock 으둜 λŒ€μ²΄κ°€ μ•ˆλ˜λŠ”μ§€
  2. get/setDelim λ©”μ„œλ“œκ°€ λ§Œλ“€μ–΄μ‘ŒμœΌλ‹ˆ this.delim 을 λ‹€λ£° λ•Œ ν•΄λ‹Ή 클래슀의 λͺ¨λ“  μ—°κ΄€ λ™μž‘μ΄ μˆ˜μ •λ˜μ–΄μ•Ό ν•˜μ§€ μ•ŠλŠλƒ

μœ„ λ‘κ°œλŠ” λ‹€λ₯Έ λ§₯λ½μ΄μ—ˆμ–΄μš”.

무튼 리뷰 반영된 μ½”λ“œμ—λŠ” wrapBlock 만 get/setDelim 만 μ μš©ν•˜κ³  μžˆλŠ”λ°, flushClose, write λ©”μ„œλ“œλ„ 같은 변경이 ν•„μš”ν•΄μš”.

}
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