markdownSpansFor function

List<InlineSpan> markdownSpansFor(
  1. String text,
  2. TextStyle base,
  3. MarkdownStyle style
)

Markdown spans for text in base (see markdownSpans).

Implementation

List<InlineSpan> markdownSpansFor(
  String text,
  TextStyle base,
  MarkdownStyle style,
) {
  final Color? color = base.color;
  final TextStyle dim = base.copyWith(
    color: style.markerColor?.toFlutterColor() ?? color?.withValues(alpha: 0.35),
  );
  final TextStyle bullet = base.copyWith(
    color: style.bulletColor?.toFlutterColor() ?? color,
    fontWeight: style.showMarkers ? FontWeight.w600 : null,
  );
  final List<InlineSpan> out = <InlineSpan>[];
  final List<String> lines = text.split('\n');
  for (int i = 0; i < lines.length; i++) {
    String line = lines[i];
    final bool last = i == lines.length - 1;
    final bool blank = line.trim().isEmpty;
    if (blank && !style.showMarkers) {
      // A blank line between paragraphs is a short gap, not a full line.
      if (!last && out.isNotEmpty) {
        out.add(
          TextSpan(
            text: '$line\n',
            style: base.copyWith(
              fontSize: style.paragraphGap <= 0 ? 0.1 : style.paragraphGap,
              height: 1,
            ),
          ),
        );
      }
      continue;
    }
    final RegExpMatch? m = _bullet.firstMatch(line) ?? _numbered.firstMatch(line);
    if (m != null) {
      if (style.showMarkers) {
        out.add(TextSpan(text: line.substring(0, m.end), style: bullet));
      } else {
        final bool numbered = m.pattern == _numbered;
        out.add(
          TextSpan(
            text: '${m[1]}${numbered ? '${m[2]}.' : '•'} ',
            style: bullet,
          ),
        );
      }
      line = line.substring(m.end);
    }
    out.addAll(_inlineSpans(line, base, style, dim));
    if (!last) out.add(TextSpan(text: '\n', style: base));
  }
  return out;
}