gpt_markdown.dart
5.46 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import 'package:flutter/material.dart';
import 'package:gpt_markdown/custom_widgets/markdown_config.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_math_fork/flutter_math.dart';
import 'package:gpt_markdown/custom_widgets/custom_divider.dart';
import 'package:gpt_markdown/custom_widgets/custom_error_image.dart';
import 'package:gpt_markdown/custom_widgets/custom_rb_cb.dart';
import 'package:gpt_markdown/custom_widgets/selectable_adapter.dart';
import 'package:gpt_markdown/custom_widgets/unordered_ordered_list.dart';
import 'dart:math';
import 'custom_widgets/code_field.dart';
import 'custom_widgets/indent_widget.dart';
import 'custom_widgets/link_button.dart';
part 'theme.dart';
part 'markdown_component.dart';
part 'md_widget.dart';
/// This widget create a full markdown widget as a column view.
class GptMarkdown extends StatelessWidget {
const GptMarkdown(
this.data, {
super.key,
this.style,
this.followLinkColor = false,
this.textDirection = TextDirection.ltr,
this.latexWorkaround,
this.textAlign,
this.imageBuilder,
this.textScaler,
this.onLinkTab,
this.latexBuilder,
this.codeBuilder,
this.sourceTagBuilder,
this.highlightBuilder,
this.linkBuilder,
this.maxLines,
this.overflow,
this.orderedListBuilder,
this.unOrderedListBuilder,
this.components,
this.inlineComponents,
});
/// The direction of the text.
final TextDirection textDirection;
/// The data to be displayed.
final String data;
/// The style of the text.
final TextStyle? style;
/// The alignment of the text.
final TextAlign? textAlign;
/// The text scaler.
final TextScaler? textScaler;
/// The callback function to handle link clicks.
final void Function(String url, String title)? onLinkTab;
/// The LaTeX workaround.
final String Function(String tex)? latexWorkaround;
final int? maxLines;
/// The overflow.
final TextOverflow? overflow;
/// The LaTeX builder.
final LatexBuilder? latexBuilder;
/// Whether to follow the link color.
final bool followLinkColor;
/// The code builder.
final CodeBlockBuilder? codeBuilder;
/// The source tag builder.
final SourceTagBuilder? sourceTagBuilder;
/// The highlight builder.
final HighlightBuilder? highlightBuilder;
/// The link builder.
final LinkBuilder? linkBuilder;
/// The image builder.
final ImageBuilder? imageBuilder;
/// The ordered list builder.
final OrderedListBuilder? orderedListBuilder;
/// The unordered list builder.
final UnOrderedListBuilder? unOrderedListBuilder;
/// The list of components.
/// ```dart
/// List<MarkdownComponent> components = [
/// CodeBlockMd(),
/// NewLines(),
/// BlockQuote(),
/// ImageMd(),
/// ATagMd(),
/// TableMd(),
/// HTag(),
/// UnOrderedList(),
/// OrderedList(),
/// RadioButtonMd(),
/// CheckBoxMd(),
/// HrLine(),
/// StrikeMd(),
/// BoldMd(),
/// ItalicMd(),
/// LatexMath(),
/// LatexMathMultiLine(),
/// HighlightedText(),
/// SourceTag(),
/// IndentMd(),
/// ];
/// ```
final List<MarkdownComponent>? components;
/// The list of inline components.
/// ```dart
/// List<MarkdownComponent> inlineComponents = [
/// ImageMd(),
/// ATagMd(),
/// TableMd(),
/// StrikeMd(),
/// BoldMd(),
/// ItalicMd(),
/// LatexMath(),
/// LatexMathMultiLine(),
/// HighlightedText(),
/// SourceTag(),
/// ];
/// ```
final List<MarkdownComponent>? inlineComponents;
/// A method to remove extra lines inside block LaTeX.
// String _removeExtraLinesInsideBlockLatex(String text) {
// return text.replaceAllMapped(
// RegExp(r"\\\[(.*?)\\\]", multiLine: true, dotAll: true),
// (match) {
// String content = match[0] ?? "";
// return content.replaceAllMapped(RegExp(r"\n[\n\ ]+"), (match) => "\n");
// },
// );
// }
@override
Widget build(BuildContext context) {
String tex = data.trim();
// print("texBefore:\n:$tex");
//去除 $$前面的空格 会导致渲染出错
tex = tex.replaceAllMapped(
RegExp(r"(?<!\\)\s*\$\$(.*?)(?<!\\)\$\$", dotAll: true),
(match) => "\n\\[${match[1] ?? ""}\\]",
);
// print("texAfter:\n:$tex");
if (!tex.contains(r"\(")) {
tex = tex.replaceAllMapped(
RegExp(r"(?<!\\)\$(.*?)(?<!\\)\$"),
(match) => "\\(${match[1] ?? ""}\\)",
);
tex = tex.splitMapJoin(
RegExp(r"\[.*?\]|\(.*?\)"),
onNonMatch: (p0) {
return p0.replaceAll("\\\$", "\$");
},
);
}
// tex = _removeExtraLinesInsideBlockLatex(tex);
return ClipRRect(
child: MdWidget(
tex,
true,
config: GptMarkdownConfig(
textDirection: textDirection,
style: style,
onLinkTab: onLinkTab,
textAlign: textAlign,
textScaler: textScaler,
followLinkColor: followLinkColor,
latexWorkaround: latexWorkaround,
latexBuilder: latexBuilder,
codeBuilder: codeBuilder,
maxLines: maxLines,
overflow: overflow,
sourceTagBuilder: sourceTagBuilder,
highlightBuilder: highlightBuilder,
linkBuilder: linkBuilder,
imageBuilder: imageBuilder,
orderedListBuilder: orderedListBuilder,
unOrderedListBuilder: unOrderedListBuilder,
components: components,
inlineComponents: inlineComponents,
),
),
);
}
}