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