Skip to content

Bring projects, wikis, and teams together with AI. AppFlowy is the AI collaborative workspace where you achieve more without losing control of your data. The leading open source Notion alternative.

fix : Code block parser implementation (#2046)

Gunjan Lunkad3039f0427fe7
diff --git a/frontend/appflowy_flutter/lib/plugins/document/application/share_bloc.dart b/frontend/appflowy_flutter/lib/plugins/document/application/share_bloc.dart
index 92c2f347a2..ddb4fe915d 100644
--- a/frontend/appflowy_flutter/lib/plugins/document/application/share_bloc.dart
+++ b/frontend/appflowy_flutter/lib/plugins/document/application/share_bloc.dart
@@ -3,6 +3,7 @@ import 'dart:io';
import 'package:appflowy/plugins/document/application/share_service.dart';
import 'package:appflowy/plugins/document/presentation/plugins/parsers/divider_node_parser.dart';
import 'package:appflowy/plugins/document/presentation/plugins/parsers/math_equation_node_parser.dart';
+import 'package:appflowy/plugins/document/presentation/plugins/parsers/code_block_node_parser.dart';
import 'package:appflowy_backend/protobuf/flowy-document/entities.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-folder/view.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
@@ -53,6 +54,7 @@ class DocShareBloc extends Bloc<DocShareEvent, DocShareState> {
return documentToMarkdown(document, customParsers: [
const DividerNodeParser(),
const MathEquationNodeParser(),
+ const CodeBlockNodeParser(),
]);
}
}
diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/plugins/parsers/code_block_node_parser.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/plugins/parsers/code_block_node_parser.dart
new file mode 100644
index 0000000000..88ec444dec
--- /dev/null
+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/plugins/parsers/code_block_node_parser.dart
@@ -0,0 +1,13 @@
+import 'package:appflowy_editor/appflowy_editor.dart';
+
+class CodeBlockNodeParser extends NodeParser {
+ const CodeBlockNodeParser();
+
+ @override
+ String get id => 'code_block';
+
+ @override
+ String transform(Node node) {
+ return '```\n${node.attributes['code_block']}\n```';
+ }
+}
diff --git a/frontend/appflowy_flutter/test/unit_test/editor/share_markdown_test.dart b/frontend/appflowy_flutter/test/unit_test/editor/share_markdown_test.dart
index 77311f395d..48afb2b614 100644
--- a/frontend/appflowy_flutter/test/unit_test/editor/share_markdown_test.dart
+++ b/frontend/appflowy_flutter/test/unit_test/editor/share_markdown_test.dart
@@ -1,5 +1,6 @@
import 'dart:convert';
+import 'package:appflowy/plugins/document/presentation/plugins/parsers/code_block_node_parser.dart';
import 'package:appflowy/plugins/document/presentation/plugins/parsers/divider_node_parser.dart';
import 'package:appflowy/plugins/document/presentation/plugins/parsers/math_equation_node_parser.dart';
import 'package:appflowy_editor/appflowy_editor.dart';
@@ -31,7 +32,31 @@ void main() {
]);
expect(result, r'$$E = MC^2$$');
});
-
+ // Changes
+ test('code block', () {
+ const text = '''
+{
+ "document":{
+ "type":"editor",
+ "children":[
+ {
+ "type":"code_block",
+ "attributes":{
+ "code_block":"Some Code"
+ }
+ }
+ ]
+ }
+}
+''';
+ final document = Document.fromJson(
+ Map<String, Object>.from(json.decode(text)),
+ );
+ final result = documentToMarkdown(document, customParsers: [
+ const CodeBlockNodeParser(),
+ ]);
+ expect(result, '```\nSome Code\n```');
+ });
test('divider', () {
const text = '''
{