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/"
|
2023-12-01 06:57:11 -06:00
|
|
|
VERSION ||= 1 # bump to invalidate caches following core changes
|
2015-03-13 00:15:13 -05:00
|
|
|
|
|
|
|
def self.languages
|
2023-01-09 06:10:19 -06:00
|
|
|
langs = Dir.glob(HIGHLIGHTJS_DIR + "languages/*.js").map { |path| File.basename(path)[0..-8] }
|
2019-01-14 11:46:35 -06:00
|
|
|
|
|
|
|
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,
|
2023-11-28 05:28:40 -06:00
|
|
|
digest:
|
2023-12-01 06:57:11 -06:00
|
|
|
Digest::SHA1.hexdigest(
|
|
|
|
bundle(lang_string.split("|")) + "|#{VERSION}|#{GlobalSetting.asset_url_salt}",
|
|
|
|
),
|
2023-11-10 14:39:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|