Skip to content

Commit

Permalink
chore: add some testing for markdown parser
Browse files Browse the repository at this point in the history
  • Loading branch information
asjqkkkk committed Feb 7, 2025
1 parent 56afe30 commit 7d6aa7e
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ import 'package:universal_platform/universal_platform.dart';

const kMultiImagePlaceholderKey = 'multiImagePlaceholderKey';

Node multiImageNode() => Node(
Node multiImageNode({List<ImageBlockData>? images}) => Node(
type: MultiImageBlockKeys.type,
attributes: {
MultiImageBlockKeys.images: MultiImageData(images: []).toJson(),
MultiImageBlockKeys.images:
MultiImageData(images: images ?? []).toJson(),
MultiImageBlockKeys.layout: MultiImageLayout.browser.toIntValue(),
},
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class CustomMultiImageNodeFileParser extends NodeParser {
);
markdownImages.add('![]($filePath)');
} else {
markdownImages.add('![]($url})');
markdownImages.add('![]($url)');
}
}
return markdownImages.join('\n');
Expand Down
33 changes: 21 additions & 12 deletions frontend/appflowy_flutter/lib/shared/markdown_to_document.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:appflowy/plugins/document/presentation/editor_plugins/plugins.da
import 'package:appflowy_backend/log.dart';
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:archive/archive.dart';
import 'package:flutter/foundation.dart';
import 'package:path/path.dart' as p;
import 'package:share_plus/share_plus.dart';

Expand Down Expand Up @@ -36,7 +37,11 @@ String customDocumentToMarkdown(Document document) {
);
}

Future<String> documentToMarkdownFiles(Document document, String path) async {
Future<String> documentToMarkdownFiles(
Document document,
String path, {
AsyncValueSetter<Archive>? onArchive,
}) async {
final List<Future<ArchiveFile>> fileFutures = [];

/// create root Archive and directory
Expand Down Expand Up @@ -75,18 +80,22 @@ Future<String> documentToMarkdownFiles(Document document, String path) async {
archive.addFile(await fileFuture);
}
if (archive.isNotEmpty) {
final zipEncoder = ZipEncoder();
final zip = zipEncoder.encode(archive);
if (zip != null) {
final zipFile = await File(path).writeAsBytes(zip);
if (Platform.isIOS) {
await Share.shareUri(zipFile.uri);
await zipFile.delete();
} else if (Platform.isAndroid) {
await Share.shareXFiles([XFile(zipFile.path)]);
await zipFile.delete();
if (onArchive == null) {
final zipEncoder = ZipEncoder();
final zip = zipEncoder.encode(archive);
if (zip != null) {
final zipFile = await File(path).writeAsBytes(zip);
if (Platform.isIOS) {
await Share.shareUri(zipFile.uri);
await zipFile.delete();
} else if (Platform.isAndroid) {
await Share.shareXFiles([XFile(zipFile.path)]);
await zipFile.delete();
}
Log.info('documentToMarkdownFiles to $path');
}
Log.info('documentToMarkdownFiles to $path');
} else {
await onArchive.call(archive);
}
}
return markdown;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import 'package:appflowy/plugins/document/presentation/editor_plugins/image/common.dart';
import 'package:appflowy/plugins/document/presentation/editor_plugins/mention/mention_page_block.dart';
import 'package:appflowy/plugins/document/presentation/editor_plugins/plugins.dart';
import 'package:appflowy/shared/markdown_to_document.dart';
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:appflowy_editor_plugins/appflowy_editor_plugins.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
Future<void> emptyCallback(_) async {
return;
}

const testDirectory = 'test/';

group('export markdown to document', () {
test('file block', () async {
final document = Document.blank()
Expand All @@ -17,21 +26,98 @@ void main() {
),
],
);
final markdown = customDocumentToMarkdown(document);
final markdown = await documentToMarkdownFiles(
document,
testDirectory,
onArchive: emptyCallback,
);
expect(markdown, '[file.txt](https://file.com)\n');
});

test('link preview', () {
test('link preview', () async {
final document = Document.blank()
..insert(
[0],
[linkPreviewNode(url: 'https://www.link_preview.com')],
);
final markdown = customDocumentToMarkdown(document);
final markdown = await documentToMarkdownFiles(
document,
testDirectory,
onArchive: emptyCallback,
);
expect(
markdown,
'[https://www.link_preview.com](https://www.link_preview.com)\n',
);
});

test('multiple images', () async {
const png1 = 'https://www.appflowy.png',
png2 = 'https://www.appflowy2.png';
final document = Document.blank()
..insert(
[0],
[
multiImageNode(
images: [
ImageBlockData(
url: png1,
type: CustomImageType.external,
),
ImageBlockData(
url: png2,
type: CustomImageType.external,
),
],
),
],
);
final markdown = await documentToMarkdownFiles(
document,
testDirectory,
onArchive: emptyCallback,
);
expect(
markdown,
'![]($png1)\n![]($png2)',
);
});

test('subpage block', () async {
const testSubpageId = 'testSubpageId';
final subpageNode = pageMentionNode(testSubpageId);
final document = Document.blank()
..insert(
[0],
[subpageNode],
);
final markdown = await documentToMarkdownFiles(
document,
testDirectory,
onArchive: emptyCallback,
);
expect(
markdown,
'[]($testSubpageId)\n',
);
});

test('date or reminder', () async {
final dateTime = DateTime.now();
final document = Document.blank()
..insert(
[0],
[dateMentionNode()],
);
final markdown = await documentToMarkdownFiles(
document,
testDirectory,
onArchive: emptyCallback,
);
expect(
markdown,
'${DateFormat.yMMMd().format(dateTime)}\n',
);
});
});
}

0 comments on commit 7d6aa7e

Please sign in to comment.