2019-01-14 12:46:35 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-03-13 01:15:13 -04:00
|
|
|
module HighlightJs
|
2019-01-21 04:12:23 -05:00
|
|
|
HIGHLIGHTJS_DIR ||= "#{Rails.root}/vendor/assets/javascripts/highlightjs/"
|
2022-09-21 18:08:21 -04: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 01:15:13 -04:00
|
|
|
|
|
|
|
def self.languages
|
2019-01-14 12:46:35 -05:00
|
|
|
langs = Dir.glob(HIGHLIGHTJS_DIR + "languages/*.js").map do |path|
|
|
|
|
File.basename(path)[0..-8]
|
2015-03-13 01:15:13 -04:00
|
|
|
end
|
2019-01-14 12:46:35 -05:00
|
|
|
|
|
|
|
langs.sort
|
2015-03-13 01:15:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.bundle(langs)
|
2019-01-14 12:46:35 -05:00
|
|
|
result = File.read(HIGHLIGHTJS_DIR + "highlight.min.js")
|
2022-09-21 18:08:21 -04:00
|
|
|
(langs - BUNDLED_LANGS).each do |lang|
|
2015-03-13 01:15:13 -04:00
|
|
|
begin
|
2019-01-14 12:46:35 -05:00
|
|
|
result << "\n" << 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
|
|
|
|
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
|