Skip to content

Commit

Permalink
chore: format code
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasXu0 committed Jul 19, 2023
1 parent a0ca6d9 commit 800ebba
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 47 deletions.
63 changes: 18 additions & 45 deletions 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,9 +57,6 @@ class DocumentMarkdownDecoder extends Converter<String, Document> {

Node _convertLineToNode(String line) {
final decoder = DeltaMarkdownDecoder();
final imageRegex = RegExp(r'^!\[.*\]\(.*\)$');
final assetRegex = RegExp(r'^\[.*\]\(.*\)$');
final htmlRegex = RegExp('^(http|https)://');
// Heading Style
if (line.startsWith('### ')) {
return headingNode(
Expand Down Expand Up @@ -94,29 +96,18 @@ class DocumentMarkdownDecoder extends Converter<String, Document> {
} else if (line.startsWith('```') && line.endsWith('```')) {
return _codeBlockNodeFromMarkdown(line, decoder);
} else if (imageRegex.hasMatch(line.trim())) {
String? filepath = extractImagePath(line.trim());
//checking if filepath is present or if the filepath is an image or not
if (filepath == null ||
(!filepath.endsWith('.png') &&
!filepath.endsWith('.jpg') &&
!filepath.endsWith('.jpeg'))) {
return paragraphNode(
attributes: {'delta': decoder.convert(line).toJson()},
);
} else {
return imageNode(url: filepath);
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
String? filepath = extractFilePath(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: "This file is cuurently not supported : $line",
);
} else {
return paragraphNode(
attributes: {'delta': decoder.convert(line).toJson()},
);
return paragraphNode(text: line);
}
}

Expand All @@ -132,31 +123,13 @@ class DocumentMarkdownDecoder extends Converter<String, Document> {
}

String? extractImagePath(String text) {
const startDelimiter = "![";
const endDelimiter = "](";
final startIndex = text.indexOf(startDelimiter);
final endIndex =
text.indexOf(endDelimiter, startIndex + startDelimiter.length);

if (startIndex != -1 && endIndex != -1) {
return text.substring(endIndex + endDelimiter.length, text.length - 1);
} else {
return null;
}
final match = imageRegex.firstMatch(text);
return match?.group(1);
}

String? extractFilePath(String text) {
const startDelimiter = "[";
const endDelimiter = "](";
final startIndex = text.indexOf(startDelimiter);
final endIndex =
text.indexOf(endDelimiter, startIndex + startDelimiter.length);

if (startIndex != -1 && endIndex != -1) {
return text.substring(endIndex + endDelimiter.length, text.length - 1);
} else {
return null;
}
final match = assetRegex.firstMatch(text);
return match?.group(1);
}

Node _codeBlockNodeFromMarkdown(
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
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,11 @@ void main() async {
"data": {
"delta": [
{
"insert": "This file is cuurently not supported : [Example file.pdf](path/to/file.pdf)"
"insert": "[Example file.pdf](path/to/file.pdf)"
}
]
}
},
},
{
"type": "paragraph",
"data": {
Expand Down

0 comments on commit 800ebba

Please sign in to comment.