You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Since this parser doesn't render HTML, I made this custom syntax for colours:
#f00[This is red] renders Text("This is red", style: TextStyle(color: 0xffff0000) #ff0000[This is red] renders Text("This is red", style: TextStyle(color: 0xffff0000) #7fff0000[This is red] renders Text("This is red", style: TextStyle(color: 0x7fff0000)
It accepts 3 (RGB), 6 (RRGGBB) or 8 (AARRGGBB) hex codes.
class_ColorSyntaxextendsMdInlineSyntax {
_ColorSyntax()
:super(
RegExp(r"#([0-9A-Fa-f]{3}|[0-9A-Fa-f]{6}|[0-9A-Fa-f]{8})\[([^\]]*)\]"),
);
@overrideMdInlineObject?parse(MdInlineParser parser, Match match) {
final color = match.group(1)!;
final text = match.group(2)!;
final markers = [
parser.consume(),
...parser.consumeBy(color.length),
parser.consume(),
];
final content = parser.consumeBy(text.length);
markers.add(parser.consume());
final children = content.map((e) =>MdText.fromSpan(e)).toList();
returnMdInlineElement(
"color",
attributes: {"color": color},
markers: markers,
children: children,
start: markers.first.start,
end: children.last.end,
);
}
}
class_ColorBuilderextendsMarkdownElementBuilder {
_ColorBuilder();
@overrideboolisBlock(element) =>false;
@overrideList<String> matchTypes =<String>["color"];
@overrideTextStyle?buildTextStyle(MarkdownElement element, TextStyle defaultStyle) {
final currentStyle = textStyle ?? textStyleMap?[element.type];
final colorAttr = element.attributes["color"]!;
final color =switch (colorAttr.length) {
3=>_parseColor3(colorAttr),
6=>_parseColor6(colorAttr),
8=>_parseColor8(colorAttr),
_ =>throwFormatException("Unrecognized color pattern '${colorAttr}'"),
};
return defaultStyle
.merge(parentStyle)
.merge(currentStyle)
.merge(TextStyle(color: color));
}
@overrideTextSpanbuildText(
String text,
MarkdownTreeElement parent,
) =>TextSpan(
text: text,
style: parent.style,
mouseCursor: renderer.mouseCursor,
);
Color_parseColor3(String color) {
final r = color[0] + color[0];
final g = color[1] + color[1];
final b = color[2] + color[2];
returnColor(int.parse("ff$r$g$b", radix:16));
}
Color_parseColor6(String color) {
returnColor(int.parse("ff${color}", radix:16));
}
Color_parseColor8(String color) {
returnColor(int.parse(color, radix:16));
}
}
The text was updated successfully, but these errors were encountered:
Since this parser doesn't render HTML, I made this custom syntax for colours:
#f00[This is red]
rendersText("This is red", style: TextStyle(color: 0xffff0000)
#ff0000[This is red]
rendersText("This is red", style: TextStyle(color: 0xffff0000)
#7fff0000[This is red]
rendersText("This is red", style: TextStyle(color: 0x7fff0000)
It accepts 3 (RGB), 6 (RRGGBB) or 8 (AARRGGBB) hex codes.
The text was updated successfully, but these errors were encountered: