From edd96658374368979cd94b2c1fd12476c357a255 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20H=C3=A4rtl?= Date: Fri, 9 Aug 2024 13:03:48 +0200 Subject: [PATCH] fix: Only insert smart picker result as preview if it is a valid URL MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- .../Suggestion/LinkPicker/suggestions.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/components/Suggestion/LinkPicker/suggestions.js b/src/components/Suggestion/LinkPicker/suggestions.js index 25c425168..f8d2625ff 100644 --- a/src/components/Suggestion/LinkPicker/suggestions.js +++ b/src/components/Suggestion/LinkPicker/suggestions.js @@ -44,6 +44,14 @@ const isValidMarkdown = (content) => { } } +const isValidUrl = (url) => { + try { + return Boolean(new URL(url)) + } catch (e) { + return false + } +} + const sortImportantFirst = (list) => { return [ ...list.filter(e => important.indexOf(e.key) > -1), @@ -77,9 +85,11 @@ export default () => createSuggestions({ } getLinkWithPicker(props.providerId, true) .then(link => { - if (hasMarkdownSyntax(link) && isValidMarkdown(link)) { + const isUrl = isValidUrl(link) + if (!isUrl) { + const isMarkdown = hasMarkdownSyntax(link) && isValidMarkdown(link) // Insert markdown content (e.g. from `text_templates` app) - const content = markdownit.render(link) + const content = isMarkdown ? markdownit.render(link) : link editor .chain() .focus()