es_toolkit.rb 2.2 KB

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