2019-01-14 11:46:35 -06:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-03-13 00:15:13 -05:00
|
|
|
module HighlightJs
|
2023-11-10 14:39:48 -06:00
|
|
|
HIGHLIGHTJS_DIR ||= "#{Rails.root}/app/assets/javascripts/node_modules/@highlightjs/cdn-assets/"
|
2015-03-13 00:15:13 -05:00
|
|
|
|
|
|
|
def self.languages
|
2019-01-14 11:46:35 -06:00
|
|
|
langs = Dir.glob(HIGHLIGHTJS_DIR + "languages/*.js").map { |path| File.basename(path)[0..-8] }
|
|
|
|
|
|
|
|
langs.sort
|
2015-03-13 00:15:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.bundle(langs)
|
2023-11-10 14:39:48 -06:00
|
|
|
lang_js =
|
|
|
|
langs.filter_map do |lang|
|
|
|
|
File.read(HIGHLIGHTJS_DIR + "languages/#{lang}.min.js")
|
2015-03-13 00:15:13 -05:00
|
|
|
rescue Errno::ENOENT
|
|
|
|
# no file, don't care
|
|
|
|
end
|
|
|
|
|
2023-11-10 14:39:48 -06:00
|
|
|
<<~JS
|
|
|
|
export default function registerLanguages(hljs) {
|
|
|
|
#{lang_js.join("\n")}
|
|
|
|
}
|
|
|
|
JS
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.cache
|
|
|
|
@lang_string_cache ||= {}
|
2015-03-13 00:15:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.version(lang_string)
|
2023-11-10 14:39:48 -06:00
|
|
|
cache_info = cache[RailsMultisite::ConnectionManagement.current_db]
|
|
|
|
|
|
|
|
return cache_info[:digest] if cache_info&.[](:lang_string) == lang_string
|
|
|
|
|
|
|
|
cache_info = {
|
|
|
|
lang_string: lang_string,
|
|
|
|
digest: Digest::SHA1.hexdigest(bundle(lang_string.split("|"))),
|
|
|
|
}
|
|
|
|
|
|
|
|
cache[RailsMultisite::ConnectionManagement.current_db] = cache_info
|
|
|
|
cache_info[:digest]
|
2015-03-13 00:15:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.path
|
|
|
|
"/highlight-js/#{Discourse.current_hostname}/#{version SiteSetting.highlighted_languages}.js"
|
|
|
|
end
|
|
|
|
end
|