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: non-spec emphasis (fix #1806 #2708) #2742

Closed
wants to merge 1 commit into from
Closed
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
101 changes: 78 additions & 23 deletions apps/editor/src/__test__/unit/convertor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,8 @@ describe('Convertor', () => {
| <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**ed**<ul><li>**mix**ed</li></ul></li></ol> |
| <ol><li>mix<i>ed</i><ul><li><strong>mix</strong>ed</li></ul></li></ol> |
| <ol><li>mix*e*d<ul><li>*mix*ed</li></ul></li></ol> |
| <ol><li>mix<em>ed</em><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> |
`;
Expand All @@ -602,8 +603,9 @@ describe('Convertor', () => {
| <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> |
| <ol><li>mix**ed**<ul><li>**mix**ed</li></ul></li></ol> |
| <ol><li>mix*e*d<ul><li>*mix*ed</li></ul></li></ol> |
| <ol><li>mix<em>ed</em><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> |
`;
Expand Down Expand Up @@ -1075,28 +1077,81 @@ describe('Convertor', () => {
});
});

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);
describe('emphasis that starts/ends with punctuation', () => {
it('should convert by using HTML tag when not preceded by whitespace or punctuation', () => {
const wwNodeJson = {
type: 'doc',
content: [
{
type: 'paragraph',
content: [
{ type: 'text', text: 'a' },
{
type: 'text',
marks: [{ type: 'emph' }],
text: '"test"',
},
],
},
],
};
const wwNode = Node.fromJSON(schema, wwNodeJson);

const result = convertor.toMarkdownText(wwNode);
const result = convertor.toMarkdownText(wwNode);

expect(result).toBe(`a<em>"test"</em>`);
});

it('should convert by using HTML tag when not succeeded by whitespace or punctuation', () => {
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`);
expect(result).toBe(`<strong>"test"</strong>a`);
});

it('should not convert by using HTML tag when preceded by whitespace', () => {
const markdown =
'test **"test"**';
const expected =
'test **"test"**';

assertConverting(markdown, expected);
});

it('should not convert by using HTML tag when preceded by punctuation', () => {
const markdown =
'test,**"test"**';
const expected =
'test,**"test"**';

assertConverting(markdown, expected);
});

it('should not convert by using HTML tag when inside another emphasis', () => {
const markdown =
'**test*test***';
const expected =
'**test*test***';

assertConverting(markdown, expected);
});
});

it('should convert empty line between lists of wysiwig to <br>', () => {
Expand Down
43 changes: 31 additions & 12 deletions apps/editor/src/convertors/toMarkdown/toMdConvertorState.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { Node, Mark } from 'prosemirror-model';

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

import { WwNodeType, WwMarkType } from '@t/wysiwyg';
import {
Expand All @@ -10,7 +18,6 @@ import {
FirstDelimFn,
InfoForPosSync,
} from '@t/convertor';
import { DEFAULT_TEXT_NOT_START_OR_END_WITH_SPACE } from '@/utils/constants';

export default class ToMdConvertorState {
private readonly nodeTypeConvertors: ToMdNodeTypeConvertorMap;
Expand Down Expand Up @@ -50,27 +57,39 @@ export default class ToMdConvertorState {
return /(^|\n)$/.test(this.result);
}

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

const isFrontNodeEndWithSpace =
index === 0 ||
isEndWithSpace(content.child(index - 1).text ?? DEFAULT_TEXT_NOT_START_OR_END_WITH_SPACE);
if (
index !== 0 &&
isStartWithPunct(content.child(index).text) &&
!isEndWithSpaceOrPunct(content.child(index - 1).text)
) {
return true;
}

const isRearNodeStartWithSpace =
index >= content.childCount - 1 ||
isStartWithSpace(content.child(index + 1).text ?? DEFAULT_TEXT_NOT_START_OR_END_WITH_SPACE);
if (
index !== content.childCount - 1 &&
isEndWithPunct(content.child(index).text) &&
!isStartWithSpaceOrPunct(content.child(index + 1).text)
) {
return true;
}

return isFrontNodeEndWithSpace && isRearNodeStartWithSpace;
return false;
}

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

if (convertor) {
const betweenSpace = this.isBetweenSpaces(parent, entering ? index : index - 1);
const nonSpecEmphasis = this.isNonSpecEmphasis(parent, entering ? index : index - 1);

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

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

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

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

Expand All @@ -222,11 +222,11 @@ export const toMdConvertors: ToMdConvertorMap = {
};
},

emph({ node }, { entering }, betweenSpace) {
emph({ node }, { entering }, nonSpecEmphasis) {
const { rawHTML } = node.attrs;
let delim = '*';

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

Expand All @@ -236,11 +236,11 @@ export const toMdConvertors: ToMdConvertorMap = {
};
},

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

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

Expand Down Expand Up @@ -368,7 +368,7 @@ function createMarkTypeConvertors(convertors: ToMdConvertorMap) {
const markTypes = Object.keys(markTypeOptions) as WwMarkType[];

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

Expand All @@ -378,7 +378,7 @@ function createMarkTypeConvertors(convertors: ToMdConvertorMap) {
// the converter is called without parameters.
const runConvertor = convertor && nodeInfo && !isUndefined(entering);
const params = runConvertor
? convertor!(nodeInfo as MarkInfo, { entering }, betweenSpace)
? convertor!(nodeInfo as MarkInfo, { entering }, nonSpecEmphasis)
: {};

return { ...params, ...markOption };
Expand Down
32 changes: 26 additions & 6 deletions apps/editor/src/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,14 +264,34 @@ export function getSortedNumPair(valueA: number, valueB: number) {
return valueA > valueB ? [valueB, valueA] : [valueA, valueB];
}

export function isStartWithSpace(text: string) {
const reStartWithSpace = /^\s(\S*)/g;
const reStartWithSpaceOrPunct = /^[\s\u2000-\u206F\u2E00-\u2E7F\\'!"#$%&()*+,\-./:;<=>?@[\]^_`{|}~]/;

return reStartWithSpace.test(text);
export function isStartWithSpaceOrPunct(text?: string) {
if (!text) return true;

return reStartWithSpaceOrPunct.test(text);
}

const reEndWithSpaceOrPunct = /[\s\u2000-\u206F\u2E00-\u2E7F\\'!"#$%&()*+,\-./:;<=>?@[\]^_`{|}~]$/;

export function isEndWithSpaceOrPunct(text?: string) {
if (!text) return true;

return reEndWithSpaceOrPunct.test(text);
}

export function isEndWithSpace(text: string) {
const reEndWithSpace = /(\S*)\s$/g;
const reStartWithPunct = /^[\u2000-\u206F\u2E00-\u2E7F\\'!"#$%&()*+,\-./:;<=>?@[\]^_`{|}~]/;

export function isStartWithPunct(text?: string) {
if (!text) return false;

return reStartWithPunct.test(text);
}

const reEndWithPunct = /[\u2000-\u206F\u2E00-\u2E7F\\'!"#$%&()*+,\-./:;<=>?@[\]^_`{|}~]$/;

export function isEndWithPunct(text?: string) {
if (!text) return false;

return reEndWithSpace.test(text);
return reEndWithPunct.test(text);
}
2 changes: 0 additions & 2 deletions apps/editor/src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,3 @@ export const reBR = /<br\s*\/*>/i;
export const reHTMLComment = /<! ---->|<!--(?:-?[^>-])(?:-?[^-])*-->/;

export const ALTERNATIVE_TAG_FOR_BR = '</p><p>';

export const DEFAULT_TEXT_NOT_START_OR_END_WITH_SPACE = 'a';
4 changes: 2 additions & 2 deletions apps/editor/types/convertor.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export type ToMdNodeTypeConvertorMap = Partial<Record<WwNodeType, ToMdNodeTypeCo
type ToMdMarkTypeConvertor = (
nodeInfo?: MarkInfo,
entering?: boolean,
betweenSpace?: boolean
nonSpecEmphasis?: boolean
) => ToMdConvertorReturnValues & ToMdMarkTypeOption;

export type ToMdMarkTypeConvertorMap = Partial<Record<WwMarkType, ToMdMarkTypeConvertor>>;
Expand All @@ -128,7 +128,7 @@ interface ToMdConvertorContext {
type ToMdConvertor = (
nodeInfo: NodeInfo | MarkInfo,
context: ToMdConvertorContext,
betweenSpace?: boolean
nonSpecEmphasis?: boolean
) => ToMdConvertorReturnValues;

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