docsIndex function

String docsIndex({
  1. Catalog catalog = Catalog.core,
  2. TrustProfile? profile,
  3. BaseForm form = BaseForm.runtime,
  4. List<DocPage>? pages,
  5. List<DocTask>? tasks,
})

The index the base ends with: the task table (need → section) and the pages with what each documents. In the runtime form, every entry is a ui_docs topic.

Implementation

String docsIndex({
  Catalog catalog = Catalog.core,
  TrustProfile? profile,
  BaseForm form = BaseForm.runtime,
  List<DocPage>? pages,
  List<DocTask>? tasks,
}) {
  final ps = pages ?? docPages;
  final ts = tasks ?? docTasks;
  final runtime = form == BaseForm.runtime;
  String fileOf(String anchor) {
    for (final p in ps) {
      if (anchor == 'page-${p.id}' || p.sections.any((s) => s.id == anchor)) return p.file;
    }
    for (final p in ps) {
      if (p.items.any((i) => RegExp(r'^(node|action|behavior|trigger)-').hasMatch(anchor) && anchor.endsWith('-$i'))) {
        return p.file;
      }
    }
    for (final p in ps) {
      if (anchor.startsWith('${p.id}-')) return p.file;
    }
    return anchor.startsWith('base-') ? 'STARTER.md' : 'index.md';
  }

  final b = StringBuffer();
  b.writeln(
    runtime
        ? 'Look up anything below with ui_docs {"topic": "<topic>"}: a page, a section, or any node type, action, trigger or behavior by name.'
        : 'Every page is in this folder; `index.md` has the full task table and an A–Z of every name.',
  );
  b.writeln();
  b.writeln('| You need | ${runtime ? 'ui_docs topic' : 'Open'} |');
  b.writeln('|---|---|');
  for (final t in ts) {
    b.writeln(
      runtime ? '| ${t.need} | "${t.anchor}" |' : '| ${t.need} | [${t.anchor}](${fileOf(t.anchor)}#${t.anchor}) |',
    );
  }
  b.writeln();
  for (final p in ps) {
    final items = runtime
        ? [
            for (final i in p.items)
              if (_allowed(catalog, profile, i)) i,
          ]
        : p.items;
    b.writeln(
      '- ${runtime ? '"${p.id}"' : '[${p.file}](${p.file})'}: ${p.title}.'
      '${items.isEmpty ? '' : ' Documents ${items.join(', ')}.'}',
    );
  }
  return toRuntimeLinksIf(b.toString(), runtime);
}