|
| 1 | +module Docs |
| 2 | + class Tanstack |
| 3 | + class EntriesFilter < Docs::EntriesFilter |
| 4 | + TYPES_BY_PREFIX = { |
| 5 | + 'reference/functions/' => 'Functions', |
| 6 | + 'reference/classes/' => 'Classes', |
| 7 | + 'reference/interfaces/' => 'Interfaces', |
| 8 | + 'reference/type-aliases/' => 'Type Aliases', |
| 9 | + 'reference/variables/' => 'Variables', |
| 10 | + 'guides/' => 'Guides', |
| 11 | + 'guide/' => 'Guides', |
| 12 | + 'plugins/' => 'Plugins', |
| 13 | + 'installation/' => 'Installation', |
| 14 | + 'routing/' => 'Routing', |
| 15 | + 'integrations/' => 'Integrations', |
| 16 | + 'how-to/' => 'How-to', |
| 17 | + 'eslint/' => 'ESLint' |
| 18 | + } |
| 19 | + |
| 20 | + # Router symbol pages are titled "useNavigate hook", "Link component", etc. |
| 21 | + SYMBOL_TYPES = { |
| 22 | + 'hook' => 'Hooks', |
| 23 | + 'function' => 'Functions', |
| 24 | + 'component' => 'Components', |
| 25 | + 'type' => 'Types', |
| 26 | + 'class' => 'Classes', |
| 27 | + 'interface' => 'Interfaces' |
| 28 | + } |
| 29 | + |
| 30 | + GETTING_STARTED = %w(installation quick-start devtools faq comparison decisions-on-dx typescript graphql react-native) |
| 31 | + |
| 32 | + def get_name |
| 33 | + return 'API Reference' if page_path == 'reference/index' |
| 34 | + name = heading_text(at_css('h1')) |
| 35 | + symbol_type ? name.sub(/\s+\S+\z/, '') : name |
| 36 | + end |
| 37 | + |
| 38 | + def get_type |
| 39 | + return 'API Reference' if page_path == 'reference/index' |
| 40 | + return symbol_type || 'API' if page_path.start_with?('api/') |
| 41 | + TYPES_BY_PREFIX.each { |prefix, type| return type if page_path.start_with?(prefix) } |
| 42 | + GETTING_STARTED.include?(page_path) ? 'Getting Started' : 'Other' |
| 43 | + end |
| 44 | + |
| 45 | + def additional_entries |
| 46 | + return [] unless page_path.start_with?('guides/', 'guide/') |
| 47 | + |
| 48 | + css('.prose h2[id]').each_with_object([]) do |node, entries| |
| 49 | + text = heading_text(node) |
| 50 | + entries << ["#{name}: #{text}", node['id']] unless text.empty? || text.casecmp?('Further Reading') |
| 51 | + end |
| 52 | + end |
| 53 | + |
| 54 | + private |
| 55 | + |
| 56 | + def page_path |
| 57 | + @page_path ||= slug.remove(%r{\Aframework/react/}) |
| 58 | + end |
| 59 | + |
| 60 | + def symbol_type |
| 61 | + return unless page_path.start_with?('api/router/') |
| 62 | + words = heading_text(at_css('h1')).split |
| 63 | + SYMBOL_TYPES[words.last.downcase] if words.length > 1 |
| 64 | + end |
| 65 | + |
| 66 | + def heading_text(node) |
| 67 | + node.content.remove('#').squish |
| 68 | + end |
| 69 | + end |
| 70 | + end |
| 71 | +end |
0 commit comments