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 <lucas.xu@appflowy.io>
This commit is contained in:
Mukund-Tandon 2024-03-20 19:03:39 +05:30 committed by GitHub
parent 73df51f35f
commit 9201cd6347
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 56 additions and 5 deletions

View File

@ -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 |

View File

@ -1,5 +1,6 @@
import 'dart:io'; import 'dart:io';
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart'; import 'package:integration_test/integration_test.dart';
@ -44,5 +45,44 @@ void main() {
tester.expectToSeePageName('test1'); tester.expectToSeePageName('test1');
tester.expectToSeePageName('test2'); 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 = <String>[];
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,);
});
}); });
} }

View File

@ -59,11 +59,11 @@ extension DocumentDataPBFromTo on DocumentDataPB {
// generate the meta // generate the meta
final childrenMap = <String, ChildrenPB>{}; final childrenMap = <String, ChildrenPB>{};
blocks.forEach((key, value) { blocks.values.where((e) => e.parentId.isNotEmpty).forEach((value) {
final parentId = value.parentId; final childrenId = blocks[value.parentId]?.childrenId;
if (parentId.isNotEmpty) { if (childrenId != null) {
childrenMap[parentId] ??= ChildrenPB.create(); childrenMap[childrenId] ??= ChildrenPB.create();
childrenMap[parentId]!.children.add(value.id); childrenMap[childrenId]!.children.add(value.id);
} }
}); });
final meta = MetaPB(childrenMap: childrenMap); final meta = MetaPB(childrenMap: childrenMap);