Toggle navigation
Toggle navigation
This project
Loading...
Sign in
flutter_package
/
gpt_markdown
Go to a project
Toggle navigation
Projects
Groups
Snippets
Help
Toggle navigation pinning
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Authored by
David NAISSE
2025-01-09 13:23:06 -0500
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
16073720497716d7f185818e3ae5f62478929a94
16073720
1 parent
330069b1
added highlightBuilder, removed default padding on codeBuilder
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
52 additions
and
11 deletions
example/lib/main.dart
lib/custom_widgets/markdow_config.dart
lib/gpt_markdown.dart
lib/markdown_component.dart
example/lib/main.dart
View file @
1607372
...
...
@@ -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(
...
...
lib/custom_widgets/markdow_config.dart
View file @
1607372
...
...
@@ -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
,
);
}
...
...
lib/gpt_markdown.dart
View file @
1607372
...
...
@@ -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
,
),
));
}
...
...
lib/markdown_component.dart
View file @
1607372
...
...
@@ -422,8 +422,20 @@ class HighlightedText extends InlineMd {
final
GptMarkdownConfig
config
,
)
{
var
match
=
exp
.
firstMatch
(
text
.
trim
());
var
conf
=
config
.
copyWith
(
style:
config
.
style
?.
copyWith
(
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
...
...
@@ -436,11 +448,11 @@ class HighlightedText extends InlineMd {
..
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
);
}
}
...
...
Please
register
or
login
to post a comment