2019-01-14 11:46:35 -06:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-03-13 00:15:13 -05:00
|
|
|
module HighlightJs
|
2019-01-21 03:12:23 -06:00
|
|
|
HIGHLIGHTJS_DIR ||= "#{Rails.root}/vendor/assets/javascripts/highlightjs/"
|
2023-01-09 06:10:19 -06: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 00:15:13 -05:00
|
|
|
|
|
|
|
def self.languages
|
2023-01-09 06:10:19 -06:00
|
|
|
langs = Dir.glob(HIGHLIGHTJS_DIR + "languages/*.js").map { |path| File.basename(path)[0..-8] }
|
2019-01-14 11:46:35 -06:00
|
|
|
|
|
|
|
langs.sort
|
2015-03-13 00:15:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.bundle(langs)
|
2019-01-14 11:46:35 -06:00
|
|
|
result = File.read(HIGHLIGHTJS_DIR + "highlight.min.js")
|
2022-09-21 17:08:21 -05:00
|
|
|
(langs - BUNDLED_LANGS).each do |lang|
|
2015-03-13 00:15:13 -05:00
|
|
|
begin
|
2019-01-14 11:46:35 -06:00
|
|
|
result << "\n" << File.read(HIGHLIGHTJS_DIR + "languages/#{lang}.min.js")
|
2015-03-13 00:15:13 -05:00
|
|
|
rescue Errno::ENOENT
|
|
|
|
# no file, don't care
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.version(lang_string)
|
2023-01-09 06:10:19 -06:00
|
|
|
(@lang_string_cache ||= {})[lang_string] ||= Digest::SHA1.hexdigest(
|
|
|
|
bundle lang_string.split("|")
|
|
|
|
)
|
2015-03-13 00:15:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.path
|
|
|
|
"/highlight-js/#{Discourse.current_hostname}/#{version SiteSetting.highlighted_languages}.js"
|
|
|
|
end
|
|
|
|
end
|