build method

  1. @override
InlineSpan build()
override

Implementation

@override
InlineSpan build() {
  String? language = preConfig.language;
  try {
    final languageValue =
        (element.children?.first as m.Element).attributes['class']!;
    language = languageValue.split('-').last;
  } catch (e) {
    language = null;
    debugPrint('get language error:$e');
  }
  final splitContents = content
      .trim()
      .split(visitor.splitRegExp ?? WidgetVisitor.defaultSplitRegExp);
  if (splitContents.last.isEmpty) splitContents.removeLast();
  final codeBuilder = preConfig.builder;
  if (codeBuilder != null) {
    return WidgetSpan(child: codeBuilder.call(content, language ?? ''));
  }
  final widget = Container(
    decoration: preConfig.decoration,
    margin: preConfig.margin,
    padding: preConfig.padding,
    width: double.infinity,
    child: SingleChildScrollView(
      scrollDirection: Axis.horizontal,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: List.generate(splitContents.length, (index) {
          final currentContent = splitContents[index];
          return ProxyRichText(
            TextSpan(
              children: highLightSpans(
                currentContent,
                language: language ?? preConfig.language,
                theme: preConfig.theme,
                textStyle: style,
                styleNotMatched: preConfig.styleNotMatched,
              ),
            ),
            richTextBuilder: visitor.richTextBuilder,
          );
        }),
      ),
    ),
  );
  return WidgetSpan(
      child:
          preConfig.wrapper?.call(widget, content, language ?? '') ?? widget);
}