From e0b3aedfff5d0928b815f2635cd4b4c23195cdfa Mon Sep 17 00:00:00 2001 From: Daniel Lautzenheiser Date: Thu, 31 Aug 2023 12:34:54 -0400 Subject: [PATCH] fix: do not auto link url from text if already inside a link (#857) --- .../slate/__tests__/autoLinkUrls.spec.ts | 28 ++++++++++++++----- .../plate/serialization/slate/autoLinkUrls.ts | 5 ++-- .../slate/deserializeMarkdown.ts | 9 +++++- .../src/widgets/relation/RelationControl.tsx | 3 -- 4 files changed, 31 insertions(+), 14 deletions(-) diff --git a/packages/core/src/widgets/markdown/plate/serialization/slate/__tests__/autoLinkUrls.spec.ts b/packages/core/src/widgets/markdown/plate/serialization/slate/__tests__/autoLinkUrls.spec.ts index 691094e9..6f1cf4c0 100644 --- a/packages/core/src/widgets/markdown/plate/serialization/slate/__tests__/autoLinkUrls.spec.ts +++ b/packages/core/src/widgets/markdown/plate/serialization/slate/__tests__/autoLinkUrls.spec.ts @@ -16,7 +16,21 @@ describe('processShortcodeConfig', () => { }, ]; - expect(autoLinkToSlate(nodes)).toEqual(slate); + expect(autoLinkToSlate(nodes, false)).toEqual(slate); + }); + + it('does not convert url to anchor node if inside link', () => { + const nodes: MdastNode[] = [ + { type: 'text', value: 'https://www.youtube.com/watch?v=p6h-rYSVX90' }, + ]; + const output: MdastNode[] = [ + { + type: 'text', + value: 'https://www.youtube.com/watch?v=p6h-rYSVX90', + }, + ]; + + expect(autoLinkToSlate(nodes, true)).toEqual(output); }); it('does not convert url in shortcode node', () => { @@ -43,7 +57,7 @@ describe('processShortcodeConfig', () => { }, ]; - expect(autoLinkToSlate(nodes)).toEqual(slate); + expect(autoLinkToSlate(nodes, false)).toEqual(slate); }); it('converts url with text before', () => { @@ -62,7 +76,7 @@ describe('processShortcodeConfig', () => { }, ]; - expect(autoLinkToSlate(nodes)).toEqual(slate); + expect(autoLinkToSlate(nodes, false)).toEqual(slate); }); it('converts url with text after', () => { @@ -81,7 +95,7 @@ describe('processShortcodeConfig', () => { }, ]; - expect(autoLinkToSlate(nodes)).toEqual(slate); + expect(autoLinkToSlate(nodes, false)).toEqual(slate); }); it('converts url with text before and after', () => { @@ -107,7 +121,7 @@ describe('processShortcodeConfig', () => { }, ]; - expect(autoLinkToSlate(nodes)).toEqual(slate); + expect(autoLinkToSlate(nodes, false)).toEqual(slate); }); it('converts multiple urls', () => { @@ -143,7 +157,7 @@ describe('processShortcodeConfig', () => { }, ]; - expect(autoLinkToSlate(nodes)).toEqual(slate); + expect(autoLinkToSlate(nodes, false)).toEqual(slate); }); it('does not convert plain text', () => { @@ -160,7 +174,7 @@ describe('processShortcodeConfig', () => { }, ]; - expect(autoLinkToSlate(nodes)).toEqual(slate); + expect(autoLinkToSlate(nodes, false)).toEqual(slate); }); }); }); diff --git a/packages/core/src/widgets/markdown/plate/serialization/slate/autoLinkUrls.ts b/packages/core/src/widgets/markdown/plate/serialization/slate/autoLinkUrls.ts index 061ca35a..9d2192fa 100644 --- a/packages/core/src/widgets/markdown/plate/serialization/slate/autoLinkUrls.ts +++ b/packages/core/src/widgets/markdown/plate/serialization/slate/autoLinkUrls.ts @@ -1,15 +1,14 @@ /* eslint-disable import/prefer-default-export */ - import { isNotEmpty } from '@staticcms/core/lib/util/string.util'; import { NodeTypes } from './ast-types'; import type { BaseMdastNode, MdastNode } from './ast-types'; -export function autoLinkToSlate(nodes: BaseMdastNode[]) { +export function autoLinkToSlate(nodes: BaseMdastNode[], isInLink: boolean) { const output: MdastNode[] = []; for (const node of nodes) { - if (node.type === 'text' && node.value) { + if (node.type === 'text' && node.value && !isInLink) { const regex = /([\w\W]*?)((?:http(?:s)?:\/\/.)?(?:www\.)?[-a-zA-Z0-9@:%._+~#=]{2,256}\.[a-z]{2,6}\b(?:[-a-zA-Z0-9@:%_+.~#?&//=]*))([\w\W]*)/g; let matches: RegExpExecArray | null; diff --git a/packages/core/src/widgets/markdown/plate/serialization/slate/deserializeMarkdown.ts b/packages/core/src/widgets/markdown/plate/serialization/slate/deserializeMarkdown.ts index f0d136d4..914bdbd1 100644 --- a/packages/core/src/widgets/markdown/plate/serialization/slate/deserializeMarkdown.ts +++ b/packages/core/src/widgets/markdown/plate/serialization/slate/deserializeMarkdown.ts @@ -90,6 +90,7 @@ function parseStyleAttribute(node: MdxTextMdastNode, allowedStyles: Record (node.type === 'text' ? { text: node.value ?? '' } : node)); diff --git a/packages/core/src/widgets/relation/RelationControl.tsx b/packages/core/src/widgets/relation/RelationControl.tsx index e627882e..0e8edf85 100644 --- a/packages/core/src/widgets/relation/RelationControl.tsx +++ b/packages/core/src/widgets/relation/RelationControl.tsx @@ -201,7 +201,6 @@ const RelationControl: FC> searchFields, inputValue, ); - console.log('file', file, 'hits', hits); } else { const expandedEntries = expandSearchEntries(entries, searchFields); hits = mergeExpandedEntries( @@ -325,8 +324,6 @@ const RelationControl: FC> const isRequired = useMemo(() => field.required ?? true, [field.required]); - console.log('field.required', field.required); - return (