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'];
    if (languageValue != null) {
      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 ?? ''));
  }

  Widget codeContent = 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: preConfig.richTextBuilder ?? visitor.richTextBuilder,
      );
    }),
  );

  codeContent = preConfig.wrapCode
      ? codeContent
      : SingleChildScrollView(
          scrollDirection: Axis.horizontal,
          child: codeContent,
        );
  final contentWrapper = preConfig.contentWrapper;

  Widget widget;
  if (contentWrapper != null) {
    widget = contentWrapper.call(codeContent, content, language ?? '');
  } else {
    widget = Container(
      decoration: preConfig.decoration,
      margin: preConfig.margin,
      padding: preConfig.padding,
      width: double.infinity,
      child: codeContent,
    );
  }

  return WidgetSpan(
      child:
          preConfig.wrapper?.call(widget, content, language ?? '') ?? widget);
}