es_toolkit.rb 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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/, "\u2063\\1") # U+2063 INVISIBLE SEPARATOR
  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. link = "#{self.class.links[:home]}#{filename.gsub(/^docs/,'').gsub(/md$/,'html')}"
  53. s += <<~HTML
  54. <div class="_attribution">
  55. <p class="_attribution-p">
  56. #{options[:attribution]}
  57. <br>
  58. <a href="#{link}" class="_attribution-link">
  59. #{link}
  60. </a>
  61. </p>
  62. </div>
  63. HTML
  64. s
  65. end
  66. def md
  67. @md ||= Redcarpet::Markdown.new(
  68. Redcarpet::Render::HTML,
  69. autolink: true,
  70. fenced_code_blocks: true,
  71. tables: true
  72. )
  73. end
  74. end
  75. end