docsTopic function

String docsTopic(
  1. String topic, {
  2. Catalog catalog = Catalog.core,
  3. TrustProfile? profile,
  4. List<DocPage>? pages,
  5. DesignSystem? designSystem,
})

One topic, sized for a tool result (a few hundred tokens): a page (its "When to use", minimal example and pitfalls, and the list of its sections), a section of a page by its anchor, or a node type, action type, trigger or behavior by name (or by its anchor, node-slider). What profile does not allow says so: it is documented, not available.

Implementation

String docsTopic(String topic,
    {Catalog catalog = Catalog.core, TrustProfile? profile, List<DocPage>? pages, DesignSystem? designSystem}) {
  // The design system's own documents and components first.
  if (designSystem != null) {
    final own = designSystemTopic(designSystem, topic);
    if (own != null) return own;
  }
  final p = profile ?? TrustProfile.agent;
  final slice = p.restrict(catalog);
  final ps = pages ?? docPages;
  var t = topic.trim().replaceFirst(RegExp(r'^[\w-]+\.md#?'), '');
  if (t.startsWith('page-')) t = t.substring(5);
  t = docAliases[t] ?? t;
  for (final page in ps) {
    if (page.id == t) return _pageTopic(page);
    for (final s in page.sections) {
      if (s.id == t) return _sectionTopic(page, s);
    }
    if (t == '${page.id}-when-to-use' || t == '${page.id}-minimal-example' || t == '${page.id}-pitfalls') {
      return _pageTopic(page);
    }
  }
  t = t.replaceFirst(RegExp(r'^(node|action|behavior|trigger)-'), '');
  if (t == 'scripts') return _scriptsTopic(p);
  if (t == 'triggers' || TriggerType.values.any((x) => x.name == t)) return _triggersTopic(t, ps);
  final w = catalog.widget(t);
  if (w != null) {
    final b = StringBuffer('# `${w.type}`\n\n');
    if (slice.widget(t) == null) {
      b.writeln('Not available here: this app does not allow this node type. Use another.\n');
    }
    if (w.doc.isNotEmpty) b.writeln('${firstSentences(w.doc, 3, max: 400)}\n');
    b.writeln(
      '- Takes: ${switch (w.children) {
        ChildModel.list => 'children${w.slots.isEmpty ? '' : ' (and slots ${w.slots.join(', ')})'}',
        ChildModel.slots => 'slots ${w.slots.join(', ')} (no children list)',
        ChildModel.none => 'no children',
      }}. Decoration: ${w.decorated ? 'yes' : 'no (fills, strokes and radius are ignored)'}.',
    );
    if (w.events.isNotEmpty) {
      b.writeln('- Triggers it fires: ${[for (final e in w.events) e.key].join(', ')} (the value as \${result}).');
    }
    b.writeln();
    b.writeln('| prop | type | |');
    b.writeln('|---|---|---|');
    for (final f in w.props.take(40)) {
      b.writeln('| `${f.key}` | ${propTypeLabel(f.type)} | ${firstSentences(f.doc, 1, max: 140)} |');
    }
    if (w.props.length > 40) b.writeln('\n${w.props.length - 40} more props not listed.');
    b.writeln();
    b.writeln(
      'Minimal: ${jsonEncode({
        'type': w.type,
        'layout': {
          'width': {'fill': 358},
          'height': w.children == ChildModel.none ? 48 : {'hug': 120},
        },
        if (w.children == ChildModel.list) 'children': ['${w.type}Child'],
        if (w.slots.isNotEmpty && w.children == ChildModel.slots) 'slots': {w.slots.first: '${w.type}${_cap(w.slots.first)}'},
      })}',
    );
    b.writeln('\nPatterns and pitfalls: ui_docs "${_pageOf(w.type, ps)}".');
    return b.toString();
  }
  final a = catalog.action(t);
  if (a != null) {
    final b = StringBuffer('# action `${a.type}`\n\n');
    if (slice.action(t) == null) b.writeln('Not available here: this app does not allow this action type.\n');
    if (a.doc.isNotEmpty) b.writeln('${firstSentences(a.doc, 3, max: 400)}\n');
    b.writeln('| field | type | |');
    b.writeln('|---|---|---|');
    for (final f in a.fields) {
      b.writeln('| `${f.key}` | ${propTypeLabel(f.type)} | ${firstSentences(f.doc, 1, max: 140)} |');
    }
    b.writeln(
      '\nIn a reaction: "actions": [{"trigger": {"type": "click"}, "actions": [{"type": "${a.type}", ...}]}]. '
      'More: ui_docs "${_pageOf(a.type, ps)}".',
    );
    return b.toString();
  }
  if (_behavior(t) case final f?) {
    final b = StringBuffer('# behavior `${f.key}`\n\nA node key beside "layout" and "props".\n\n${_fieldLine(f)}\n');
    final item = f.type.item;
    final fields = f.type.kind == PropKind.object
        ? f.type.fields
        : (item != null && item.kind == PropKind.object ? item.fields : const []);
    if (fields.isNotEmpty) {
      b.writeln('\n| field | type | |\n|---|---|---|');
      for (final x in fields.take(30)) {
        b.writeln('| `${x.key}` | ${propTypeLabel(x.type)} | ${firstSentences(x.doc, 1, max: 140)} |');
      }
    }
    b.writeln('\nMore: ui_docs "${_pageOf(f.key, ps)}".');
    return b.toString();
  }
  for (final page in ps) {
    if (page.items.contains(t)) {
      return '# `$t`\n\nNot available here: `$t` belongs to a feature library this app does not include. '
          'It is documented on the ${page.title} page (ui_docs "${page.id}").\n';
    }
  }
  if (RegExp(r'^(type|enum)-(\w+)$').firstMatch(t) case final m?) {
    if (_findType(catalog, m[2]!) case final type?) return _typeTopic(type);
  }
  return 'No topic "$topic". Topics: the pages ${[for (final page in ps) '"${page.id}"'].join(', ')}; a section by its id; '
      'any node type, action, trigger or behavior by name.';
}