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: convert by using HTML tag when it is not between spaces #2572

Merged
merged 5 commits into from
Jun 28, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
41 changes: 39 additions & 2 deletions apps/editor/src/__test__/unit/convertor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { source, oneLineTrim } from 'common-tags';

import { Context, MdNode, Parser, HTMLConvertorMap } from '@toast-ui/toastmark';

import { Schema } from 'prosemirror-model';
import { Node, Schema } from 'prosemirror-model';
import { createSpecs } from '@/wysiwyg/specCreator';

import Convertor from '@/convertors/convertor';
Expand Down Expand Up @@ -595,7 +595,20 @@ describe('Convertor', () => {
| ![altText](imgUrl) **mixed**<ul><li>[linkText](linkUrl) mixed</li></ul> |
`;

assertConverting(markdown, `${markdown}\n`);
const expected = source`
| thead |
| ----- |
| <ul><li>bullet</li></ul> |
| <ol><li>ordered</li></ol> |
| <ul><li>nested<ul><li>nested</li></ul></li></ul> |
| <ul><li>nested<ul><li>nested</li><li>nested</li></ul></li></ul> |
| <ol><li>mix<strong>ed</strong><ul><li><strong>mix</strong>ed</li></ul></li></ol> |
| <ol><li>mix<i>ed</i><ul><li><strong>mix</strong>ed</li></ul></li></ol> |
| foo<ul><li>bar</li></ul>baz |
| ![altText](imgUrl) **mixed**<ul><li>[linkText](linkUrl) mixed</li></ul> |
`;

assertConverting(markdown, `${expected}\n`);
});

it('table with unmatched html list', () => {
Expand Down Expand Up @@ -1061,4 +1074,28 @@ describe('Convertor', () => {
assertConverting(markdown, markdown);
});
});

it('should convert by using HTML tag when delimiter is not preceded an alphanumeric', () => {
const wwNodeJson = {
type: 'doc',
content: [
{
type: 'paragraph',
content: [
{
type: 'text',
marks: [{ type: 'strong' }],
text: '"test"',
},
{ type: 'text', text: 'a' },
],
},
],
};
const wwNode = Node.fromJSON(schema, wwNodeJson);

const result = convertor.toMarkdownText(wwNode);

expect(result).toBe(`<strong>"test"</strong>a`);
});
});
18 changes: 16 additions & 2 deletions apps/editor/src/convertors/toMarkdown/toMdConvertorState.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Node, Mark } from 'prosemirror-model';

import { includes, escape, last } from '@/utils/common';
import { includes, escape, last, isEndWithSpace, isStartWithSpace } from '@/utils/common';

import { WwNodeType, WwMarkType } from '@t/wysiwyg';
import {
Expand Down Expand Up @@ -49,11 +49,25 @@ export default class ToMdConvertorState {
return /(^|\n)$/.test(this.result);
}

private isBetweenSpaces(parent: Node, index: number) {
const { content } = parent;

const isFrontNodeEndWithSpace =
index === 0 || isEndWithSpace(content.child(index - 1).text ?? 'a');

Choose a reason for hiding this comment

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

기본값으로 처리한 'a' 는 공백 문자를 넣으면 isEndWithSpace 혹은 isStartWithSpace 가 제대로 동작하지 않으니까 임의로 넣은 문자인가요?

Copy link
Contributor

Choose a reason for hiding this comment

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

그러게요. 'a'가 뭘 나타내는건지 의미를 만들어 상수화 시켜야겠습니다.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

넵, 해당 문자 의미 만들어서 상수화 했습니다.


const isRearNodeStartWithSpace =
index >= content.childCount - 1 || isStartWithSpace(content.child(index + 1).text ?? 'a');

return isFrontNodeEndWithSpace && isRearNodeStartWithSpace;
}

private markText(mark: Mark, entering: boolean, parent: Node, index: number) {
const convertor = this.getMarkConvertor(mark);

if (convertor) {
const { delim, rawHTML } = convertor({ node: mark, parent, index }, entering);
const betweenSpace = this.isBetweenSpaces(parent, entering ? index : index - 1);

const { delim, rawHTML } = convertor({ node: mark, parent, index }, entering, betweenSpace);

return (rawHTML as string) || (delim as string);
}
Expand Down
39 changes: 31 additions & 8 deletions apps/editor/src/convertors/toMarkdown/toMdConvertors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,29 +208,50 @@ export const toMdConvertors: ToMdConvertorMap = {
};
},

strong({ node }, { entering }) {
strong({ node }, { entering }, betweenSpace) {
const { rawHTML } = node.attrs;
let delim;

if (betweenSpace) {
delim = '**';
} else {

Choose a reason for hiding this comment

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

delim 변수를 선언할 때 기본값을 정하고, 분기문은 한번만 둬도 되지 않을까요?

let delim = '**';

if (!betweenSpace) {
  delim = entering ? '<strong>' : '</strong>';
}

delim = entering ? '<strong>' : '</strong>';
}

return {
delim: '**',
delim,
rawHTML: entering ? getOpenRawHTML(rawHTML) : getCloseRawHTML(rawHTML),
};
},

emph({ node }, { entering }) {
emph({ node }, { entering }, betweenSpace) {
Copy link
Contributor

Choose a reason for hiding this comment

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

같이 처리했네요. 좋습니다. 👍👍👍

const { rawHTML } = node.attrs;
let delim;

if (betweenSpace) {
delim = '*';
} else {
delim = entering ? '<em>' : '</em>';
}

return {
delim: '*',
delim,
rawHTML: entering ? getOpenRawHTML(rawHTML) : getCloseRawHTML(rawHTML),
};
},

strike({ node }, { entering }) {
strike({ node }, { entering }, betweenSpace) {
const { rawHTML } = node.attrs;
let delim;

if (betweenSpace) {
delim = '~~';
} else {
delim = entering ? '<del>' : '</del>';
}

return {
delim: '~~',
delim,
rawHTML: entering ? getOpenRawHTML(rawHTML) : getCloseRawHTML(rawHTML),
};
},
Expand Down Expand Up @@ -353,7 +374,7 @@ function createMarkTypeConvertors(convertors: ToMdConvertorMap) {
const markTypes = Object.keys(markTypeOptions) as WwMarkType[];

markTypes.forEach((type) => {
markTypeConvertors[type] = (nodeInfo, entering) => {
markTypeConvertors[type] = (nodeInfo, entering, betweenSpace) => {
const markOption = markTypeOptions[type];
const convertor = convertors[type];

Expand All @@ -362,7 +383,9 @@ function createMarkTypeConvertors(convertors: ToMdConvertorMap) {
// When calling the converter without using `delim` and `rawHTML` values,
// the converter is called without parameters.
const runConvertor = convertor && nodeInfo && !isUndefined(entering);
const params = runConvertor ? convertor!(nodeInfo as MarkInfo, { entering }) : {};
const params = runConvertor
? convertor!(nodeInfo as MarkInfo, { entering }, betweenSpace)
: {};

return { ...params, ...markOption };
};
Expand Down
12 changes: 12 additions & 0 deletions apps/editor/src/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,3 +257,15 @@ export function assign(targetObj: Record<string, any>, obj: Record<string, any>
export function getSortedNumPair(valueA: number, valueB: number) {
return valueA > valueB ? [valueB, valueA] : [valueA, valueB];
}

export function isStartWithSpace(text: string) {
const reStartWithSpace = /^\s(\S*)/g;

return reStartWithSpace.test(text);
}

export function isEndWithSpace(text: string) {
const reEndWithSpace = /(\S*)\s$/g;

return reEndWithSpace.test(text);
}
6 changes: 4 additions & 2 deletions apps/editor/types/convertor.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ export type ToMdNodeTypeConvertorMap = Partial<Record<WwNodeType, ToMdNodeTypeCo

type ToMdMarkTypeConvertor = (
nodeInfo?: MarkInfo,
entering?: boolean
entering?: boolean,
betweenSpace?: boolean
) => ToMdConvertorReturnValues & ToMdMarkTypeOption;

export type ToMdMarkTypeConvertorMap = Partial<Record<WwMarkType, ToMdMarkTypeConvertor>>;
Expand All @@ -124,7 +125,8 @@ interface ToMdConvertorContext {

type ToMdConvertor = (
nodeInfo: NodeInfo | MarkInfo,
context: ToMdConvertorContext
context: ToMdConvertorContext,
betweenSpace?: boolean
) => ToMdConvertorReturnValues;

export type ToMdConvertorMap = Partial<Record<WwNodeType | MdNodeType, ToMdConvertor>>;
Expand Down