es_toolkit.rb 2.3 KB

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