Skip to content

Commit

Permalink
fix: markdown to document with image url having properties after exte…
Browse files Browse the repository at this point in the history
…nsion (#795)
  • Loading branch information
stevenosse authored Jun 4, 2024
1 parent 067c8e7 commit 6ae9159
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:appflowy_editor/src/plugins/markdown/decoder/parser/custom_node_parser.dart';
import 'package:appflowy_editor/src/plugins/markdown/decoder/table_markdown_decoder.dart';
import 'package:markdown/markdown.dart' as md;
import 'package:path/path.dart' as p;

class DocumentMarkdownDecoder extends Converter<String, Document> {
DocumentMarkdownDecoder({
Expand Down Expand Up @@ -207,9 +206,11 @@ class DocumentMarkdownDecoder extends Converter<String, Document> {
return codeBlockNodeFromMarkdown(line, decoder);
} else if (imageRegex.hasMatch(line.trim())) {
final filePath = extractImagePath(line.trim());
// checking if filepath is present or if the filepath is an image or not
if (filePath == null ||
!['.png', '.jpg', 'jpeg'].contains(p.extension(filePath))) {
final regex = RegExp(
r'\.(png|jpg|jpeg|gif|bmp|svg|webp|tiff|ico)(\?.*)?$',
caseSensitive: false,
);
if (filePath == null || !regex.hasMatch(filePath)) {
return paragraphNode(text: line.trim());
}
return imageNode(url: filePath);
Expand Down
21 changes: 21 additions & 0 deletions test/plugins/markdown/decoder/document_markdown_decoder_test.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:convert';

import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:appflowy_editor/src/plugins/markdown/decoder/document_markdown_decoder.dart';
import 'package:appflowy_editor/src/plugins/markdown/decoder/parser/custom_node_parser.dart';
import 'package:flutter_test/flutter_test.dart';
Expand Down Expand Up @@ -953,5 +954,25 @@ Hello [AppFlowy Subpage](123456789abcd) **Hello** [AppFlowy Subpage](987654321ab

expect(result.toJson(), data);
});

test('image with properties after extension', () async {
const markdown = '''
## Welcome to AppFlowy
![Example image](https://example.com/image.png?key=value)''';

final expected = Document.blank()
..insert(
[0],
[
headingNode(text: 'Welcome to AppFlowy', level: 2),
paragraphNode(),
imageNode(url: 'https://example.com/image.png?key=value'),
],
);
final result = DocumentMarkdownDecoder().convert(markdown);

expect(result.toJson(), expected.toJson());
});
});
}

0 comments on commit 6ae9159

Please sign in to comment.