2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-03-13 13:15:13 +08:00
|
|
|
class HighlightJsController < ApplicationController
|
2017-08-31 12:06:56 +08:00
|
|
|
skip_before_action :preload_json, :redirect_to_login_if_required, :check_xhr, :verify_authenticity_token, only: [:show]
|
2015-03-13 13:15:13 +08:00
|
|
|
|
2021-01-29 10:14:49 +08:00
|
|
|
before_action :apply_cdn_headers, only: [:show]
|
|
|
|
|
2015-03-13 13:15:13 +08:00
|
|
|
def show
|
2015-05-22 14:15:46 +08:00
|
|
|
|
|
|
|
no_cookies
|
|
|
|
|
2015-03-13 13:15:13 +08:00
|
|
|
RailsMultisite::ConnectionManagement.with_hostname(params[:hostname]) do
|
|
|
|
|
|
|
|
current_version = HighlightJs.version(SiteSetting.highlighted_languages)
|
|
|
|
|
|
|
|
if current_version != params[:version]
|
|
|
|
return redirect_to path(HighlightJs.path)
|
|
|
|
end
|
|
|
|
|
|
|
|
# note, this can be slightly optimised by caching the bundled file, it cuts down on N reads
|
|
|
|
# our nginx config caches this so in practical terms it does not really matter and keeps
|
|
|
|
# code simpler
|
2020-09-03 03:32:57 +08:00
|
|
|
languages = SiteSetting.highlighted_languages.split('|')
|
|
|
|
|
|
|
|
# TODO: some languages require to be loaded before others
|
|
|
|
# this limitation should be fixed in highlight js 11, remove it when available
|
|
|
|
prepended_languages = ['csharp', 'c', 'c-like']
|
|
|
|
prepended_languages.each do |lang|
|
|
|
|
if languages.include?(lang)
|
|
|
|
languages.insert(0, languages.delete(lang))
|
|
|
|
else
|
|
|
|
languages.insert(0, lang)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
highlight_js = HighlightJs.bundle(languages)
|
2015-03-13 13:15:13 +08:00
|
|
|
|
|
|
|
response.headers["Last-Modified"] = 10.years.ago.httpdate
|
|
|
|
response.headers["Content-Length"] = highlight_js.bytesize.to_s
|
2017-02-24 02:05:00 +08:00
|
|
|
immutable_for 1.year
|
2015-03-13 13:15:13 +08:00
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
render plain: highlight_js, disposition: nil, content_type: 'application/javascript'
|
2015-03-13 13:15:13 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|