generate static method

List<InlineSpan> generate(
  1. BuildContext context,
  2. String text,
  3. GptMarkdownConfig config
)

Generate widget for markdown widget

Implementation

static List<InlineSpan> generate(
  BuildContext context,
  String text,
  final GptMarkdownConfig config,
) {
  List<InlineSpan> spans = [];
  List<String> regexes =
      components.map<String>((e) => e.exp.pattern).toList();
  final combinedRegex = RegExp(
    regexes.join("|"),
    multiLine: true,
    dotAll: true,
  );
  List<String> elements = [];
  text.splitMapJoin(
    combinedRegex,
    onMatch: (p0) {
      String element = p0[0] ?? "";
      elements.add(element);
      for (var each in components) {
        if (each.exp.hasMatch(element)) {
          if (each is InlineMd) {
            spans.add(each.span(
              context,
              element,
              config,
            ));
          } else {
            if (each is BlockMd) {
              spans.addAll([
                TextSpan(
                  text: "\n ",
                  style: TextStyle(
                    fontSize: 0,
                    height: 0,
                    color: config.style?.color,
                  ),
                ),
                each.span(
                  context,
                  element,
                  config,
                ),
                TextSpan(
                  text: "\n ",
                  style: TextStyle(
                    fontSize: 0,
                    height: 0,
                    color: config.style?.color,
                  ),
                ),
              ]);
            }
          }
          return "";
        }
      }
      return "";
    },
    onNonMatch: (p0) {
      spans.add(
        TextSpan(
          text: p0,
          style: config.style,
        ),
      );
      return "";
    },
  );

  return spans;
}