Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import assets from markdown file #316

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion lib/src/plugins/markdown/decoder/document_markdown_decoder.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import 'dart:convert';

import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:path/path.dart' as p;

class DocumentMarkdownDecoder extends Converter<String, Document> {
final imageRegex = RegExp(r'^!\[[^\]]*\]\((.*?)\)');
final assetRegex = RegExp(r'^\[[^\]]*\]\((.*?)\)');
final htmlRegex = RegExp('^(http|https)://');

@override
Document convert(String input) {
final lines = input.split('\n');
Expand Down Expand Up @@ -52,7 +57,6 @@ class DocumentMarkdownDecoder extends Converter<String, Document> {

Node _convertLineToNode(String line) {
final decoder = DeltaMarkdownDecoder();

// Heading Style
if (line.startsWith('### ')) {
return headingNode(
Expand Down Expand Up @@ -91,6 +95,20 @@ class DocumentMarkdownDecoder extends Converter<String, Document> {
return Node(type: 'divider');
} else if (line.startsWith('```') && line.endsWith('```')) {
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))) {
return paragraphNode(text: line.trim());
}
return imageNode(url: filePath);
} else if (assetRegex.hasMatch(line.trim())) {
// this might be a url or a file like pdf, videos, etc
final filepath = extractFilePath(line.trim());
if (filepath != null && !htmlRegex.hasMatch(filepath)) {
return paragraphNode(text: line);
}
}

if (line.isNotEmpty) {
Expand All @@ -104,6 +122,16 @@ class DocumentMarkdownDecoder extends Converter<String, Document> {
);
}

String? extractImagePath(String text) {
final match = imageRegex.firstMatch(text);
return match?.group(1);
}

String? extractFilePath(String text) {
final match = assetRegex.firstMatch(text);
return match?.group(1);
}

Node _codeBlockNodeFromMarkdown(
String markdown,
DeltaMarkdownDecoder decoder,
Expand Down
2 changes: 2 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ dependencies:
nanoid: ^1.0.0
visibility_detector: ^0.4.0+2
file_picker: ^5.3.1
path: ^1.8.3

dev_dependencies:
flutter_test:
sdk: flutter
Expand Down
33 changes: 33 additions & 0 deletions test/plugins/markdown/decoder/document_markdown_decoder_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,35 @@ void main() async {
]
}
},
{
"type": "paragraph",
"data": {
"delta": []
}
},
{
"type": "paragraph",
"data": {
"delta": [
{
"insert": "[Example file.pdf](path/to/file.pdf)"
}
]
}
},
{
"type": "paragraph",
"data": {
"delta": []
}
},
{
"type": "image",
"data": {
"url": "path/to/image.png",
"align": "center"
}
},
{
"type": "paragraph",
"data": {
Expand Down Expand Up @@ -334,6 +363,10 @@ You can also use ***AppFlowy Editor*** as a component to build your own app.
* Select text to trigger to the toolbar to format your notes.

If you have questions or feedback, please submit an issue on Github or join the community along with 1000+ builders!

[Example file.pdf](path/to/file.pdf)

![Example image](path/to/image.png)
''';
final result = DocumentMarkdownDecoder().convert(markdown);
final data = jsonDecode(example);
Expand Down