From 9201cd634758db86ce5f8f47fe66379a2edb0879 Mon Sep 17 00:00:00 2001 From: Mukund-Tandon <71614009+Mukund-Tandon@users.noreply.github.com> Date: Wed, 20 Mar 2024 19:03:39 +0530 Subject: [PATCH] fix: import table from markdown (#4881) * fix: fixed error while importing table from markdown * test: added test for the changes made * fix: made changes from code review * chore: remove the force unwrap code --------- Co-authored-by: Lucas.Xu --- .../markdowns/markdown_with_table.md | 11 +++++ .../uncategorized/import_files_test.dart | 40 +++++++++++++++++++ .../document_data_pb_extension.dart | 10 ++--- 3 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 frontend/appflowy_flutter/assets/test/workspaces/markdowns/markdown_with_table.md diff --git a/frontend/appflowy_flutter/assets/test/workspaces/markdowns/markdown_with_table.md b/frontend/appflowy_flutter/assets/test/workspaces/markdowns/markdown_with_table.md new file mode 100644 index 0000000000..5998220774 --- /dev/null +++ b/frontend/appflowy_flutter/assets/test/workspaces/markdowns/markdown_with_table.md @@ -0,0 +1,11 @@ +# AppFlowy Test Markdown import with table + +# Table + +| S.No. | Column 2 | +| --- | --- | +| 1. | row 1 | +| 2. | row 2 | +| 3. | row 3 | +| 4. | row 4 | +| 5. | row 5 | \ No newline at end of file diff --git a/frontend/appflowy_flutter/integration_test/desktop/uncategorized/import_files_test.dart b/frontend/appflowy_flutter/integration_test/desktop/uncategorized/import_files_test.dart index 58d8cc75be..8f356b8406 100644 --- a/frontend/appflowy_flutter/integration_test/desktop/uncategorized/import_files_test.dart +++ b/frontend/appflowy_flutter/integration_test/desktop/uncategorized/import_files_test.dart @@ -1,5 +1,6 @@ import 'dart:io'; +import 'package:appflowy_editor/appflowy_editor.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:integration_test/integration_test.dart'; @@ -44,5 +45,44 @@ void main() { tester.expectToSeePageName('test1'); tester.expectToSeePageName('test2'); }); + + testWidgets('import markdown file with table', (tester) async { + final context = await tester.initializeAppFlowy(); + await tester.tapGoButton(); + + // expect to see a getting started page + tester.expectToSeePageName(gettingStarted); + + await tester.tapAddViewButton(); + await tester.tapImportButton(); + + const testFileName = 'markdown_with_table.md'; + final paths = []; + final str = await rootBundle.loadString( + 'assets/test/workspaces/markdowns/$testFileName', + ); + final path = p.join(context.applicationDataDirectory, testFileName); + paths.add(path); + File(path).writeAsStringSync(str); + // mock get files + mockPickFilePaths( + paths: paths, + ); + + await tester.tapTextAndMarkdownButton(); + + tester.expectToSeePageName('markdown_with_table'); + + // expect to see all content of markdown file along with table + await tester.openPage('markdown_with_table'); + + final importedPageEditorState = tester.editor.getCurrentEditorState(); + expect(importedPageEditorState.getNodeAtPath([0])!.type, + HeadingBlockKeys.type,); + expect(importedPageEditorState.getNodeAtPath([2])!.type, + HeadingBlockKeys.type,); + expect(importedPageEditorState.getNodeAtPath([4])!.type, + TableBlockKeys.type,); + }); }); } diff --git a/frontend/appflowy_flutter/lib/plugins/document/application/document_data_pb_extension.dart b/frontend/appflowy_flutter/lib/plugins/document/application/document_data_pb_extension.dart index 1c98b970f2..a6d8974921 100644 --- a/frontend/appflowy_flutter/lib/plugins/document/application/document_data_pb_extension.dart +++ b/frontend/appflowy_flutter/lib/plugins/document/application/document_data_pb_extension.dart @@ -59,11 +59,11 @@ extension DocumentDataPBFromTo on DocumentDataPB { // generate the meta final childrenMap = {}; - blocks.forEach((key, value) { - final parentId = value.parentId; - if (parentId.isNotEmpty) { - childrenMap[parentId] ??= ChildrenPB.create(); - childrenMap[parentId]!.children.add(value.id); + blocks.values.where((e) => e.parentId.isNotEmpty).forEach((value) { + final childrenId = blocks[value.parentId]?.childrenId; + if (childrenId != null) { + childrenMap[childrenId] ??= ChildrenPB.create(); + childrenMap[childrenId]!.children.add(value.id); } }); final meta = MetaPB(childrenMap: childrenMap);