1
0
Эх сурвалжийг харах

Merge pull request #1972 from freeCodeCamp/mdn-fix-bcd

MDN scrapers: fix browser compatibility table
Simon Legner 2 жил өмнө
parent
commit
214da8c130

+ 16 - 31
lib/docs/filters/mdn/compat_tables.rb

@@ -71,27 +71,14 @@ module Docs
       end
 
       def request_bcd_uris
-        url = current_url.to_s + '/index.json'
-        response = Request.run url
-        index_json = JSON.load response.body
-
-        uris = []
-
-        index_json['doc']['body'].each do |element|
-          uris.push(element['value']['dataURL']) if element['type'] == 'browser_compatibility' and element['value']['dataURL']
-        end
-
-        uris.map! do |uri|
-          tmp_uri = URI.parse(base_url.to_s)
-          tmp_uri.path = uri
-          uri = tmp_uri.to_s
-        end
-
-        return uris
+        hydration = JSON.load at_css('#hydration').text
+        files = hydration['doc']['browserCompat'] || []
+        files.map { |file| "https://bcd.developer.mozilla.org/bcd/api/v0/current/#{file}.json" }
       end
 
       def generate_compatibility_table_wrapper(url)
         response = Request.run url
+        return "" unless response.success?
         @json_data = JSON.load(response.body)['data']
 
         html_table = generate_basic_html_table()
@@ -202,32 +189,30 @@ module Docs
 
           if version_removed[0]
             format_string = "<td class=bc-supports-no>"
+          elsif version_added[0] == 'No'
+            format_string = "<td class=bc-supports-no>"
+          elsif version_added[0] == '?'
+            format_string = "<td class=bc-supports-unknown>"
           else
-            if version_added[0] == 'No'
-              format_string = "<td class=bc-supports-no>"
-            elsif version_added[0] == '?'
-              format_string = "<td class=bc-supports-unknown>"
-            else
-              format_string = "<td class=bc-supports-yes>"
-            end
+            format_string = "<td class=bc-supports-yes>"
           end
 
           for value in (0..version_added.length-1) do
             if version_removed[value]
-              format_string += "<div>#{version_added[value]}-#{version_removed[value]}</div>"
+              version_string = "#{version_added[value]}–#{version_removed[value]}"
             else
-              if version_added[value] == 'No'
-                format_string += "<div>#{version_added[value]}</div>"
-              else
-                format_string += "<div>#{version_added[value]}</div>"
-              end
+              version_string = version_added[value]
             end
 
             if notes[value]
-              format_string += "<div>#{notes[value]}</div>"
+              format_string += "<details><summary>#{version_string}</summary>#{notes[value]}</details>"
+            else
+              format_string += "<div>#{version_string}</div>"
             end
           end
 
+          format_string += "</td>"
+
         else
           format_string = "<td class=bc-supports-unknown><div>?</div></td>"
         end

+ 1 - 1
lib/docs/scrapers/mdn/css.rb

@@ -1,6 +1,6 @@
 module Docs
   class Css < Mdn
-    # release = '2023-01-06'
+    # release = '2023-04-24'
     self.name = 'CSS'
     self.base_url = 'https://developer.mozilla.org/en-US/docs/Web/CSS'
     self.root_path = '/Reference'

+ 1 - 1
lib/docs/scrapers/mdn/dom.rb

@@ -1,7 +1,7 @@
 module Docs
   class Dom < Mdn
 
-    # release = '2023-01-06'
+    # release = '2023-04-24'
     self.name = 'Web APIs'
     self.slug = 'dom'
     self.base_url = 'https://developer.mozilla.org/en-US/docs/Web/API'

+ 1 - 1
lib/docs/scrapers/mdn/html.rb

@@ -2,7 +2,7 @@ module Docs
   class Html < Mdn
     prepend FixInternalUrlsBehavior
 
-    # release = '2023-01-06'
+    # release = '2023-04-24'
     self.name = 'HTML'
     self.base_url = 'https://developer.mozilla.org/en-US/docs/Web/HTML'
     self.links = {

+ 1 - 1
lib/docs/scrapers/mdn/javascript.rb

@@ -3,7 +3,7 @@ module Docs
     prepend FixInternalUrlsBehavior
     prepend FixRedirectionsBehavior
 
-    # release = '2023-01-06'
+    # release = '2023-04-24'
     self.name = 'JavaScript'
     self.base_url = 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference'
     self.links = {

+ 2 - 1
lib/docs/scrapers/mdn/mdn.rb

@@ -7,7 +7,8 @@ module Docs
       code: 'https://github.com/mdn/content'
     }
 
-    html_filters.push 'mdn/clean_html', 'mdn/compat_tables'
+    html_filters.insert_before 'container', 'mdn/compat_tables' # needs access to <script type="application/json" id="hydration">
+    html_filters.push 'mdn/clean_html'
 
     options[:container] = '#content > .main-page-content'
     options[:trailing_slash] = false

+ 1 - 1
lib/docs/scrapers/mdn/svg.rb

@@ -3,7 +3,7 @@ module Docs
     prepend FixInternalUrlsBehavior
     prepend FixRedirectionsBehavior
 
-    # release = '2022-09-06'
+    # release = '2023-04-24'
     self.name = 'SVG'
     self.base_url = 'https://developer.mozilla.org/en-US/docs/Web/SVG'
     self.links = {

+ 1 - 1
lib/docs/scrapers/mdn/web_extensions.rb

@@ -1,6 +1,6 @@
 module Docs
   class WebExtensions < Mdn
-    # release = '2022-09-06'
+    # release = '2023-04-24'
     self.name = 'Web Extensions'
     self.slug = 'web_extensions'
     self.links = {