David NAISSE

added highlightBuilder, removed default padding on codeBuilder

... ... @@ -99,6 +99,7 @@ class MarkdownHelper {
You can use Markdown to format text easily. Here are some examples:
- `Highlighted Text`: `This text is highlighted`
- **Bold Text**: **This text is bold**
- *Italic Text*: *This text is italicized*
- [Link](https://www.example.com): [This is a link](https://www.example.com)
... ... @@ -266,6 +267,29 @@ Markdown and LaTeX can be powerful tools for formatting text and mathematical ex
style: const TextStyle(
fontSize: 15,
),
highlightBuilder: (context, text, style) {
return Container(
padding: const EdgeInsets.symmetric(horizontal: 4, vertical: 2),
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.secondaryContainer,
borderRadius: BorderRadius.circular(4),
border: Border.all(
color: Theme.of(context).colorScheme.secondary.withOpacity(0.5),
width: 1,
),
),
child: Text(
text,
style: TextStyle(
color: Theme.of(context).colorScheme.onSecondaryContainer,
fontFamily: 'monospace',
fontWeight: FontWeight.bold,
fontSize: style.fontSize != null ? style.fontSize! * 0.9 : 13.5,
height: style.height,
),
),
);
},
latexWorkaround: (tex) {
List<String> stack = [];
tex = tex.splitMapJoin(
... ...
... ... @@ -12,6 +12,7 @@ class GptMarkdownConfig {
this.followLinkColor = false,
this.codeBuilder,
this.sourceTagBuilder,
this.highlightBuilder,
this.maxLines,
this.overflow,
});
... ... @@ -32,6 +33,7 @@ class GptMarkdownConfig {
codeBuilder;
final int? maxLines;
final TextOverflow? overflow;
final Widget Function(BuildContext context, String text, TextStyle style)? highlightBuilder;
GptMarkdownConfig copyWith({
TextStyle? style,
... ... @@ -51,6 +53,7 @@ class GptMarkdownConfig {
codeBuilder,
final int? maxLines,
final TextOverflow? overflow,
final Widget Function(BuildContext context, String text, TextStyle style)? highlightBuilder,
}) {
return GptMarkdownConfig(
style: style ?? this.style,
... ... @@ -65,6 +68,7 @@ class GptMarkdownConfig {
sourceTagBuilder: sourceTagBuilder ?? this.sourceTagBuilder,
maxLines: maxLines ?? this.maxLines,
overflow: overflow ?? this.overflow,
highlightBuilder: highlightBuilder ?? this.highlightBuilder,
);
}
... ...
... ... @@ -34,6 +34,7 @@ class GptMarkdown extends StatelessWidget {
this.latexBuilder,
this.codeBuilder,
this.sourceTagBuilder,
this.highlightBuilder,
this.maxLines,
this.overflow,
});
... ... @@ -53,6 +54,7 @@ class GptMarkdown extends StatelessWidget {
final Widget Function(BuildContext context, String name, String code)?
codeBuilder;
final Widget Function(BuildContext, String, TextStyle)? sourceTagBuilder;
final Widget Function(BuildContext context, String text, TextStyle style)? highlightBuilder;
@override
Widget build(BuildContext context) {
... ... @@ -93,6 +95,7 @@ class GptMarkdown extends StatelessWidget {
maxLines: maxLines,
overflow: overflow,
sourceTagBuilder: sourceTagBuilder,
highlightBuilder: highlightBuilder,
),
));
}
... ...
... ... @@ -422,25 +422,37 @@ class HighlightedText extends InlineMd {
final GptMarkdownConfig config,
) {
var match = exp.firstMatch(text.trim());
var conf = config.copyWith(
style: config.style?.copyWith(
fontWeight: FontWeight.bold,
background: Paint()
..color = GptMarkdownTheme.of(context).highlightColor
..strokeCap = StrokeCap.round
..strokeJoin = StrokeJoin.round,
) ??
TextStyle(
fontWeight: FontWeight.bold,
background: Paint()
..color = GptMarkdownTheme.of(context).highlightColor
..strokeCap = StrokeCap.round
..strokeJoin = StrokeJoin.round,
),
);
var highlightedText = match?[1] ?? "";
if (config.highlightBuilder != null) {
return WidgetSpan(
alignment: PlaceholderAlignment.middle,
child: config.highlightBuilder!(
context,
highlightedText,
config.style ?? const TextStyle(),
),
);
}
var style = config.style?.copyWith(
fontWeight: FontWeight.bold,
background: Paint()
..color = GptMarkdownTheme.of(context).highlightColor
..strokeCap = StrokeCap.round
..strokeJoin = StrokeJoin.round,
) ??
TextStyle(
fontWeight: FontWeight.bold,
background: Paint()
..color = GptMarkdownTheme.of(context).highlightColor
..strokeCap = StrokeCap.round
..strokeJoin = StrokeJoin.round,
);
return TextSpan(
text: match?[1],
style: conf.style,
text: highlightedText,
style: style,
);
}
}
... ... @@ -892,11 +904,9 @@ class CodeBlockMd extends BlockMd {
String codes = this.exp.firstMatch(text)?[2] ?? "";
String name = this.exp.firstMatch(text)?[1] ?? "";
codes = codes.replaceAll(r"```", "").trim();
return Padding(
padding: const EdgeInsets.all(16.0),
child: config.codeBuilder != null
? config.codeBuilder?.call(context, name, codes)
: CodeField(name: name, codes: codes),
);
return config.codeBuilder != null
? config.codeBuilder!(context, name, codes)
: CodeField(name: name, codes: codes);
}
}
... ...