From 095d66f19f61912622821478c9dd67d345174e8a Mon Sep 17 00:00:00 2001 From: Yash Agarwal <235240049+yashagarwal6@users.noreply.github.com> Date: Wed, 2 Dec 2026 01:09:50 +0530 Subject: [PATCH] Update webpack 5 documentation (5.113.0) --- lib/docs/filters/webpack/clean_html.rb | 17 +++++--- lib/docs/filters/webpack/entries.rb | 27 ++++++++---- lib/docs/scrapers/webpack.rb | 5 ++- test/lib/docs/scrapers/webpack_test.rb | 60 ++++++++++++++++++++++++++ 4 files changed, 94 insertions(+), 15 deletions(-) create mode 100644 test/lib/docs/scrapers/webpack_test.rb diff --git a/lib/docs/filters/webpack/clean_html.rb b/lib/docs/filters/webpack/clean_html.rb index b57190a94c..bdf4a819c0 100644 --- a/lib/docs/filters/webpack/clean_html.rb +++ b/lib/docs/filters/webpack/clean_html.rb @@ -2,15 +2,24 @@ module Docs class Webpack class CleanHtmlFilter < Filter def call + if version.to_f >= 5 + @doc = at_css('#main-content > .markdown') + # The text cleanup removes empty elements, so keep fragment IDs on headings. + css('h2 > span[id], h3 > span[id], h4 > span[id], h5 > span[id], h6 > span[id]').each do |anchor| + anchor.parent['id'] = anchor['id'] + anchor.remove + end + end at_css('h1').content = 'webpack' if root_page? css('hr', '.page__edit', 'hr + h3:contains("Contributors")', 'hr + h2:contains("Contributors")', 'td > .title', '.adjacent-links', '.contributors', '.icon-link', '#maintainers', '#maintainers + table', '#maintainer', '#maintainer + table', - '.page-links__link', '.page-links__gap').remove + '.page-links__link', '.page-links__gap', 'a:has(> .header-link)', + 'button.copy-button', '[class*="print:hidden"]').remove - css('> div', '.tip-content', '.header span').each do |node| + css('> div', '.tip-content', '.header span', '.code-block-wrapper').each do |node| node.before(node.children).remove end @@ -20,10 +29,6 @@ def call node.parent.content = node.parent.content end - css('*').each do |node| - node.remove if node['class'] && node['class'][/print:hidden/] - end - # for webpack-contrib /loaders and /plugins shields = at_css('img[src*="shields.io"]') shields.ancestors('p')[0].remove if shields diff --git a/lib/docs/filters/webpack/entries.rb b/lib/docs/filters/webpack/entries.rb index a293286f29..30d47bae23 100644 --- a/lib/docs/filters/webpack/entries.rb +++ b/lib/docs/filters/webpack/entries.rb @@ -22,16 +22,27 @@ def get_type def additional_entries if slug.start_with?('configuration') - css('h2 > [id]').each_with_object [] do |node, entries| - next if version.to_f < 5 && node.previous.try(:content).present? - entries << [node.parent.content, node['id']] + if version.to_f >= 5 + css('h2[id], h3[id]').map { |heading| [heading.content.strip, heading['id']] } + else + css('h2 > [id]').each_with_object [] do |node, entries| + next if node.previous.try(:content).present? + entries << [node.parent.content, node['id']] + end end elsif slug.start_with?('api') && slug != 'api/parser' - css('.header[id] code').each_with_object [] do |node, entries| - next if version.to_f < 5 && (node.previous.try(:content).present? || node.next.try(:content).present?) - name = node.content.sub(/\(.*\)/, '()') - name.prepend "#{self.name.split(':').first}: " - entries << [name, node['id']] + prefix = name.split(':').first + if version.to_f >= 5 + css('h2[id]').map do |heading| + title = heading.content.strip.sub(/\(.*\)/, '()') + ["#{prefix}: #{title}", heading['id']] + end + else + css('.header[id] code').each_with_object [] do |node, entries| + next if node.previous.try(:content).present? || node.next.try(:content).present? + heading = node.content.sub(/\(.*\)/, '()') + entries << ["#{prefix}: #{heading}", node['id']] + end end else [] diff --git a/lib/docs/scrapers/webpack.rb b/lib/docs/scrapers/webpack.rb index 4d5c60fb2d..5058f6aa31 100644 --- a/lib/docs/scrapers/webpack.rb +++ b/lib/docs/scrapers/webpack.rb @@ -36,8 +36,11 @@ class Webpack < UrlScraper HTML version '5' do - self.release = '5.97.1' + self.release = '5.113.0' self.base_url = 'https://webpack.js.org/' + # The sidebar links to pages that are not linked from the document body. + options[:container] = 'div:has(> #main-content)' + options[:skip_patterns] = [%r{/printable\z}] end version '4' do diff --git a/test/lib/docs/scrapers/webpack_test.rb b/test/lib/docs/scrapers/webpack_test.rb new file mode 100644 index 0000000000..de8a8a6630 --- /dev/null +++ b/test/lib/docs/scrapers/webpack_test.rb @@ -0,0 +1,60 @@ +require_relative '../../../test_helper' +require_relative '../../../../lib/docs' + +class WebpackScraperTest < Minitest::Test + def test_crawls_sidebar_links_and_keeps_only_document_content + result = scrape('configuration/module', <<~HTML) +

Module

+
+

UseEntry.sourceTest

+
+ +
const options = {};
+
+
+
Edit this page
+ HTML + + assert_includes result[:internal_urls], 'https://webpack.js.org/guides/getting-started' + refute_includes result[:internal_urls], 'https://webpack.js.org/guides/printable' + entry = result[:entries].find { |item| item.name == 'UseEntry.sourceTest' } + assert_equal 'configuration/module#useentrysourcetest', entry.path + assert_equal 'Configuration', entry.type + + output = Nokogiri::HTML.fragment(result[:output]) + assert output.at_css('#useentrysourcetest') + assert_equal 'const options = {};', output.at_css('pre').content + assert_equal 'js', output.at_css('pre')['data-language'] + refute_includes output.text, 'Getting Started' + refute_includes output.text, 'Edit this page' + assert_empty output.css('h3 > a, button, .code-block-wrapper') + end + + def test_indexes_v5_api_headings_with_the_page_name + result = scrape('api/compiler', <<~HTML) +

Compiler

+

run(callback)

+ HTML + + entry = result[:entries].find { |item| item.name == 'Compiler: run()' } + assert_equal 'api/compiler#run', entry.path + assert_equal 'API', entry.type + end + + private + + def scrape(path, body) + html = <<~HTML +
+ +
#{body}
+
+ HTML + scraper = Docs::Webpack.versions.find { |version| version.version == '5' }.new + context = scraper.options.merge(url: Docs::URL.parse("https://webpack.js.org/#{path}"), download_images: false) + scraper.pipeline.call(Docs::Parser.new(html).html, context) + end +end