Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions lib/docs/filters/webpack/clean_html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
27 changes: 19 additions & 8 deletions lib/docs/filters/webpack/entries.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
[]
Expand Down
5 changes: 4 additions & 1 deletion lib/docs/scrapers/webpack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
60 changes: 60 additions & 0 deletions test/lib/docs/scrapers/webpack_test.rb
Original file line number Diff line number Diff line change
@@ -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)
<h1>Module</h1>
<div>
<h3><span id="useentrysourcetest"></span>UseEntry.sourceTest<a href="#useentrysourcetest"><span class="header-link"></span></a></h3>
<div class="code-block-wrapper">
<button class="copy-button">Copy</button>
<pre><code class="hljs language-js"><span>const</span> options = {};</code></pre>
</div>
</div>
<div class="print:hidden">Edit this page</div>
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)
<h1>Compiler</h1>
<div><h2><span id="run"></span><code>run(callback)</code></h2></div>
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
<div>
<nav>
<a href="/guides/getting-started/">Getting Started</a>
<a href="/guides/printable/">Printable guides</a>
</nav>
<main id="main-content"><div class="markdown">#{body}</div></main>
</div>
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