pageFor method

Component? pageFor(
  1. String path, {
  2. String? query,
  3. int page = 1,
})

Resolve o caminho no componente correspondente, ou null se não existir. query é o termo de busca (/blog?q=...) e page a página (?page=N).

Implementation

Component? pageFor(String path, {String? query, int page = 1}) {
  if (path == '/blog') {
    return BlogIndexPage(
      posts: _published,
      taxonomy: taxonomy,
      query: query,
      page: page,
    );
  }
  if (path == '/blog/categoria') {
    return TaxonomyIndexPage(
      title: 'Categorias',
      description: 'Todas as categorias do blog.',
      links: [
        for (final c in taxonomy.categories)
          (
            label: c.category.segments.join(' / '),
            path: c.category.path,
            count: c.posts.length,
          ),
      ],
    );
  }
  if (path == '/blog/tag') {
    return TaxonomyIndexPage(
      title: 'Tags',
      description: 'Todas as tags do blog.',
      links: [
        for (final t in taxonomy.tags)
          (label: '#${t.tag.name}', path: t.tag.path, count: t.posts.length),
      ],
    );
  }
  if (path.startsWith('/blog/categoria/')) {
    final key = path.substring('/blog/categoria/'.length);
    final archive = _first(
      taxonomy.categories.where((c) => c.category.key == key),
    );
    if (archive == null) return null;
    return ArchivePage(
      title: 'Categoria: ${archive.category.name}',
      description: 'Artigos em ${archive.category.name}.',
      posts: archive.posts,
      basePath: archive.category.path,
      page: page,
    );
  }
  if (path.startsWith('/blog/tag/')) {
    final slug = path.substring('/blog/tag/'.length);
    final archive = _first(taxonomy.tags.where((t) => t.tag.slug == slug));
    if (archive == null) return null;
    return ArchivePage(
      title: 'Tag: ${archive.tag.name}',
      description: 'Artigos com a tag ${archive.tag.name}.',
      posts: archive.posts,
      basePath: archive.tag.path,
      page: page,
    );
  }
  // /blog/<slug>
  final slug = path.substring('/blog/'.length);
  final post = _first(_published.where((p) => p.slug == slug));
  if (post == null) return null;
  return BlogPostPage(post: post, htmlBody: _md.toHtml(post.bodyMarkdown));
}