2019-01-15 01:46:35 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-03-13 13:15:13 +08:00
|
|
|
module HighlightJs
|
2019-01-21 17:12:23 +08:00
|
|
|
HIGHLIGHTJS_DIR ||= "#{Rails.root}/vendor/assets/javascripts/highlightjs/"
|
2022-09-22 06:08:21 +08:00
|
|
|
BUNDLED_LANGS = %w(bash c cpp csharp css diff go graphql ini java javascript json kotlin less lua makefile xml markdown objectivec perl php php-template plaintext python python-repl r ruby rust scss shell sql swift typescript vbnet wasm yaml)
|
2015-03-13 13:15:13 +08:00
|
|
|
|
|
|
|
def self.languages
|
2019-01-15 01:46:35 +08:00
|
|
|
langs = Dir.glob(HIGHLIGHTJS_DIR + "languages/*.js").map do |path|
|
|
|
|
File.basename(path)[0..-8]
|
2015-03-13 13:15:13 +08:00
|
|
|
end
|
2019-01-15 01:46:35 +08:00
|
|
|
|
|
|
|
langs.sort
|
2015-03-13 13:15:13 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.bundle(langs)
|
2019-01-15 01:46:35 +08:00
|
|
|
result = File.read(HIGHLIGHTJS_DIR + "highlight.min.js")
|
2022-09-22 06:08:21 +08:00
|
|
|
(langs - BUNDLED_LANGS).each do |lang|
|
2015-03-13 13:15:13 +08:00
|
|
|
begin
|
2019-01-15 01:46:35 +08:00
|
|
|
result << "\n" << File.read(HIGHLIGHTJS_DIR + "languages/#{lang}.min.js")
|
2015-03-13 13:15:13 +08:00
|
|
|
rescue Errno::ENOENT
|
|
|
|
# no file, don't care
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.version(lang_string)
|
|
|
|
(@lang_string_cache ||= {})[lang_string] ||=
|
|
|
|
Digest::SHA1.hexdigest(bundle lang_string.split("|"))
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.path
|
|
|
|
"/highlight-js/#{Discourse.current_hostname}/#{version SiteSetting.highlighted_languages}.js"
|
|
|
|
end
|
|
|
|
end
|