-
Notifications
You must be signed in to change notification settings - Fork 293
Expand file tree
/
Copy pathdocument_markdown_decoder.dart
More file actions
134 lines (112 loc) · 3.71 KB
/
document_markdown_decoder.dart
File metadata and controls
134 lines (112 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import 'dart:convert';
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:appflowy_editor/src/plugins/markdown/decoder/custom_syntaxes/underline_syntax.dart';
import 'package:collection/collection.dart';
import 'package:markdown/markdown.dart' as md;
import 'custom_syntaxes/formula_syntax.dart';
class DocumentMarkdownDecoder extends Converter<String, Document> {
DocumentMarkdownDecoder({
this.markdownElementParsers = const [],
this.inlineSyntaxes = const [],
});
final List<CustomMarkdownParser> markdownElementParsers;
final List<md.InlineSyntax> inlineSyntaxes;
@override
Document convert(String input) {
final formattedMarkdown = _formatMarkdown(input);
final List<md.Node> mdNodes = md.Document(
extensionSet: md.ExtensionSet.gitHubFlavored,
inlineSyntaxes: [
...inlineSyntaxes,
FormulaInlineSyntax(),
UnderlineInlineSyntax(),
],
encodeHtml: false,
).parse(formattedMarkdown);
final List<md.Node> processedNodes = _processNodes(mdNodes);
final document = Document.blank();
final nodes = processedNodes
.map((e) => _parseNode(e))
.nonNulls
.flattened
.toList(growable: false); // avoid lazy evaluation
if (nodes.isNotEmpty) {
document.insert([0], nodes);
}
return document;
}
List<md.Node> _processNodes(List<md.Node> nodes) {
List<md.Node> result = [];
for (var node in nodes) {
if (node is md.Element &&
node.children != null &&
node.children!.length > 1) {
// Store image elements that need to be extracted
List<int> imageIndices = [];
// Find all image elements
for (var i = 0; i < node.children!.length; i++) {
var child = node.children![i];
if (child is md.Element && child.tag == 'img') {
imageIndices.add(i);
}
}
if (imageIndices.isNotEmpty) {
// Extract images from back to front to maintain correct indices
for (var i = imageIndices.length - 1; i >= 0; i--) {
var index = imageIndices[i];
var imageElement = node.children!.removeAt(index);
// Create a paragraph element containing the image
result.add(md.Element('p', [imageElement]));
}
// Add the original node if it still has children
if (node.children!.isNotEmpty) {
result.add(node);
}
} else {
result.add(node);
}
} else {
result.add(node);
}
}
return result;
}
// handle node itself and its children
List<Node> _parseNode(md.Node mdNode) {
List<Node> nodes = [];
for (final parser in markdownElementParsers) {
nodes = parser.transform(
mdNode,
markdownElementParsers,
);
if (nodes.isNotEmpty) {
break;
}
}
if (nodes.isEmpty) {
AppFlowyEditorLog.editor.debug(
'empty result from node: $mdNode, text: ${mdNode.textContent}',
);
}
return nodes;
}
String _formatMarkdown(String markdown) {
// Rule 1: single '\n' between text and image, add double '\n'
String result = markdown.replaceAllMapped(
RegExp(r'([^\n])\n!\[([^\]]*)\]\(([^)]+)\)', multiLine: true),
(match) {
final text = match[1] ?? '';
final altText = match[2] ?? '';
final url = match[3] ?? '';
return '$text\n\n';
},
);
// Rule 2: without '\n' between text and image, add double '\n'
result = result.replaceAllMapped(
RegExp(r'([^\n])!\[([^\]]*)\]\(([^)]+)\)'),
(match) => '${match[1]}\n\n![${match[2]}](${match[3]})',
);
// Add another rules here.
return result;
}
}