|
| 1 | +module Docs |
| 2 | + class EsToolkit < FileScraper |
| 3 | + self.name = "es-toolkit" |
| 4 | + self.slug = "es_toolkit" |
| 5 | + self.type = "simple" |
| 6 | + self.links = { |
| 7 | + code: "https://github.com/toss/es-toolkit", |
| 8 | + home: "https://es-toolkit.slash.page", |
| 9 | + } |
| 10 | + |
| 11 | + options[:attribution] = <<-HTML |
| 12 | + © 2024-2025, Viva Republica<br> |
| 13 | + Licensed under the MIT License. |
| 14 | + HTML |
| 15 | + |
| 16 | + def get_latest_version(opts) |
| 17 | + get_github_tags("toss", "es-toolkit", opts).first["name"] |
| 18 | + end |
| 19 | + |
| 20 | + def build_pages(&block) |
| 21 | + internal("docs/intro.md", path: "index", &block) |
| 22 | + Dir.chdir(source_directory) do |
| 23 | + Dir["docs/reference/**/*.md"] |
| 24 | + end.each { internal(_1, &block) } |
| 25 | + end |
| 26 | + |
| 27 | + protected |
| 28 | + |
| 29 | + def internal(filename, path: nil, &block) |
| 30 | + path ||= filename[%r{docs/reference/(.*/.*).md$}, 1] |
| 31 | + |
| 32 | + # calculate name/type |
| 33 | + if path != "index" |
| 34 | + name = filename[%r{([^/]+).md$}, 1] |
| 35 | + type = path.split("/")[0..-2] |
| 36 | + type = type.map(&:capitalize).join(" ") |
| 37 | + # really bad way to sort |
| 38 | + type = type.gsub(/^(Compat|Error)\b/, "~ \\1") |
| 39 | + else |
| 40 | + name = type = nil |
| 41 | + end |
| 42 | + |
| 43 | + # now yield |
| 44 | + entries = [Entry.new(name, path, type)] |
| 45 | + output = render(filename) |
| 46 | + store_path = "#{path}.html" |
| 47 | + yield({entries:, output:, path:, store_path:}) |
| 48 | + end |
| 49 | + |
| 50 | + # render/style HTML |
| 51 | + def render(filename) |
| 52 | + s = md.render(request_one(filename).body) |
| 53 | + |
| 54 | + # kill all links, they don't work |
| 55 | + s.gsub!(%r{<a href="[^"]+">(.*?)</a>}, "<span>\\1</span>") |
| 56 | + |
| 57 | + # syntax highlighting |
| 58 | + s.gsub!(%r{<pre><code class="typescript">}, "<pre data-language='typescript'><code class='typescript'>") |
| 59 | + |
| 60 | + # h3 => h4 |
| 61 | + s.gsub!(%r{(</?h)3>}, "\\14>") |
| 62 | + |
| 63 | + # manually add attribution |
| 64 | + s += <<~HTML |
| 65 | + <div class="_attribution"> |
| 66 | + <p class="_attribution-p"> |
| 67 | + #{options[:attribution]} |
| 68 | + <br> |
| 69 | + <a href="#{self.class.links[:home]}" class="_attribution-link"> |
| 70 | + #{self.class.links[:home]} |
| 71 | + </a> |
| 72 | + </p> |
| 73 | + </div> |
| 74 | + HTML |
| 75 | + s |
| 76 | + end |
| 77 | + |
| 78 | + def md |
| 79 | + @md ||= Redcarpet::Markdown.new( |
| 80 | + Redcarpet::Render::HTML, |
| 81 | + autolink: true, |
| 82 | + fenced_code_blocks: true, |
| 83 | + tables: true |
| 84 | + ) |
| 85 | + end |
| 86 | + end |
| 87 | +end |
0 commit comments