mirror of
https://github.com/discourse/discourse.git
synced 2024-11-22 11:44:49 +08:00
0878dde213
- Remove vendored copy - Update Rails implementation to look for language definitions in node_modules - Use webpack-based dynamic import for hljs core - Use browser-native dynamic import for site-specific language bundle (and fallback to webpack-based dynamic import in tests) - Simplify markdown implementation to allow all languages into the `lang-{blah}` className - Now that all languages are passed through, resolve aliases at runtime to avoid the need for the pre-built `highlightjs-aliases` index
49 lines
1.2 KiB
Ruby
49 lines
1.2 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module HighlightJs
|
|
HIGHLIGHTJS_DIR ||= "#{Rails.root}/app/assets/javascripts/node_modules/@highlightjs/cdn-assets/"
|
|
|
|
def self.languages
|
|
langs = Dir.glob(HIGHLIGHTJS_DIR + "languages/*.js").map { |path| File.basename(path)[0..-8] }
|
|
|
|
langs.sort
|
|
end
|
|
|
|
def self.bundle(langs)
|
|
lang_js =
|
|
langs.filter_map do |lang|
|
|
File.read(HIGHLIGHTJS_DIR + "languages/#{lang}.min.js")
|
|
rescue Errno::ENOENT
|
|
# no file, don't care
|
|
end
|
|
|
|
<<~JS
|
|
export default function registerLanguages(hljs) {
|
|
#{lang_js.join("\n")}
|
|
}
|
|
JS
|
|
end
|
|
|
|
def self.cache
|
|
@lang_string_cache ||= {}
|
|
end
|
|
|
|
def self.version(lang_string)
|
|
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]
|
|
end
|
|
|
|
def self.path
|
|
"/highlight-js/#{Discourse.current_hostname}/#{version SiteSetting.highlighted_languages}.js"
|
|
end
|
|
end
|