Skip to content

Commit e35e322

Browse files
committed
fix: support special superlink's display
1 parent 68d185e commit e35e322

4 files changed

Lines changed: 180 additions & 3 deletions

File tree

‎src/slate-convert/md-to-slate/transform.js‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,35 @@ const INLINE_KEY_MAP = {
2626
emphasis: 'italic',
2727
};
2828

29+
const transformLinkTextNodes = (nodes, marks = {}) => {
30+
if (!Array.isArray(nodes)) return null;
31+
32+
const textNodes = [];
33+
for (let i = 0; i < nodes.length; i++) {
34+
const node = nodes[i];
35+
if (node.type === 'text') {
36+
textNodes.push({ id: slugid.nice(), text: node.value || '', ...marks });
37+
continue;
38+
}
39+
40+
const mark = INLINE_KEY_MAP[node.type];
41+
if (!mark) return null;
42+
43+
const children = transformLinkTextNodes(node.children, { ...marks, [mark]: true });
44+
if (!children) return null;
45+
textNodes.push(...children);
46+
}
47+
return textNodes;
48+
};
49+
2950
// <strong><em>aa<em>bb<em></strong>
3051
const applyMarkForInlineItem = (result, item, textNode = {}) => {
3152
const { type, children, value } = item;
3253

3354
if (type === LINK) {
3455
const child = children.length === 0 ? { type: 'text', value: '' } : children[0];
35-
const linkChildren = [{ id: slugid.nice(), text: child.value || '' }];
56+
const textNodes = transformLinkTextNodes(children);
57+
const linkChildren = textNodes?.length ? textNodes : [{ id: slugid.nice(), text: child.value || '' }];
3658
const link = {
3759
id: slugid.nice(),
3860
type: LINK,
@@ -455,4 +477,3 @@ export const formatMdToSlate = (children) => {
455477
return handler(child);
456478
}).flat();
457479
};
458-

‎src/slate-convert/slate-to-md/transform.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ const transformInlineChildren = (result, item) => {
111111
type: 'link',
112112
url: getElementHref(item),
113113
title: item.title || null,
114-
children: [transformTextNode(item.children[0])],
114+
children: item.children.map(transformTextNode),
115115
};
116116
result.push(link);
117117
return result;

‎tests/slate-convert/md-to-slate/link.test.js‎

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,117 @@ describe('link test', () => {
2727

2828
expect(formatChildren(nodes)).toEqual(expectResult);
2929
});
30+
31+
it('paragraph > bold link', () => {
32+
const mdString = '[**example**](https://example.com)';
33+
const nodes = mdStringToSlate(mdString);
34+
const expectResult = [{
35+
type: 'paragraph',
36+
children: [
37+
{
38+
text: '',
39+
},
40+
{
41+
type: 'link',
42+
url: 'https://example.com',
43+
title: null,
44+
children: [
45+
{ text: 'example', bold: true }
46+
]
47+
},
48+
{
49+
text: ''
50+
}
51+
]
52+
}];
53+
54+
expect(formatChildren(nodes)).toEqual(expectResult);
55+
});
56+
57+
it('paragraph > italic link', () => {
58+
const mdString = '[*example*](https://example.com)';
59+
const nodes = mdStringToSlate(mdString);
60+
const expectResult = [{
61+
type: 'paragraph',
62+
children: [
63+
{
64+
text: '',
65+
},
66+
{
67+
type: 'link',
68+
url: 'https://example.com',
69+
title: null,
70+
children: [
71+
{ text: 'example', italic: true }
72+
]
73+
},
74+
{
75+
text: ''
76+
}
77+
]
78+
}];
79+
80+
expect(formatChildren(nodes)).toEqual(expectResult);
81+
});
82+
83+
it('paragraph > bold italic link', () => {
84+
const mdString = '[***example***](https://example.com)';
85+
const nodes = mdStringToSlate(mdString);
86+
const expectResult = [{
87+
type: 'paragraph',
88+
children: [
89+
{
90+
text: '',
91+
},
92+
{
93+
type: 'link',
94+
url: 'https://example.com',
95+
title: null,
96+
children: [
97+
{ text: 'example', italic: true, bold: true }
98+
]
99+
},
100+
{
101+
text: ''
102+
}
103+
]
104+
}];
105+
106+
expect(formatChildren(nodes)).toEqual(expectResult);
107+
});
108+
109+
it('paragraph > link with nested styles', () => {
110+
const mdString = '[**bold and *italic***](https://example.com)';
111+
const nodes = mdStringToSlate(mdString);
112+
const link = formatChildren(nodes)[0].children[1];
113+
114+
expect(link.children).toEqual([
115+
{ text: 'bold and ', bold: true },
116+
{ text: 'italic', bold: true, italic: true },
117+
]);
118+
});
119+
120+
it('paragraph > link with plain and bold text', () => {
121+
const mdString = '[a **bold** b](https://example.com)';
122+
const nodes = mdStringToSlate(mdString);
123+
const link = formatChildren(nodes)[0].children[1];
124+
125+
expect(link.children).toEqual([
126+
{ text: 'a ' },
127+
{ text: 'bold', bold: true },
128+
{ text: ' b' },
129+
]);
130+
});
131+
132+
it('paragraph > link with multiple bold texts', () => {
133+
const mdString = '[**a** **b**](https://example.com)';
134+
const nodes = mdStringToSlate(mdString);
135+
const link = formatChildren(nodes)[0].children[1];
136+
137+
expect(link.children).toEqual([
138+
{ text: 'a', bold: true },
139+
{ text: ' ' },
140+
{ text: 'b', bold: true },
141+
]);
142+
});
30143
});

‎tests/slate-convert/slate-to-md/link.test.js‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,47 @@ describe('link test', () => {
4949

5050
expect(formatSlateToMd(nodes)).toEqual(expectResult);
5151
});
52+
53+
it('paragraph > link with styled children', () => {
54+
const nodes = [{
55+
type: 'paragraph',
56+
children: [
57+
{ text: '' },
58+
{
59+
type: 'link',
60+
url: 'https://example.com',
61+
title: null,
62+
children: [
63+
{ text: 'a ' },
64+
{ text: 'bold', bold: true },
65+
{ text: ' b' },
66+
]
67+
},
68+
{ text: '' }
69+
]
70+
}];
71+
72+
const expectResult = [{
73+
type: 'paragraph',
74+
children: [
75+
{ type: 'text', value: '' },
76+
{
77+
type: 'link',
78+
url: 'https://example.com',
79+
title: null,
80+
children: [
81+
{ type: 'text', value: 'a ' },
82+
{
83+
type: 'strong',
84+
children: [{ type: 'text', value: 'bold' }]
85+
},
86+
{ type: 'text', value: ' b' },
87+
]
88+
},
89+
{ type: 'text', value: '' },
90+
]
91+
}];
92+
93+
expect(formatSlateToMd(nodes)).toEqual(expectResult);
94+
});
5295
});

0 commit comments

Comments
 (0)