buildTextSpan function

InlineSpan buildTextSpan(
  1. BuildContext context,
  2. Node node, {
  3. void onLinkTap(
    1. String url,
    2. String title
    )?,
  4. void onHashtagTap(
    1. String tag
    )?,
  5. Widget? imageBuilder(
    1. String src,
    2. String alt
    )?,
})

Implementation

InlineSpan buildTextSpan(
  BuildContext context,
  md.Node node, {
void Function(String url, String title)? onLinkTap,
  void Function(String tag)? onHashtagTap,
  Widget? Function(String src, String alt)? imageBuilder,
}) {
  if (node is md.Text) {
    final text = node.textContent;
    if (onHashtagTap != null && text.contains('#')) {
      final spans = _splitTextByHashtags(text, onHashtagTap, context);
      if (spans.length == 1) return spans.first;
      return TextSpan(children: spans);
    }
    return TextSpan(text: text);
  }
  if (node is md.UnparsedContent) {
    return TextSpan(text: node.textContent);
  }
  if (node is md.Element) {
    final theme = LdTheme.of(context);
    final children =
        node.children
            ?.map(
              (e) => buildTextSpan(
                context,
                e,
                onLinkTap: onLinkTap,
                onHashtagTap: onHashtagTap,
                imageBuilder: imageBuilder,
              ),
            )
            .toList() ??
        [];
    return switch (node.tag) {
      'strong' => TextSpan(
        children: children,
        style: const TextStyle(fontWeight: FontWeight.bold),
      ),
      'em' || 'i' => TextSpan(
        children: children,
        style: const TextStyle(fontStyle: FontStyle.italic),
      ),
      'a' => _buildAnchorSpan(
        context,
        node,
        children,
        onLinkTap: onLinkTap,
      ),
      'sup' => _buildSuperscriptSpan(
        context,
        node,
        children,
        onLinkTap: onLinkTap,
        onHashtagTap: onHashtagTap,
        imageBuilder: imageBuilder,
      ),
      'code' => TextSpan(
        children: children,
        text: children.isEmpty ? node.textContent : null,
        style: TextStyle(
          fontFamily: theme.monoFontFamily,
          package: theme.monoFontFamilyPackage,
          fontSize: _kCodeFontSize,
          background: Paint()..color = theme.neutralShade(3),
        ),
      ),
      'br' => const TextSpan(text: '\n'),
      'p' => TextSpan(children: children),
      _ => TextSpan(children: children),
    };
  }
  return TextSpan(text: node.textContent);
}