2019-01-15 01:46:35 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-03-13 13:15:13 +08:00
|
|
|
module HighlightJs
|
2024-09-03 17:51:07 +08:00
|
|
|
HIGHLIGHTJS_DIR ||=
|
|
|
|
"#{Rails.root}/app/assets/javascripts/discourse/node_modules/@highlightjs/cdn-assets/"
|
2023-12-01 20:57:11 +08:00
|
|
|
VERSION ||= 1 # bump to invalidate caches following core changes
|
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 { |path| File.basename(path)[0..-8] }
|
|
|
|
|
|
|
|
langs.sort
|
2015-03-13 13:15:13 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.bundle(langs)
|
2023-11-11 04:39:48 +08:00
|
|
|
lang_js =
|
|
|
|
langs.filter_map do |lang|
|
|
|
|
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
|
|
|
|
|
2023-11-11 04:39:48 +08:00
|
|
|
<<~JS
|
|
|
|
export default function registerLanguages(hljs) {
|
|
|
|
#{lang_js.join("\n")}
|
|
|
|
}
|
|
|
|
JS
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.cache
|
|
|
|
@lang_string_cache ||= {}
|
2015-03-13 13:15:13 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.version(lang_string)
|
2023-11-11 04:39:48 +08: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 19:28:40 +08:00
|
|
|
digest:
|
2023-12-01 20:57:11 +08:00
|
|
|
Digest::SHA1.hexdigest(
|
|
|
|
bundle(lang_string.split("|")) + "|#{VERSION}|#{GlobalSetting.asset_url_salt}",
|
|
|
|
),
|
2023-11-11 04:39:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
cache[RailsMultisite::ConnectionManagement.current_db] = cache_info
|
|
|
|
cache_info[:digest]
|
2015-03-13 13:15:13 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.path
|
|
|
|
"/highlight-js/#{Discourse.current_hostname}/#{version SiteSetting.highlighted_languages}.js"
|
|
|
|
end
|
|
|
|
end
|