From ed0e87405cfaa8a5cb831cfebde21749d9d58e66 Mon Sep 17 00:00:00 2001 From: Sivin Varghese <64252451+iamsivin@users.noreply.github.com> Date: Thu, 8 Jan 2026 20:07:52 +0530 Subject: [PATCH] fix: Strip autolinks `<...>` when links are not supported (#13204) # Pull Request Template ## Description This PR fixes the crash that occurred when inserting canned responses containing **autolinks** (e.g. ``) into reply channels that **do not support links**, such as **Twilio SMS**. ### Steps to reproduce 1. Create a canned response with an autolink, for example: ``. 2. Open a conversation in a channel that does not support links (e.g. SMS). 3. Insert the canned response into the reply box. ### Cause * Currently, only standard markdown links (`[text](url)`) are handled when stripping unsupported formats from canned responses. * Autolinks (``) are not handled during this process. * As a result, **Error: Token type link_open not supported by Markdown parser** ### Solution * Extended the markdown link parsing logic to explicitly handle **autolinks** in addition to standard markdown links. * When a canned response containing an autolink is inserted into a reply box for a channel that does not support links (e.g. SMS), the angle brackets (`< >`) are stripped. * The autolink is safely pasted as **plain text URL**, preventing parser errors and editor crashes. Fixes https://linear.app/chatwoot/issue/CW-6256/error-token-type-link-open-not-supported-by-markdown-parser Sentry issues [[1](https://chatwoot-p3.sentry.io/issues/7103543778/?environment=production&project=4507182691975168&query=is%3Aunresolved%20markdown&referrer=issue-stream)], [[2](https://chatwoot-p3.sentry.io/issues/7104325962/?environment=production&project=4507182691975168&query=is%3Aunresolved%20markdown&referrer=issue-stream )] ## Type of change - [x] Bug fix (non-breaking change which fixes an issue) ## Checklist: - [x] My code follows the style guidelines of this project - [x] I have performed a self-review of my code - [x] I have commented on my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [x] My changes generate no new warnings - [x] I have added tests that prove my fix is effective or that my feature works - [x] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged and published in downstream modules --- app/javascript/dashboard/constants/editor.js | 7 +++++-- .../dashboard/helper/specs/editorHelper.spec.js | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/app/javascript/dashboard/constants/editor.js b/app/javascript/dashboard/constants/editor.js index 95ac94b69..6b1a99f23 100644 --- a/app/javascript/dashboard/constants/editor.js +++ b/app/javascript/dashboard/constants/editor.js @@ -237,8 +237,11 @@ export const MARKDOWN_PATTERNS = [ patterns: [{ pattern: /`([^`]+)`/g, replacement: '$1' }], }, { - type: 'link', // PM: link, eg: [text](url) - patterns: [{ pattern: /\[([^\]]+)\]\([^)]+\)/g, replacement: '$1' }], + type: 'link', // PM: link, eg: [text](url) or + patterns: [ + { pattern: /\[([^\]]+)\]\([^)]+\)/g, replacement: '$1' }, // [text](url) -> text + { pattern: /<(https?:\/\/[^>]+)>/g, replacement: '$1' }, // -> url (autolinks) + ], }, ]; diff --git a/app/javascript/dashboard/helper/specs/editorHelper.spec.js b/app/javascript/dashboard/helper/specs/editorHelper.spec.js index 1ae765ca7..f928b1d28 100644 --- a/app/javascript/dashboard/helper/specs/editorHelper.spec.js +++ b/app/javascript/dashboard/helper/specs/editorHelper.spec.js @@ -896,6 +896,11 @@ describe('stripUnsupportedFormatting', () => { expect(stripUnsupportedFormatting(content, fullSchema)).toBe(content); }); + it('preserves autolinks when schema supports links', () => { + const content = 'Check out '; + expect(stripUnsupportedFormatting(content, fullSchema)).toBe(content); + }); + it('preserves lists when schema supports them', () => { const content = '- item 1\n- item 2\n1. first\n2. second'; expect(stripUnsupportedFormatting(content, fullSchema)).toBe(content); @@ -967,6 +972,18 @@ describe('stripUnsupportedFormatting', () => { ).toBe('Check this link'); }); + it('converts autolinks to plain URLs when schema does not support links', () => { + const content = 'Visit for more info'; + const expected = 'Visit https://cegrafic.com/catalogo/ for more info'; + expect(stripUnsupportedFormatting(content, emptySchema)).toBe(expected); + }); + + it('handles multiple autolinks in content', () => { + const content = 'Check and '; + const expected = 'Check https://example.com and https://test.com'; + expect(stripUnsupportedFormatting(content, emptySchema)).toBe(expected); + }); + it('strips bullet list markers', () => { expect( stripUnsupportedFormatting('- item 1\n- item 2', emptySchema)