buildWidgets method

List<Widget> buildWidgets(
  1. String data, {
  2. ValueCallback<List<Toc>>? onTocList,
  3. MarkdownConfig? config,
})

convert data to widgets onTocList can provider Toc list

Implementation

List<Widget> buildWidgets(String data,
    {ValueCallback<List<Toc>>? onTocList, MarkdownConfig? config}) {
  final mdConfig = config ?? MarkdownConfig.defaultConfig;
  final m.Document document = m.Document(
    extensionSet: extensionSet ?? m.ExtensionSet.gitHubFlavored,
    encodeHtml: false,
    inlineSyntaxes: inlineSyntaxList,
    blockSyntaxes: blockSyntaxList,
  );
  final regExp = splitRegExp ?? WidgetVisitor.defaultSplitRegExp;
  final List<String> lines = data.split(regExp);
  final List<m.Node> nodes = document.parseLines(lines);
  final List<Toc> tocList = [];
  final visitor = WidgetVisitor(
      config: mdConfig,
      generators: generators,
      textGenerator: textGenerator,
      richTextBuilder: richTextBuilder,
      splitRegExp: regExp,
      onNodeAccepted: (node, index) {
        onNodeAccepted?.call(node, index);
        if (node is HeadingNode) {
          final listLength = tocList.length;
          tocList.add(
              Toc(node: node, widgetIndex: index, selfIndex: listLength));
        }
      });
  final spans = visitor.visit(nodes);
  onTocList?.call(tocList);
  final List<Widget> widgets = [];
  for (var span in spans) {
    final textSpan = spanNodeBuilder?.call(span) ?? span.build();
    final richText = richTextBuilder?.call(textSpan) ?? Text.rich(textSpan);
    widgets.add(Padding(padding: linesMargin, child: richText));
  }
  return widgets;
}