From b00b29da676faac56b8d317bd0128eff64017c3a Mon Sep 17 00:00:00 2001 From: Cat <114286961+CatHood0@users.noreply.github.com> Date: Fri, 5 Jul 2024 22:03:04 -0400 Subject: [PATCH] fix: common link is detected as a video link (#1978) * fix: common link is detected as a video link * chore: dart fixes --------- Co-authored-by: CatHood0 --- lib/src/utils/delta_x_utils.dart | 8 ++++---- test/utils/delta_x_test.dart | 19 +++++++++++++++++-- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/lib/src/utils/delta_x_utils.dart b/lib/src/utils/delta_x_utils.dart index b7767b1e..497fa29b 100644 --- a/lib/src/utils/delta_x_utils.dart +++ b/lib/src/utils/delta_x_utils.dart @@ -20,7 +20,7 @@ class UnderlineSyntax extends md.DelimiterSyntax { class VideoSyntax extends md.LinkSyntax { VideoSyntax({super.linkResolver}) : super( - pattern: r'\[', + pattern: r'\[!\[', startCharacter: _$lbracket, ); @@ -60,13 +60,13 @@ final videoRule = hmd.Rule('video', filters: ['iframe', 'video'], if (!_youtubeVideoUrlValidator.hasMatch(src ?? '')) { return ''; } - return '[$content]($src)'; + return '[![$content]($src)'; } final src = node.getAttribute('src'); if (src == null || !_youtubeVideoUrlValidator.hasMatch(src)) { return node.outerHTML; } - return '[$content]($src)'; + return '[![$content]($src)'; } //by now, we can only access to src final src = node.getAttribute('src'); @@ -76,5 +76,5 @@ final videoRule = hmd.Rule('video', filters: ['iframe', 'video'], return node.outerHTML; } final title = node.getAttribute('title'); - return '[$title]($src)'; + return '[![$title]($src)'; }); diff --git a/test/utils/delta_x_test.dart b/test/utils/delta_x_test.dart index 489a7493..dc0bfa1d 100644 --- a/test/utils/delta_x_test.dart +++ b/test/utils/delta_x_test.dart @@ -13,8 +13,11 @@ void main() { ''; const htmlWithVideoTag = - ''' -'''; + ''; + + const htmlWithNormalLinkAndVideo = + 'fdsfsd
'; + final expectedDeltaEmp = Delta.fromOperations([ Operation.insert( 'This is a normal sentence, and this section has greater emp'), @@ -34,6 +37,13 @@ void main() { Operation.insert('\n'), ]); + final expectedDeltaLinkAndVideoLink = Delta.fromOperations([ + Operation.insert('fdsfsd', {'link': 'https://www.macrumors.com/'}), + Operation.insert('\n\n'), + Operation.insert({'video': 'https://www.youtube.com/embed/dQw4w9WgXcQ'}), + Operation.insert('\n'), + ]); + test('should detect emphasis and parse correctly', () { final delta = DeltaX.fromHtml(htmlWithEmp); expect(delta, expectedDeltaEmp); @@ -53,4 +63,9 @@ void main() { final delta = DeltaX.fromHtml(htmlWithVideoTag); expect(delta, expectedDeltaVideo); }); + + test('should detect by different way normal link and video link', () { + final delta = DeltaX.fromHtml(htmlWithNormalLinkAndVideo); + expect(delta, expectedDeltaLinkAndVideoLink); + }); }