From aeee307bf27aaf0a2e611084e86cb8e5d99c0202 Mon Sep 17 00:00:00 2001 From: Jason Rudolph Date: Mon, 1 Jul 2019 12:31:22 -0400 Subject: [PATCH] Begin populating search index with API docs With these changes, we populate the search index with all instance methods present in the latest version of Atom. Next up, we'll need to add classes, class methods, and instance properties to the search index. Co-Authored-By: Antonio Scandurra --- content/search.json | 15 +++++++++++++++ lib/helpers.rb | 1 + lib/search.rb | 19 +++++++++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 lib/search.rb diff --git a/content/search.json b/content/search.json index b2a4ad278b..ceb3f1c2f8 100644 --- a/content/search.json +++ b/content/search.json @@ -13,6 +13,21 @@ }, <% end %> <% end %> + + <% latest_atom_version_apis.each do |klass| %> + <% klass.fetch('instanceMethods', []).each do |instance_method| %> + <% body = compress(safe_embed(instance_method['description'])) %> + { + "title": "<%= klass['name'] %>::<%= instance_method['name'] %>", + "category": "", + "category_url": "", + "url": "/api/<%= latest_atom_version_number %>/<%= klass['name'] %>/#instance-<%= instance_method['name'] %>", + "body": "<%= body %>", + "excerpt": "<%= body[0..140] %>…" + }, + <% end %> + <% end %> + {} ] } diff --git a/lib/helpers.rb b/lib/helpers.rb index d61bb7664d..acb5be9bd3 100644 --- a/lib/helpers.rb +++ b/lib/helpers.rb @@ -5,6 +5,7 @@ require 'active_support/core_ext/string' require_relative 'chapters' +require_relative 'search' require_relative 'sections' require_relative 'strings' diff --git a/lib/search.rb b/lib/search.rb new file mode 100644 index 0000000000..b1eb02135f --- /dev/null +++ b/lib/search.rb @@ -0,0 +1,19 @@ +require('json') + +def latest_atom_version_apis + items = @items.find_all("/api/v#{latest_atom_version_number}/*.json") + items.map { |item| JSON.parse(File.read(item.raw_filename)) } +end + +def latest_atom_version_number + @latest_atom_version_number ||= begin + versions = @items.find_all('/api/*/*.json').map { |item| item.path.split('/')[2] }.uniq + + exploded_version_numbers = versions.map { |version| + version_without_prefix = version[1..-1] + version_without_prefix.split('.') + } + + exploded_version_numbers.sort.last.join('.') + end +end