Skip to content
This repository was archived by the owner on Sep 9, 2024. It is now read-only.

Commit e0b3aed

Browse files
authored
fix: do not auto link url from text if already inside a link (#857)
1 parent 4325905 commit e0b3aed

File tree

4 files changed

+31
-14
lines changed

4 files changed

+31
-14
lines changed

packages/core/src/widgets/markdown/plate/serialization/slate/__tests__/autoLinkUrls.spec.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,21 @@ describe('processShortcodeConfig', () => {
1616
},
1717
];
1818

19-
expect(autoLinkToSlate(nodes)).toEqual(slate);
19+
expect(autoLinkToSlate(nodes, false)).toEqual(slate);
20+
});
21+
22+
it('does not convert url to anchor node if inside link', () => {
23+
const nodes: MdastNode[] = [
24+
{ type: 'text', value: 'https://www.youtube.com/watch?v=p6h-rYSVX90' },
25+
];
26+
const output: MdastNode[] = [
27+
{
28+
type: 'text',
29+
value: 'https://www.youtube.com/watch?v=p6h-rYSVX90',
30+
},
31+
];
32+
33+
expect(autoLinkToSlate(nodes, true)).toEqual(output);
2034
});
2135

2236
it('does not convert url in shortcode node', () => {
@@ -43,7 +57,7 @@ describe('processShortcodeConfig', () => {
4357
},
4458
];
4559

46-
expect(autoLinkToSlate(nodes)).toEqual(slate);
60+
expect(autoLinkToSlate(nodes, false)).toEqual(slate);
4761
});
4862

4963
it('converts url with text before', () => {
@@ -62,7 +76,7 @@ describe('processShortcodeConfig', () => {
6276
},
6377
];
6478

65-
expect(autoLinkToSlate(nodes)).toEqual(slate);
79+
expect(autoLinkToSlate(nodes, false)).toEqual(slate);
6680
});
6781

6882
it('converts url with text after', () => {
@@ -81,7 +95,7 @@ describe('processShortcodeConfig', () => {
8195
},
8296
];
8397

84-
expect(autoLinkToSlate(nodes)).toEqual(slate);
98+
expect(autoLinkToSlate(nodes, false)).toEqual(slate);
8599
});
86100

87101
it('converts url with text before and after', () => {
@@ -107,7 +121,7 @@ describe('processShortcodeConfig', () => {
107121
},
108122
];
109123

110-
expect(autoLinkToSlate(nodes)).toEqual(slate);
124+
expect(autoLinkToSlate(nodes, false)).toEqual(slate);
111125
});
112126

113127
it('converts multiple urls', () => {
@@ -143,7 +157,7 @@ describe('processShortcodeConfig', () => {
143157
},
144158
];
145159

146-
expect(autoLinkToSlate(nodes)).toEqual(slate);
160+
expect(autoLinkToSlate(nodes, false)).toEqual(slate);
147161
});
148162

149163
it('does not convert plain text', () => {
@@ -160,7 +174,7 @@ describe('processShortcodeConfig', () => {
160174
},
161175
];
162176

163-
expect(autoLinkToSlate(nodes)).toEqual(slate);
177+
expect(autoLinkToSlate(nodes, false)).toEqual(slate);
164178
});
165179
});
166180
});

packages/core/src/widgets/markdown/plate/serialization/slate/autoLinkUrls.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
/* eslint-disable import/prefer-default-export */
2-
32
import { isNotEmpty } from '@staticcms/core/lib/util/string.util';
43
import { NodeTypes } from './ast-types';
54

65
import type { BaseMdastNode, MdastNode } from './ast-types';
76

8-
export function autoLinkToSlate(nodes: BaseMdastNode[]) {
7+
export function autoLinkToSlate(nodes: BaseMdastNode[], isInLink: boolean) {
98
const output: MdastNode[] = [];
109

1110
for (const node of nodes) {
12-
if (node.type === 'text' && node.value) {
11+
if (node.type === 'text' && node.value && !isInLink) {
1312
const regex =
1413
/([\w\W]*?)((?:http(?:s)?:\/\/.)?(?:www\.)?[-a-zA-Z0-9@:%._+~#=]{2,256}\.[a-z]{2,6}\b(?:[-a-zA-Z0-9@:%_+.~#?&//=]*))([\w\W]*)/g;
1514
let matches: RegExpExecArray | null;

packages/core/src/widgets/markdown/plate/serialization/slate/deserializeMarkdown.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ function parseStyleAttribute(node: MdxTextMdastNode, allowedStyles: Record<strin
9090

9191
export interface Options {
9292
isInTable?: boolean;
93+
isInLink?: boolean;
9394
isInTableHeaderRow?: boolean;
9495
tableAlign?: (string | null)[];
9596
useMdx: boolean;
@@ -102,6 +103,7 @@ export default function deserializeMarkdown(node: MdastNode, options: Options) {
102103

103104
const {
104105
isInTable = false,
106+
isInLink = false,
105107
isInTableHeaderRow = false,
106108
tableAlign,
107109
useMdx,
@@ -110,6 +112,7 @@ export default function deserializeMarkdown(node: MdastNode, options: Options) {
110112
} = options ?? {};
111113

112114
const selfIsTable = node.type === 'table';
115+
const selfIsLink = node.type === 'link';
113116
const selfIsTableHeaderRow = node.type === 'tableRow' && index === 0;
114117

115118
const nodeChildren = node.children;
@@ -123,6 +126,7 @@ export default function deserializeMarkdown(node: MdastNode, options: Options) {
123126
},
124127
{
125128
isInTable: selfIsTable || isInTable,
129+
isInLink: selfIsLink || isInLink,
126130
isInTableHeaderRow: selfIsTableHeaderRow || isInTableHeaderRow,
127131
useMdx,
128132
shortcodeConfigs,
@@ -351,7 +355,10 @@ export default function deserializeMarkdown(node: MdastNode, options: Options) {
351355
return { text: '' };
352356
}
353357

354-
const nodes = autoLinkToSlate(processShortcodeConfigsToSlate(shortcodeConfigs, [node]));
358+
const nodes = autoLinkToSlate(
359+
processShortcodeConfigsToSlate(shortcodeConfigs, [node]),
360+
isInLink,
361+
);
355362

356363
return nodes.map(node => (node.type === 'text' ? { text: node.value ?? '' } : node));
357364

packages/core/src/widgets/relation/RelationControl.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,6 @@ const RelationControl: FC<WidgetControlProps<string | string[], RelationField>>
201201
searchFields,
202202
inputValue,
203203
);
204-
console.log('file', file, 'hits', hits);
205204
} else {
206205
const expandedEntries = expandSearchEntries(entries, searchFields);
207206
hits = mergeExpandedEntries(
@@ -325,8 +324,6 @@ const RelationControl: FC<WidgetControlProps<string | string[], RelationField>>
325324

326325
const isRequired = useMemo(() => field.required ?? true, [field.required]);
327326

328-
console.log('field.required', field.required);
329-
330327
return (
331328
<Field
332329
inputRef={ref}

0 commit comments

Comments
 (0)