|
| 1 | +module Docs |
| 2 | + class Lit |
| 3 | + class CleanHtmlFilter < Filter |
| 4 | + def call |
| 5 | + |
| 6 | + css('.offscreen, #inlineToc, a.anchor, [aria-hidden="true"], #prevAndNextLinks').remove |
| 7 | + |
| 8 | + css('[tabindex]').remove_attribute('tabindex') |
| 9 | + |
| 10 | + # Removing the side navigation. |
| 11 | + css('#docsNavWrapper, #rhsTocWrapper').remove |
| 12 | + |
| 13 | + # Removing this extra div. |
| 14 | + div = at_css('#articleWrapper') |
| 15 | + article = div.at_css('article') |
| 16 | + article.remove_attribute('id') |
| 17 | + div.replace(article) |
| 18 | + |
| 19 | + # Expanding and replacing the <template>, statically. |
| 20 | + # This code is a hacky incomplete implementation of |
| 21 | + # https://github.com/lit/lit.dev/blob/main/packages/lit-dev-content/src/components/litdev-aside.ts |
| 22 | + css('litdev-aside').each do |node| |
| 23 | + frag = Nokogiri::HTML::DocumentFragment.new(node.document) |
| 24 | + template = node.at_css('template') |
| 25 | + aside = template.children.first |
| 26 | + aside['class'] = 'litdev-aside' |
| 27 | + frag.add_child(aside) |
| 28 | + template.remove |
| 29 | + div = Nokogiri::XML::Node.new('div', @doc) |
| 30 | + div.add_child(node.children) |
| 31 | + aside.add_child(div) |
| 32 | + node.replace(aside) |
| 33 | + end |
| 34 | + |
| 35 | + # Removing the live playground examples. |
| 36 | + # https://github.com/lit/lit.dev/blob/main/packages/lit-dev-content/src/components/litdev-example.ts |
| 37 | + # Someday we can try enabling the live examples by adding appropriate code to assets/javascripts/views/pages/. |
| 38 | + css('litdev-example').each do |node| |
| 39 | + node.remove |
| 40 | + end |
| 41 | + |
| 42 | + # Cleaning up the preformatted example code. |
| 43 | + css('pre:has(code[class])').each do |node| |
| 44 | + lang = node.at_css('code')['class'] |
| 45 | + lang.sub! /^language-/, '' |
| 46 | + node.content = node.css('.cm-line').map(&:content).join("\n") |
| 47 | + node['data-language'] = lang |
| 48 | + end |
| 49 | + |
| 50 | + # Cleaning up example import. |
| 51 | + css('div.import').each do |node| |
| 52 | + pre = Nokogiri::XML::Node.new('pre', @doc) |
| 53 | + pre.content = node.css('.cm-line').map(&:content).join("\n") |
| 54 | + pre['data-language'] = 'javascript' |
| 55 | + node.replace(pre) |
| 56 | + end |
| 57 | + |
| 58 | + # Moving the "kind" to inside the header. |
| 59 | + # Because it looks better this way. |
| 60 | + css('.kindTag').each do |kindtag| |
| 61 | + heading = kindtag.parent |
| 62 | + next unless heading['class'].include? 'heading' |
| 63 | + h = heading.at_css('h2, h3, h4') |
| 64 | + h.prepend_child(kindtag) |
| 65 | + end |
| 66 | + |
| 67 | + # View source |
| 68 | + css('h2 ~ a.viewSourceLink, h3 ~ a.viewSourceLink, h4 ~ a.viewSourceLink').each do |node| |
| 69 | + node['class'] = 'view-source' |
| 70 | + node.content = 'Source' |
| 71 | + node.previous_element << node |
| 72 | + end |
| 73 | + |
| 74 | + css('.mdnIcon').each do |node| |
| 75 | + parent = node.parent |
| 76 | + node.remove |
| 77 | + parent.content = parent.content.strip |
| 78 | + end |
| 79 | + |
| 80 | + doc |
| 81 | + end |
| 82 | + end |
| 83 | + end |
| 84 | +end |
0 commit comments