markdown_config.dart
5.79 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
import 'package:flutter/material.dart';
import 'package:gpt_markdown/gpt_markdown.dart';
/// A builder function for the ordered list.
typedef OrderedListBuilder =
Widget Function(
BuildContext context,
String no,
Widget child,
GptMarkdownConfig config,
);
/// A builder function for the unordered list.
typedef UnOrderedListBuilder =
Widget Function(
BuildContext context,
Widget child,
GptMarkdownConfig config,
);
/// A builder function for the source tag.
typedef SourceTagBuilder =
Widget Function(BuildContext context, String content, TextStyle textStyle);
/// A builder function for the code block.
typedef CodeBlockBuilder =
Widget Function(
BuildContext context,
String name,
String code,
bool closed,
);
/// A builder function for the LaTeX.
typedef LatexBuilder =
Widget Function(
BuildContext context,
String tex,
TextStyle textStyle,
bool inline,
);
/// A builder function for the link.
typedef LinkBuilder =
Widget Function(
BuildContext context,
String text,
String url,
TextStyle style,
);
/// A builder function for the highlight.
typedef HighlightBuilder =
Widget Function(BuildContext context, String text, TextStyle style);
/// A builder function for the image.
typedef ImageBuilder = Widget Function(BuildContext context, String imageUrl);
/// A configuration class for the GPT Markdown component.
///
/// The [GptMarkdownConfig] class is used to configure the GPT Markdown component.
/// It takes a [style] parameter to set the style of the text,
/// a [textDirection] parameter to set the direction of the text,
/// and an optional [onLinkTab] parameter to handle link clicks.
class GptMarkdownConfig {
const GptMarkdownConfig({
this.style,
this.textDirection = TextDirection.ltr,
this.onLinkTab,
this.textAlign,
this.textScaler,
this.latexWorkaround,
this.latexBuilder,
this.followLinkColor = false,
this.codeBuilder,
this.sourceTagBuilder,
this.highlightBuilder,
this.orderedListBuilder,
this.unOrderedListBuilder,
this.linkBuilder,
this.imageBuilder,
this.maxLines,
this.overflow,
this.components,
this.inlineComponents,
});
/// The direction of the text.
final TextDirection textDirection;
/// 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;
/// The LaTeX builder.
final LatexBuilder? latexBuilder;
/// The source tag builder.
final SourceTagBuilder? sourceTagBuilder;
/// Whether to follow the link color.
final bool followLinkColor;
/// The code builder.
final CodeBlockBuilder? codeBuilder;
/// The Ordered List builder.
final OrderedListBuilder? orderedListBuilder;
/// The Unordered List builder.
final UnOrderedListBuilder? unOrderedListBuilder;
/// The maximum number of lines.
final int? maxLines;
/// The overflow.
final TextOverflow? overflow;
/// The highlight builder.
final HighlightBuilder? highlightBuilder;
/// The link builder.
final LinkBuilder? linkBuilder;
/// The image builder.
final ImageBuilder? imageBuilder;
/// The list of components.
final List<MarkdownComponent>? components;
/// The list of inline components.
final List<MarkdownComponent>? inlineComponents;
/// A copy of the configuration with the specified parameters.
GptMarkdownConfig copyWith({
TextStyle? style,
TextDirection? textDirection,
final void Function(String url, String title)? onLinkTab,
final TextAlign? textAlign,
final TextScaler? textScaler,
final String Function(String tex)? latexWorkaround,
final LatexBuilder? latexBuilder,
final SourceTagBuilder? sourceTagBuilder,
final bool? followLinkColor,
final CodeBlockBuilder? codeBuilder,
final int? maxLines,
final TextOverflow? overflow,
final HighlightBuilder? highlightBuilder,
final LinkBuilder? linkBuilder,
final ImageBuilder? imageBuilder,
final OrderedListBuilder? orderedListBuilder,
final UnOrderedListBuilder? unOrderedListBuilder,
final List<MarkdownComponent>? components,
final List<MarkdownComponent>? inlineComponents,
}) {
return GptMarkdownConfig(
style: style ?? this.style,
textDirection: textDirection ?? this.textDirection,
onLinkTab: onLinkTab ?? this.onLinkTab,
textAlign: textAlign ?? this.textAlign,
textScaler: textScaler ?? this.textScaler,
latexWorkaround: latexWorkaround ?? this.latexWorkaround,
latexBuilder: latexBuilder ?? this.latexBuilder,
followLinkColor: followLinkColor ?? this.followLinkColor,
codeBuilder: codeBuilder ?? this.codeBuilder,
sourceTagBuilder: sourceTagBuilder ?? this.sourceTagBuilder,
maxLines: maxLines ?? this.maxLines,
overflow: overflow ?? this.overflow,
highlightBuilder: highlightBuilder ?? this.highlightBuilder,
linkBuilder: linkBuilder ?? this.linkBuilder,
imageBuilder: imageBuilder ?? this.imageBuilder,
orderedListBuilder: orderedListBuilder ?? this.orderedListBuilder,
unOrderedListBuilder: unOrderedListBuilder ?? this.unOrderedListBuilder,
components: components ?? this.components,
inlineComponents: inlineComponents ?? this.inlineComponents,
);
}
/// A method to get a rich text widget from an inline span.
Text getRich(InlineSpan span) {
return Text.rich(
span,
textDirection: textDirection,
textScaler: textScaler,
textAlign: textAlign,
maxLines: maxLines,
overflow: overflow,
);
}
}