mirror of
https://github.com/discourse/discourse.git
synced 2024-11-26 01:53:39 +08:00
edb201f55b
At some point moved from hljs pure source to their CDN assets, but we did not change the way we created the HLJS bundle. The CDN asset comes with their "common" languages already included, so we were duplicating around 35 languagues in the bundle we create. This patch includes a list of their current common langs so we never double bundle those. Changes in size are significant. Numbers before minimization/compression are 747.53 KB before and 117.57 KB after.
37 lines
1.1 KiB
Ruby
37 lines
1.1 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module HighlightJs
|
|
HIGHLIGHTJS_DIR ||= "#{Rails.root}/vendor/assets/javascripts/highlightjs/"
|
|
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)
|
|
|
|
def self.languages
|
|
langs = Dir.glob(HIGHLIGHTJS_DIR + "languages/*.js").map do |path|
|
|
File.basename(path)[0..-8]
|
|
end
|
|
|
|
langs.sort
|
|
end
|
|
|
|
def self.bundle(langs)
|
|
result = File.read(HIGHLIGHTJS_DIR + "highlight.min.js")
|
|
(langs - BUNDLED_LANGS).each do |lang|
|
|
begin
|
|
result << "\n" << File.read(HIGHLIGHTJS_DIR + "languages/#{lang}.min.js")
|
|
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
|